Skip to content

Commit c7f46e8

Browse files
authored
Remove utils.chdir. NFC (#23382)
This utility was only used in test code so move it there.
1 parent 2a6eeeb commit c7f46e8

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

test/common.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,17 @@ def create_file(name, contents, binary=False, absolute=False):
735735
name.write_text(contents, encoding='utf-8')
736736

737737

738+
@contextlib.contextmanager
739+
def chdir(dir):
740+
"""A context manager that performs actions in the given directory."""
741+
orig_cwd = os.getcwd()
742+
os.chdir(dir)
743+
try:
744+
yield
745+
finally:
746+
os.chdir(orig_cwd)
747+
748+
738749
def make_executable(name):
739750
Path(name).chmod(stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
740751

test/test_browser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from common import requires_wasm2js, also_with_wasm2js, parameterize, find_browser_test_file
2828
from tools import shared
2929
from tools import ports
30-
from tools import utils
3130
from tools.shared import EMCC, WINDOWS, FILE_PACKAGER, PIPE, DEBUG
3231
from tools.utils import delete_dir
3332

@@ -5455,7 +5454,7 @@ def test_webpack(self, es6):
54555454
else:
54565455
shutil.copytree(test_file('webpack'), 'webpack')
54575456
outfile = 'src/hello.js'
5458-
with utils.chdir('webpack'):
5457+
with common.chdir('webpack'):
54595458
self.compile_btest('hello_world.c', ['-sEXIT_RUNTIME', '-sMODULARIZE', '-sENVIRONMENT=web,worker', '-o', outfile])
54605459
self.run_process(shared.get_npm_cmd('webpack') + ['--mode=development', '--no-devtool'])
54615460
shutil.copy('webpack/src/hello.wasm', 'webpack/dist/')

test/test_other.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ def test_cmake(self, test_dir, output_file, cmake_args):
939939
cmakelistsdir = test_file('cmake', test_dir)
940940
builddir = 'out_' + generator.replace(' ', '_').lower()
941941
os.mkdir(builddir)
942-
with utils.chdir(builddir):
942+
with common.chdir(builddir):
943943
# Run Cmake
944944
cmd = [EMCMAKE, 'cmake'] + cmake_args + ['-G', generator, cmakelistsdir]
945945

@@ -3119,7 +3119,7 @@ def test_scons(self):
31193119
# this test copies the site_scons directory alongside the test
31203120
shutil.copytree(test_file('scons/simple'), 'test')
31213121
shutil.copytree(path_from_root('tools/scons/site_scons'), Path('test/site_scons'))
3122-
with utils.chdir('test'):
3122+
with common.chdir('test'):
31233123
self.run_process(['scons'])
31243124
output = self.run_js('scons_integration.js', assert_returncode=5)
31253125
self.assertContained('If you see this - the world is all right!', output)
@@ -3146,7 +3146,7 @@ def test_scons_env(self):
31463146
}
31473147
})
31483148

3149-
with utils.chdir('test'):
3149+
with common.chdir('test'):
31503150
self.run_process(['scons', '--expected-env', expected_to_propagate])
31513151

31523152
@requires_scons
@@ -3165,13 +3165,13 @@ def test_scons_env_no_emscons(self):
31653165
}
31663166
})
31673167

3168-
with utils.chdir('test'):
3168+
with common.chdir('test'):
31693169
self.run_process(['scons', '--expected-env', expected_to_propagate])
31703170

31713171
@requires_scons
31723172
def test_emscons(self):
31733173
shutil.copytree(test_file('scons/simple'), 'test')
3174-
with utils.chdir('test'):
3174+
with common.chdir('test'):
31753175
self.run_process([path_from_root('emscons'), 'scons'])
31763176
output = self.run_js('scons_integration.js', assert_returncode=5)
31773177
self.assertContained('If you see this - the world is all right!', output)
@@ -3192,7 +3192,7 @@ def test_emscons_env(self):
31923192
}
31933193
})
31943194

3195-
with utils.chdir('test'):
3195+
with common.chdir('test'):
31963196
self.run_process([path_from_root('emscons'), 'scons', '--expected-env', expected_to_propagate])
31973197

31983198
def test_embind_fail(self):

test/test_sanity.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pathlib import Path
1414
from subprocess import PIPE, STDOUT
1515

16+
import common
1617
from common import RunnerCore, path_from_root, env_modify, test_file
1718
from common import create_file, ensure_dir, make_executable, with_env_modify
1819
from common import crossplatform, parameterized, EMBUILDER
@@ -515,7 +516,7 @@ def test_emconfig(self):
515516

516517
temp_dir = tempfile.mkdtemp(prefix='emscripten_temp_')
517518

518-
with utils.chdir(temp_dir):
519+
with common.chdir(temp_dir):
519520
self.run_process([EMCC, '--em-config', custom_config_filename] + MINIMAL_HELLO_WORLD + ['-O2'])
520521
result = self.run_js('a.out.js')
521522

test/test_sockets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import common
2020
from common import BrowserCore, no_windows, create_file, test_file, read_file
2121
from common import parameterized, requires_native_clang, crossplatform, PYTHON, NON_ZERO
22-
from tools import config, utils
22+
from tools import config
2323
from tools.shared import EMCC, path_from_root, run_process, CLANG_CC
2424

2525
npm_checked = False
@@ -272,7 +272,7 @@ def test_sockets_select_server_closes_connection_rw(self):
272272
def test_enet(self):
273273
# this is also a good test of raw usage of emconfigure and emmake
274274
shutil.copytree(test_file('third_party', 'enet'), 'enet')
275-
with utils.chdir('enet'):
275+
with common.chdir('enet'):
276276
self.run_process([path_from_root('emconfigure'), './configure', '--disable-shared'])
277277
self.run_process([path_from_root('emmake'), 'make'])
278278
enet = [self.in_dir('enet', '.libs', 'libenet.a'), '-I' + self.in_dir('enet', 'include')]

tools/utils.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# University of Illinois/NCSA Open Source License. Both these licenses can be
44
# found in the LICENSE file.
55

6-
import contextlib
76
import os
87
import shutil
98
import sys
@@ -47,17 +46,6 @@ def removeprefix(string, prefix):
4746
return string
4847

4948

50-
@contextlib.contextmanager
51-
def chdir(dir):
52-
"""A context manager that performs actions in the given directory."""
53-
orig_cwd = os.getcwd()
54-
os.chdir(dir)
55-
try:
56-
yield
57-
finally:
58-
os.chdir(orig_cwd)
59-
60-
6149
def read_file(file_path):
6250
"""Read from a file opened in text mode"""
6351
with open(file_path, encoding='utf-8') as fh:

0 commit comments

Comments
 (0)