From fe66a740b5718e0b23e31b077695b1907fe84f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 15 Aug 2024 18:21:27 +0200 Subject: [PATCH] add fixture to capture output in tests less fragile Now we always remove previous output before capturing. --- pkgs/clan-cli/tests/conftest.py | 1 + pkgs/clan-cli/tests/stdout.py | 23 +++++++++++++++++++++++ pkgs/clan-cli/tests/test_cli.py | 8 ++++---- pkgs/clan-cli/tests/test_create_flake.py | 17 +++++++++-------- pkgs/clan-cli/tests/test_history_cli.py | 16 ++++++++-------- pkgs/clan-cli/tests/test_vms_cli.py | 9 +++++---- 6 files changed, 50 insertions(+), 24 deletions(-) create mode 100644 pkgs/clan-cli/tests/stdout.py diff --git a/pkgs/clan-cli/tests/conftest.py b/pkgs/clan-cli/tests/conftest.py index a0374aeea..80220daee 100644 --- a/pkgs/clan-cli/tests/conftest.py +++ b/pkgs/clan-cli/tests/conftest.py @@ -15,6 +15,7 @@ pytest_plugins = [ "ports", "host_group", "fixtures_flakes", + "stdout", ] diff --git a/pkgs/clan-cli/tests/stdout.py b/pkgs/clan-cli/tests/stdout.py new file mode 100644 index 000000000..5106498db --- /dev/null +++ b/pkgs/clan-cli/tests/stdout.py @@ -0,0 +1,23 @@ +from typing import Any + +import pytest +from pytest import CaptureFixture + + +class CaptureOutput: + def __init__(self, capsys: CaptureFixture) -> None: + self.capsys = capsys + + def __enter__(self) -> "CaptureOutput": + self.capsys.readouterr() + return self + + def __exit__(self, exc_type: Any, exc_value: Any, exc_traceback: Any) -> bool: + res = self.capsys.readouterr() + self.out = res.out + self.err = res.err + + +@pytest.fixture +def capture_output(capsys: CaptureFixture) -> CaptureOutput: + return CaptureOutput(capsys) diff --git a/pkgs/clan-cli/tests/test_cli.py b/pkgs/clan-cli/tests/test_cli.py index 5f4762bcb..03b0c5ae1 100644 --- a/pkgs/clan-cli/tests/test_cli.py +++ b/pkgs/clan-cli/tests/test_cli.py @@ -1,9 +1,9 @@ import pytest from helpers import cli +from stdout import CaptureOutput -def test_help(capsys: pytest.CaptureFixture) -> None: - with pytest.raises(SystemExit): +def test_help(capture_output: CaptureOutput) -> None: + with capture_output as output, pytest.raises(SystemExit): cli.run(["--help"]) - captured = capsys.readouterr() - assert captured.out.startswith("usage:") + assert output.out.startswith("usage:") diff --git a/pkgs/clan-cli/tests/test_create_flake.py b/pkgs/clan-cli/tests/test_create_flake.py index d0e920c80..75f7bdd80 100644 --- a/pkgs/clan-cli/tests/test_create_flake.py +++ b/pkgs/clan-cli/tests/test_create_flake.py @@ -5,14 +5,15 @@ from pathlib import Path import pytest from fixtures_flakes import substitute from helpers import cli +from stdout import CaptureOutput @pytest.mark.impure def test_create_flake( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture, temporary_home: Path, clan_core: Path, + capture_output: CaptureOutput, ) -> None: flake_dir = temporary_home / "test-flake" @@ -29,7 +30,6 @@ def test_create_flake( monkeypatch.chdir(flake_dir) cli.run(["machines", "create", "machine1"]) - capsys.readouterr() # flush cache # create a hardware-configuration.nix that doesn't throw an eval error @@ -39,8 +39,9 @@ def test_create_flake( ) as hw_config_nix: hw_config_nix.write("{}") - cli.run(["machines", "list"]) - assert "machine1" in capsys.readouterr().out + with capture_output as output: + cli.run(["machines", "list"]) + assert "machine1" in output.out flake_show = subprocess.run( ["nix", "flake", "show", "--json"], check=True, @@ -57,9 +58,9 @@ def test_create_flake( @pytest.mark.impure def test_ui_template( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture, temporary_home: Path, clan_core: Path, + capture_output: CaptureOutput, ) -> None: flake_dir = temporary_home / "test-flake" url = f"{clan_core}#minimal" @@ -73,10 +74,10 @@ def test_ui_template( monkeypatch.chdir(flake_dir) cli.run(["machines", "create", "machine1"]) - capsys.readouterr() # flush cache - cli.run(["machines", "list"]) - assert "machine1" in capsys.readouterr().out + with capture_output as output: + cli.run(["machines", "list"]) + assert "machine1" in output.out flake_show = subprocess.run( ["nix", "flake", "show", "--json"], check=True, diff --git a/pkgs/clan-cli/tests/test_history_cli.py b/pkgs/clan-cli/tests/test_history_cli.py index fd664b091..88b7e4ed1 100644 --- a/pkgs/clan-cli/tests/test_history_cli.py +++ b/pkgs/clan-cli/tests/test_history_cli.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING import pytest from fixtures_flakes import FlakeForTest from helpers import cli -from pytest import CaptureFixture +from stdout import CaptureOutput from clan_cli.dirs import user_history_file from clan_cli.history.add import HistoryEntry @@ -32,15 +32,15 @@ def test_history_add( @pytest.mark.impure def test_history_list( - capsys: CaptureFixture, + capture_output: CaptureOutput, test_flake_with_core: FlakeForTest, ) -> None: - capsys.readouterr() - cli.run(["history", "list"]) - assert str(test_flake_with_core.path) not in capsys.readouterr().out + with capture_output as output: + cli.run(["history", "list"]) + assert str(test_flake_with_core.path) not in output.out cli.run(["history", "add", f"clan://{test_flake_with_core.path}#vm1"]) - capsys.readouterr() - cli.run(["history", "list"]) - assert str(test_flake_with_core.path) in capsys.readouterr().out + with capture_output as output: + cli.run(["history", "list"]) + assert str(test_flake_with_core.path) in output.out diff --git a/pkgs/clan-cli/tests/test_vms_cli.py b/pkgs/clan-cli/tests/test_vms_cli.py index f00f269ec..3aeb7f78b 100644 --- a/pkgs/clan-cli/tests/test_vms_cli.py +++ b/pkgs/clan-cli/tests/test_vms_cli.py @@ -3,6 +3,7 @@ from pathlib import Path from typing import TYPE_CHECKING import pytest +from stdout import CaptureOutput from tests.fixtures_flakes import FlakeForTest, generate_flake from tests.helpers import cli @@ -18,11 +19,11 @@ no_kvm = not os.path.exists("/dev/kvm") @pytest.mark.impure def test_inspect( - test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture + test_flake_with_core: FlakeForTest, capture_output: CaptureOutput ) -> None: - cli.run(["vms", "inspect", "--flake", str(test_flake_with_core.path), "vm1"]) - out = capsys.readouterr() # empty the buffer - assert "Cores" in out.out + with capture_output as output: + cli.run(["vms", "inspect", "--flake", str(test_flake_with_core.path), "vm1"]) + assert "Cores" in output.out @pytest.mark.skipif(no_kvm, reason="Requires KVM")