|
| 1 | +// Probe for deploy.mjs argument handling (issue #3281). |
| 2 | +// |
| 3 | +// Runs deploy.mjs in a temp dir with stubbed `bws` / `wrangler` binaries on |
| 4 | +// PATH and a stubbed global fetch (via --import), and pins the three cases: |
| 5 | +// --dry-run -> validation only, exits 0, nothing deployed |
| 6 | +// --dry-rnu -> usage error, non-zero exit, ZERO external calls logged |
| 7 | +// (no args) -> the real deploy sequence runs against the stubs |
| 8 | +// Run with: node --test deploy.test.mjs |
| 9 | +import assert from "node:assert/strict"; |
| 10 | +import { execFile } from "node:child_process"; |
| 11 | +import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; |
| 12 | +import { tmpdir } from "node:os"; |
| 13 | +import { dirname, join } from "node:path"; |
| 14 | +import test from "node:test"; |
| 15 | +import { fileURLToPath } from "node:url"; |
| 16 | +import { promisify } from "node:util"; |
| 17 | + |
| 18 | +const run = promisify(execFile); |
| 19 | +const here = dirname(fileURLToPath(import.meta.url)); |
| 20 | + |
| 21 | +function makeSandbox() { |
| 22 | + const dir = mkdtempSync(join(tmpdir(), "deploy-probe-")); |
| 23 | + const bin = join(dir, "bin"); |
| 24 | + mkdirSync(bin); |
| 25 | + copyFileSync(join(here, "deploy.mjs"), join(dir, "deploy.mjs")); |
| 26 | + copyFileSync(join(here, "config.json"), join(dir, "config.json")); |
| 27 | + const log = join(dir, "calls.log"); |
| 28 | + writeFileSync(log, ""); |
| 29 | + // bws stub: logs the call, returns a stub value for every secret |
| 30 | + // deploy.mjs looks up (the account token plus each caller's tokenKey, |
| 31 | + // derived from config.json so the probe survives config edits). |
| 32 | + const config = JSON.parse(readFileSync(join(here, "config.json"), "utf8")); |
| 33 | + const keys = ["cloudflare.api-token-account-wide", ...Object.values(config.callers).map((caller) => caller.tokenKey)]; |
| 34 | + const secretsJson = JSON.stringify(keys.map((key) => ({ key, value: `stub-${key}` }))); |
| 35 | + writeFileSync(join(bin, "bws"), `#!/bin/sh\necho "bws $@" >> "${log}"\nprintf '%s' '${secretsJson}'\n`); |
| 36 | + writeFileSync(join(bin, "wrangler"), `#!/bin/sh\necho "wrangler $@" >> "${log}"\nexit 0\n`); |
| 37 | + chmodSync(join(bin, "bws"), 0o755); |
| 38 | + chmodSync(join(bin, "wrangler"), 0o755); |
| 39 | + // fetch stub: every retired Worker reports 404 (already gone), so the |
| 40 | + // retire loop logs and moves on without a wrangler delete. |
| 41 | + const fetchStub = join(dir, "fetch-stub.mjs"); |
| 42 | + writeFileSync(fetchStub, `globalThis.fetch = async () => ({ status: 404, ok: false });\n`); |
| 43 | + return { dir, bin, log, fetchStub }; |
| 44 | +} |
| 45 | + |
| 46 | +async function runDeploy(sb, args) { |
| 47 | + try { |
| 48 | + const { stdout, stderr } = await run("node", ["--import", sb.fetchStub, join(sb.dir, "deploy.mjs"), ...args], { |
| 49 | + env: { |
| 50 | + ...process.env, |
| 51 | + PATH: `${sb.bin}:${process.env.PATH}`, |
| 52 | + CLOUDFLARE_ACCOUNT_ID: "stub-account", |
| 53 | + }, |
| 54 | + }); |
| 55 | + return { code: 0, stdout, stderr }; |
| 56 | + } catch (error) { |
| 57 | + return { code: error.code, stdout: error.stdout ?? "", stderr: error.stderr ?? "" }; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +test("--dry-run validates only and deploys nothing", async () => { |
| 62 | + const sb = makeSandbox(); |
| 63 | + const result = await runDeploy(sb, ["--dry-run"]); |
| 64 | + assert.equal(result.code, 0, result.stderr); |
| 65 | + assert.match(result.stdout, /nothing deployed/); |
| 66 | + const calls = readFileSync(sb.log, "utf8"); |
| 67 | + assert.match(calls, /bws secret list/); |
| 68 | + assert.match(calls, /wrangler deploy --dry-run/); |
| 69 | + assert.doesNotMatch(calls, /wrangler deploy --config/); |
| 70 | + assert.doesNotMatch(calls, /secret put/); |
| 71 | +}); |
| 72 | + |
| 73 | +test("--dry-rnu (typo) aborts with a usage error before any external call", async () => { |
| 74 | + const sb = makeSandbox(); |
| 75 | + const result = await runDeploy(sb, ["--dry-rnu"]); |
| 76 | + assert.notEqual(result.code, 0); |
| 77 | + assert.match(result.stderr, /usage: deploy\.mjs \[--dry-run\]/); |
| 78 | + assert.equal(readFileSync(sb.log, "utf8"), "", "no bws/wrangler call may be logged"); |
| 79 | +}); |
| 80 | + |
| 81 | +test("--bogus-flag aborts with a usage error before any external call", async () => { |
| 82 | + const sb = makeSandbox(); |
| 83 | + const result = await runDeploy(sb, ["--bogus-flag"]); |
| 84 | + assert.notEqual(result.code, 0); |
| 85 | + assert.match(result.stderr, /unrecognized argument\(s\): --bogus-flag/); |
| 86 | + assert.equal(readFileSync(sb.log, "utf8"), "", "no bws/wrangler call may be logged"); |
| 87 | +}); |
| 88 | + |
| 89 | +test("no args runs the real deploy sequence against the stubs", async () => { |
| 90 | + const sb = makeSandbox(); |
| 91 | + const result = await runDeploy(sb, []); |
| 92 | + assert.equal(result.code, 0, result.stderr); |
| 93 | + assert.match(result.stdout, /deployed mail with callers/); |
| 94 | + const calls = readFileSync(sb.log, "utf8"); |
| 95 | + assert.match(calls, /bws secret list/); |
| 96 | + assert.match(calls, /wrangler deploy --dry-run/); |
| 97 | + assert.match(calls, /wrangler deploy --config/); |
| 98 | + assert.match(calls, /wrangler secret put CALLERS/); |
| 99 | +}); |