forked from pacta-dev/pacta-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_check.py
More file actions
220 lines (177 loc) · 7.91 KB
/
Copy pathtest_check.py
File metadata and controls
220 lines (177 loc) · 7.91 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from unittest.mock import patch
from pacta import PACTA_VERSION
from pacta.cli.main import main
from pacta.core.engine import CheckResult
from pacta.reporting.types import Report, RuleRef, RunInfo, Severity, Summary, Violation
from pacta.snapshot.types import Snapshot, SnapshotMeta
def _empty_snapshot(repo_root: str) -> Snapshot:
return Snapshot(
schema_version=1,
meta=SnapshotMeta(repo_root=repo_root, commit="abc123", branch="main"),
nodes=(),
edges=(),
violations=(),
)
def _make_report(repo_root: str, violations=None, engine_errors=None) -> Report:
violations = violations or []
engine_errors = engine_errors or []
by_severity = {}
by_status = {}
by_rule = {}
for v in violations:
sev_key = v.rule.severity.value
by_severity[sev_key] = by_severity.get(sev_key, 0) + 1
by_status[v.status] = by_status.get(v.status, 0) + 1
by_rule[v.rule.id] = by_rule.get(v.rule.id, 0) + 1
return Report(
tool="pacta",
version=PACTA_VERSION,
run=RunInfo(
repo_root=repo_root,
commit=None,
branch=None,
model_file=None,
rules_files=(),
baseline_ref=None,
mode="full",
created_at=None,
tool_version=PACTA_VERSION,
metadata={},
),
summary=Summary(
total_violations=len(violations),
by_severity=by_severity,
by_status=by_status,
by_rule=by_rule,
engine_errors=len(engine_errors),
),
violations=tuple(violations),
engine_errors=tuple(engine_errors),
diff=None,
)
class TestCheckCommand:
"""Tests for the check CLI command."""
def test_check_parser_accepts_valid_args(self, tmp_path):
"""Test that check command parses arguments correctly."""
repo_root = tmp_path / "repo"
repo_root.mkdir()
snapshot = _empty_snapshot(str(repo_root))
report = _make_report(str(repo_root))
check_result = CheckResult(snapshot=snapshot, report=report, diff=None)
with (
patch("pacta.cli.check.FsSnapshotStore") as mock_store_cls,
patch("pacta.cli.check.DefaultPactaEngine") as mock_engine_cls,
):
store = mock_store_cls.return_value
store.exists.return_value = True
store.load.return_value = snapshot
mock_engine_cls.return_value.check.return_value = check_result
exit_code = main(["check", str(repo_root)])
assert exit_code == 0
def test_check_with_custom_ref(self, tmp_path):
"""Test check command with --ref flag."""
repo_root = tmp_path / "repo"
repo_root.mkdir()
snapshot = _empty_snapshot(str(repo_root))
report = _make_report(str(repo_root))
check_result = CheckResult(snapshot=snapshot, report=report, diff=None)
with (
patch("pacta.cli.check.FsSnapshotStore") as mock_store_cls,
patch("pacta.cli.check.DefaultPactaEngine") as mock_engine_cls,
):
store = mock_store_cls.return_value
store.exists.return_value = True
store.load.return_value = snapshot
mock_engine_cls.return_value.check.return_value = check_result
exit_code = main(["check", str(repo_root), "--ref", "baseline"])
assert exit_code == 0
store.load.assert_called_once_with("baseline")
def test_check_missing_snapshot_returns_error(self, tmp_path):
"""Test that check returns error when snapshot ref doesn't exist."""
repo_root = tmp_path / "repo"
repo_root.mkdir()
with patch("pacta.cli.check.FsSnapshotStore") as mock_store_cls:
store = mock_store_cls.return_value
store.exists.return_value = False
exit_code = main(["check", str(repo_root), "--ref", "nonexistent"])
assert exit_code == 2
def test_check_with_violations_returns_exit_code_1(self, tmp_path):
"""Test that check returns exit code 1 when violations are found."""
repo_root = tmp_path / "repo"
repo_root.mkdir()
snapshot = _empty_snapshot(str(repo_root))
violation = Violation(
rule=RuleRef(id="test", name="Test", severity=Severity.ERROR),
message="bad import",
location=None,
status="new",
)
report = _make_report(str(repo_root), violations=[violation])
check_result = CheckResult(snapshot=snapshot, report=report, diff=None)
with (
patch("pacta.cli.check.FsSnapshotStore") as mock_store_cls,
patch("pacta.cli.check.DefaultPactaEngine") as mock_engine_cls,
):
store = mock_store_cls.return_value
store.exists.return_value = True
store.load.return_value = snapshot
mock_engine_cls.return_value.check.return_value = check_result
exit_code = main(["check", str(repo_root)])
assert exit_code == 1
def test_check_updates_existing_snapshot(self, tmp_path):
"""Test that check updates the existing snapshot object in-place."""
repo_root = tmp_path / "repo"
repo_root.mkdir()
snapshot = _empty_snapshot(str(repo_root))
report = _make_report(str(repo_root))
check_result = CheckResult(snapshot=snapshot, report=report, diff=None)
with (
patch("pacta.cli.check.FsSnapshotStore") as mock_store_cls,
patch("pacta.cli.check.DefaultPactaEngine") as mock_engine_cls,
):
store = mock_store_cls.return_value
store.exists.return_value = True
store.load.return_value = snapshot
store.resolve_ref.return_value = "abcd1234"
mock_engine_cls.return_value.check.return_value = check_result
main(["check", str(repo_root), "--ref", "myref"])
store.update_object.assert_called_once_with("abcd1234", check_result.snapshot)
def test_check_with_save_ref_creates_additional_ref(self, tmp_path):
"""Test that --save-ref saves under an additional ref."""
repo_root = tmp_path / "repo"
repo_root.mkdir()
snapshot = _empty_snapshot(str(repo_root))
report = _make_report(str(repo_root))
check_result = CheckResult(snapshot=snapshot, report=report, diff=None)
with (
patch("pacta.cli.check.FsSnapshotStore") as mock_store_cls,
patch("pacta.cli.check.DefaultPactaEngine") as mock_engine_cls,
):
store = mock_store_cls.return_value
store.exists.return_value = True
store.load.return_value = snapshot
store.resolve_ref.return_value = "abcd1234"
mock_engine_cls.return_value.check.return_value = check_result
main(["check", str(repo_root), "--ref", "myref", "--save-ref", "extra"])
store.update_object.assert_called_once()
store.save.assert_called_once()
assert "extra" in store.save.call_args.kwargs.get(
"refs", store.save.call_args[1] if len(store.save.call_args) > 1 else []
)
def test_check_json_format(self, tmp_path):
"""Test check command with JSON output."""
repo_root = tmp_path / "repo"
repo_root.mkdir()
snapshot = _empty_snapshot(str(repo_root))
report = _make_report(str(repo_root))
check_result = CheckResult(snapshot=snapshot, report=report, diff=None)
with (
patch("pacta.cli.check.FsSnapshotStore") as mock_store_cls,
patch("pacta.cli.check.DefaultPactaEngine") as mock_engine_cls,
):
store = mock_store_cls.return_value
store.exists.return_value = True
store.load.return_value = snapshot
mock_engine_cls.return_value.check.return_value = check_result
exit_code = main(["check", str(repo_root), "--format", "json"])
assert exit_code == 0