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