Skip to content

Commit 9f6dd1f

Browse files
authored
[lit] Implement builtin umask (llvm#94621)
This allows a few more tests to use lit's internal shell.
1 parent 390f9b1 commit 9f6dd1f

File tree

9 files changed

+58
-7
lines changed

9 files changed

+58
-7
lines changed

llvm/test/tools/llvm-dwarfutil/ELF/X86/mirror-permissions-unix.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
## Setting the umask to 0 ensures deterministic permissions across
44
## test environments.
55
# UNSUPPORTED: system-windows
6-
# REQUIRES: shell
76

87
# RUN: touch %t
98
# RUN: chmod 0777 %t

llvm/test/tools/llvm-objcopy/ELF/mirror-permissions-unix.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
## Setting the umask to 0 ensures deterministic permissions across
77
## test environments.
88
# UNSUPPORTED: system-windows
9-
# REQUIRES: shell
109

1110
# RUN: touch %t
1211
# RUN: chmod 0777 %t

llvm/test/tools/llvm-objcopy/ELF/respect-umask.test

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
## This tests that the umask is respected when
22
## assigning permissions of output files.
33

4-
## Windows has no umask so this test makes no sense, nor would
5-
## it work because there is no umask(1) in a Windows environment
4+
## Windows has no umask so this test makes no sense.
65
# UNSUPPORTED: system-windows
7-
# REQUIRES: shell
86

97
# RUN: rm -f %t
108
# RUN: touch %t

llvm/utils/lit/lit/TestRunner.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ class ShellEnvironment(object):
8787
we maintain a dir stack for pushd/popd.
8888
"""
8989

90-
def __init__(self, cwd, env):
90+
def __init__(self, cwd, env, umask=-1):
9191
self.cwd = cwd
9292
self.env = dict(env)
93+
self.umask = umask
9394
self.dirStack = []
9495

9596
def change_dir(self, newdir):
@@ -565,6 +566,20 @@ class SHFILEOPSTRUCTW(Structure):
565566
return ShellCommandResult(cmd, "", stderr.getvalue(), exitCode, False)
566567

567568

569+
def executeBuiltinUmask(cmd, shenv):
570+
"""executeBuiltinUmask - Change the current umask."""
571+
if os.name != "posix":
572+
raise InternalShellError(cmd, "'umask' not supported on this system")
573+
if len(cmd.args) != 2:
574+
raise InternalShellError(cmd, "'umask' supports only one argument")
575+
try:
576+
# Update the umask in the parent environment.
577+
shenv.umask = int(cmd.args[1], 8)
578+
except ValueError as err:
579+
raise InternalShellError(cmd, "Error: 'umask': %s" % str(err))
580+
return ShellCommandResult(cmd, "", "", 0, False)
581+
582+
568583
def executeBuiltinColon(cmd, cmd_shenv):
569584
"""executeBuiltinColon - Discard arguments and exit with status 0."""
570585
return ShellCommandResult(cmd, "", "", 0, False)
@@ -719,6 +734,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
719734
"popd": executeBuiltinPopd,
720735
"pushd": executeBuiltinPushd,
721736
"rm": executeBuiltinRm,
737+
"umask": executeBuiltinUmask,
722738
":": executeBuiltinColon,
723739
}
724740
# To avoid deadlock, we use a single stderr stream for piped
@@ -740,7 +756,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
740756
# env FOO=1 llc < %s | env BAR=2 llvm-mc | FileCheck %s
741757
# env FOO=1 %{another_env_plus_cmd} | FileCheck %s
742758
if cmd_shenv is shenv:
743-
cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env)
759+
cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env, shenv.umask)
744760
args = updateEnv(cmd_shenv, args)
745761
if not args:
746762
raise InternalShellError(j, "Error: 'env' requires a" " subcommand")
@@ -884,6 +900,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
884900
close_fds=kUseCloseFDs,
885901
universal_newlines=True,
886902
errors="replace",
903+
umask=cmd_shenv.umask,
887904
)
888905
)
889906
proc_not_counts.append(not_count)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import lit.formats
2+
3+
config.name = "shtest-umask"
4+
config.suffixes = [".txt"]
5+
config.test_format = lit.formats.ShTest(execute_external=False)
6+
if sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
7+
config.available_features.add("system-windows")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# RUN: umask bad
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Windows has no umask so this test makes no sense.
2+
# UNSUPPORTED: system-windows
3+
4+
# RUN: touch %t
5+
# RUN: chmod 644 %t && ls -l %t | cut -f 1 -d ' ' > %t.644
6+
# RUN: chmod 600 %t && ls -l %t | cut -f 1 -d ' ' > %t.600
7+
# RUN: chmod 666 %t && ls -l %t | cut -f 1 -d ' ' > %t.666
8+
9+
# RUN: umask 022 && rm %t && touch %t && ls -l %t | cut -f 1 -d ' ' | cmp - %t.644
10+
# RUN: umask 177 && rm %t && touch %t && ls -l %t | cut -f 1 -d ' ' | cmp - %t.600
11+
# RUN: umask 000 && rm %t && touch %t && ls -l %t | cut -f 1 -d ' ' | cmp - %t.666
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# RUN: umask 0 0

llvm/utils/lit/tests/shtest-umask.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Check the umask command
2+
3+
# RUN: not %{lit} -a -v %{inputs}/shtest-umask | FileCheck -match-full-lines %s
4+
5+
# CHECK: -- Testing: 3 tests{{.*}}
6+
7+
# CHECK-LABEL: FAIL: shtest-umask :: umask-bad-arg.txt ({{[^)]*}})
8+
# CHECK: umask bad
9+
# CHECK: # | Error: 'umask': invalid literal {{.*}}
10+
11+
# CHECK-LABEL: FAIL: shtest-umask :: umask-too-many-args.txt ({{[^)]*}})
12+
# CHECK: umask 0 0
13+
# CHECK: # | 'umask' supports only one argument
14+
15+
# CHECK: Total Discovered Tests: 3
16+
# CHECK: {{Passed|Unsupported}}: 1 {{\([0-9]*\.[0-9]*%\)}}
17+
# CHECK: Failed{{ *}}: 2 {{\([0-9]*\.[0-9]*%\)}}
18+
# CHECK-NOT: {{.}}

0 commit comments

Comments
 (0)