From 609b208d9110e3122f2cee2e1ed2ded29e39d7f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 15 Aug 2024 19:38:58 +0200 Subject: [PATCH] fix remaining places not using captured_output --- pkgs/clan-cli/tests/test_flakes_cli.py | 28 +++++------ pkgs/clan-cli/tests/test_machines_cli.py | 32 ++++++------ pkgs/clan-cli/tests/test_secrets_cli.py | 63 +++++++++++------------- pkgs/clan-cli/tests/test_ssh_cli.py | 9 ++-- 4 files changed, 63 insertions(+), 69 deletions(-) diff --git a/pkgs/clan-cli/tests/test_flakes_cli.py b/pkgs/clan-cli/tests/test_flakes_cli.py index 6bb57fc86..5ff74a2ef 100644 --- a/pkgs/clan-cli/tests/test_flakes_cli.py +++ b/pkgs/clan-cli/tests/test_flakes_cli.py @@ -3,6 +3,7 @@ from typing import TYPE_CHECKING import pytest from fixtures_flakes import FlakeForTest from helpers import cli +from stdout import CaptureOutput if TYPE_CHECKING: pass @@ -10,18 +11,17 @@ if TYPE_CHECKING: @pytest.mark.impure def test_flakes_inspect( - test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture + test_flake_with_core: FlakeForTest, capture_output: CaptureOutput ) -> None: - cli.run( - [ - "flakes", - "inspect", - "--flake", - str(test_flake_with_core.path), - "--machine", - "vm1", - ] - ) - out = capsys.readouterr() # empty the buffer - - assert "Icon" in out.out + with capture_output as output: + cli.run( + [ + "flakes", + "inspect", + "--flake", + str(test_flake_with_core.path), + "--machine", + "vm1", + ] + ) + assert "Icon" in output.out diff --git a/pkgs/clan-cli/tests/test_machines_cli.py b/pkgs/clan-cli/tests/test_machines_cli.py index 1bfe53c0e..f8994471b 100644 --- a/pkgs/clan-cli/tests/test_machines_cli.py +++ b/pkgs/clan-cli/tests/test_machines_cli.py @@ -1,36 +1,32 @@ import pytest from fixtures_flakes import FlakeForTest from helpers import cli +from stdout import CaptureOutput @pytest.mark.impure def test_machine_subcommands( - test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture + test_flake_with_core: FlakeForTest, + capture_output: CaptureOutput, ) -> None: cli.run( ["machines", "create", "--flake", str(test_flake_with_core.path), "machine1"] ) - capsys.readouterr() - cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) + with capture_output as output: + cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) - out = capsys.readouterr() - - assert "machine1" in out.out - assert "vm1" in out.out - assert "vm2" in out.out - - capsys.readouterr() - print(out) + print(output.out) + assert "machine1" in output.out + assert "vm1" in output.out + assert "vm2" in output.out cli.run( ["machines", "delete", "--flake", str(test_flake_with_core.path), "machine1"] ) - capsys.readouterr() - cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) - out = capsys.readouterr() - - assert "machine1" not in out.out - assert "vm1" in out.out - assert "vm2" in out.out + with capture_output as output: + cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) + assert "machine1" not in output.out + assert "vm1" in output.out + assert "vm2" in output.out diff --git a/pkgs/clan-cli/tests/test_secrets_cli.py b/pkgs/clan-cli/tests/test_secrets_cli.py index e7522cc85..0deeb622c 100644 --- a/pkgs/clan-cli/tests/test_secrets_cli.py +++ b/pkgs/clan-cli/tests/test_secrets_cli.py @@ -20,7 +20,7 @@ log = logging.getLogger(__name__) def _test_identities( what: str, test_flake: FlakeForTest, - capsys: pytest.CaptureFixture, + capture_output: CaptureOutput, age_keys: list["KeyPair"], ) -> None: sops_folder = test_flake.path / "sops" @@ -65,24 +65,22 @@ def _test_identities( ] ) - capsys.readouterr() # empty the buffer - cli.run( - [ - "secrets", - what, - "get", - "--flake", - str(test_flake.path), - "foo", - ] - ) - out = capsys.readouterr() # empty the buffer - assert age_keys[1].pubkey in out.out + with capture_output as output: + cli.run( + [ + "secrets", + what, + "get", + "--flake", + str(test_flake.path), + "foo", + ] + ) + assert age_keys[1].pubkey in output.out - capsys.readouterr() # empty the buffer - cli.run(["secrets", what, "list", "--flake", str(test_flake.path)]) - out = capsys.readouterr() # empty the buffer - assert "foo" in out.out + with capture_output as output: + cli.run(["secrets", what, "list", "--flake", str(test_flake.path)]) + assert "foo" in output.out cli.run(["secrets", what, "remove", "--flake", str(test_flake.path), "foo"]) assert not (sops_folder / what / "foo" / "key.json").exists() @@ -90,30 +88,29 @@ def _test_identities( with pytest.raises(ClanError): # already removed cli.run(["secrets", what, "remove", "--flake", str(test_flake.path), "foo"]) - capsys.readouterr() - cli.run(["secrets", what, "list", "--flake", str(test_flake.path)]) - out = capsys.readouterr() - assert "foo" not in out.out + with capture_output as output: + cli.run(["secrets", what, "list", "--flake", str(test_flake.path)]) + assert "foo" not in output.out def test_users( - test_flake: FlakeForTest, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] + test_flake: FlakeForTest, capture_output: CaptureOutput, age_keys: list["KeyPair"] ) -> None: - _test_identities("users", test_flake, capsys, age_keys) + _test_identities("users", test_flake, capture_output, age_keys) def test_machines( - test_flake: FlakeForTest, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] + test_flake: FlakeForTest, capture_output: CaptureOutput, age_keys: list["KeyPair"] ) -> None: - _test_identities("machines", test_flake, capsys, age_keys) + _test_identities("machines", test_flake, capture_output, age_keys) def test_groups( - test_flake: FlakeForTest, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] + test_flake: FlakeForTest, capture_output: CaptureOutput, age_keys: list["KeyPair"] ) -> None: - capsys.readouterr() # empty the buffer - cli.run(["secrets", "groups", "list", "--flake", str(test_flake.path)]) - assert capsys.readouterr().out == "" + with capture_output as output: + cli.run(["secrets", "groups", "list", "--flake", str(test_flake.path)]) + assert output.out == "" with pytest.raises(ClanError): # machine does not exist yet cli.run( @@ -198,9 +195,9 @@ def test_groups( ] ) - capsys.readouterr() # empty the buffer - cli.run(["secrets", "groups", "list", "--flake", str(test_flake.path)]) - out = capsys.readouterr().out + with capture_output as output: + cli.run(["secrets", "groups", "list", "--flake", str(test_flake.path)]) + out = output.out assert "user1" in out assert "machine1" in out diff --git a/pkgs/clan-cli/tests/test_ssh_cli.py b/pkgs/clan-cli/tests/test_ssh_cli.py index 4b1aca3cb..604530d1a 100644 --- a/pkgs/clan-cli/tests/test_ssh_cli.py +++ b/pkgs/clan-cli/tests/test_ssh_cli.py @@ -4,19 +4,20 @@ import sys import pytest import pytest_subprocess.fake_process from pytest_subprocess import utils +from stdout import CaptureOutput import clan_cli from clan_cli.ssh import cli def test_no_args( - capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch + monkeypatch: pytest.MonkeyPatch, + capture_output: CaptureOutput, ) -> None: monkeypatch.setattr(sys, "argv", ["", "ssh"]) - with pytest.raises(SystemExit): + with capture_output as output, pytest.raises(SystemExit): clan_cli.main() - captured = capsys.readouterr() - assert captured.err.startswith("usage:") + assert output.err.startswith("usage:") # using fp fixture from pytest-subprocess