[clang] [clang][test] Rewrote test to work with lit internal shell syntax (PR #105902)

Connie Zhu via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 26 12:10:35 PDT 2024


https://github.com/connieyzhu updated https://github.com/llvm/llvm-project/pull/105902

>From 5801e58f5e89fb90a3b18414e1cb959d027b4fee Mon Sep 17 00:00:00 2001
From: Connie Zhu <connieyzhu at google.com>
Date: Fri, 23 Aug 2024 17:23:22 +0000
Subject: [PATCH 1/3] [clang][test] Rewrote test to work with lit internal
 shell syntax

This patch rewrites a test that uses command substitution $() and the
stat command, which are not supported by lit's internal shell. Instead
of using this syntax to perform the file size comparison done in this
test, a Python script is used instead to perform the same operation.
---
 clang/test/Modules/compare-file-size.py  | 22 ++++++++++++++++++++++
 clang/test/Modules/reduced-bmi-size.cppm |  3 +--
 clang/test/lit.cfg.py                    |  2 ++
 3 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Modules/compare-file-size.py

diff --git a/clang/test/Modules/compare-file-size.py b/clang/test/Modules/compare-file-size.py
new file mode 100644
index 00000000000000..ca3b16442353c7
--- /dev/null
+++ b/clang/test/Modules/compare-file-size.py
@@ -0,0 +1,22 @@
+import argparse
+import os
+
+def get_file_size(file_path):
+    try:
+        return os.path.getsize(file_path)
+    except:
+        print(f"Unable to get file size of {file_path}")
+        return None
+
+def main():
+    parser = argparse.ArgumentParser()
+
+    parser.add_argument("file1", type=str)
+    parser.add_argument("file2", type=str)
+
+    args = parser.parse_args()
+
+    return get_file_size(args.file1) < get_file_size(args.file2)
+
+if __name__ == "__main__":
+    main()
diff --git a/clang/test/Modules/reduced-bmi-size.cppm b/clang/test/Modules/reduced-bmi-size.cppm
index 664f45f5c6a5a7..6d62573bc7aa39 100644
--- a/clang/test/Modules/reduced-bmi-size.cppm
+++ b/clang/test/Modules/reduced-bmi-size.cppm
@@ -10,7 +10,6 @@
 // RUN: %clang_cc1 -std=c++20 -emit-module-interface %s -o %t/a.pcm
 // RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %s -o %t/a.reduced.pcm
 //
-// %s implies the current source file. So we can't use it directly.
-// RUN: [ $(stat -c%\s "%t/a.pcm") -le $(stat -c%\s "%t/a.reduced.pcm") ]
+// RUN: %{python} %S/compare-file-size.py %t/a.pcm %t/a.reduced.pcm
 
 export module a;
diff --git a/clang/test/lit.cfg.py b/clang/test/lit.cfg.py
index 92a3361ce672e2..59330a6d51a611 100644
--- a/clang/test/lit.cfg.py
+++ b/clang/test/lit.cfg.py
@@ -74,6 +74,8 @@
 
 config.substitutions.append(("%PATH%", config.environment["PATH"]))
 
+config.substitutions.append(("%{python}", '"%s"' % (sys.executable)))
+
 
 # For each occurrence of a clang tool name, replace it with the full path to
 # the build directory holding that tool.  We explicitly specify the directories

>From fa882e33eaab820219a704ba15a1cc10f2f93b07 Mon Sep 17 00:00:00 2001
From: Connie Zhu <connieyzhu at google.com>
Date: Mon, 26 Aug 2024 18:47:31 +0000
Subject: [PATCH 2/3] [clang][test] Modified python program to return error
 code when failing to get file size

This patch removes the print statement that executes in the case of
exceptions, opting to let the system return its own error code when
failing to get the file size of a certain file. There are also some NFC
changes: adding description for compare-file-size.py and changing the
%{python} syntax to use the exisitng lit substitution %python.
---
 clang/test/Modules/compare-file-size.py  | 24 ++++++++----------------
 clang/test/Modules/reduced-bmi-size.cppm |  2 +-
 clang/test/lit.cfg.py                    |  2 --
 3 files changed, 9 insertions(+), 19 deletions(-)

diff --git a/clang/test/Modules/compare-file-size.py b/clang/test/Modules/compare-file-size.py
index ca3b16442353c7..30319726eff861 100644
--- a/clang/test/Modules/compare-file-size.py
+++ b/clang/test/Modules/compare-file-size.py
@@ -1,22 +1,14 @@
+# This program takes in two file path arguments and returns true if the
+# file size of the first file is smaller than the file size of the second file
+
 import argparse
 import os
 
-def get_file_size(file_path):
-    try:
-        return os.path.getsize(file_path)
-    except:
-        print(f"Unable to get file size of {file_path}")
-        return None
-
-def main():
-    parser = argparse.ArgumentParser()
-
-    parser.add_argument("file1", type=str)
-    parser.add_argument("file2", type=str)
+parser = argparse.ArgumentParser()
 
-    args = parser.parse_args()
+parser.add_argument("file1", type=str)
+parser.add_argument("file2", type=str)
 
-    return get_file_size(args.file1) < get_file_size(args.file2)
+args = parser.parse_args()
 
-if __name__ == "__main__":
-    main()
+return os.path.getsize(args.file1) < os.path.getsize(args.file2)
diff --git a/clang/test/Modules/reduced-bmi-size.cppm b/clang/test/Modules/reduced-bmi-size.cppm
index 6d62573bc7aa39..6e3323266561e8 100644
--- a/clang/test/Modules/reduced-bmi-size.cppm
+++ b/clang/test/Modules/reduced-bmi-size.cppm
@@ -10,6 +10,6 @@
 // RUN: %clang_cc1 -std=c++20 -emit-module-interface %s -o %t/a.pcm
 // RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %s -o %t/a.reduced.pcm
 //
-// RUN: %{python} %S/compare-file-size.py %t/a.pcm %t/a.reduced.pcm
+// RUN: %python %S/compare-file-size.py %t/a.pcm %t/a.reduced.pcm
 
 export module a;
diff --git a/clang/test/lit.cfg.py b/clang/test/lit.cfg.py
index 59330a6d51a611..92a3361ce672e2 100644
--- a/clang/test/lit.cfg.py
+++ b/clang/test/lit.cfg.py
@@ -74,8 +74,6 @@
 
 config.substitutions.append(("%PATH%", config.environment["PATH"]))
 
-config.substitutions.append(("%{python}", '"%s"' % (sys.executable)))
-
 
 # For each occurrence of a clang tool name, replace it with the full path to
 # the build directory holding that tool.  We explicitly specify the directories

>From 742e95dd4db4ee65f43937093b9f0fdd1456be00 Mon Sep 17 00:00:00 2001
From: Connie Zhu <connieyzhu at google.com>
Date: Mon, 26 Aug 2024 19:09:00 +0000
Subject: [PATCH 3/3] [clang][test][NFC] Edited comment to have clearer
 description

---
 clang/test/Modules/compare-file-size.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/Modules/compare-file-size.py b/clang/test/Modules/compare-file-size.py
index 30319726eff861..3e5d04ab612f08 100644
--- a/clang/test/Modules/compare-file-size.py
+++ b/clang/test/Modules/compare-file-size.py
@@ -1,5 +1,5 @@
-# This program takes in two file path arguments and returns true if the
-# file size of the first file is smaller than the file size of the second file
+# This program takes in two file path arguments in the form 'compare-file-size.py file1 file2'
+# Returns true if the file size of the file1 is smaller than the file size of file2
 
 import argparse
 import os



More information about the cfe-commits mailing list