diff --git a/packages/cli/tests/case2-install.test.mjs b/packages/cli/tests/case2-install.test.mjs index 6509bc65a..932ec4372 100644 --- a/packages/cli/tests/case2-install.test.mjs +++ b/packages/cli/tests/case2-install.test.mjs @@ -250,6 +250,64 @@ test("the install refuses root, a business id mismatch, another --home and a con assert.equal(existsSync(x.business), false); }); +// The selectors (rename design r2): --org/--business and --org-file/--business-file are two +// families, each its own scope. A bad selector refuses with exit 4 before anything is read or written. +const rest = (x) => ["--commit", COMMIT, "--profile", "library", "--personas", x.staging]; + +test("the install takes --org and --org-file, alone or matching their old spelling in either order", (t) => { + for (const sel of [ + (f) => ["--org", "acme", "--org-file", f], + (f) => ["--business", "acme", "--org", "acme", "--business-file", f, "--org-file", f], + (f) => ["--org", "acme", "--business", "acme", "--org-file", f, "--business-file", f], + ]) { + const x = world(t); + const r = install(x, { args: [...sel(x.input), ...rest(x)] }); + assert.equal(r.code, 0, r.out); + assert.equal(JSON.parse(readFileSync(x.business, "utf8")).id, "acme"); + assert.equal(calls(x), `stack-copy copy ${COMMIT} --copies ${join(x.home, ".local", "share", "mosaic-stack", "copies")}\n`); + } +}); + +test("the install refuses a bad selector in either family before it reads or writes anything", (t) => { + const x = world(t); + const f = x.input, g = `${x.root}/input/../input/acme.json`; // join() would normalize it to f + assert.notEqual(g, f); + const usage = "usage: case2-install.sh --business ID --business-file FILE --commit SHA [--profile factory|library] [--personas DIR] [--home DIR]\n"; + const notId = (v) => `case2-install: --business must be a business id: ${v}\n`; + for (const [args, out] of [ + [["--business", "acme", "--org", "beta", "--business-file", f], "case2-install: --business acme and --org beta conflict\n"], + [["--org", "acme", "--business", "beta", "--business-file", f], "case2-install: --org acme and --business beta conflict\n"], + [["--business", "acme", "--business", "acme", "--business-file", f], "case2-install: --business given twice\n"], + [["--org", "acme", "--org", "acme", "--business-file", f], "case2-install: --org given twice\n"], + [["--business-file", f, "--org"], usage], + [["--business-file", f, "--business"], usage], + [["--org", "", "--business-file", f], usage], + [["--org", "--business-file", f], notId("--business-file")], + [["--business", "-x", "--business-file", f], notId("-x")], + [["--org", "Acme", "--business-file", f], notId("Acme")], + [["--org", "a.b", "--business-file", f], notId("a.b")], + // The file family compares the text given: the same file named another way still conflicts. + [["--org", "acme", "--business-file", f, "--org-file", g], `case2-install: --business-file ${f} and --org-file ${g} conflict\n`], + [["--org", "acme", "--org-file", g, "--business-file", f], `case2-install: --org-file ${g} and --business-file ${f} conflict\n`], + [["--org", "acme", "--org-file", f, "--org-file", f], "case2-install: --org-file given twice\n"], + [["--org", "acme", "--business-file", f, "--business-file", f], "case2-install: --business-file given twice\n"], + [["--org", "acme", "--org-file"], usage], + [["--org", "acme", "--org-file", ""], usage], + [["--org", "acme", "--org-file", "--commit", COMMIT], "case2-install: --org-file needs one value, not --commit\n"], + ]) { + const last = ["--business-file", "--business", "--org", "--org-file"].includes(args.at(-1)); + const r = install(x, { args: last ? args : [...args, ...rest(x)] }); + assert.equal(r.code, 4, args.join(" ")); + assert.equal(r.out, out, args.join(" ")); + } + // A selector spelling taken as another option's value is that option's text. + const r = install(x, { args: ["--org", "acme", "--business-file", f, "--commit", "--org", "--profile", "library", "--personas", x.staging] }); + assert.equal(r.code, 4, r.out); + assert.equal(r.out, "case2-install: --commit must be a full 40-character commit id: --org\n"); + assert.equal(calls(x), ""); + assert.equal(existsSync(x.business), false); +}); + test("the library install needs --personas", (t) => { const x = world(t); const r = install(x, { args: ["--business", "acme", "--business-file", x.input, "--commit", COMMIT] }); @@ -433,7 +491,7 @@ function dependencyFreeClone(t) { const x = world(t); const clone = join(x.root, "clone"); mkdirSync(join(clone, "scripts"), { recursive: true }); - for (const file of ["case2-install.sh", "case2-persona-import.mjs"]) { + for (const file of ["case2-install.sh", "case2-persona-import.mjs", "selector.sh"]) { cpSync(join(REPO, "scripts", file), join(clone, "scripts", file)); } // Isolate the persona step; bootstrap and business validation are covered above. diff --git a/scripts/case2-install.sh b/scripts/case2-install.sh index 7968bea06..4e83ece41 100755 --- a/scripts/case2-install.sh +++ b/scripts/case2-install.sh @@ -5,6 +5,10 @@ # case2-install.sh --business ID --business-file FILE --commit SHA # [--profile factory|library] [--personas DIR] [--home DIR] # +# --org ID is the same option as --business ID, and --org-file FILE the same +# as --business-file FILE. They are two families: each spelling once, and a +# second spelling of a family must carry the same text. +# # Steps, in order: refuse root; scripts/bootstrap.sh; stack-copy.sh copy # SHA, whose npm ci installs the pinned harness; for library, the persona # import's install from the staging DIR; the business file at 0600; for @@ -39,11 +43,30 @@ REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" STACK_COPY="${CASE2_STACK_COPY:-$REPO/scripts/stack-copy.sh}" usage() { echo 'usage: case2-install.sh --business ID --business-file FILE --commit SHA [--profile factory|library] [--personas DIR] [--home DIR]' >&2; exit 4; } +# shellcheck source=scripts/selector.sh +. "$REPO/scripts/selector.sh" +# The selectors (rename design r2): the id family and the file family are separate scopes. A +# missing value is a usage error; a flag-shaped or invalid id is the business-id refusal below; a +# flag-shaped file, a repeated spelling and a conflict say why. All exit 4, as before. +id_refused() { + case "$1" in + missing) usage ;; + flag|invalid) echo "case2-install: --business must be a business id: $BUSINESS" >&2; exit 4 ;; + *) echo "case2-install: $2" >&2; exit 4 ;; + esac +} +file_refused() { + case "$1" in + missing) usage ;; + *) echo "case2-install: $2" >&2; exit 4 ;; + esac +} +business_id_ok() { [[ "$1" =~ ^[a-z0-9][a-z0-9-]{0,63}$ ]]; } PROFILE=library BUSINESS='' BFILE='' COMMIT='' PERSONAS='' HOME_ARG="${HOME:-}" while [ $# -gt 0 ]; do case "$1" in - --business) [ $# -ge 2 ] || usage; BUSINESS="$2"; shift 2 ;; - --business-file) [ $# -ge 2 ] || usage; BFILE="$2"; shift 2 ;; + --business|--org) BUSINESS="${2-}"; selector_take id id id_refused business_id_ok "$1" "${@:2:1}"; shift 2 ;; + --business-file|--org-file) selector_take file file file_refused - "$1" "${@:2:1}"; BFILE="$2"; shift 2 ;; --commit) [ $# -ge 2 ] || usage; COMMIT="$2"; shift 2 ;; --profile) [ $# -ge 2 ] || usage; PROFILE="$2"; shift 2 ;; --personas) [ $# -ge 2 ] || usage; PERSONAS="$2"; shift 2 ;;