fix remaining places not using captured_output

This commit is contained in:
Jörg Thalheim
2024-08-15 19:38:58 +02:00
parent 223b97d665
commit 609b208d91
4 changed files with 63 additions and 69 deletions

View File

@@ -3,6 +3,7 @@ from typing import TYPE_CHECKING
import pytest import pytest
from fixtures_flakes import FlakeForTest from fixtures_flakes import FlakeForTest
from helpers import cli from helpers import cli
from stdout import CaptureOutput
if TYPE_CHECKING: if TYPE_CHECKING:
pass pass
@@ -10,8 +11,9 @@ if TYPE_CHECKING:
@pytest.mark.impure @pytest.mark.impure
def test_flakes_inspect( def test_flakes_inspect(
test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture test_flake_with_core: FlakeForTest, capture_output: CaptureOutput
) -> None: ) -> None:
with capture_output as output:
cli.run( cli.run(
[ [
"flakes", "flakes",
@@ -22,6 +24,4 @@ def test_flakes_inspect(
"vm1", "vm1",
] ]
) )
out = capsys.readouterr() # empty the buffer assert "Icon" in output.out
assert "Icon" in out.out

View File

@@ -1,36 +1,32 @@
import pytest import pytest
from fixtures_flakes import FlakeForTest from fixtures_flakes import FlakeForTest
from helpers import cli from helpers import cli
from stdout import CaptureOutput
@pytest.mark.impure @pytest.mark.impure
def test_machine_subcommands( def test_machine_subcommands(
test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture test_flake_with_core: FlakeForTest,
capture_output: CaptureOutput,
) -> None: ) -> None:
cli.run( cli.run(
["machines", "create", "--flake", str(test_flake_with_core.path), "machine1"] ["machines", "create", "--flake", str(test_flake_with_core.path), "machine1"]
) )
capsys.readouterr() with capture_output as output:
cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)])
out = capsys.readouterr() print(output.out)
assert "machine1" in output.out
assert "machine1" in out.out assert "vm1" in output.out
assert "vm1" in out.out assert "vm2" in output.out
assert "vm2" in out.out
capsys.readouterr()
print(out)
cli.run( cli.run(
["machines", "delete", "--flake", str(test_flake_with_core.path), "machine1"] ["machines", "delete", "--flake", str(test_flake_with_core.path), "machine1"]
) )
capsys.readouterr() with capture_output as output:
cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)])
out = capsys.readouterr() assert "machine1" not in output.out
assert "vm1" in output.out
assert "machine1" not in out.out assert "vm2" in output.out
assert "vm1" in out.out
assert "vm2" in out.out

View File

@@ -20,7 +20,7 @@ log = logging.getLogger(__name__)
def _test_identities( def _test_identities(
what: str, what: str,
test_flake: FlakeForTest, test_flake: FlakeForTest,
capsys: pytest.CaptureFixture, capture_output: CaptureOutput,
age_keys: list["KeyPair"], age_keys: list["KeyPair"],
) -> None: ) -> None:
sops_folder = test_flake.path / "sops" sops_folder = test_flake.path / "sops"
@@ -65,7 +65,7 @@ def _test_identities(
] ]
) )
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run( cli.run(
[ [
"secrets", "secrets",
@@ -76,13 +76,11 @@ def _test_identities(
"foo", "foo",
] ]
) )
out = capsys.readouterr() # empty the buffer assert age_keys[1].pubkey in output.out
assert age_keys[1].pubkey in out.out
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run(["secrets", what, "list", "--flake", str(test_flake.path)]) cli.run(["secrets", what, "list", "--flake", str(test_flake.path)])
out = capsys.readouterr() # empty the buffer assert "foo" in output.out
assert "foo" in out.out
cli.run(["secrets", what, "remove", "--flake", str(test_flake.path), "foo"]) cli.run(["secrets", what, "remove", "--flake", str(test_flake.path), "foo"])
assert not (sops_folder / what / "foo" / "key.json").exists() assert not (sops_folder / what / "foo" / "key.json").exists()
@@ -90,30 +88,29 @@ def _test_identities(
with pytest.raises(ClanError): # already removed with pytest.raises(ClanError): # already removed
cli.run(["secrets", what, "remove", "--flake", str(test_flake.path), "foo"]) cli.run(["secrets", what, "remove", "--flake", str(test_flake.path), "foo"])
capsys.readouterr() with capture_output as output:
cli.run(["secrets", what, "list", "--flake", str(test_flake.path)]) cli.run(["secrets", what, "list", "--flake", str(test_flake.path)])
out = capsys.readouterr() assert "foo" not in output.out
assert "foo" not in out.out
def test_users( def test_users(
test_flake: FlakeForTest, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] test_flake: FlakeForTest, capture_output: CaptureOutput, age_keys: list["KeyPair"]
) -> None: ) -> None:
_test_identities("users", test_flake, capsys, age_keys) _test_identities("users", test_flake, capture_output, age_keys)
def test_machines( def test_machines(
test_flake: FlakeForTest, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] test_flake: FlakeForTest, capture_output: CaptureOutput, age_keys: list["KeyPair"]
) -> None: ) -> None:
_test_identities("machines", test_flake, capsys, age_keys) _test_identities("machines", test_flake, capture_output, age_keys)
def test_groups( def test_groups(
test_flake: FlakeForTest, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] test_flake: FlakeForTest, capture_output: CaptureOutput, age_keys: list["KeyPair"]
) -> None: ) -> None:
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run(["secrets", "groups", "list", "--flake", str(test_flake.path)]) cli.run(["secrets", "groups", "list", "--flake", str(test_flake.path)])
assert capsys.readouterr().out == "" assert output.out == ""
with pytest.raises(ClanError): # machine does not exist yet with pytest.raises(ClanError): # machine does not exist yet
cli.run( cli.run(
@@ -198,9 +195,9 @@ def test_groups(
] ]
) )
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run(["secrets", "groups", "list", "--flake", str(test_flake.path)]) cli.run(["secrets", "groups", "list", "--flake", str(test_flake.path)])
out = capsys.readouterr().out out = output.out
assert "user1" in out assert "user1" in out
assert "machine1" in out assert "machine1" in out

View File

@@ -4,19 +4,20 @@ import sys
import pytest import pytest
import pytest_subprocess.fake_process import pytest_subprocess.fake_process
from pytest_subprocess import utils from pytest_subprocess import utils
from stdout import CaptureOutput
import clan_cli import clan_cli
from clan_cli.ssh import cli from clan_cli.ssh import cli
def test_no_args( def test_no_args(
capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch monkeypatch: pytest.MonkeyPatch,
capture_output: CaptureOutput,
) -> None: ) -> None:
monkeypatch.setattr(sys, "argv", ["", "ssh"]) monkeypatch.setattr(sys, "argv", ["", "ssh"])
with pytest.raises(SystemExit): with capture_output as output, pytest.raises(SystemExit):
clan_cli.main() clan_cli.main()
captured = capsys.readouterr() assert output.err.startswith("usage:")
assert captured.err.startswith("usage:")
# using fp fixture from pytest-subprocess # using fp fixture from pytest-subprocess