Merge pull request 'use capture_output fixture in more places' (#1899) from Mic92-flake-update-2024-08-12 into main

This commit is contained in:
clan-bot
2024-08-15 17:25:57 +00:00
3 changed files with 55 additions and 57 deletions

View File

@@ -2,6 +2,6 @@ import pytest
from helpers import cli from helpers import cli
def test_help(capfd: pytest.CaptureFixture) -> None: def test_help() -> None:
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
cli.run(["clan-app", "--help"]) cli.run(["clan-app", "--help"])

View File

@@ -4,6 +4,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:
from age_keys import KeyPair from age_keys import KeyPair
@@ -12,7 +13,7 @@ if TYPE_CHECKING:
def test_import_sops( def test_import_sops(
test_root: Path, test_root: Path,
test_flake: FlakeForTest, test_flake: FlakeForTest,
capsys: pytest.CaptureFixture, capture_output: CaptureOutput,
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
age_keys: list["KeyPair"], age_keys: list["KeyPair"],
) -> None: ) -> None:
@@ -88,11 +89,11 @@ def test_import_sops(
] ]
cli.run(cmd) cli.run(cmd)
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "users", "list", "--flake", str(test_flake.path)]) cli.run(["secrets", "users", "list", "--flake", str(test_flake.path)])
users = sorted(capsys.readouterr().out.rstrip().split()) users = sorted(output.out.rstrip().split())
assert users == ["user1", "user2"] assert users == ["user1", "user2"]
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "get", "--flake", str(test_flake.path), "secret-key"]) cli.run(["secrets", "get", "--flake", str(test_flake.path), "secret-key"])
assert capsys.readouterr().out == "secret-value" assert output.out == "secret-value"

View File

