@rightkit/ci 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,161 +0,0 @@
1
- import test from "node:test";
2
- import assert from "node:assert/strict";
3
- import fs from "node:fs";
4
- import os from "node:os";
5
- import path from "node:path";
6
- import { spawnSync } from "node:child_process";
7
- import { fileURLToPath } from "node:url";
8
- import { renderTemplate } from "./render.mjs";
9
- import { buildRenderContext, lockfilePatchStep } from "./manifest.mjs";
10
-
11
- const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)));
12
- const templatesDir = path.join(packageRoot, "templates");
13
-
14
- const TEMPLATES = { node: "ci.yml", rust: "profiles/rust-hybrid/ci.yml" };
15
- const INSTALL_LINE = " - run: pnpm install --frozen-lockfile";
16
- const STEP_NAME = " - name: Print pnpm-lock.yaml patch when the lockfile is stale";
17
-
18
- const NODE_BASE = {
19
- schemaVersion: 1,
20
- compile: "github-actions-only",
21
- lanes: ["ci"],
22
- matrix: { os: ["ubuntu-latest"], node: ["24"] },
23
- templateVersion: "1.0.0",
24
- };
25
- const RUST_BASE = {
26
- ...NODE_BASE,
27
- profile: "rust-hybrid",
28
- gate: { path: "scripts/gate.sh" },
29
- matrix: { os: ["xcode-27", "windows-2025"], node: ["26.8.1"] },
30
- ciAllOs: true,
31
- };
32
- const manifestFor = (profile, packageManager) => ({ ...(profile === "rust" ? RUST_BASE : NODE_BASE), packageManager });
33
-
34
- function render(profile, manifest, override = {}) {
35
- const context = { ...buildRenderContext(structuredClone(manifest), "/repos/widgets"), ...override };
36
- return renderTemplate(fs.readFileSync(path.join(templatesDir, TEMPLATES[profile]), "utf8"), context);
37
- }
38
-
39
- // The step's run body with the YAML block indent removed, as GitHub Actions runs it.
40
- function runBody(step) {
41
- const head = " run: |\n";
42
- const body = step.slice(step.indexOf(head) + head.length);
43
- return body.split("\n").map((line) => line.slice(10)).join("\n");
44
- }
45
-
46
- test("pnpm: both ci templates print the patch step directly after the frozen install", () => {
47
- for (const profile of ["node", "rust"]) {
48
- const out = render(profile, manifestFor(profile, "pnpm@11.24.0"));
49
- assert.equal(out.split(STEP_NAME).length - 1, 1, `${profile}: exactly one patch step`);
50
- assert.equal(out.split(INSTALL_LINE).length - 1, 1, `${profile}: exactly one frozen install`);
51
- assert.ok(out.includes(`${INSTALL_LINE}\n${STEP_NAME}`), `${profile}: step follows the install`);
52
- assert.match(out, / if: failure\(\)\n/);
53
- }
54
- });
55
-
56
- test("pnpm: the patch step is the only difference from a render without it", () => {
57
- for (const profile of ["node", "rust"]) {
58
- const manifest = manifestFor(profile, "pnpm@11.24.0");
59
- const step = lockfilePatchStep(manifest);
60
- const withStep = render(profile, manifest);
61
- const withoutStep = render(profile, manifest, { PNPM_LOCK_PATCH_STEP: "" });
62
- assert.equal(withoutStep.split(INSTALL_LINE).length - 1, 1, `${profile}: install line is unique`);
63
- assert.equal(withStep, withoutStep.replace(INSTALL_LINE, INSTALL_LINE + step), `${profile}: only the new step differs`);
64
- }
65
- });
66
-
67
- test("npm and yarn: no patch step and no pnpm lockfile reference in either ci template", () => {
68
- for (const pm of ["npm@10.9.2", "yarn@4.5.0"]) {
69
- for (const profile of ["node", "rust"]) {
70
- const manifest = manifestFor(profile, pm);
71
- assert.equal(lockfilePatchStep(manifest), "", `${pm} ${profile}: no step generated`);
72
- const out = render(profile, manifest);
73
- assert.doesNotMatch(out, /lockfile-only|pnpm-lock\.yaml/, `${pm} ${profile}: no pnpm lockfile text`);
74
- }
75
- }
76
- });
77
-
78
- test("patch step runs only after a failed install, never masks it, and prints the lockfile diff", () => {
79
- const step = lockfilePatchStep(manifestFor("node", "pnpm@11.24.0"));
80
- assert.match(step, /^\n {6}- name: /);
81
- assert.match(step, /\n {8}if: failure\(\)\n/);
82
- const body = runBody(step);
83
- assert.match(body, /pnpm install --lockfile-only/);
84
- assert.match(body, /git diff -- pnpm-lock\.yaml/);
85
- assert.doesNotMatch(body, /\bexit\b/, "the step must not choose the job result");
86
- });
87
-
88
- test("patch step behaves on a real stale, fresh and failing lockfile", (t) => {
89
- if (process.platform === "win32") return t.skip("POSIX shell behaviour");
90
- const git = spawnSync("git", ["--version"], { encoding: "utf8" });
91
- if (git.error) return t.skip("git not installed");
92
- const body = runBody(lockfilePatchStep(manifestFor("node", "pnpm@11.24.0")));
93
- const root = fs.mkdtempSync(path.join(os.tmpdir(), "right-git-pnpm-lock-"));
94
- t.after(() => fs.rmSync(root, { recursive: true, force: true }));
95
- const repo = path.join(root, "repo");
96
- const bin = path.join(root, "bin");
97
- fs.mkdirSync(repo);
98
- fs.mkdirSync(bin);
99
- fs.writeFileSync(path.join(root, "step.sh"), `${body}\n`);
100
- // Stand-in for pnpm: `install --lockfile-only` rewrites the lockfile to $FAKE_PNPM_LOCK and exits $FAKE_PNPM_STATUS.
101
- fs.writeFileSync(path.join(bin, "pnpm"), [
102
- "#!/bin/sh",
103
- 'if [ "$1" = "install" ] && [ "$2" = "--lockfile-only" ]; then',
104
- ' printf \'%s\\n\' "$FAKE_PNPM_LOCK" > pnpm-lock.yaml',
105
- ' exit "${FAKE_PNPM_STATUS:-0}"',
106
- "fi",
107
- 'echo "unexpected pnpm arguments: $*" >&2',
108
- "exit 2",
109
- "",
110
- ].join("\n"));
111
- fs.chmodSync(path.join(bin, "pnpm"), 0o755);
112
- const gitRun = (args) => spawnSync("git", args, { cwd: repo, encoding: "utf8" });
113
- gitRun(["init", "-q"]);
114
- fs.writeFileSync(path.join(repo, "pnpm-lock.yaml"), "lockfileVersion: v1\n");
115
- gitRun(["add", "pnpm-lock.yaml"]);
116
- gitRun(["-c", "user.name=test", "-c", "user.email=test@example.invalid", "commit", "-qm", "init"]);
117
-
118
- const runStep = (env) => spawnSync("bash", ["--noprofile", "--norc", "-eo", "pipefail", path.join(root, "step.sh")], {
119
- cwd: repo,
120
- encoding: "utf8",
121
- env: { ...process.env, PATH: `${bin}${path.delimiter}${process.env.PATH}`, ...env },
122
- });
123
-
124
- const stale = runStep({ FAKE_PNPM_LOCK: "lockfileVersion: v2", FAKE_PNPM_STATUS: "0" });
125
- assert.equal(stale.status, 0, stale.stderr);
126
- assert.match(stale.stdout, /::group::pnpm-lock\.yaml patch/);
127
- assert.match(stale.stdout, /^-lockfileVersion: v1$/m);
128
- assert.match(stale.stdout, /^\+lockfileVersion: v2$/m);
129
- assert.match(stale.stdout, /::error file=pnpm-lock\.yaml::/);
130
- assert.doesNotMatch(stale.stdout, /::notice::/);
131
-
132
- gitRun(["checkout", "--", "pnpm-lock.yaml"]);
133
- const fresh = runStep({ FAKE_PNPM_LOCK: "lockfileVersion: v1", FAKE_PNPM_STATUS: "0" });
134
- assert.equal(fresh.status, 0, fresh.stderr);
135
- assert.match(fresh.stdout, /::notice::pnpm-lock\.yaml already matches package\.json/);
136
- assert.doesNotMatch(fresh.stdout, /::group::/);
137
-
138
- const failing = runStep({ FAKE_PNPM_LOCK: "lockfileVersion: v1", FAKE_PNPM_STATUS: "1" });
139
- assert.equal(failing.status, 0, "the step itself succeeds; the failed install already failed the job");
140
- assert.match(failing.stdout, /::error::pnpm install --lockfile-only failed/);
141
- assert.doesNotMatch(failing.stdout, /::group::/);
142
- });
143
-
144
- test("rendered pnpm ci workflows pass actionlint with the patch step", (t) => {
145
- const probe = spawnSync("actionlint", ["-version"], { encoding: "utf8" });
146
- if (probe.error) return t.skip("actionlint not installed");
147
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "right-git-pnpm-patch-actionlint-"));
148
- t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
149
- fs.mkdirSync(path.join(dir, ".github", "workflows"), { recursive: true });
150
- fs.writeFileSync(path.join(dir, ".github", "actionlint.yaml"), "self-hosted-runner:\n labels:\n - xcode-*\n");
151
- const files = [
152
- ["node-pnpm.yml", render("node", manifestFor("node", "pnpm@11.24.0"))],
153
- ["rust-pnpm.yml", render("rust", manifestFor("rust", "pnpm@11.24.0"))],
154
- ].map(([name, text]) => {
155
- const file = path.join(dir, ".github", "workflows", name);
156
- fs.writeFileSync(file, text);
157
- return file;
158
- });
159
- const result = spawnSync("actionlint", ["-shellcheck=", "-pyflakes=", "-config-file", path.join(dir, ".github", "actionlint.yaml"), ...files], { encoding: "utf8" });
160
- assert.equal(result.status, 0, result.stdout + result.stderr);
161
- });