Skip to content

Commit bfe724f

Browse files
authored
[test] test_sanity fixes. NFC (#25798)
Fix tests `sanity.test_required_config_settings`, which would not pass if `wasm-opt` is in PATH, and test `sanity.test_firstrun` which would not pass if `clang` is in PATH.
1 parent 9aabfb4 commit bfe724f

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

test/test_sanity.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ def make_fake_clang(filename, version, targets='wasm32 - WebAssembly 32-bit'):
100100
make_fake_tool(filename + '++', version, output)
101101

102102

103+
# Return a new PATH that has no directories that would contain the given tool.
104+
def path_without_tool(env_path, tool_bin):
105+
tool_bin = utils.exe_suffix(tool_bin)
106+
python_path = os.path.normpath(os.path.dirname(sys.executable))
107+
108+
def ignore_path(p):
109+
# We cannot ignore a path element that contains the python executable itself, otherwise
110+
# the bootstrap script will fail
111+
if os.path.isfile(os.path.join(p, tool_bin)) and os.path.normpath(p) != python_path:
112+
return True
113+
return False
114+
115+
old_path = env_path.split(os.pathsep)
116+
return os.pathsep.join([d for d in old_path if not ignore_path(d)])
117+
118+
103119
SANITY_MESSAGE = 'Emscripten: Running sanity checks'
104120

105121
# arguments to build a minimal hello world program, without even libc
@@ -153,7 +169,7 @@ def do(self, command, env=None):
153169

154170
return self.run_process(command, stdout=PIPE, stderr=STDOUT, check=False, env=env).stdout
155171

156-
def check_working(self, command, expected=None):
172+
def check_working(self, command, expected=None, env=None):
157173
if type(command) is not list:
158174
command = [command]
159175
if command == [EMCC]:
@@ -164,7 +180,7 @@ def check_working(self, command, expected=None):
164180
else:
165181
expected = 'could not find the following tests: blahblah'
166182

167-
output = self.do(command)
183+
output = self.do(command, env=env)
168184
self.assertContained(expected, output)
169185
return output
170186

@@ -178,8 +194,13 @@ def test_aaa_normal(self):
178194

179195
@with_env_modify({'EM_CONFIG': None})
180196
def test_firstrun(self):
197+
# Remove from PATH every directory that contains clang.exe so config setup
198+
# cannot accidentally succeed by virtue of locating tools in PATH.
199+
env = os.environ.copy()
200+
env['PATH'] = path_without_tool(env['PATH'], 'clang')
201+
181202
default_config = path_from_root('.emscripten')
182-
output = self.do([EMCC, '-v'])
203+
output = self.do([EMCC, '-v'], env=env)
183204
self.assertContained('emcc: warning: config file not found: %s. You can create one by hand or run `emcc --generate-config`' % default_config, output)
184205

185206
temp_bin = os.path.abspath('bin')
@@ -688,11 +709,16 @@ def test_required_config_settings(self):
688709
# with no binaryen root, an error is shown
689710
restore_and_set_up()
690711

712+
# Remove wasm-opt from PATH so config setup cannot accidentally succeed by
713+
# virtue of locating it in PATH.
714+
env = os.environ.copy()
715+
env['PATH'] = path_without_tool(env['PATH'], 'wasm-opt')
716+
691717
open(EM_CONFIG, 'a').write('\nBINARYEN_ROOT = ""\n')
692-
self.check_working([EMCC, test_file('hello_world.c')], 'BINARYEN_ROOT is set to empty value in %s' % EM_CONFIG)
718+
self.check_working([EMCC, test_file('hello_world.c')], 'BINARYEN_ROOT is set to empty value in %s' % EM_CONFIG, env=env)
693719

694720
open(EM_CONFIG, 'a').write('\ndel BINARYEN_ROOT\n')
695-
self.check_working([EMCC, test_file('hello_world.c')], 'BINARYEN_ROOT not set in config (%s), and `wasm-opt` not found in PATH' % EM_CONFIG)
721+
self.check_working([EMCC, test_file('hello_world.c')], 'BINARYEN_ROOT not set in config (%s), and `wasm-opt` not found in PATH' % EM_CONFIG, env=env)
696722

697723
@no_windows('Test relies on Unix-specific make_fake_tool')
698724
def test_empty_config(self):
@@ -830,21 +856,9 @@ def test_bootstrap_without_em_config(self):
830856
for e in ['LLVM_ROOT', 'EMSDK_NODE', 'EMSDK_PYTHON', 'EMSDK', 'EMSCRIPTEN', 'BINARYEN_ROOT', 'EMCC_SKIP_SANITY_CHECK', 'EM_CONFIG']:
831857
env.pop(e, None)
832858

833-
python_path = os.path.normpath(os.path.dirname(sys.executable))
834-
835859
# Remove from PATH every directory that contains clang.exe so that bootstrap.py cannot
836860
# accidentally succeed by virtue of locating tools in PATH.
837-
def ignore_path(p):
838-
clang_bin = utils.exe_suffix('clang')
839-
# We cannot ignore a path element that contains the python executable itself, otherwise
840-
# the bootstrap script will fail
841-
if os.path.isfile(os.path.join(p, clang_bin)) and os.path.normpath(p) != python_path:
842-
return True
843-
return False
844-
845-
old_path = env['PATH'].split(os.pathsep)
846-
new_path = [d for d in old_path if not ignore_path(d)]
847-
env['PATH'] = os.pathsep.join(new_path)
861+
env['PATH'] = path_without_tool(env['PATH'], 'clang')
848862

849863
# Running bootstrap.py should not fail
850864
self.run_process([shared.bat_suffix(path_from_root('bootstrap'))], env=env)

0 commit comments

Comments
 (0)