Repository navigation
Expand file tree
/
Copy pathtest_output.py
More file actions
162 lines (128 loc) · 5.07 KB
/
Copy pathtest_output.py
File metadata and controls
162 lines (128 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""Tests for output verbosity and run_step helper."""
from io import StringIO
from dataclasses import dataclass
from rich.console import Console
from psa.core.output import (
Verbosity,
get_verbosity,
print_error,
print_info,
print_success,
print_warning,
run_step,
set_verbosity,
)
@dataclass
class FakeResult:
success: bool
output: str = ""
def _capture(func, *args) -> str:
"""Capture rich console output as plain text."""
buf = StringIO()
# Temporarily swap consoles
import psa.core.output as mod
orig_console = mod.console
orig_err = mod.error_console
mod.console = Console(file=buf, force_terminal=False)
mod.error_console = Console(file=buf, stderr=False, force_terminal=False)
try:
func(*args)
finally:
mod.console = orig_console
mod.error_console = orig_err
return buf.getvalue()
class TestVerbosity:
def setup_method(self):
set_verbosity(Verbosity.DEFAULT)
def teardown_method(self):
set_verbosity(Verbosity.DEFAULT)
def test_roundtrip(self):
set_verbosity(Verbosity.QUIET)
assert get_verbosity() == Verbosity.QUIET
set_verbosity(Verbosity.VERBOSE)
assert get_verbosity() == Verbosity.VERBOSE
def test_default(self):
assert get_verbosity() == Verbosity.DEFAULT
class TestRunStep:
def setup_method(self):
set_verbosity(Verbosity.DEFAULT)
def teardown_method(self):
set_verbosity(Verbosity.DEFAULT)
def test_returns_result(self):
result = run_step("Test", lambda: 42)
assert result == 42
def test_returns_fake_result(self):
fake = FakeResult(success=True, output="ok")
result = run_step("Test", lambda: fake)
assert result is fake
def test_quiet_no_output(self):
set_verbosity(Verbosity.QUIET)
out = _capture(lambda: run_step("Test", lambda: FakeResult(success=True)))
assert out.strip() == ""
def test_default_shows_checkmark(self):
out = _capture(lambda: run_step("Stopping", lambda: FakeResult(success=True)))
assert "Stopping" in out
def test_failure_shows_x(self):
out = _capture(lambda: run_step("Stopping", lambda: FakeResult(success=False)))
assert "Stopping" in out
def test_verbose_shows_output(self):
set_verbosity(Verbosity.VERBOSE)
fake = FakeResult(success=True, output="raw line 1\nraw line 2")
out = _capture(lambda: run_step("Test", lambda: fake))
assert "raw line 1" in out
assert "raw line 2" in out
def test_warn_if_shows_tilde_on_expected_failure(self):
fake = FakeResult(success=False, output="already stopped")
out = _capture(lambda: run_step("Stopping", lambda: fake, warn_if=lambda r: True))
assert "~" in out
assert "Stopping" in out
def test_warn_if_not_triggered_on_success(self):
"""warn_if should be ignored when result is successful."""
called = []
fake = FakeResult(success=True, output="ok")
out = _capture(lambda: run_step("Stopping", lambda: fake, warn_if=lambda r: called.append(1) or True))
# warn_if should not be called for successful results
assert len(called) == 0
assert "~" not in out
def test_warn_if_false_shows_x(self):
"""When warn_if returns False, normal failure icon shown."""
fake = FakeResult(success=False, output="real error")
out = _capture(lambda: run_step("Stopping", lambda: fake, warn_if=lambda r: False))
assert "~" not in out
assert "Stopping" in out
def test_warn_if_none_default(self):
"""Without warn_if, failures show normal icon."""
fake = FakeResult(success=False, output="error")
out = _capture(lambda: run_step("Stopping", lambda: fake))
assert "~" not in out
def test_warn_if_receives_result(self):
"""warn_if callable receives the actual result object."""
fake = FakeResult(success=False, output="domain not running")
received = []
_capture(lambda: run_step("Test", lambda: fake, warn_if=lambda r: received.append(r) or True))
assert len(received) == 1
assert received[0] is fake
class TestPrintHelpers:
def setup_method(self):
set_verbosity(Verbosity.DEFAULT)
def teardown_method(self):
set_verbosity(Verbosity.DEFAULT)
def test_info_visible_default(self):
out = _capture(print_info, "hello")
assert "hello" in out
def test_info_silenced_quiet(self):
set_verbosity(Verbosity.QUIET)
out = _capture(print_info, "hello")
assert out.strip() == ""
def test_success_silenced_quiet(self):
set_verbosity(Verbosity.QUIET)
out = _capture(print_success, "done")
assert out.strip() == ""
def test_warning_silenced_quiet(self):
set_verbosity(Verbosity.QUIET)
out = _capture(print_warning, "warn")
assert out.strip() == ""
def test_error_always_visible(self):
set_verbosity(Verbosity.QUIET)
out = _capture(print_error, "fail")
assert "fail" in out