From e846425dddd9bf3648efb73aac64a79146cf615d Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 30 Oct 2024 11:33:38 +0000 Subject: [PATCH 1/2] Add a test for the environment variable QUARTO_VERSION_REQUIREMENT Follow up on 00249a46bab6e3e45adb9900677a33e599cb10d3 which closed #10442 --- tests/docs/quarto-required.qmd | 1 + tests/smoke/render/render-required.test.ts | 23 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/tests/docs/quarto-required.qmd b/tests/docs/quarto-required.qmd index f4d8242f5b2..78fcd072785 100644 --- a/tests/docs/quarto-required.qmd +++ b/tests/docs/quarto-required.qmd @@ -3,6 +3,7 @@ title: Hello Callout! format: html: quarto-required: "<= 0.0.0" + html+norequire: default --- This file will not be rendered as it does not respect the quarto version requirement \ No newline at end of file diff --git a/tests/smoke/render/render-required.test.ts b/tests/smoke/render/render-required.test.ts index 68a216e9a2a..fd500129cc7 100644 --- a/tests/smoke/render/render-required.test.ts +++ b/tests/smoke/render/render-required.test.ts @@ -16,3 +16,26 @@ testQuartoCmd( [input], [printsMessage("ERROR", /does not satisfy version/)] ); + +let oldVersionRequirement: string | undefined; + +testQuartoCmd( + "render", + [input, "--to", "html+norequire"], + [printsMessage("ERROR", /does not meet semver requirement/)], + { + setup: async () => { + // Save current version of QUARTO_VERSION_REQUIREMENT env var and set it to a value that will not be satisfied + oldVersionRequirement = Deno.env.get("QUARTO_VERSION_REQUIREMENT"); + Deno.env.set("QUARTO_VERSION_REQUIREMENT", "< 0.0.0"); + }, + teardown: async () => { + // Restore QUARTO_VERSION_REQUIREMENT + if (oldVersionRequirement) { + Deno.env.set("QUARTO_VERSION_REQUIREMENT", oldVersionRequirement); + } else { + Deno.env.delete("QUARTO_VERSION_REQUIREMENT"); + } + }, + } +); From d3a80b45406285345ba41603b9567b46608c3bfe Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 30 Oct 2024 11:34:34 +0000 Subject: [PATCH 2/2] Mock Deno.exit() in the test to throw an error This is required as Deno.exit(1) after `error()` is making `test.execute()` exit without possibility to cath the error and log the error to be verified. Our test logic does not even fail on this Deno.exit() happening. Tests continue and nor OK or FAILED is thrown. Our usual pattern to throw an error when using error(), but it may not apply in this case for checking version requirement in quarto.ts. This is why mocking is chosen here. --- tests/smoke/render/render-required.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/smoke/render/render-required.test.ts b/tests/smoke/render/render-required.test.ts index fd500129cc7..4cb83d36bb3 100644 --- a/tests/smoke/render/render-required.test.ts +++ b/tests/smoke/render/render-required.test.ts @@ -18,6 +18,7 @@ testQuartoCmd( ); let oldVersionRequirement: string | undefined; +let originalDenoExit: typeof Deno.exit; testQuartoCmd( "render", @@ -28,6 +29,12 @@ testQuartoCmd( // Save current version of QUARTO_VERSION_REQUIREMENT env var and set it to a value that will not be satisfied oldVersionRequirement = Deno.env.get("QUARTO_VERSION_REQUIREMENT"); Deno.env.set("QUARTO_VERSION_REQUIREMENT", "< 0.0.0"); + // Mock Deno.exit to throw an error instead of exiting + // Otherwise we would not check the error assertion + originalDenoExit = Deno.exit; + Deno.exit = (code?: number) => { + throw new Error(`Deno.exit called with code: ${code}`); + }; }, teardown: async () => { // Restore QUARTO_VERSION_REQUIREMENT @@ -36,6 +43,8 @@ testQuartoCmd( } else { Deno.env.delete("QUARTO_VERSION_REQUIREMENT"); } + // Restore Deno.exit + Deno.exit = originalDenoExit; }, } );