Skip to content

Commit bf9a1c5

Browse files
committed
Added some unit tests for the test utils + fix smol review comments
1 parent 9558aab commit bf9a1c5

File tree

5 files changed

+100
-20
lines changed

5 files changed

+100
-20
lines changed

test/unit/core/cliUtils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("CliUtils", () => {
2929
expect((await cliUtils.stat(binPath))?.size).toBe(4);
3030
});
3131

32-
it.skipIf(isWindows)("version", async () => {
32+
it.skipIf(isWindows())("version", async () => {
3333
const binPath = path.join(tmp, "version");
3434
await expect(cliUtils.version(binPath)).rejects.toThrow("ENOENT");
3535

test/unit/globalFlags.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type WorkspaceConfiguration } from "vscode";
33

44
import { getGlobalFlags } from "@/globalFlags";
55

6-
import { quoteCommand } from "../utils/platform";
6+
import { isWindows } from "../utils/platform";
77

88
describe("Global flags suite", () => {
99
it("should return global-config and header args when no global flags configured", () => {
@@ -80,3 +80,9 @@ describe("Global flags suite", () => {
8080
]);
8181
});
8282
});
83+
84+
function quoteCommand(value: string): string {
85+
// Used to escape environment variables in commands. See `getHeaderArgs` in src/headers.ts
86+
const quote = isWindows() ? '"' : "'";
87+
return `${quote}${value}${quote}`;
88+
}

test/utils/platform.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import {
4+
expectPathsEqual,
5+
exitCommand,
6+
printCommand,
7+
printEnvCommand,
8+
isWindows,
9+
} from "./platform";
10+
11+
describe("platform utils", () => {
12+
describe("printCommand", () => {
13+
it("should generate a simple node command", () => {
14+
const result = printCommand("hello world");
15+
expect(result).toBe("node -e \"process.stdout.write('hello world')\"");
16+
});
17+
18+
it("should escape special characters", () => {
19+
const result = printCommand('path\\to\\file\'s "name"\nline2\rcarriage');
20+
expect(result).toBe(
21+
'node -e "process.stdout.write(\'path\\\\to\\\\file\\\'s \\"name\\"\\nline2\\rcarriage\')"',
22+
);
23+
});
24+
});
25+
26+
describe("exitCommand", () => {
27+
it("should generate node commands with various exit codes", () => {
28+
expect(exitCommand(0)).toBe('node -e "process.exit(0)"');
29+
expect(exitCommand(1)).toBe('node -e "process.exit(1)"');
30+
expect(exitCommand(42)).toBe('node -e "process.exit(42)"');
31+
expect(exitCommand(-1)).toBe('node -e "process.exit(-1)"');
32+
});
33+
});
34+
35+
describe("printEnvCommand", () => {
36+
it("should generate node commands that print env variables", () => {
37+
expect(printEnvCommand("url", "CODER_URL")).toBe(
38+
"node -e \"process.stdout.write('url=' + process.env.CODER_URL)\"",
39+
);
40+
expect(printEnvCommand("token", "CODER_TOKEN")).toBe(
41+
"node -e \"process.stdout.write('token=' + process.env.CODER_TOKEN)\"",
42+
);
43+
// Will fail to execute but that's fine
44+
expect(printEnvCommand("", "")).toBe(
45+
"node -e \"process.stdout.write('=' + process.env.)\"",
46+
);
47+
});
48+
});
49+
50+
describe("expectPathsEqual", () => {
51+
it("should consider identical paths equal", () => {
52+
expectPathsEqual("same/path", "same/path");
53+
});
54+
55+
it("should throw when paths are different", () => {
56+
expect(() =>
57+
expectPathsEqual("path/to/file1", "path/to/file2"),
58+
).toThrow();
59+
});
60+
61+
it("should handle empty paths", () => {
62+
expectPathsEqual("", "");
63+
});
64+
65+
it.runIf(isWindows())(
66+
"should consider paths with different separators equal on Windows",
67+
() => {
68+
expectPathsEqual("path/to/file", "path\\to\\file");
69+
expectPathsEqual("C:/path/to/file", "C:\\path\\to\\file");
70+
expectPathsEqual(
71+
"C:/path with spaces/file",
72+
"C:\\path with spaces\\file",
73+
);
74+
},
75+
);
76+
77+
it.skipIf(isWindows())(
78+
"should consider backslash as literal on non-Windows",
79+
() => {
80+
expect(() =>
81+
expectPathsEqual("path/to/file", "path\\to\\file"),
82+
).toThrow();
83+
},
84+
);
85+
});
86+
});

test/utils/platform.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import os from "node:os";
22
import path from "node:path";
33
import { expect } from "vitest";
44

5-
export const isWindows = os.platform() === "win32";
5+
export function isWindows(): boolean {
6+
return os.platform() === "win32";
7+
}
68

79
/**
810
* Returns a platform-independent command that outputs the given text.
@@ -39,15 +41,6 @@ export function expectPathsEqual(actual: string, expected: string) {
3941
expect(normalizePath(actual)).toBe(normalizePath(expected));
4042
}
4143

42-
export function quoteCommand(value: string): string {
43-
const quote = getPlatformQuote();
44-
return `${quote}${value}${quote}`;
45-
}
46-
4744
function normalizePath(p: string): string {
48-
return p.replaceAll(path.sep, "/");
49-
}
50-
51-
function getPlatformQuote(): string {
52-
return isWindows ? '"' : "'";
45+
return p.replaceAll(path.sep, path.posix.sep);
5346
}

vitest.config.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@ export default defineConfig({
55
test: {
66
globals: true,
77
environment: "node",
8-
include: ["test/unit/**/*.test.ts", "test/integration/**/*.test.ts"],
9-
exclude: [
10-
"test/integration/**",
11-
"**/node_modules/**",
12-
"**/out/**",
13-
"**/*.d.ts",
14-
],
8+
include: ["test/unit/**/*.test.ts", "test/utils/**/*.test.ts"],
9+
exclude: ["**/node_modules/**", "**/out/**", "**/*.d.ts"],
1510
pool: "threads",
1611
fileParallelism: true,
1712
coverage: {

0 commit comments

Comments
 (0)