[llvm] 8f64b02 - [lit] Allow passing extra commands to executeShTest

Louis Dionne via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 24 12:02:50 PDT 2020


Author: Louis Dionne
Date: 2020-03-24T15:02:37-04:00
New Revision: 8f64b02d336a2197ce948026f5b2008b68d73a58

URL: https://github.com/llvm/llvm-project/commit/8f64b02d336a2197ce948026f5b2008b68d73a58
DIFF: https://github.com/llvm/llvm-project/commit/8f64b02d336a2197ce948026f5b2008b68d73a58.diff

LOG: [lit] Allow passing extra commands to executeShTest

This allows creating custom test formats on top of `executeShTest` that
inject commands at the beginning of the file being parsed, without
requiring these commands to physically appear in the test file itself.

For example, one could define a test format that prints out additional
debug information at the beginning of each test. More realistically,
this has been used to define custom test formats like one that supports
compilation failure tests (e.g. with the extension `compile.fail.cpp`)
by injecting a command that calls the compiler on the file itself and
expects it to fail.

Without this change, the only alternative is to create a temporary file
with the same content as the original test, then prepend the desired
`// RUN:` lines to that file, and call `executeShTest` on that file
instead. This is both slow and cumbersome to do.

Differential Revision: https://reviews.llvm.org/D76290

Added: 
    llvm/utils/lit/tests/Inputs/shtest-inject/lit.cfg
    llvm/utils/lit/tests/Inputs/shtest-inject/test-empty.txt
    llvm/utils/lit/tests/Inputs/shtest-inject/test-many.txt
    llvm/utils/lit/tests/Inputs/shtest-inject/test-one.txt
    llvm/utils/lit/tests/shtest-inject.py

Modified: 
    llvm/utils/lit/lit/TestRunner.py

Removed: 
    


################################################################################
diff  --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index a9518b2b5a0b..4ee3b673c28f 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -1488,13 +1488,17 @@ def _runShTest(test, litConfig, useExternalSh, script, tmpBase):
 
 
 def executeShTest(test, litConfig, useExternalSh,
-                  extra_substitutions=[]):
+                  extra_substitutions=[],
+                  preamble_commands=[]):
     if test.config.unsupported:
         return lit.Test.Result(Test.UNSUPPORTED, 'Test is unsupported')
 
-    script = parseIntegratedTestScript(test)
-    if isinstance(script, lit.Test.Result):
-        return script
+    script = list(preamble_commands)
+    parsed = parseIntegratedTestScript(test, require_script=not script)
+    if isinstance(parsed, lit.Test.Result):
+        return parsed
+    script += parsed
+
     if litConfig.noExecute:
         return lit.Test.Result(Test.PASS)
 

diff  --git a/llvm/utils/lit/tests/Inputs/shtest-inject/lit.cfg b/llvm/utils/lit/tests/Inputs/shtest-inject/lit.cfg
new file mode 100644
index 000000000000..65a02e0081a2
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-inject/lit.cfg
@@ -0,0 +1,17 @@
+import lit
+
+class CustomFormat(lit.formats.TestFormat):
+    def execute(self, test, litConfig):
+        commands = [
+            'echo "THIS WAS"',
+            'echo "INJECTED"'
+        ]
+        return lit.TestRunner.executeShTest(test, litConfig,
+                                            useExternalSh=False,
+                                            preamble_commands=commands)
+
+config.name = 'shtest-inject'
+config.suffixes = ['.txt']
+config.test_format = CustomFormat()
+config.test_source_root = None
+config.test_exec_root = None

diff  --git a/llvm/utils/lit/tests/Inputs/shtest-inject/test-empty.txt b/llvm/utils/lit/tests/Inputs/shtest-inject/test-empty.txt
new file mode 100644
index 000000000000..293607453a74
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-inject/test-empty.txt
@@ -0,0 +1,3 @@
+
+# This test voluntarily has no RUN lines or anything else. The RUN lines are
+# injected by the test format.

diff  --git a/llvm/utils/lit/tests/Inputs/shtest-inject/test-many.txt b/llvm/utils/lit/tests/Inputs/shtest-inject/test-many.txt
new file mode 100644
index 000000000000..bc990580edfd
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-inject/test-many.txt
@@ -0,0 +1,7 @@
+
+# This test has several RUN lines, but more run lines are prepended to it by
+# the test format in use.
+
+# RUN: echo "IN THE FILE"
+# RUN: echo "IF IT WORKS"
+# RUN: echo "AS EXPECTED"

diff  --git a/llvm/utils/lit/tests/Inputs/shtest-inject/test-one.txt b/llvm/utils/lit/tests/Inputs/shtest-inject/test-one.txt
new file mode 100644
index 000000000000..ab66fc9ef749
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-inject/test-one.txt
@@ -0,0 +1,5 @@
+
+# This test has one RUN line, but more run lines are prepended to it by the
+# test format in use.
+
+# RUN: echo "IN THE FILE"

diff  --git a/llvm/utils/lit/tests/shtest-inject.py b/llvm/utils/lit/tests/shtest-inject.py
new file mode 100644
index 000000000000..f51f083f3990
--- /dev/null
+++ b/llvm/utils/lit/tests/shtest-inject.py
@@ -0,0 +1,49 @@
+# Check that we can inject commands at the beginning of a ShTest using a custom
+# test format.
+
+# RUN: %{lit} -j 1 %{inputs}/shtest-inject/test-empty.txt --show-all | FileCheck --check-prefix=CHECK-TEST1 %s
+#
+# CHECK-TEST1: Script:
+# CHECK-TEST1: --
+# CHECK-TEST1: echo "THIS WAS"
+# CHECK-TEST1: echo "INJECTED"
+# CHECK-TEST1: --
+#
+# CHECK-TEST1: THIS WAS
+# CHECK-TEST1: INJECTED
+#
+# CHECK-TEST1: Expected Passes    : 1
+
+# RUN: %{lit} -j 1 %{inputs}/shtest-inject/test-one.txt --show-all | FileCheck --check-prefix=CHECK-TEST2 %s
+#
+# CHECK-TEST2: Script:
+# CHECK-TEST2: --
+# CHECK-TEST2: echo "THIS WAS"
+# CHECK-TEST2: echo "INJECTED"
+# CHECK-TEST2: echo "IN THE FILE"
+# CHECK-TEST2: --
+#
+# CHECK-TEST2: THIS WAS
+# CHECK-TEST2: INJECTED
+# CHECK-TEST2: IN THE FILE
+#
+# CHECK-TEST2: Expected Passes    : 1
+
+# RUN: %{lit} -j 1 %{inputs}/shtest-inject/test-many.txt --show-all | FileCheck --check-prefix=CHECK-TEST3 %s
+#
+# CHECK-TEST3: Script:
+# CHECK-TEST3: --
+# CHECK-TEST3: echo "THIS WAS"
+# CHECK-TEST3: echo "INJECTED"
+# CHECK-TEST3: echo "IN THE FILE"
+# CHECK-TEST3: echo "IF IT WORKS"
+# CHECK-TEST3: echo "AS EXPECTED"
+# CHECK-TEST3: --
+#
+# CHECK-TEST3: THIS WAS
+# CHECK-TEST3: INJECTED
+# CHECK-TEST3: IN THE FILE
+# CHECK-TEST3: IF IT WORKS
+# CHECK-TEST3: AS EXPECTED
+#
+# CHECK-TEST3: Expected Passes    : 1


        


More information about the llvm-commits mailing list