Skip to content

Commit

Permalink
Drop the venv requirement from running tests (#1661)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Feb 13, 2024
1 parent 6db83fe commit b917d30
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source_up
export PYTEST_CHECK_TEST_DUPLICATE=0
export PYTEST_CHECK_TEST_ID_REGEX=0
export PYTEST_MAX_TEST_ID_LENGTH=0
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,6 @@ dmypy.json
/src/ansible_navigator/_version.py
out

# other
.DS_Store
.envrc
15 changes: 0 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,6 @@ def patch_curses(monkeypatch) -> None:
monkeypatch.setattr("curses.endwin", lambda: None)


@pytest.fixture
def use_venv(monkeypatch: pytest.MonkeyPatch) -> None:
"""Set the path such that it includes the virtual environment.
:param monkeypatch: Fixture for patching
:raises AssertionError: If the virtual environment is not set
"""
venv_path = os.environ.get("VIRTUAL_ENV")
if venv_path is None:
msg = "VIRTUAL_ENV environment variable was not set but tox should have set it."
raise AssertionError(msg)
path_prepend = Path.cwd() / venv_path / "bin"
monkeypatch.setenv("PATH", str(path_prepend), prepend=os.pathsep)


@pytest.fixture(name="settings_samples")
def _settings_samples() -> tuple[str, str]:
"""Provide the full settings samples.
Expand Down
10 changes: 4 additions & 6 deletions tests/integration/_tmux_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def __enter__(self) -> TmuxSession:
"""Enter the tmux session.
:return: The tmux session
:raises AssertionError: If VIRTUAL_ENV environment variable was not set
:raises ValueError: If the time is exceeded for finding the shell prompt
"""
# pylint: disable=attribute-defined-outside-init
Expand All @@ -131,10 +130,7 @@ def __enter__(self) -> TmuxSession:
# expect inside of tmux, so we can't depend on it. We *must* determine
# it before we enter tmux. Do this before we switch to bash
venv_path = os.environ.get("VIRTUAL_ENV")
if venv_path is None:
msg = "VIRTUAL_ENV environment variable was not set but tox should have set it."
raise AssertionError(msg)
venv = os.path.join(shlex.quote(venv_path), "bin", "activate")
venv = "" if venv_path is None else os.path.join(shlex.quote(venv_path), "bin", "activate")

# get the USER before we start a clean shell
user = os.environ.get("USER")
Expand All @@ -147,7 +143,9 @@ def __enter__(self) -> TmuxSession:
self._pane.send_keys(f"export PS1={self.cli_prompt}")

# set environment variables for this session
tmux_common = [f". {venv}"]
tmux_common = []
if venv:
tmux_common.append(f". {venv}")
tmux_common.append("export TERM=xterm")
tmux_common.append("export LANG=en_US.UTF-8")
tmux_common.append(f"export HOME='{home}'")
Expand Down
1 change: 0 additions & 1 deletion tests/integration/actions/doc/test_stdout_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def command(self) -> tuple[str, ...]:
)


@pytest.mark.usefixtures("use_venv")
@pytest.mark.parametrize(argnames="data", argvalues=StdoutCliTests, ids=id_func)
@pytest.mark.parametrize(argnames="exec_env", argvalues=(True, False), ids=("ee_true", "ee_false"))
def test(
Expand Down
1 change: 0 additions & 1 deletion tests/integration/diagnostics/test_from_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from ansible_navigator.utils.functions import remove_ansi


@pytest.mark.usefixtures("use_venv")
def test(
monkeypatch: pytest.MonkeyPatch,
settings_env_var_to_full: tuple[Path, SettingsFileType],
Expand Down
13 changes: 7 additions & 6 deletions tests/integration/settings_from_cli/test_json_schema_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,17 @@ def test(data: Scenario, subtests: Any, tmp_path: Path):
"""
assert data.settings_file.exists()
venv_path = os.environ.get("VIRTUAL_ENV")
if venv_path is None:
msg = "VIRTUAL_ENV environment variable was not set but tox should have set it."
raise AssertionError(msg)
venv = Path(venv_path, "bin", "activate")
venv_prefix = ""
if venv_path is not None:
venv = str(Path(venv_path, "bin", "activate"))
venv_prefix = f"source {venv} && "
log_file = tmp_path / "log.txt"

command = list(data.command) + ["--lf", str(log_file)]

bash_wrapped = f"/bin/bash -c 'source {venv!s} && {shlex_join(command)}'"
env = {"ANSIBLE_NAVIGATOR_CONFIG": str(data.settings_file), "NO_COLOR": "true"}
bash_wrapped = f"/bin/bash -c '{venv_prefix!s}{shlex_join(command)}'"
# Some os.environ are required in order to make it work, likely HOME and PATH at least.
env = os.environ | {"ANSIBLE_NAVIGATOR_CONFIG": str(data.settings_file), "NO_COLOR": "true"}
proc_out = subprocess.run(
bash_wrapped,
check=False,
Expand Down
1 change: 0 additions & 1 deletion tests/integration/test_stdout_exit_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ def command(self) -> list[str]:
)


@pytest.mark.usefixtures("use_venv")
@pytest.mark.parametrize(argnames="pae", argvalues=(True, False), ids=("pae_true", "pae_false"))
@pytest.mark.parametrize(argnames="exec_env", argvalues=(True, False), ids=("ee_true", "ee_false"))
@pytest.mark.parametrize(argnames="data", argvalues=StdoutCliTests, ids=id_func)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/version_migration/test_v1_v2_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


@pytest.mark.parametrize("file", files)
def test_all(
def test_version(
file: str,
request: pytest.FixtureRequest,
test_dir_fixture_dir: Path,
Expand Down
11 changes: 5 additions & 6 deletions tests/smoke/no_test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
from ansible_navigator.command_runner import CommandRunner


def _get_venv():
def _get_venv_prefix() -> str:
venv_path = os.environ.get("VIRTUAL_ENV")
if venv_path is None:
msg = "VIRTUAL_ENV environment variable was not set but tox should have set it."
raise AssertionError(msg)
return ""
venv = Path(venv_path, "bin", "activate")
return venv
return f". {venv} && "


@dataclass
Expand All @@ -35,8 +34,8 @@ class NavigatorCommand(Command):
def __post_init__(self):
"""Post the init."""
self.identity = self.command
venv = _get_venv()
self.command = f". {venv} && ansible-navigator {self.command} {self.set_env}"
venv = _get_venv_prefix()
self.command = f"{venv}ansible-navigator {self.command} {self.set_env}"


@dataclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"""


@pytest.mark.usefixtures("use_venv")
@ee_states
def test_from_ansible_cfg(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
"""Confirm inventory is used from a valid ansible.cfg.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"""


@pytest.mark.usefixtures("use_venv")
@ee_states
def test_valid_config(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
"""Confirm a valid ansible.cfg is parsed.
Expand All @@ -49,7 +48,6 @@ def test_valid_config(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatc
assert parsed_cfg.config.text == ANSIBLE_CFG_VALID.splitlines()


@pytest.mark.usefixtures("use_venv")
@ee_states
def test_valid_configurator(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
"""Confirm a valid ansible.cfg is parsed using configurator.
Expand Down Expand Up @@ -80,7 +78,6 @@ def test_valid_configurator(ee_enabled, tmp_path: Path, monkeypatch: pytest.Monk
)


@pytest.mark.usefixtures("use_venv")
@ee_states
def test_valid_home(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
"""Confirm a valid .ansible.cfg is parsed when in the home directory.
Expand Down Expand Up @@ -119,7 +116,6 @@ def test_valid_home(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatch)
"""


@pytest.mark.usefixtures("use_venv")
@ee_states
def test_invalid_config(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
"""Confirm a invalid ansible.cfg raises errors.
Expand All @@ -140,7 +136,6 @@ def test_invalid_config(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPa
assert "12345" in parsed_cfg.exit_messages[1].message


@pytest.mark.usefixtures("use_venv")
@ee_states
def test_invalid_configurator(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
"""Confirm a invalid ansible.cfg raises errors using configurator.
Expand Down

0 comments on commit b917d30

Please sign in to comment.