Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed Jan 20, 2024
1 parent b1ced56 commit 198c473
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/isolated_environment/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions tests/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sys


def main() -> int:
"""Main function."""
print("Hello World!")
return 0


if __name__ == "__main__":
sys.exit(main())
50 changes: 50 additions & 0 deletions tests/test_run_py_file.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 198c473

Please sign in to comment.