Merge pull request 'add fixture to capture output more fragile' (#1898) from Mic92-flake-update-2024-08-12 into main
This commit is contained in:
@@ -15,6 +15,7 @@ pytest_plugins = [
|
|||||||
"ports",
|
"ports",
|
||||||
"host_group",
|
"host_group",
|
||||||
"fixtures_flakes",
|
"fixtures_flakes",
|
||||||
|
"stdout",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
23
pkgs/clan-cli/tests/stdout.py
Normal file
23
pkgs/clan-cli/tests/stdout.py
Normal file
@@ -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)
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from helpers import cli
|
from helpers import cli
|
||||||
|
from stdout import CaptureOutput
|
||||||
|
|
||||||
|
|
||||||
def test_help(capsys: pytest.CaptureFixture) -> None:
|
def test_help(capture_output: CaptureOutput) -> None:
|
||||||
with pytest.raises(SystemExit):
|
with capture_output as output, pytest.raises(SystemExit):
|
||||||
cli.run(["--help"])
|
cli.run(["--help"])
|
||||||
captured = capsys.readouterr()
|
assert output.out.startswith("usage:")
|
||||||
assert captured.out.startswith("usage:")
|
|
||||||
|
|||||||
@@ -5,14 +5,15 @@ from pathlib import Path
|
|||||||
import pytest
|
import pytest
|
||||||
from fixtures_flakes import substitute
|
from fixtures_flakes import substitute
|
||||||
from helpers import cli
|
from helpers import cli
|
||||||
|
from stdout import CaptureOutput
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.impure
|
@pytest.mark.impure
|
||||||
def test_create_flake(
|
def test_create_flake(
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
capsys: pytest.CaptureFixture,
|
|
||||||
temporary_home: Path,
|
temporary_home: Path,
|
||||||
clan_core: Path,
|
clan_core: Path,
|
||||||
|
capture_output: CaptureOutput,
|
||||||
) -> None:
|
) -> None:
|
||||||
flake_dir = temporary_home / "test-flake"
|
flake_dir = temporary_home / "test-flake"
|
||||||
|
|
||||||
@@ -29,7 +30,6 @@ def test_create_flake(
|
|||||||
|
|
||||||
monkeypatch.chdir(flake_dir)
|
monkeypatch.chdir(flake_dir)
|
||||||
cli.run(["machines", "create", "machine1"])
|
cli.run(["machines", "create", "machine1"])
|
||||||
capsys.readouterr() # flush cache
|
|
||||||
|
|
||||||
# create a hardware-configuration.nix that doesn't throw an eval error
|
# create a hardware-configuration.nix that doesn't throw an eval error
|
||||||
|
|
||||||
@@ -39,8 +39,9 @@ def test_create_flake(
|
|||||||
) as hw_config_nix:
|
) as hw_config_nix:
|
||||||
hw_config_nix.write("{}")
|
hw_config_nix.write("{}")
|
||||||
|
|
||||||
cli.run(["machines", "list"])
|
with capture_output as output:
|
||||||
assert "machine1" in capsys.readouterr().out
|
cli.run(["machines", "list"])
|
||||||
|
assert "machine1" in output.out
|
||||||
flake_show = subprocess.run(
|
flake_show = subprocess.run(
|
||||||
["nix", "flake", "show", "--json"],
|
["nix", "flake", "show", "--json"],
|
||||||
check=True,
|
check=True,
|
||||||
@@ -57,9 +58,9 @@ def test_create_flake(
|
|||||||
@pytest.mark.impure
|
@pytest.mark.impure
|
||||||
def test_ui_template(
|
def test_ui_template(
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
capsys: pytest.CaptureFixture,
|
|
||||||
temporary_home: Path,
|
temporary_home: Path,
|
||||||
clan_core: Path,
|
clan_core: Path,
|
||||||
|
capture_output: CaptureOutput,
|
||||||
) -> None:
|
) -> None:
|
||||||
flake_dir = temporary_home / "test-flake"
|
flake_dir = temporary_home / "test-flake"
|
||||||
url = f"{clan_core}#minimal"
|
url = f"{clan_core}#minimal"
|
||||||
@@ -73,10 +74,10 @@ def test_ui_template(
|
|||||||
|
|
||||||
monkeypatch.chdir(flake_dir)
|
monkeypatch.chdir(flake_dir)
|
||||||
cli.run(["machines", "create", "machine1"])
|
cli.run(["machines", "create", "machine1"])
|
||||||
capsys.readouterr() # flush cache
|
|
||||||
|
|
||||||
cli.run(["machines", "list"])
|
with capture_output as output:
|
||||||
assert "machine1" in capsys.readouterr().out
|
cli.run(["machines", "list"])
|
||||||
|
assert "machine1" in output.out
|
||||||
flake_show = subprocess.run(
|
flake_show = subprocess.run(
|
||||||
["nix", "flake", "show", "--json"],
|
["nix", "flake", "show", "--json"],
|
||||||
check=True,
|
check=True,
|
||||||
|
|||||||
@@ -4,7 +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 pytest import CaptureFixture
|
from stdout import CaptureOutput
|
||||||
|
|
||||||
from clan_cli.dirs import user_history_file
|
from clan_cli.dirs import user_history_file
|
||||||
from clan_cli.history.add import HistoryEntry
|
from clan_cli.history.add import HistoryEntry
|
||||||
@@ -32,15 +32,15 @@ def test_history_add(
|
|||||||
|
|
||||||
@pytest.mark.impure
|
@pytest.mark.impure
|
||||||
def test_history_list(
|
def test_history_list(
|
||||||
capsys: CaptureFixture,
|
capture_output: CaptureOutput,
|
||||||
test_flake_with_core: FlakeForTest,
|
test_flake_with_core: FlakeForTest,
|
||||||
) -> None:
|
) -> None:
|
||||||
capsys.readouterr()
|
with capture_output as output:
|
||||||
cli.run(["history", "list"])
|
cli.run(["history", "list"])
|
||||||
assert str(test_flake_with_core.path) not in capsys.readouterr().out
|
assert str(test_flake_with_core.path) not in output.out
|
||||||
|
|
||||||
cli.run(["history", "add", f"clan://{test_flake_with_core.path}#vm1"])
|
cli.run(["history", "add", f"clan://{test_flake_with_core.path}#vm1"])
|
||||||
|
|
||||||
capsys.readouterr()
|
with capture_output as output:
|
||||||
cli.run(["history", "list"])
|
cli.run(["history", "list"])
|
||||||
assert str(test_flake_with_core.path) in capsys.readouterr().out
|
assert str(test_flake_with_core.path) in output.out
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from pathlib import Path
|
|||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from stdout import CaptureOutput
|
||||||
|
|
||||||
from tests.fixtures_flakes import FlakeForTest, generate_flake
|
from tests.fixtures_flakes import FlakeForTest, generate_flake
|
||||||
from tests.helpers import cli
|
from tests.helpers import cli
|
||||||
@@ -18,11 +19,11 @@ no_kvm = not os.path.exists("/dev/kvm")
|
|||||||
|
|
||||||
@pytest.mark.impure
|
@pytest.mark.impure
|
||||||
def test_inspect(
|
def test_inspect(
|
||||||
test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture
|
test_flake_with_core: FlakeForTest, capture_output: CaptureOutput
|
||||||
) -> None:
|
) -> None:
|
||||||
cli.run(["vms", "inspect", "--flake", str(test_flake_with_core.path), "vm1"])
|
with capture_output as output:
|
||||||
out = capsys.readouterr() # empty the buffer
|
cli.run(["vms", "inspect", "--flake", str(test_flake_with_core.path), "vm1"])
|
||||||
assert "Cores" in out.out
|
assert "Cores" in output.out
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(no_kvm, reason="Requires KVM")
|
@pytest.mark.skipif(no_kvm, reason="Requires KVM")
|
||||||
|
|||||||
Reference in New Issue
Block a user