Move chdir contextmanager to utils.py. NFC. (#13091)

This commit is contained in:
Sam Clegg 2020-12-22 11:05:43 -08:00 коммит произвёл GitHub
Родитель e61e33c96b
Коммит ca07b207f3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 23 добавлений и 26 удалений

11
tests/runner.py поставляемый
Просмотреть файл

@ -226,17 +226,6 @@ def with_env_modify(updates):
return decorated
@contextlib.contextmanager
def chdir(dir):
"""A context manager that performs actions in the given directory."""
orig_cwd = os.getcwd()
os.chdir(dir)
try:
yield
finally:
os.chdir(orig_cwd)
def ensure_dir(dirname):
if not os.path.isdir(dirname):
os.makedirs(dirname)

6
tests/test_other.py поставляемый
Просмотреть файл

@ -32,7 +32,7 @@ from tools.shared import try_delete, config
from tools.shared import EMCC, EMXX, EMAR, EMRANLIB, PYTHON, FILE_PACKAGER, WINDOWS, EM_BUILD_VERBOSE
from tools.shared import CLANG_CC, CLANG_CXX, LLVM_AR, LLVM_DWARFDUMP
from runner import RunnerCore, path_from_root, is_slow_test, ensure_dir, disabled, make_executable
from runner import env_modify, no_mac, no_windows, requires_native_clang, chdir, with_env_modify
from runner import env_modify, no_mac, no_windows, requires_native_clang, with_env_modify
from runner import create_test_file, parameterized, NON_ZERO, node_pthreads
from tools import shared, building, utils
import jsrun
@ -2014,7 +2014,7 @@ int f() {
# this test copies the site_scons directory alongside the test
shutil.copytree(path_from_root('tests', 'scons'), 'test')
shutil.copytree(path_from_root('tools', 'scons', 'site_scons'), os.path.join('test', 'site_scons'))
with chdir('test'):
with utils.chdir('test'):
self.run_process(['scons'])
output = self.run_js('scons_integration.js', assert_returncode=5)
self.assertContained('If you see this - the world is all right!', output)
@ -2026,7 +2026,7 @@ int f() {
# uses the emscons wrapper which requires EMSCRIPTEN_TOOLPATH to find
# site_scons
shutil.copytree(path_from_root('tests', 'scons'), 'test')
with chdir('test'):
with utils.chdir('test'):
self.run_process([path_from_root('emscons'), 'scons'])
output = self.run_js('scons_integration.js', assert_returncode=5)
self.assertContained('If you see this - the world is all right!', output)

6
tests/test_sanity.py поставляемый
Просмотреть файл

@ -12,14 +12,14 @@ import tempfile
import zipfile
from subprocess import PIPE, STDOUT
from runner import RunnerCore, path_from_root, env_modify, chdir
from runner import RunnerCore, path_from_root, env_modify
from runner import create_test_file, ensure_dir, make_executable
from tools.config import config_file, EM_CONFIG
from tools.shared import PYTHON, EMCC
from tools.shared import CANONICAL_TEMP_DIR
from tools.shared import try_delete, config
from tools.shared import EXPECTED_LLVM_VERSION, Cache
from tools import shared, system_libs
from tools import shared, system_libs, utils
SANITY_FILE = shared.Cache.get_path('sanity.txt', root=True)
commands = [[EMCC], [PYTHON, path_from_root('tests', 'runner.py'), 'blahblah']]
@ -521,7 +521,7 @@ fi
temp_dir = tempfile.mkdtemp(prefix='emscripten_temp_')
with chdir(temp_dir):
with utils.chdir(temp_dir):
self.run_process([EMCC, '--em-config', custom_config_filename] + MINIMAL_HELLO_WORLD + ['-O2'])
result = self.run_js('a.out.js')

6
tests/test_sockets.py поставляемый
Просмотреть файл

@ -24,8 +24,8 @@ except Exception:
# which is the same behavior as before.
pass
import clang_native
from runner import BrowserCore, no_windows, chdir, create_test_file
from tools import shared, config
from runner import BrowserCore, no_windows, create_test_file
from tools import shared, config, utils
from tools.shared import PYTHON, EMCC, path_from_root, WINDOWS, run_process, CLANG_CC
npm_checked = False
@ -286,7 +286,7 @@ class sockets(BrowserCore):
# this is also a good test of raw usage of emconfigure and emmake
shared.try_delete('enet')
shutil.copytree(path_from_root('tests', 'third_party', 'enet'), 'enet')
with chdir('enet'):
with utils.chdir('enet'):
self.run_process([path_from_root('emconfigure'), './configure'])
self.run_process([path_from_root('emmake'), 'make'])
enet = [self.in_dir('enet', '.libs', 'libenet.a'), '-I' + self.in_dir('enet', 'include')]

Просмотреть файл

@ -15,7 +15,7 @@ import tarfile
import zipfile
from glob import iglob
from . import shared, building, ports, config
from . import shared, building, ports, config, utils
from . import deps_info
from tools.shared import mangle_c_symbol_name, demangle_c_symbol_name
@ -1816,12 +1816,8 @@ class Ports(object):
z = tarfile.open(fullpath, 'r:gz')
else:
z = zipfile.ZipFile(fullpath, 'r')
try:
cwd = os.getcwd()
os.chdir(fullname)
with utils.chdir(fullname):
z.extractall()
finally:
os.chdir(cwd)
State.unpacked = True

Просмотреть файл

@ -3,6 +3,7 @@
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.
import contextlib
import os
import sys
@ -32,6 +33,17 @@ def safe_ensure_dirs(dirname):
raise
@contextlib.contextmanager
def chdir(dir):
"""A context manager that performs actions in the given directory."""
orig_cwd = os.getcwd()
os.chdir(dir)
try:
yield
finally:
os.chdir(orig_cwd)
# Finds the given executable 'program' in PATH. Operates like the Unix tool 'which'.
def which(program):
def is_exe(fpath):