@@ -7,6 +7,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
from clan_cli.errors import ClanError from clan_cli.errors import ClanError
@@ -243,20 +244,20 @@ def use_key(key: str, monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
def test_secrets( def test_secrets(
test_flake: FlakeForTest, test_flake: FlakeForTest,
capsys: pytest.CaptureFixture, capture_output: CaptureOutput,
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
age_keys: list["KeyPair"], age_keys: list["KeyPair"],
) -> None: ) -> None:
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run(["secrets", "list", "--flake", str(test_flake.path)]) cli.run(["secrets", "list", "--flake", str(test_flake.path)])
assert capsys.readouterr().out == "" assert output.out == ""
monkeypatch.setenv("SOPS_NIX_SECRET", "foo") monkeypatch.setenv("SOPS_NIX_SECRET", "foo")
monkeypatch.setenv("SOPS_AGE_KEY_FILE", str(test_flake.path / ".." / "age.key")) monkeypatch.setenv("SOPS_AGE_KEY_FILE", str(test_flake.path / ".." / "age.key"))
cli.run(["secrets", "key", "generate", "--flake", str(test_flake.path)]) cli.run(["secrets", "key", "generate", "--flake", str(test_flake.path)])
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run(["secrets", "key", "show", "--flake", str(test_flake.path)]) cli.run(["secrets", "key", "show", "--flake", str(test_flake.path)])
key = capsys.readouterr().out key = output.out
assert key.startswith("age1") assert key.startswith("age1")
cli.run( cli.run(
["secrets", "users", "add", "--flake", str(test_flake.path), "testuser", key] ["secrets", "users", "add", "--flake", str(test_flake.path), "testuser", key]
@@ -265,12 +266,12 @@ def test_secrets(
with pytest.raises(ClanError): # does not exist yet with pytest.raises(ClanError): # does not exist yet
cli.run(["secrets", "get", "--flake", str(test_flake.path), "nonexisting"]) cli.run(["secrets", "get", "--flake", str(test_flake.path), "nonexisting"])
cli.run(["secrets", "set", "--flake", str(test_flake.path), "initialkey"]) cli.run(["secrets", "set", "--flake", str(test_flake.path), "initialkey"])
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "get", "--flake", str(test_flake.path), "initialkey"]) cli.run(["secrets", "get", "--flake", str(test_flake.path), "initialkey"])
assert capsys.readouterr().out == "foo" assert output.out == "foo"
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "users", "list", "--flake", str(test_flake.path)]) cli.run(["secrets", "users", "list", "--flake", str(test_flake.path)])
users = capsys.readouterr().out.rstrip().split("\n") users = output.out.rstrip().split("\n")
assert len(users) == 1, f"users: {users}" assert len(users) == 1, f"users: {users}"
owner = users[0] owner = users[0]
@@ -280,17 +281,17 @@ def test_secrets(
cli.run(["secrets", "rename", "--flake", str(test_flake.path), "initialkey", "key"]) cli.run(["secrets", "rename", "--flake", str(test_flake.path), "initialkey", "key"])
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run(["secrets", "list", "--flake", str(test_flake.path)]) cli.run(["secrets", "list", "--flake", str(test_flake.path)])
assert capsys.readouterr().out == "key\n" assert output.out == "key\n"
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run(["secrets", "list", "--flake", str(test_flake.path), "nonexisting"]) cli.run(["secrets", "list", "--flake", str(test_flake.path), "nonexisting"])
assert capsys.readouterr().out == "" assert output.out == ""
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run(["secrets", "list", "--flake", str(test_flake.path), "key"]) cli.run(["secrets", "list", "--flake", str(test_flake.path), "key"])
assert capsys.readouterr().out == "key\n" assert output.out == "key\n"
cli.run( cli.run(
[ [
@@ -314,15 +315,14 @@ def test_secrets(
"key", "key",
] ]
) )
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "machines", "list", "--flake", str(test_flake.path)]) cli.run(["secrets", "machines", "list", "--flake", str(test_flake.path)])
assert capsys.readouterr().out == "machine1\n" assert output.out == "machine1\n"
with use_key(age_keys[1].privkey, monkeypatch): with use_key(age_keys[1].privkey, monkeypatch):
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"]) cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"])
assert output.out == "foo"
assert capsys.readouterr().out == "foo"
# rotate machines key # rotate machines key
cli.run( cli.run(
@@ -340,10 +340,9 @@ def test_secrets(
# should also rotate the encrypted secret # should also rotate the encrypted secret
with use_key(age_keys[0].privkey, monkeypatch): with use_key(age_keys[0].privkey, monkeypatch):
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"]) cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"])
assert output.out == "foo"
assert capsys.readouterr().out == "foo"
cli.run( cli.run(
[ [
@@ -379,10 +378,9 @@ def test_secrets(
"key", "key",
] ]
) )
capsys.readouterr() with capture_output as output, use_key(age_keys[1].privkey, monkeypatch):
with use_key(age_keys[1].privkey, monkeypatch):
cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"]) cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"])
assert capsys.readouterr().out == "foo" assert output.out == "foo"
cli.run( cli.run(
[ [
"secrets", "secrets",
@@ -441,7 +439,6 @@ def test_secrets(
] ]
) )
capsys.readouterr() # empty the buffer
cli.run( cli.run(
[ [
"secrets", "secrets",
@@ -455,9 +452,9 @@ def test_secrets(
) )
with use_key(age_keys[1].privkey, monkeypatch): with use_key(age_keys[1].privkey, monkeypatch):
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"]) cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"])
assert capsys.readouterr().out == "foo" assert output.out == "foo"
# extend group will update secrets # extend group will update secrets
cli.run( cli.run(
@@ -484,9 +481,9 @@ def test_secrets(
) )
with use_key(age_keys[2].privkey, monkeypatch): # user2 with use_key(age_keys[2].privkey, monkeypatch): # user2
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"]) cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"])
assert capsys.readouterr().out == "foo" assert output.out == "foo"
cli.run( cli.run(
[ [
@@ -501,9 +498,9 @@ def test_secrets(
) )
with pytest.raises(ClanError), use_key(age_keys[2].privkey, monkeypatch): with pytest.raises(ClanError), use_key(age_keys[2].privkey, monkeypatch):
# user2 is not in the group anymore # user2 is not in the group anymore
capsys.readouterr() with capture_output as output:
cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"]) cli.run(["secrets", "get", "--flake", str(test_flake.path), "key"])
print(capsys.readouterr().out) print(output.out)
cli.run( cli.run(
[ [
@@ -520,6 +517,6 @@ def test_secrets(
cli.run(["secrets", "remove", "--flake", str(test_flake.path), "key"]) cli.run(["secrets", "remove", "--flake", str(test_flake.path), "key"])
cli.run(["secrets", "remove", "--flake", str(test_flake.path), "key2"]) cli.run(["secrets", "remove", "--flake", str(test_flake.path), "key2"])
capsys.readouterr() # empty the buffer with capture_output as output:
cli.run(["secrets", "list", "--flake", str(test_flake.path)]) cli.run(["secrets", "list", "--flake", str(test_flake.path)])
assert capsys.readouterr().out == "" assert output.out == ""