Skip to content

Commit 1837e2e

Browse files
committed
Add tests for build/preview CLI flags
1 parent 3342ca5 commit 1837e2e

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

tests/test_build_from_repo.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,96 @@ def test_build_shallow_ignored_without_from_repo():
306306
with patch.object(GreatDocs, "build") as mock_build:
307307
result = runner.invoke(cli, ["build", "--shallow", "--project-path", "."])
308308
assert "--shallow is ignored" in result.output
309+
310+
311+
# ── --preview flag tests ──────────────────────────────────────────────────────
312+
313+
314+
def test_build_preview_calls_preview_site():
315+
"""--preview after --from-repo calls preview_site with the output dir."""
316+
runner = CliRunner()
317+
with (
318+
patch.object(GreatDocs, "build_from_repo") as mock_bfr,
319+
patch.object(GreatDocs, "preview_site") as mock_preview,
320+
):
321+
result = runner.invoke(
322+
cli,
323+
[
324+
"build",
325+
"--from-repo",
326+
"https://github.com/x/y.git",
327+
"--output-dir",
328+
"/tmp/out",
329+
"--preview",
330+
],
331+
)
332+
assert result.exit_code == 0
333+
mock_bfr.assert_called_once()
334+
mock_preview.assert_called_once_with("/tmp/out")
335+
336+
337+
def test_build_preview_default_output_dir():
338+
"""--preview without --output-dir uses the default site path."""
339+
runner = CliRunner()
340+
with (
341+
patch.object(GreatDocs, "build_from_repo") as mock_bfr,
342+
patch.object(GreatDocs, "preview_site") as mock_preview,
343+
):
344+
result = runner.invoke(
345+
cli,
346+
["build", "--from-repo", "https://github.com/x/y.git", "--preview"],
347+
)
348+
assert result.exit_code == 0
349+
mock_preview.assert_called_once()
350+
site_arg = mock_preview.call_args[0][0]
351+
assert site_arg.endswith("great-docs/_site")
352+
353+
354+
def test_build_preview_ignored_without_from_repo():
355+
"""--preview without --from-repo emits a warning."""
356+
runner = CliRunner()
357+
with runner.isolated_filesystem():
358+
Path("pyproject.toml").write_text('[project]\nname = "x"\n')
359+
Path("great-docs.yml").write_text("name: x\n")
360+
with patch.object(GreatDocs, "build") as mock_build:
361+
result = runner.invoke(cli, ["build", "--preview", "--project-path", "."])
362+
assert "--preview is ignored" in result.output
363+
364+
365+
# ── preview --site-dir tests ─────────────────────────────────────────────────
366+
367+
368+
def test_preview_site_dir():
369+
"""preview --site-dir calls preview_site with the given path."""
370+
runner = CliRunner()
371+
with runner.isolated_filesystem():
372+
Path("mysite").mkdir()
373+
(Path("mysite") / "index.html").write_text("<html></html>")
374+
site_path = str(Path("mysite").resolve())
375+
with patch.object(GreatDocs, "preview_site") as mock_preview:
376+
result = runner.invoke(cli, ["preview", "--site-dir", site_path])
377+
mock_preview.assert_called_once_with(site_path, port=3000)
378+
379+
380+
def test_preview_site_dir_project_path_warning():
381+
"""preview --site-dir with --project-path emits a warning."""
382+
runner = CliRunner()
383+
with runner.isolated_filesystem():
384+
Path("mysite").mkdir()
385+
(Path("mysite") / "index.html").write_text("<html></html>")
386+
Path("proj").mkdir()
387+
site_path = str(Path("mysite").resolve())
388+
proj_path = str(Path("proj").resolve())
389+
with patch.object(GreatDocs, "preview_site"):
390+
result = runner.invoke(
391+
cli,
392+
["preview", "--site-dir", site_path, "--project-path", proj_path],
393+
)
394+
assert "--project-path is ignored" in result.output
395+
396+
397+
def test_preview_site_missing_index():
398+
"""preview_site exits when index.html is missing."""
399+
with tempfile.TemporaryDirectory() as tmp:
400+
with pytest.raises(SystemExit):
401+
GreatDocs.preview_site(tmp)

0 commit comments

Comments
 (0)