Repository navigation
Expand file tree
/
Copy pathtemplate.test.ts
More file actions
211 lines (198 loc) · 9 KB
/
Copy pathtemplate.test.ts
File metadata and controls
211 lines (198 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { spawnSync } from "node:child_process";
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vite-plus/test";
import { parse as parseYaml } from "yaml";
const templatePath = fileURLToPath(new URL("../../azure/setup-vp.yml", import.meta.url));
const template = readFileSync(templatePath, "utf8");
const docs = parseYaml(template);
const { version } = JSON.parse(
readFileSync(new URL("../../package.json", import.meta.url), "utf8"),
) as { version: string };
type Shell = "bash" | "powershell";
const prepareSteps = docs.steps.filter((step: { displayName?: string }) =>
step.displayName?.startsWith("setup-vp prepare"),
) as Array<{ bash?: string; powershell?: string; env: Record<string, string> }>;
function prepareStep(shell: Shell) {
return prepareSteps.find((step) => step[shell])!;
}
function runPrepare(shell: Shell, setupRef: string) {
const directory = mkdtempSync(join(tmpdir(), "setup-vp-azure-template-"));
const download = join(directory, "download");
const bootstrap = join(directory, "bootstrap");
const marker = join(directory, "injected");
// Render the real template source as Azure would, so reintroducing inline
// parameter interpolation makes the injection payloads executable again.
const script = prepareStep(shell)
[shell]!.replaceAll("${{ parameters.setupRef }}", () => setupRef)
.replaceAll("$(Agent.TempDirectory)", () => directory);
const prelude =
shell === "bash"
? `
curl() {
printf '%s' "$6" > "$SETUP_VP_TEST_DOWNLOAD"
printf '%s\\n' 'printf "%s" "$SETUP_VP_SETUP_REF" > "$SETUP_VP_TEST_BOOTSTRAP"' > "$8"
}
`
: `
function Invoke-WebRequest {
param([string]$Uri, [string]$OutFile, [int]$TimeoutSec)
[IO.File]::WriteAllText($env:SETUP_VP_TEST_DOWNLOAD, $Uri)
Set-Content -LiteralPath $OutFile -Value '[IO.File]::WriteAllText($env:SETUP_VP_TEST_BOOTSTRAP, $env:SETUP_VP_SETUP_REF)'
}
`;
const scriptPath = join(directory, shell === "bash" ? "prepare.sh" : "prepare.ps1");
writeFileSync(scriptPath, prelude + script);
try {
const result = spawnSync(
shell === "bash" ? "bash" : "powershell.exe",
shell === "bash"
? [scriptPath]
: ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", scriptPath],
{
encoding: "utf8",
timeout: 10_000,
env: {
...process.env,
AGENT_TEMPDIRECTORY: directory,
SETUP_VP_SETUP_REF: setupRef,
SETUP_VP_TEST_DOWNLOAD: download,
SETUP_VP_TEST_BOOTSTRAP: bootstrap,
SETUP_VP_TEST_MARKER: marker,
},
},
);
expect(result.error).toBeUndefined();
return {
...result,
download: existsSync(download) ? readFileSync(download, "utf8") : undefined,
bootstrap: existsSync(bootstrap) ? readFileSync(bootstrap, "utf8") : undefined,
injected: existsSync(marker),
};
} finally {
rmSync(directory, { recursive: true, force: true });
}
}
describe("azure/setup-vp.yml", () => {
it("declares the documented parameters with defaults", () => {
const parameters = docs.parameters as Array<{
name: string;
type: string;
default: unknown;
}>;
const byName = Object.fromEntries(parameters.map((entry) => [entry.name, entry]));
expect(byName.version).toMatchObject({ type: "string", default: "" });
expect(byName.versionFile).toMatchObject({ type: "string", default: "" });
expect(byName.nodeVersionFile).toMatchObject({ type: "string", default: "" });
expect(byName.bootstrapNodeVersion).toMatchObject({ type: "string", default: "24.x" });
expect(byName.stepName).toMatchObject({ type: "string", default: "setupVp" });
expect(byName.workingDirectory).toMatchObject({ type: "string", default: "." });
expect(byName.runInstall).toMatchObject({ type: "object", default: true });
expect(byName.sfw).toMatchObject({ type: "boolean", default: false });
expect(byName.registryUrl).toMatchObject({ type: "string", default: "" });
expect(byName.authEnv).toMatchObject({ type: "object", default: {} });
expect(byName.scope).toMatchObject({ type: "string", default: "" });
expect(byName.setupRef).toMatchObject({ type: "string", default: `v${version}` });
expect(byName.nodeVersion).toMatchObject({ type: "string", default: "" });
expect(byName.packageManager).toMatchObject({ type: "object", default: "" });
expect(byName.nodeManager).toMatchObject({ type: "string", default: "" });
expect(byName.cache).toMatchObject({ type: "boolean", default: false });
expect(byName.cacheDependencyPath).toMatchObject({ type: "string", default: "" });
});
it("orders UseNode, prepare, Cache@2, and finalize", () => {
const steps = docs.steps as Array<Record<string, unknown>>;
const flattened = JSON.stringify(steps);
expect(flattened).toContain("UseNode@1");
expect(flattened).toContain("setup-vp prepare");
expect(flattened).toContain("Cache@2");
expect(flattened).toContain("setup-vp finalize");
expect(flattened.indexOf("setup-vp prepare")).toBeLessThan(flattened.indexOf("Cache@2"));
expect(flattened.indexOf("Cache@2")).toBeLessThan(flattened.indexOf("setup-vp finalize"));
});
it("serializes runInstall with convertToJson and avoids main downloads", () => {
expect(template).toContain("${{ convertToJson(parameters.runInstall) }}");
expect(
template.split("SETUP_VP_PACKAGE_MANAGER: ${{ convertToJson(parameters.packageManager) }}"),
).toHaveLength(3);
expect(template).not.toMatch(/setup-vp\/main\//);
expect(template).toContain("$(SETUP_VP_LOCK_FILE)");
expect(template).toContain("cacheHitVar: SETUP_VP_CACHE_HIT");
expect(template.match(/NODE_AUTH_TOKEN: \$\(NODE_AUTH_TOKEN\)/g) ?? []).toHaveLength(2);
});
it("includes Unix and Windows branches", () => {
expect(template).toContain("Windows_NT");
expect(template).toContain("bootstrap.sh");
expect(template).toContain("bootstrap.ps1");
expect(template).toContain("name: ${{ parameters.stepName }}Windows");
expect(template).toContain("name: ${{ parameters.stepName }}Unix");
expect(template.match(/\$\(SETUP_VP_BOOTSTRAP_NODE\)/g)).toHaveLength(2);
expect(template.split("${{ insert }}: ${{ parameters.authEnv }}")).toHaveLength(3);
});
it("selects the agent shell at runtime", () => {
expect(template).not.toContain("${{ if eq(variables['Agent.OS'], 'Windows_NT') }}");
expect(template).not.toContain("${{ if ne(variables['Agent.OS'], 'Windows_NT') }}");
expect(template).toContain(
"condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))",
);
expect(template).toContain(
"condition: and(succeeded(), ne(variables['Agent.OS'], 'Windows_NT'))",
);
});
it.each(["bash", "powershell"] as const)(
"passes setupRef only through %s's environment",
(shell) => {
const step = prepareStep(shell);
expect(step.env.SETUP_VP_SETUP_REF).toBe("${{ parameters.setupRef }}");
expect(step[shell]).not.toContain("${{");
},
);
for (const shell of ["bash", "powershell"] as const) {
describe.skipIf((shell === "powershell") !== (process.platform === "win32"))(
`${shell} prepare`,
() => {
it.each([`v${version}`, "a".repeat(40), "refs/tags/v1.21.1", "feature/test_ref-1"])(
"downloads and runs the bootstrap for %s",
(setupRef) => {
const result = runPrepare(shell, setupRef);
expect(result.status, result.stderr).toBe(0);
const extension = shell === "bash" ? "sh" : "ps1";
expect(result.download).toBe(
`https://raw.githubusercontent.com/voidzero-dev/setup-vp/${setupRef}/azure/bootstrap.${extension}`,
);
expect(result.bootstrap).toBe(setupRef);
expect(result.injected).toBe(false);
},
);
it.each([
"",
"../other-repo/main",
"refs/../main",
"refs/./main",
"refs//main",
"v1%2f..",
"v1?query",
"v1#fragment",
"v1 with spaces",
"v1\n",
"v1\r\n",
shell === "bash"
? 'v1"; printf injected > "$SETUP_VP_TEST_MARKER"; exit 0; #'
: 'v1"; Set-Content -LiteralPath $env:SETUP_VP_TEST_MARKER -Value injected; exit 0; #',
shell === "bash"
? '$(printf injected > "$SETUP_VP_TEST_MARKER")'
: "$(Set-Content -LiteralPath $env:SETUP_VP_TEST_MARKER -Value injected)",
'`printf injected > "$SETUP_VP_TEST_MARKER"`',
])("rejects unsafe setupRef %j before downloading or executing commands", (setupRef) => {
const result = runPrepare(shell, setupRef);
expect(result.injected).toBe(false);
expect(result.download).toBeUndefined();
expect(result.bootstrap).toBeUndefined();
expect(result.status).not.toBe(0);
expect(result.stderr).toContain("invalid setupRef");
});
},
);
}
});