[llvm] [llvm-lit] Precommit Tests for implementing the `unset` command in lit internal shell (PR #104618)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 21 11:12:01 PDT 2024
https://github.com/Harini0924 updated https://github.com/llvm/llvm-project/pull/104618
>From 3e6ea6c452858575290a04d445389c4d64669de6 Mon Sep 17 00:00:00 2001
From: Harini <harinidonthula at google.com>
Date: Tue, 13 Aug 2024 07:18:12 +0000
Subject: [PATCH 1/2] [llvm-lit] Testing `unset` command expected failures
This suite of tests is designed to verify the expected failures
when attempting to use the unset command within the lit internal shell.
These tests ensure that the unset command fails with the correct error
messages and exit codes, demonstrating that the command is not recognized
or supported in this environment. These failures are expected and validate
the current behavior of the lit internal shell when handling the `unset` command.
---
.../lit/tests/Inputs/shtest-unset/lit.cfg | 10 +++++
.../shtest-unset/unset-multiple-variables.txt | 3 ++
.../Inputs/shtest-unset/unset-no-args.txt | 3 ++
.../unset-nonexistent-variable.txt | 3 ++
.../Inputs/shtest-unset/unset-variable.txt | 3 ++
llvm/utils/lit/tests/shtest-unset.py | 42 +++++++++++++++++++
6 files changed, 64 insertions(+)
create mode 100644 llvm/utils/lit/tests/Inputs/shtest-unset/lit.cfg
create mode 100644 llvm/utils/lit/tests/Inputs/shtest-unset/unset-multiple-variables.txt
create mode 100644 llvm/utils/lit/tests/Inputs/shtest-unset/unset-no-args.txt
create mode 100644 llvm/utils/lit/tests/Inputs/shtest-unset/unset-nonexistent-variable.txt
create mode 100644 llvm/utils/lit/tests/Inputs/shtest-unset/unset-variable.txt
create mode 100644 llvm/utils/lit/tests/shtest-unset.py
diff --git a/llvm/utils/lit/tests/Inputs/shtest-unset/lit.cfg b/llvm/utils/lit/tests/Inputs/shtest-unset/lit.cfg
new file mode 100644
index 00000000000000..ec8c43263be721
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-unset/lit.cfg
@@ -0,0 +1,10 @@
+import lit.formats
+
+config.name = "shtest-unset"
+config.suffixes = [".txt"]
+config.test_format = lit.formats.ShTest()
+config.test_source_root = None
+config.test_exec_root = None
+config.environment["FOO"] = "1"
+config.environment["BAR"] = "2"
+config.substitutions.append(("%{python}", '"%s"' % (sys.executable)))
diff --git a/llvm/utils/lit/tests/Inputs/shtest-unset/unset-multiple-variables.txt b/llvm/utils/lit/tests/Inputs/shtest-unset/unset-multiple-variables.txt
new file mode 100644
index 00000000000000..f7dc7e80bd93b4
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-unset/unset-multiple-variables.txt
@@ -0,0 +1,3 @@
+## Tests the 'unset' command with multiple variables.
+
+# RUN: unset FOO BAR
diff --git a/llvm/utils/lit/tests/Inputs/shtest-unset/unset-no-args.txt b/llvm/utils/lit/tests/Inputs/shtest-unset/unset-no-args.txt
new file mode 100644
index 00000000000000..bc9f27710c5bfb
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-unset/unset-no-args.txt
@@ -0,0 +1,3 @@
+## Tests the 'unset' command when no arguments are provided.
+
+# RUN: unset
diff --git a/llvm/utils/lit/tests/Inputs/shtest-unset/unset-nonexistent-variable.txt b/llvm/utils/lit/tests/Inputs/shtest-unset/unset-nonexistent-variable.txt
new file mode 100644
index 00000000000000..5fbe3fb757291b
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-unset/unset-nonexistent-variable.txt
@@ -0,0 +1,3 @@
+## Test the behavior of the 'unset' command when trying to unset a variable that does not exist.
+
+# RUN: unset NONEXISTENT
diff --git a/llvm/utils/lit/tests/Inputs/shtest-unset/unset-variable.txt b/llvm/utils/lit/tests/Inputs/shtest-unset/unset-variable.txt
new file mode 100644
index 00000000000000..11adcd234e7177
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-unset/unset-variable.txt
@@ -0,0 +1,3 @@
+## Tests the 'unset' command with a single variable
+
+# RUN: unset FOO
diff --git a/llvm/utils/lit/tests/shtest-unset.py b/llvm/utils/lit/tests/shtest-unset.py
new file mode 100644
index 00000000000000..fdcc2ed5d86fce
--- /dev/null
+++ b/llvm/utils/lit/tests/shtest-unset.py
@@ -0,0 +1,42 @@
+## Check that the 'unset' command fails as expected for various tests.
+
+# RUN: not %{lit} -a -v %{inputs}/shtest-unset \
+# RUN: | FileCheck -match-full-lines %s
+#
+# END.
+
+## Check that the 'unset' command's expected failures.
+
+# CHECK: -- Testing: 4 tests{{.*}}
+
+# CHECK: FAIL: shtest-unset :: unset-multiple-variables.txt{{.*}}
+# CHECK: unset FOO BAR
+# CHECK-NEXT: # executed command: unset FOO BAR
+# CHECK-NEXT: # .---command stderr------------
+# CHECK-NEXT: # | 'unset': command not found
+# CHECK: # error: command failed with exit status: 127
+
+# CHECK: FAIL: shtest-unset :: unset-no-args.txt{{.*}}
+# CHECK: unset
+# CHECK-NEXT: # executed command: unset
+# CHECK-NEXT: # .---command stderr------------
+# CHECK-NEXT: # | 'unset': command not found
+# CHECK: # error: command failed with exit status: 127
+
+# CHECK: FAIL: shtest-unset :: unset-nonexistent-variable.txt{{.*}}
+# CHECK: unset NONEXISTENT
+# CHECK-NEXT: # executed command: unset NONEXISTENT
+# CHECK-NEXT: # .---command stderr------------
+# CHECK-NEXT: # | 'unset': command not found
+# CHECK: # error: command failed with exit status: 127
+
+# CHECK: FAIL: shtest-unset :: unset-variable.txt{{.*}}
+# CHECK: unset FOO
+# CHECK-NEXT: # executed command: unset FOO
+# CHECK-NEXT: # .---command stderr------------
+# CHECK-NEXT: # | 'unset': command not found
+# CHECK: # error: command failed with exit status: 127
+
+# CHECK: Total Discovered Tests: 4
+# CHECK: Failed: 4 {{\([0-9]*\.[0-9]*%\)}}
+# CHECK-NOT: {{.}}
>From e8a00787a87f1cea3752a87b54d67851fc5e1bb6 Mon Sep 17 00:00:00 2001
From: Harini <harinidonthula at google.com>
Date: Wed, 21 Aug 2024 18:06:56 +0000
Subject: [PATCH 2/2] [llvm-lit] Implement executeBuiltinUnset Function and
Update Error Messaging
This patch introduces the executeBuiltinUnset function in TestRunner.py,
which I created to handle the 'unset' command. The function raises an
InternalShellError with a clear message advising users to use env -u VARIABLE
as an alternative since the unset command is not supported by the lit internal shell.
Additionally, the test in shtest-unset.py has been updated to verify this error message.
---
llvm/utils/lit/lit/TestRunner.py | 9 +++++++++
llvm/utils/lit/tests/shtest-unset.py | 8 ++++----
2 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index da7fa86fd39173..c96b7927dd37ca 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -570,6 +570,14 @@ def executeBuiltinColon(cmd, cmd_shenv):
return ShellCommandResult(cmd, "", "", 0, False)
+def executeBuiltinUnset(cmd, shenv):
+ """executeBuiltinUnset - Handle the 'unset' command."""
+ raise InternalShellError(
+ cmd,
+ "'unset' command is not supported by the lit internal shell. Please use 'env -u VARIABLE' to unset environment variables.",
+ )
+
+
def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
"""Return the standard fds for cmd after applying redirects
@@ -720,6 +728,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
"pushd": executeBuiltinPushd,
"rm": executeBuiltinRm,
":": executeBuiltinColon,
+ "unset": executeBuiltinUnset,
}
# To avoid deadlock, we use a single stderr stream for piped
# output. This is null until we have seen some output using
diff --git a/llvm/utils/lit/tests/shtest-unset.py b/llvm/utils/lit/tests/shtest-unset.py
index fdcc2ed5d86fce..6517864e8f9bd8 100644
--- a/llvm/utils/lit/tests/shtest-unset.py
+++ b/llvm/utils/lit/tests/shtest-unset.py
@@ -13,28 +13,28 @@
# CHECK: unset FOO BAR
# CHECK-NEXT: # executed command: unset FOO BAR
# CHECK-NEXT: # .---command stderr------------
-# CHECK-NEXT: # | 'unset': command not found
+# CHECK-NEXT: # | 'unset' command is not supported by the lit internal shell. Please use 'env -u VARIABLE' to unset environment variables.
# CHECK: # error: command failed with exit status: 127
# CHECK: FAIL: shtest-unset :: unset-no-args.txt{{.*}}
# CHECK: unset
# CHECK-NEXT: # executed command: unset
# CHECK-NEXT: # .---command stderr------------
-# CHECK-NEXT: # | 'unset': command not found
+# CHECK-NEXT: # | 'unset' command is not supported by the lit internal shell. Please use 'env -u VARIABLE' to unset environment variables.
# CHECK: # error: command failed with exit status: 127
# CHECK: FAIL: shtest-unset :: unset-nonexistent-variable.txt{{.*}}
# CHECK: unset NONEXISTENT
# CHECK-NEXT: # executed command: unset NONEXISTENT
# CHECK-NEXT: # .---command stderr------------
-# CHECK-NEXT: # | 'unset': command not found
+# CHECK-NEXT: # | 'unset' command is not supported by the lit internal shell. Please use 'env -u VARIABLE' to unset environment variables.
# CHECK: # error: command failed with exit status: 127
# CHECK: FAIL: shtest-unset :: unset-variable.txt{{.*}}
# CHECK: unset FOO
# CHECK-NEXT: # executed command: unset FOO
# CHECK-NEXT: # .---command stderr------------
-# CHECK-NEXT: # | 'unset': command not found
+# CHECK-NEXT: # | 'unset' command is not supported by the lit internal shell. Please use 'env -u VARIABLE' to unset environment variables.
# CHECK: # error: command failed with exit status: 127
# CHECK: Total Discovered Tests: 4
More information about the llvm-commits
mailing list