|
| 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 | +}); |
0 commit comments