-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathshell.test.ts
More file actions
64 lines (53 loc) · 2.32 KB
/
Copy pathshell.test.ts
File metadata and controls
64 lines (53 loc) · 2.32 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
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vite-plus/test";
import { commandPath, exportShellEnv, shellQuote } from "./shell.js";
const tempDirs: string[] = [];
function tempDir(): string {
const dir = mkdtempSync(path.join(tmpdir(), "setup-vp-test-"));
tempDirs.push(dir);
return dir;
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
rmSync(dir, { recursive: true, force: true });
}
});
describe("GitLab shell helpers", () => {
it("quotes PowerShell values literally and validates variable names", () => {
const envFile = path.join(tempDir(), "env.ps1");
const env = { SETUP_VP_ENV_FILE: envFile, SETUP_VP_ENV_FORMAT: "powershell" };
exportShellEnv("TOKEN", "it's $literal; `text", env);
expect(readFileSync(envFile, "utf8")).toBe("$env:TOKEN = 'it''s $literal; `text'\n");
expect(() => exportShellEnv("TOKEN; whoami", "value", env)).toThrow(
"Invalid environment variable name",
);
});
it("quotes shell environment values", () => {
expect(shellQuote("plain")).toBe("'plain'");
expect(shellQuote("has spaces")).toBe("'has spaces'");
expect(shellQuote("it's ok")).toBe("'it'\\''s ok'");
});
it("appends shell exports when an env file is configured", () => {
const dir = tempDir();
const envFile = path.join(dir, "env.sh");
writeFileSync(envFile, "", "utf8");
exportShellEnv("SETUP_VP_TEST", "value with ' quote", { SETUP_VP_ENV_FILE: envFile });
expect(readFileSync(envFile, "utf8")).toBe("export SETUP_VP_TEST='value with '\\'' quote'\n");
});
it("does not write shell exports without an env file or value", () => {
exportShellEnv("SETUP_VP_TEST", "value", {});
exportShellEnv("SETUP_VP_TEST", undefined, { SETUP_VP_ENV_FILE: "unused" });
});
it("finds commands on PATH", () => {
expect(commandPath("sh")).toBeTruthy();
expect(commandPath("setup-vp-command-that-should-not-exist")).toBeUndefined();
});
it("does not interpolate command names into shell source", () => {
const dir = tempDir();
const marker = path.join(dir, "injected");
expect(commandPath(`sh"; printf injected > ${marker}; #`)).toBeUndefined();
expect(existsSync(marker)).toBe(false);
});
});