Skip to content

Commit 04ace6f

Browse files
Copilotoscarlevin
andcommitted
Add test_build_generate to test pretext build -g behavior
Co-authored-by: oscarlevin <6504596+oscarlevin@users.noreply.github.com> Agent-Logs-Url: https://github.com/PreTeXtBook/pretext-cli/sessions/782c1d2c-2645-447a-9a8d-371f40e95808
1 parent 8a96901 commit 04ace6f

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

tests/test_cli.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,91 @@ def test_init_and_update_with_git(tmp_path: Path, script_runner: ScriptRunner) -
237237
assert f"pretext == {pretext.VERSION}\n" in lines[1]
238238

239239

240+
def test_build_generate(tmp_path: Path, script_runner: ScriptRunner) -> None:
241+
"""
242+
Test the behavior of `pretext build -g` (force generate) and `pretext build -q` (no generate).
243+
244+
Expected behaviors (per docs/asset-generation.md):
245+
- `pretext build -q`: no assets are generated, even if source has changed.
246+
- `pretext build -g`: all assets generated regardless of whether source has changed.
247+
- `pretext build -g -q`: warning issued; proceeds as if neither flag was set.
248+
- Regular `pretext build` after a successful build: assets NOT regenerated when source is unchanged.
249+
"""
250+
graphics_path = tmp_path / "graphics"
251+
shutil.copytree(EXAMPLES_DIR / "projects" / "graphics", graphics_path)
252+
253+
asymptote_asset = graphics_path / "generated-assets" / "asymptote" / "test.html"
254+
prefigure_asset = graphics_path / "generated-assets" / "prefigure" / "pftest.svg"
255+
256+
# --- Test -q: build without generating any assets ---
257+
result = script_runner.run(
258+
[PTX_CMD, "-v", "debug", "build", "-q"], cwd=graphics_path
259+
)
260+
assert result.success
261+
assert not asymptote_asset.exists(), (
262+
"Assets should NOT be generated when using -q (no-generate)"
263+
)
264+
assert not prefigure_asset.exists(), (
265+
"Assets should NOT be generated when using -q (no-generate)"
266+
)
267+
268+
# --- Test -g: build with force-generate ---
269+
result = script_runner.run(
270+
[PTX_CMD, "-v", "debug", "build", "-g"], cwd=graphics_path
271+
)
272+
assert result.success
273+
assert asymptote_asset.exists(), (
274+
"Asymptote asset should be generated when using -g (force generate)"
275+
)
276+
assert prefigure_asset.exists(), (
277+
"Prefigure asset should be generated when using -g (force generate)"
278+
)
279+
assert (graphics_path / "output" / "web").exists(), (
280+
"Build output should exist after `pretext build -g`"
281+
)
282+
283+
# --- Test regular build does NOT regenerate when source is unchanged ---
284+
# Delete the generated asset to simulate a missing file after a previous build.
285+
asymptote_asset.unlink()
286+
assert not asymptote_asset.exists()
287+
288+
# A regular build (no -g flag) should NOT regenerate since source hash has not changed.
289+
result = script_runner.run(
290+
[PTX_CMD, "-v", "debug", "build"], cwd=graphics_path
291+
)
292+
assert result.success
293+
assert not asymptote_asset.exists(), (
294+
"Regular `pretext build` should NOT regenerate assets when source is unchanged"
295+
)
296+
297+
# --- Test -g regenerates even though source is unchanged ---
298+
result = script_runner.run(
299+
[PTX_CMD, "-v", "debug", "build", "-g"], cwd=graphics_path
300+
)
301+
assert result.success
302+
assert asymptote_asset.exists(), (
303+
"`pretext build -g` should regenerate assets even when source is unchanged"
304+
)
305+
306+
# --- Test -g -q together: warning issued, behaves like normal build ---
307+
# Remove an asset to confirm it is NOT regenerated (warning clears both flags, behaves like normal build).
308+
asymptote_asset.unlink()
309+
result = script_runner.run(
310+
[PTX_CMD, "-v", "debug", "build", "-g", "-q"], cwd=graphics_path
311+
)
312+
assert result.success
313+
# The warning message about conflicting flags should appear in the output.
314+
combined_output = result.stdout + result.stderr
315+
assert "doesn't make sense" in combined_output, (
316+
"A warning about using -g and -q together should be emitted"
317+
)
318+
# Since the flags cancel each other, behavior is like a normal build with no source changes,
319+
# so assets should NOT be regenerated.
320+
assert not asymptote_asset.exists(), (
321+
"With -g and -q together, assets should not be regenerated (flags cancel each other)"
322+
)
323+
324+
240325
def test_generate_graphics(tmp_path: Path, script_runner: ScriptRunner) -> None:
241326
graphics_path = tmp_path / "graphics"
242327
shutil.copytree(EXAMPLES_DIR / "projects" / "graphics", graphics_path)

0 commit comments

Comments
 (0)