diff --git a/edq/testing/cli.py b/edq/testing/cli.py index e2b18c4..19755e0 100644 --- a/edq/testing/cli.py +++ b/edq/testing/cli.py @@ -53,6 +53,7 @@ def __init__(self, base_dir: str, data_dir: str, temp_dir: str, + work_dir: typing.Union[str, None] = None, cli: typing.Union[str, None] = None, arguments: typing.Union[typing.List[str], None] = None, error: bool = False, @@ -103,6 +104,13 @@ def __init__(self, edq.util.dirent.mkdir(temp_dir) + self.work_dir: str = os.getcwd() + """ The directory the test runs from. """ + + if (work_dir is not None): + work_dir_expanded = self._expand_paths(work_dir) + self.work_dir = work_dir_expanded + if (cli is None): raise ValueError("Missing CLI module.") @@ -270,6 +278,9 @@ def __method(self: edq.testing.unittest.BaseTest) -> None: old_args = sys.argv sys.argv = [test_info.module.__file__] + test_info.arguments + previous_work_directory = os.getcwd() + os.chdir(test_info.work_dir) + try: with contextlib.redirect_stdout(io.StringIO()) as stdout_output: with contextlib.redirect_stderr(io.StringIO()) as stderr_output: @@ -290,6 +301,8 @@ def __method(self: edq.testing.unittest.BaseTest) -> None: if (isinstance(ex, SystemExit) and (ex.__context__ is not None)): stderr_text = self.format_error_string(ex.__context__) finally: + os.chdir(previous_work_directory) + sys.argv = old_args if (not test_info.split_stdout_stderr):