Skip to content

Commit 15cc2df

Browse files
committed
Handle directory changes in a context manager
1 parent c8054c4 commit 15cc2df

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

scripts/ci/run-crt-tests

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55

66
import os
77
import sys
8+
from contextlib import contextmanager
89
from subprocess import check_call
910

1011
_dname = os.path.dirname
1112

1213
REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__))))
13-
os.chdir(os.path.join(REPO_ROOT, "tests"))
14+
15+
16+
@contextmanager
17+
def cd(path):
18+
"""Change directory while inside context manager."""
19+
cwd = os.getcwd()
20+
try:
21+
os.chdir(path)
22+
yield
23+
finally:
24+
os.chdir(cwd)
1425

1526

1627
def run(command):
@@ -23,4 +34,6 @@ except ImportError:
2334
print("MISSING DEPENDENCY: awscrt must be installed to run the crt tests.")
2435
sys.exit(1)
2536

26-
run(f'{REPO_ROOT}/scripts/ci/run-tests unit/ functional/')
37+
if __name__ == "__main__":
38+
with cd(os.path.join(REPO_ROOT, "tests")):
39+
run(f"{REPO_ROOT}/scripts/ci/run-tests unit/ functional/")

scripts/ci/run-integ-tests

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@
44
# binary package not from the CWD.
55

66
import os
7+
from contextlib import contextmanager
78
from subprocess import check_call
89

910
_dname = os.path.dirname
1011

1112
REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__))))
12-
os.chdir(os.path.join(REPO_ROOT, "tests"))
13+
14+
15+
@contextmanager
16+
def cd(path):
17+
"""Change directory while inside context manager."""
18+
cwd = os.getcwd()
19+
try:
20+
os.chdir(path)
21+
yield
22+
finally:
23+
os.chdir(cwd)
1324

1425

1526
def run(command):
1627
return check_call(command, shell=True)
1728

1829

19-
run(f"{REPO_ROOT}/scripts/ci/run-tests --with-cov integration")
30+
if __name__ == "__main__":
31+
with cd(os.path.join(REPO_ROOT, "tests")):
32+
run(f"{REPO_ROOT}/scripts/ci/run-tests --with-cov integration")

scripts/ci/run-tests

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@
55

66
import argparse
77
import os
8+
from contextlib import contextmanager
89
from subprocess import check_call
910

1011
_dname = os.path.dirname
1112

1213
REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__))))
1314
PACKAGE = "boto3"
14-
os.chdir(os.path.join(REPO_ROOT, "tests"))
15+
16+
17+
@contextmanager
18+
def cd(path):
19+
"""Change directory while inside context manager."""
20+
cwd = os.getcwd()
21+
try:
22+
os.chdir(path)
23+
yield
24+
finally:
25+
os.chdir(cwd)
1526

1627

1728
def run(command):
@@ -20,7 +31,7 @@ def run(command):
2031

2132
def process_args(args):
2233
runner = args.test_runner
23-
test_args = ''
34+
test_args = ""
2435
if args.with_cov:
2536
test_args += (
2637
f"--with-xunit --cover-erase --with-coverage "
@@ -57,4 +68,5 @@ if __name__ == "__main__":
5768

5869
cmd = f"{test_runner} {test_args}{test_dirs}"
5970
print(f"Running {cmd}...")
60-
run(cmd)
71+
with cd(os.path.join(REPO_ROOT, "tests")):
72+
run(cmd)

0 commit comments

Comments
 (0)