[llvm] [lit] Implement builtin umask (PR #94621)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 7 06:39:27 PDT 2024


https://github.com/jayfoad updated https://github.com/llvm/llvm-project/pull/94621

>From 2c7bab227cf1ca0dd68c79579833ebdb98e7fea2 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Thu, 6 Jun 2024 14:55:37 +0100
Subject: [PATCH 1/3] [lit] Implement builtin umask

This allows a few more tests to use lit's internal shell.
---
 .../ELF/X86/mirror-permissions-unix.test      |  1 -
 .../ELF/mirror-permissions-unix.test          |  1 -
 .../tools/llvm-objcopy/ELF/respect-umask.test |  1 -
 llvm/utils/lit/lit/TestRunner.py              | 19 +++++++++++++++++--
 4 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/llvm/test/tools/llvm-dwarfutil/ELF/X86/mirror-permissions-unix.test b/llvm/test/tools/llvm-dwarfutil/ELF/X86/mirror-permissions-unix.test
index a95d1c0aafa21..fdcba4dcd666b 100644
--- a/llvm/test/tools/llvm-dwarfutil/ELF/X86/mirror-permissions-unix.test
+++ b/llvm/test/tools/llvm-dwarfutil/ELF/X86/mirror-permissions-unix.test
@@ -3,7 +3,6 @@
 ## Setting the umask to 0 ensures deterministic permissions across
 ## test environments.
 # UNSUPPORTED: system-windows
-# REQUIRES: shell
 
 # RUN: touch %t
 # RUN: chmod 0777 %t
diff --git a/llvm/test/tools/llvm-objcopy/ELF/mirror-permissions-unix.test b/llvm/test/tools/llvm-objcopy/ELF/mirror-permissions-unix.test
index 8f4993f4f3d29..66a481a2230d1 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/mirror-permissions-unix.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/mirror-permissions-unix.test
@@ -6,7 +6,6 @@
 ## Setting the umask to 0 ensures deterministic permissions across
 ## test environments.
 # UNSUPPORTED: system-windows
-# REQUIRES: shell
 
 # RUN: touch %t
 # RUN: chmod 0777 %t
diff --git a/llvm/test/tools/llvm-objcopy/ELF/respect-umask.test b/llvm/test/tools/llvm-objcopy/ELF/respect-umask.test
index 376e33a217819..f4d3099056168 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/respect-umask.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/respect-umask.test
@@ -4,7 +4,6 @@
 ## Windows has no umask so this test makes no sense, nor would
 ## it work because there is no umask(1) in a Windows environment
 # UNSUPPORTED: system-windows
-# REQUIRES: shell
 
 # RUN: rm -f %t
 # RUN: touch %t
diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index da7fa86fd3917..d2627728eb09e 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -87,10 +87,11 @@ class ShellEnvironment(object):
     we maintain a dir stack for pushd/popd.
     """
 
-    def __init__(self, cwd, env):
+    def __init__(self, cwd, env, umask=-1):
         self.cwd = cwd
         self.env = dict(env)
         self.dirStack = []
+        self.umask = umask
 
     def change_dir(self, newdir):
         if os.path.isabs(newdir):
@@ -565,6 +566,18 @@ class SHFILEOPSTRUCTW(Structure):
     return ShellCommandResult(cmd, "", stderr.getvalue(), exitCode, False)
 
 
+def executeBuiltinUmask(cmd, shenv):
+    """executeBuiltinUmask - Change the current umask."""
+    if len(cmd.args) != 2:
+        raise InternalShellError(cmd, "'umask' supports only one argument")
+    try:
+        # Update the umask in the parent environment.
+        shenv.umask = int(cmd.args[1], 8)
+    except ValueError as err:
+        raise InternalShellError(cmd, "Error: 'umask': %s" % str(err))
+    return ShellCommandResult(cmd, "", "", 0, False)
+
+
 def executeBuiltinColon(cmd, cmd_shenv):
     """executeBuiltinColon - Discard arguments and exit with status 0."""
     return ShellCommandResult(cmd, "", "", 0, False)
@@ -719,6 +732,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
         "popd": executeBuiltinPopd,
         "pushd": executeBuiltinPushd,
         "rm": executeBuiltinRm,
+        "umask": executeBuiltinUmask,
         ":": executeBuiltinColon,
     }
     # To avoid deadlock, we use a single stderr stream for piped
@@ -740,7 +754,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
                 #   env FOO=1 llc < %s | env BAR=2 llvm-mc | FileCheck %s
                 #   env FOO=1 %{another_env_plus_cmd} | FileCheck %s
                 if cmd_shenv is shenv:
-                    cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env)
+                    cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env, shenv.umask)
                 args = updateEnv(cmd_shenv, args)
                 if not args:
                     raise InternalShellError(j, "Error: 'env' requires a" " subcommand")
@@ -884,6 +898,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
                     close_fds=kUseCloseFDs,
                     universal_newlines=True,
                     errors="replace",
+                    umask=cmd_shenv.umask,
                 )
             )
             proc_not_counts.append(not_count)

>From 3e649aa23ef6f88c04bec1735eb563390eb166f9 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Thu, 6 Jun 2024 16:31:01 +0100
Subject: [PATCH 2/3] Update Windows umask explanation

---
 llvm/test/tools/llvm-objcopy/ELF/respect-umask.test | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/test/tools/llvm-objcopy/ELF/respect-umask.test b/llvm/test/tools/llvm-objcopy/ELF/respect-umask.test
index f4d3099056168..02e9b93f5376f 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/respect-umask.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/respect-umask.test
@@ -1,8 +1,7 @@
 ## This tests that the umask is respected when
 ## assigning permissions of output files.
 
-## Windows has no umask so this test makes no sense, nor would
-## it work because there is no umask(1) in a Windows environment
+## Windows has no umask so this test makes no sense.
 # UNSUPPORTED: system-windows
 
 # RUN: rm -f %t

>From 9c5bafc286a9a3334b6f5cb828939a1745bbcf66 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Fri, 7 Jun 2024 14:39:14 +0100
Subject: [PATCH 3/3] Add tests

---
 .../lit/tests/Inputs/shtest-umask/lit.cfg      |  5 +++++
 .../Inputs/shtest-umask/umask-bad-arg.txt      |  1 +
 .../lit/tests/Inputs/shtest-umask/umask-ok.txt | 11 +++++++++++
 .../shtest-umask/umask-too-many-args.txt       |  1 +
 llvm/utils/lit/tests/shtest-umask.py           | 18 ++++++++++++++++++
 5 files changed, 36 insertions(+)
 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-umask/lit.cfg
 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-umask/umask-bad-arg.txt
 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-umask/umask-ok.txt
 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-umask/umask-too-many-args.txt
 create mode 100644 llvm/utils/lit/tests/shtest-umask.py

diff --git a/llvm/utils/lit/tests/Inputs/shtest-umask/lit.cfg b/llvm/utils/lit/tests/Inputs/shtest-umask/lit.cfg
new file mode 100644
index 0000000000000..c630677652303
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-umask/lit.cfg
@@ -0,0 +1,5 @@
+import lit.formats
+
+config.name = "shtest-umask"
+config.suffixes = [".txt"]
+config.test_format = lit.formats.ShTest(execute_external=False)
diff --git a/llvm/utils/lit/tests/Inputs/shtest-umask/umask-bad-arg.txt b/llvm/utils/lit/tests/Inputs/shtest-umask/umask-bad-arg.txt
new file mode 100644
index 0000000000000..639bfd4b7b4f3
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-umask/umask-bad-arg.txt
@@ -0,0 +1 @@
+# RUN: umask bad
diff --git a/llvm/utils/lit/tests/Inputs/shtest-umask/umask-ok.txt b/llvm/utils/lit/tests/Inputs/shtest-umask/umask-ok.txt
new file mode 100644
index 0000000000000..9d43efbddf2cf
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-umask/umask-ok.txt
@@ -0,0 +1,11 @@
+## Windows has no umask so this test makes no sense.
+# UNSUPPORTED: system-windows
+
+# RUN: touch %t
+# RUN: chmod 644 %t && ls -l %t | cut -f 1 -d ' ' > %t.644
+# RUN: chmod 600 %t && ls -l %t | cut -f 1 -d ' ' > %t.600
+# RUN: chmod 666 %t && ls -l %t | cut -f 1 -d ' ' > %t.666
+
+# RUN: umask 022 && rm %t && touch %t && ls -l %t | cut -f 1 -d ' ' | cmp - %t.644
+# RUN: umask 177 && rm %t && touch %t && ls -l %t | cut -f 1 -d ' ' | cmp - %t.600
+# RUN: umask 000 && rm %t && touch %t && ls -l %t | cut -f 1 -d ' ' | cmp - %t.666
diff --git a/llvm/utils/lit/tests/Inputs/shtest-umask/umask-too-many-args.txt b/llvm/utils/lit/tests/Inputs/shtest-umask/umask-too-many-args.txt
new file mode 100644
index 0000000000000..0b7461b5bc3fe
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-umask/umask-too-many-args.txt
@@ -0,0 +1 @@
+# RUN: umask 0 0
diff --git a/llvm/utils/lit/tests/shtest-umask.py b/llvm/utils/lit/tests/shtest-umask.py
new file mode 100644
index 0000000000000..f3e1e64c364aa
--- /dev/null
+++ b/llvm/utils/lit/tests/shtest-umask.py
@@ -0,0 +1,18 @@
+# Check the umask command
+
+# RUN: not %{lit} -a -v %{inputs}/shtest-umask | FileCheck -match-full-lines %s
+
+# CHECK: -- Testing: 3 tests{{.*}}
+
+# CHECK-LABEL: FAIL: shtest-umask :: umask-bad-arg.txt ({{[^)]*}})
+# CHECK: umask bad
+# CHECK: # | Error: 'umask': invalid literal {{.*}}
+
+# CHECK-LABEL: FAIL: shtest-umask :: umask-too-many-args.txt ({{[^)]*}})
+# CHECK: umask 0 0
+# CHECK: # | 'umask' supports only one argument
+
+# CHECK: Total Discovered Tests: 3
+# CHECK: {{Passed|Unresolved}}: 1 {{\([0-9]*\.[0-9]*%\)}}
+# CHECK: Failed{{ *}}: 2 {{\([0-9]*\.[0-9]*%\)}}
+# CHECK-NOT: {{.}}



More information about the llvm-commits mailing list