diff --git a/.changeset/checkout-flag-precedence.md b/.changeset/checkout-flag-precedence.md new file mode 100644 index 000000000..bf0d34217 --- /dev/null +++ b/.changeset/checkout-flag-precedence.md @@ -0,0 +1,5 @@ +--- +'@openfn/cli': patch +--- + +An explicit `--checkout` or `--no-checkout` on `project deploy` now overrides `OPENFN_NO_CHECKOUT_AFTER_DEPLOY` diff --git a/packages/cli/src/projects/options.ts b/packages/cli/src/projects/options.ts index cf266a8d5..855a09431 100644 --- a/packages/cli/src/projects/options.ts +++ b/packages/cli/src/projects/options.ts @@ -65,13 +65,13 @@ export const checkout: CLIOption = { name: 'checkout', yargs: { boolean: true, - default: true, description: 'Checkout the project after deploying. Use --no-checkout or OPENFN_NO_CHECKOUT_AFTER_DEPLOY=true to skip', }, ensure: (opts: any) => { - if (process.env.OPENFN_NO_CHECKOUT_AFTER_DEPLOY === 'true') { - opts.checkout = false; + // An explicit --checkout or --no-checkout always beats the env var + if (opts.checkout === undefined) { + opts.checkout = process.env.OPENFN_NO_CHECKOUT_AFTER_DEPLOY !== 'true'; } }, }; diff --git a/packages/cli/test/projects/options.test.ts b/packages/cli/test/projects/options.test.ts new file mode 100644 index 000000000..56025ce31 --- /dev/null +++ b/packages/cli/test/projects/options.test.ts @@ -0,0 +1,63 @@ +import test from 'ava'; +import yargs from 'yargs'; + +import { command } from '../../src/projects/deploy'; + +const ENV = 'OPENFN_NO_CHECKOUT_AFTER_DEPLOY'; +const original = process.env[ENV]; + +test.afterEach.always(() => { + if (original === undefined) { + delete process.env[ENV]; + } else { + process.env[ENV] = original; + } +}); + +// Parsing runs the command handler (ensure), so this returns the final options +const parse = (cmd: string) => + yargs() + .command(command as any) + .parse(cmd) as any; + +test.serial('checkout defaults to true', (t) => { + delete process.env[ENV]; + const options = parse('deploy'); + t.true(options.checkout); +}); + +test.serial('env=true with no flag skips checkout', (t) => { + process.env[ENV] = 'true'; + const options = parse('deploy'); + t.false(options.checkout); +}); + +test.serial('env=false with no flag checks out', (t) => { + process.env[ENV] = 'false'; + const options = parse('deploy'); + t.true(options.checkout); +}); + +test.serial('env=1 (truthy but not "true") with no flag checks out', (t) => { + process.env[ENV] = '1'; + const options = parse('deploy'); + t.true(options.checkout); +}); + +test.serial('env=true with --checkout checks out', (t) => { + process.env[ENV] = 'true'; + const options = parse('deploy --checkout'); + t.true(options.checkout); +}); + +test.serial('env=false with --no-checkout skips checkout', (t) => { + process.env[ENV] = 'false'; + const options = parse('deploy --no-checkout'); + t.false(options.checkout); +}); + +test.serial('env unset with --no-checkout skips checkout', (t) => { + delete process.env[ENV]; + const options = parse('deploy --no-checkout'); + t.false(options.checkout); +});