Skip to content

Commit 0ba015a

Browse files
authored
Remove tempfile usage from test_sanity.py. NFC (emscripten-core#25617)
In both cases the tempfile usage was not necessary. test_emconfig: In this case we can just create a new config file in the CWD. Because the test corrupts the main config file there is no risk that the test is accidentally finding the config file via CWD alone so there is no need to change directory into an external temp dir. test_firstrun: We an just create the fake `bin` files in a subdirectory of the current directory where the test runs. No need for an external temp dir here. This change also makes 3 of the imports in this file no longer needed!
1 parent 2a6586c commit 0ba015a

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

test/test_sanity.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55

66
import glob
77
import os
8-
import platform
98
import re
109
import shutil
1110
import stat
12-
import tempfile
1311
import time
1412
from pathlib import Path
1513
from subprocess import PIPE, STDOUT
1614

17-
import common
1815
from common import (
1916
EMBUILDER,
2017
RunnerCore,
@@ -180,32 +177,31 @@ def test_aaa_normal(self):
180177

181178
@with_env_modify({'EM_CONFIG': None})
182179
def test_firstrun(self):
183-
default_config = path_from_root('.emscripten')
180+
default_config = EM_CONFIG
184181
output = self.do([EMCC, '-v'])
185182
self.assertContained('emcc: warning: config file not found: %s. You can create one by hand or run `emcc --generate-config`' % default_config, output)
186183

187-
try:
188-
temp_bin = tempfile.mkdtemp()
184+
temp_bin = os.path.abspath('bin')
185+
os.mkdir(temp_bin)
189186

190-
def make_new_executable(name):
191-
utils.write_file(os.path.join(temp_bin, name), '')
192-
make_executable(os.path.join(temp_bin, name))
187+
def make_new_executable(name):
188+
utils.write_file(os.path.join(temp_bin, name), '')
189+
make_executable(os.path.join(temp_bin, name))
193190

194-
make_new_executable('wasm-ld')
195-
make_new_executable('node')
191+
make_new_executable('wasm-ld')
192+
make_new_executable('node')
196193

197-
with env_modify({'PATH': temp_bin + os.pathsep + os.environ['PATH']}):
198-
output = self.do([EMCC, '--generate-config'])
199-
finally:
200-
shutil.rmtree(temp_bin)
201-
config_data = utils.read_file(default_config)
194+
with env_modify({'PATH': temp_bin + os.pathsep + os.environ['PATH']}):
195+
output = self.do([EMCC, '--generate-config'])
196+
197+
config_data = utils.read_file(default_config)
202198

203199
self.assertContained('An Emscripten settings file has been generated at:', output)
204200
self.assertContained(default_config, output)
205201
self.assertContained('It contains our best guesses for the important paths, which are:', output)
206202
self.assertContained('LLVM_ROOT', output)
207203
self.assertContained('NODE_JS', output)
208-
if platform.system() != 'Windows':
204+
if not utils.WINDOWS:
209205
# os.chmod can't make files executable on Windows
210206
self.assertIdentical(temp_bin, re.search("^ *LLVM_ROOT *= (.*)$", output, re.M).group(1))
211207
possible_nodes = [os.path.join(temp_bin, 'node')]
@@ -524,29 +520,13 @@ def test_emcc_cache_flag(self, use_response_files, relative):
524520

525521
def test_emconfig(self):
526522
restore_and_set_up()
527-
528-
fd, custom_config_filename = tempfile.mkstemp(prefix='.emscripten_config_')
529-
530-
orig_config = utils.read_file(EM_CONFIG)
531-
532-
# Move the ~/.emscripten to a custom location.
533-
with os.fdopen(fd, "w") as f:
534-
f.write(get_basic_config())
523+
create_file('custom_config', get_basic_config())
535524

536525
# Make a syntax error in the original config file so that attempting to access it would fail.
537-
utils.write_file(EM_CONFIG, 'asdfasdfasdfasdf\n\'\'\'' + orig_config)
538-
539-
temp_dir = tempfile.mkdtemp(prefix='emscripten_temp_')
540-
541-
with common.chdir(temp_dir):
542-
self.run_process([EMCC, '--em-config', custom_config_filename] + MINIMAL_HELLO_WORLD + ['-O2'])
543-
result = self.run_js('a.out.js')
544-
545-
self.assertContained('hello, world!', result)
526+
utils.write_file(EM_CONFIG, 'asdfasdfasdfasdf\n')
546527

547-
# Clean up created temp files.
548-
os.remove(custom_config_filename)
549-
shutil.rmtree(temp_dir)
528+
self.run_process([EMCC, '--em-config', 'custom_config'] + MINIMAL_HELLO_WORLD)
529+
self.assertContained('hello, world!', self.run_js('a.out.js'))
550530

551531
def test_emcc_ports(self):
552532
restore_and_set_up()

0 commit comments

Comments
 (0)