From 00a06eab44853c35ab59138746a76fd6e6f60f6c Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 18 Jun 2026 20:38:29 +0800 Subject: [PATCH] refactor(test): dedupe installer npm fixtures --- test/scripts/install-cli.test.ts | 72 ++-------------------------- test/scripts/install-npm-fixtures.ts | 70 +++++++++++++++++++++++++++ test/scripts/install-sh.test.ts | 72 ++-------------------------- 3 files changed, 78 insertions(+), 136 deletions(-) create mode 100644 test/scripts/install-npm-fixtures.ts diff --git a/test/scripts/install-cli.test.ts b/test/scripts/install-cli.test.ts index a8e39491623..319c234e99b 100644 --- a/test/scripts/install-cli.test.ts +++ b/test/scripts/install-cli.test.ts @@ -14,6 +14,10 @@ import { import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; +import { + writeNpmBeforePolicyFixture, + writeNpmFreshnessConflictFixture, +} from "./install-npm-fixtures.js"; const SCRIPT_PATH = "scripts/install-cli.sh"; @@ -34,74 +38,6 @@ function linkRequiredShellTools(bin: string) { } } -function writeNpmFreshnessConflictFixture(path: string, argsLog: string) { - writeFileSync( - path, - [ - "#!/usr/bin/env bash", - "set -euo pipefail", - `printf '%s\\n' "$*" >> ${JSON.stringify(argsLog)}`, - 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "min-release-age" ]]; then', - " printf 'null\\n'", - " exit 0", - "fi", - 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "before" ]]; then', - " printf 'Wed May 13 2026 21:25:20 GMT-0300 (Brasilia Standard Time)\\n'", - " exit 0", - "fi", - 'for arg in "$@"; do', - ' if [[ "$arg" == --before=* ]]; then', - " printf '%s\\n' 'Exit prior to config file resolving' >&2", - " printf '%s\\n' 'cause' >&2", - " printf '%s\\n' '--min-release-age cannot be provided when using --before' >&2", - " exit 64", - " fi", - "done", - 'for arg in "$@"; do', - ' if [[ "$arg" == "--min-release-age=0" ]]; then', - " exit 0", - " fi", - "done", - "exit 65", - "", - ].join("\n"), - ); - chmodSync(path, 0o755); -} - -function writeNpmBeforePolicyFixture(path: string, argsLog: string) { - writeFileSync( - path, - [ - "#!/usr/bin/env bash", - "set -euo pipefail", - `printf '%s\\n' "$*" >> ${JSON.stringify(argsLog)}`, - 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "min-release-age" ]]; then', - " printf 'null\\n'", - " exit 0", - "fi", - 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "before" ]]; then', - " printf 'Wed May 13 2026 21:25:20 GMT-0300 (Brasilia Standard Time)\\n'", - " exit 0", - "fi", - 'for arg in "$@"; do', - ' if [[ "$arg" == "--min-release-age=0" ]]; then', - " printf '%s\\n' 'min-release-age should not be selected for project-only npmrc' >&2", - " exit 64", - " fi", - "done", - 'for arg in "$@"; do', - ' if [[ "$arg" == --before=* ]]; then', - " exit 0", - " fi", - "done", - "exit 65", - "", - ].join("\n"), - ); - chmodSync(path, 0o755); -} - describe("install-cli.sh", () => { const script = readFileSync(SCRIPT_PATH, "utf8"); diff --git a/test/scripts/install-npm-fixtures.ts b/test/scripts/install-npm-fixtures.ts new file mode 100644 index 00000000000..2ccd767bcee --- /dev/null +++ b/test/scripts/install-npm-fixtures.ts @@ -0,0 +1,70 @@ +// NPM CLI fixture writers used by installer shell-script tests. +import { chmodSync, writeFileSync } from "node:fs"; + +export function writeNpmFreshnessConflictFixture(path: string, argsLog: string) { + writeFileSync( + path, + [ + "#!/usr/bin/env bash", + "set -euo pipefail", + `printf '%s\\n' "$*" >> ${JSON.stringify(argsLog)}`, + 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "min-release-age" ]]; then', + " printf 'null\\n'", + " exit 0", + "fi", + 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "before" ]]; then', + " printf 'Wed May 13 2026 21:25:20 GMT-0300 (Brasilia Standard Time)\\n'", + " exit 0", + "fi", + 'for arg in "$@"; do', + ' if [[ "$arg" == --before=* ]]; then', + " printf '%s\\n' 'Exit prior to config file resolving' >&2", + " printf '%s\\n' 'cause' >&2", + " printf '%s\\n' '--min-release-age cannot be provided when using --before' >&2", + " exit 64", + " fi", + "done", + 'for arg in "$@"; do', + ' if [[ "$arg" == "--min-release-age=0" ]]; then', + " exit 0", + " fi", + "done", + "exit 65", + "", + ].join("\n"), + ); + chmodSync(path, 0o755); +} + +export function writeNpmBeforePolicyFixture(path: string, argsLog: string) { + writeFileSync( + path, + [ + "#!/usr/bin/env bash", + "set -euo pipefail", + `printf '%s\\n' "$*" >> ${JSON.stringify(argsLog)}`, + 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "min-release-age" ]]; then', + " printf 'null\\n'", + " exit 0", + "fi", + 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "before" ]]; then', + " printf 'Wed May 13 2026 21:25:20 GMT-0300 (Brasilia Standard Time)\\n'", + " exit 0", + "fi", + 'for arg in "$@"; do', + ' if [[ "$arg" == "--min-release-age=0" ]]; then', + " printf '%s\\n' 'min-release-age should not be selected for project-only npmrc' >&2", + " exit 64", + " fi", + "done", + 'for arg in "$@"; do', + ' if [[ "$arg" == --before=* ]]; then', + " exit 0", + " fi", + "done", + "exit 65", + "", + ].join("\n"), + ); + chmodSync(path, 0o755); +} diff --git a/test/scripts/install-sh.test.ts b/test/scripts/install-sh.test.ts index 2e95d607273..5a41af7c865 100644 --- a/test/scripts/install-sh.test.ts +++ b/test/scripts/install-sh.test.ts @@ -4,6 +4,10 @@ import { chmodSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; +import { + writeNpmBeforePolicyFixture, + writeNpmFreshnessConflictFixture, +} from "./install-npm-fixtures.js"; const SCRIPT_PATH = "scripts/install.sh"; @@ -26,74 +30,6 @@ function runInstallShell(script: string, env: NodeJS.ProcessEnv = {}) { } } -function writeNpmFreshnessConflictFixture(path: string, argsLog: string) { - writeFileSync( - path, - [ - "#!/usr/bin/env bash", - "set -euo pipefail", - `printf '%s\\n' "$*" >> ${JSON.stringify(argsLog)}`, - 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "min-release-age" ]]; then', - " printf 'null\\n'", - " exit 0", - "fi", - 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "before" ]]; then', - " printf 'Wed May 13 2026 21:25:20 GMT-0300 (Brasilia Standard Time)\\n'", - " exit 0", - "fi", - 'for arg in "$@"; do', - ' if [[ "$arg" == --before=* ]]; then', - " printf '%s\\n' 'Exit prior to config file resolving' >&2", - " printf '%s\\n' 'cause' >&2", - " printf '%s\\n' '--min-release-age cannot be provided when using --before' >&2", - " exit 64", - " fi", - "done", - 'for arg in "$@"; do', - ' if [[ "$arg" == "--min-release-age=0" ]]; then', - " exit 0", - " fi", - "done", - "exit 65", - "", - ].join("\n"), - ); - chmodSync(path, 0o755); -} - -function writeNpmBeforePolicyFixture(path: string, argsLog: string) { - writeFileSync( - path, - [ - "#!/usr/bin/env bash", - "set -euo pipefail", - `printf '%s\\n' "$*" >> ${JSON.stringify(argsLog)}`, - 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "min-release-age" ]]; then', - " printf 'null\\n'", - " exit 0", - "fi", - 'if [[ "$1" == "config" && "$2" == "get" && "$3" == "before" ]]; then', - " printf 'Wed May 13 2026 21:25:20 GMT-0300 (Brasilia Standard Time)\\n'", - " exit 0", - "fi", - 'for arg in "$@"; do', - ' if [[ "$arg" == "--min-release-age=0" ]]; then', - " printf '%s\\n' 'min-release-age should not be selected for project-only npmrc' >&2", - " exit 64", - " fi", - "done", - 'for arg in "$@"; do', - ' if [[ "$arg" == --before=* ]]; then', - " exit 0", - " fi", - "done", - "exit 65", - "", - ].join("\n"), - ); - chmodSync(path, 0o755); -} - describe("install.sh", () => { const script = readFileSync(SCRIPT_PATH, "utf8");