diff --git a/src/isolated_environment/__init__.py b/src/isolated_environment/__init__.py index e5e9404..f72bc46 100644 --- a/src/isolated_environment/__init__.py +++ b/src/isolated_environment/__init__.py @@ -1,6 +1,6 @@ from pathlib import Path from subprocess import CompletedProcess -from typing import Any +from typing import Any, Union from .api import IsolatedEnvironment # noqa: F401 from .requirements import Requirements # noqa: F401 @@ -9,9 +9,11 @@ def isolated_environment( - env_path: Path, requirements: list[str] | None = None + env_path: Union[Path, str], requirements: list[str] | None = None ) -> dict[str, Any]: """Creates an isolated environment.""" + if isinstance(env_path, str): + env_path = Path(env_path) # type: ignore requirements = requirements or [] reqs = Requirements(requirements) iso_env = IsolatedEnvironment(env_path, reqs) @@ -20,11 +22,13 @@ def isolated_environment( def isolated_environment_cmd( - env_path: Path, + env_path: Union[Path, str], cmd_list: list[str], requirements: list[str] | None, ) -> CompletedProcess: """Creates an isolated environment.""" + if isinstance(env_path, str): + env_path = Path(env_path) # type: ignore requirements = requirements or [] reqs = Requirements(requirements) iso_env = IsolatedEnvironment(env_path, reqs) diff --git a/tests/run.py b/tests/run.py new file mode 100644 index 0000000..9905989 --- /dev/null +++ b/tests/run.py @@ -0,0 +1,11 @@ +import sys + + +def main() -> int: + """Main function.""" + print("Hello World!") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/test_run_py_file.py b/tests/test_run_py_file.py new file mode 100644 index 0000000..ebb6e8f --- /dev/null +++ b/tests/test_run_py_file.py @@ -0,0 +1,50 @@ +""" +Unit test file. +""" + +import subprocess +import unittest +from pathlib import Path +from tempfile import TemporaryDirectory + +from isolated_environment import isolated_environment + +HERE = Path(__file__).parent + +RUN_PY = HERE / "run.py" + + +class MainTester(unittest.TestCase): + """Main tester class.""" + + def test_shell(self) -> None: + """Test command line interface (CLI).""" + with TemporaryDirectory() as tmp_dir: + venv_path = Path(tmp_dir) / "venv" + env = isolated_environment(venv_path, []) + cmd_list = [ + "python", + str(RUN_PY), + ] + stdout: str = subprocess.check_output( + cmd_list, env=env, shell=True, universal_newlines=True + ) + self.assertEqual("Hello World!\n", stdout) + + def test_no_shell(self) -> None: + """Test command line interface (CLI).""" + with TemporaryDirectory() as tmp_dir: + venv_path = Path(tmp_dir) / "venv" + env = isolated_environment(venv_path, []) + cmd_list = [ + "python", + str(RUN_PY), + ] + stdout: str = subprocess.check_output( + cmd_list, env=env, shell=False, universal_newlines=True + ) + self.assertEqual("Hello World!\n", stdout) + + +if __name__ == "__main__": + unittest.main()