From a544eb6482b210c654a332f8f0a1c1d7acf4116e Mon Sep 17 00:00:00 2001 From: Peter Rice Date: Sun, 8 Jun 2025 03:15:41 -0400 Subject: [PATCH] patch qbpm.paths to prevent tests touching $HOME they didn't before either, this is just more complete --- tests/__init__.py | 10 ++++++++++ tests/test_choose.py | 2 ++ tests/test_main.py | 25 ++++++++++--------------- tests/test_profiles.py | 2 ++ 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..8076cb0 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,10 @@ +from pathlib import Path + +import pytest + + +@pytest.fixture(autouse=True) +def no_homedir_fixture(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr("qbpm.paths.default_qbpm_config_dir", lambda: tmp_path) + monkeypatch.setattr("qbpm.paths.default_qbpm_application_dir", lambda: tmp_path) + monkeypatch.setattr("qbpm.paths.default_profile_dir", lambda: tmp_path) diff --git a/tests/test_choose.py b/tests/test_choose.py index 63cd95b..6b03aae 100644 --- a/tests/test_choose.py +++ b/tests/test_choose.py @@ -3,6 +3,8 @@ from pathlib import Path from qbpm.choose import choose_profile, find_menu +from . import no_homedir_fixture # noqa: F401 + def write_script(parent_dir: Path, name: str = "menu", contents: str = "") -> Path: parent_dir.mkdir(exist_ok=True) diff --git a/tests/test_main.py b/tests/test_main.py index 2eed2d9..2588298 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -5,15 +5,16 @@ from click.testing import CliRunner from qbpm.main import main -no_desktop = "--no-desktop-file" +from . import no_homedir_fixture # noqa: F401 + + +def run(*args: str): + return CliRunner().invoke(main, args) def test_profile_dir_option(tmp_path: Path): (tmp_path / "config.py").touch() - runner = CliRunner() - result = runner.invoke( - main, ["-P", str(tmp_path), "new", "-C", str(tmp_path), no_desktop, "test"] - ) + result = run("-P", str(tmp_path), "new", "-C", str(tmp_path), "test") assert result.exit_code == 0 assert result.output.strip() == str(tmp_path / "test") assert tmp_path / "test" in list(tmp_path.iterdir()) @@ -22,8 +23,7 @@ def test_profile_dir_option(tmp_path: Path): def test_profile_dir_env(tmp_path: Path): environ["QBPM_PROFILE_DIR"] = str(tmp_path) (tmp_path / "config.py").touch() - runner = CliRunner() - result = runner.invoke(main, ["new", "-C", str(tmp_path), no_desktop, "test"]) + result = run("new", "-C", str(tmp_path), "test") assert result.exit_code == 0 assert result.output.strip() == str(tmp_path / "test") assert tmp_path / "test" in list(tmp_path.iterdir()) @@ -33,8 +33,7 @@ def test_config_dir_option(tmp_path: Path): environ["QBPM_PROFILE_DIR"] = str(tmp_path) config = tmp_path / "config.py" config.touch() - runner = CliRunner() - result = runner.invoke(main, ["new", "-C", str(tmp_path), no_desktop, "test"]) + result = run("new", "-C", str(tmp_path), "test") assert result.exit_code == 0 assert str(config) in (tmp_path / "test/config/config.py").read_text() @@ -44,8 +43,7 @@ def test_relative_config_dir(tmp_path: Path): config = tmp_path / "config.py" config.touch() chdir(tmp_path) - runner = CliRunner() - result = runner.invoke(main, ["new", "-C", ".", no_desktop, "test"]) + result = run("new", "-C", ".", "test") assert result.exit_code == 0 assert str(config) in (tmp_path / "test/config/config.py").read_text() @@ -55,10 +53,7 @@ def test_from_session(tmp_path: Path): (tmp_path / "config.py").touch() session = tmp_path / "test.yml" session.write_text("windows:\n") - runner = CliRunner() - result = runner.invoke( - main, ["from-session", "-C", str(tmp_path), no_desktop, str(session)] - ) + result = run("from-session", "-C", str(tmp_path), str(session)) assert result.exit_code == 0 assert result.output.strip() == str(tmp_path / "test") assert (tmp_path / "test/data/sessions/_autosave.yml").read_text() == ("windows:\n") diff --git a/tests/test_profiles.py b/tests/test_profiles.py index 98a4502..5ef1ef8 100644 --- a/tests/test_profiles.py +++ b/tests/test_profiles.py @@ -3,6 +3,8 @@ from pathlib import Path from qbpm import profiles from qbpm.profiles import Profile +from . import no_homedir_fixture # noqa: F401 + def check_is_empty(path: Path): assert len(list(path.iterdir())) == 0 -- 2.51.2