-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathtest_dry_run.py
More file actions
90 lines (68 loc) · 3.17 KB
/
test_dry_run.py
File metadata and controls
90 lines (68 loc) · 3.17 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
"""Tests for the `graphify dry-run` CLI command."""
import sys
import pytest
from pathlib import Path
from unittest.mock import patch
def _run_main(argv):
"""Run graphify.__main__.main() with the given argv, capture stdout."""
import io
from graphify.__main__ import main
buf = io.StringIO()
exit_code = 0
with patch("sys.argv", argv), patch("sys.stdout", buf):
try:
main()
except SystemExit as e:
exit_code = e.code or 0
return buf.getvalue(), exit_code
def test_dry_run_prints_summary(tmp_path):
"""dry-run on a directory with code files prints a file-count summary."""
(tmp_path / "app.py").write_text("x = 1\n")
(tmp_path / "utils.py").write_text("def f(): pass\n")
out, code = _run_main(["graphify", "dry-run", str(tmp_path)])
assert code == 0
assert "Corpus scan" in out
assert "Code files" in out
assert "Total" in out
def test_dry_run_no_files_written(tmp_path):
"""dry-run must not create graphify-out/ or any output files."""
(tmp_path / "readme.md").write_text("# hello\n")
_run_main(["graphify", "dry-run", str(tmp_path)])
assert not (tmp_path / "graphify-out").exists()
def test_dry_run_default_path(tmp_path, monkeypatch):
"""dry-run with no path argument defaults to the current directory."""
(tmp_path / "main.py").write_text("print('hi')\n")
monkeypatch.chdir(tmp_path)
out, code = _run_main(["graphify", "dry-run"])
assert code == 0
assert "Corpus scan" in out
def test_dry_run_missing_path(tmp_path):
"""dry-run with a non-existent path exits non-zero."""
with pytest.raises(SystemExit) as exc:
with patch("sys.argv", ["graphify", "dry-run", str(tmp_path / "nonexistent")]):
from graphify.__main__ import main
main()
assert exc.value.code != 0
def test_dry_run_no_graphify_out_written(tmp_path):
"""dry-run output says no files were written."""
(tmp_path / "a.py").write_text("a = 1\n")
out, _ = _run_main(["graphify", "dry-run", str(tmp_path)])
assert "No files were written" in out
def test_dry_run_office_no_sidecar_written(tmp_path):
"""dry-run must not write office sidecars even when .docx/.xlsx files are present."""
from unittest.mock import MagicMock, patch as mpatch
# Create a fake .docx so detect sees it as an office file
(tmp_path / "report.docx").write_bytes(b"PK\x03\x04") # minimal docx magic bytes
with mpatch("graphify.detect.convert_office_file") as mock_convert:
_run_main(["graphify", "dry-run", str(tmp_path)])
mock_convert.assert_not_called()
def test_dry_run_office_missing_deps_warns(tmp_path):
"""dry-run warns when office deps are missing and content would be empty in a real run."""
from unittest.mock import patch as mpatch
(tmp_path / "report.docx").write_bytes(b"PK\x03\x04")
# Simulate missing python-docx: docx_to_markdown returns ""
with mpatch("graphify.detect.docx_to_markdown", return_value=""):
out, code = _run_main(["graphify", "dry-run", str(tmp_path)])
assert code == 0
assert "office deps missing" in out.lower() or "office" in out.lower()
assert "pip install graphify[office]" in out