-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathrunTerminalCommand.test.ts
More file actions
146 lines (125 loc) · 4.17 KB
/
runTerminalCommand.test.ts
File metadata and controls
146 lines (125 loc) · 4.17 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
import {
getShellCommand,
isRunningInWsl,
runTerminalCommandTool,
} from "./runTerminalCommand.js";
describe("runTerminalCommandTool", () => {
const isWindows = process.platform === "win32";
const isMac = process.platform === "darwin";
const isLinux = process.platform === "linux";
describe("basic platform-specific terminal execution", () => {
it("should execute a simple echo command", async () => {
let command: string;
let expectedOutput: string;
if (isWindows) {
command = 'Write-Output "hello world"';
expectedOutput = "hello world";
} else {
command = 'echo "hello world"';
expectedOutput = "hello world";
}
const result = await runTerminalCommandTool.run({ command });
expect(result.trim()).toBe(expectedOutput);
});
it("should get current directory", async () => {
let command: string;
if (isWindows) {
command = "Get-Location | Select-Object -ExpandProperty Path";
} else {
command = "pwd";
}
const result = await runTerminalCommandTool.run({ command });
if (isWindows) {
// Windows paths like C:\path\to\dir
expect(result.trim()).toMatch(/^[A-Za-z]:\\.*/);
} else {
// Unix paths like /path/to/dir
expect(result.trim()).toMatch(/^\/.*$/);
}
});
it("should list directory contents", async () => {
let command: string;
if (isWindows) {
command = "Get-ChildItem | Select-Object -ExpandProperty Name";
} else {
command = "ls";
}
const result = await runTerminalCommandTool.run({ command });
// Should return some directory content (not empty)
expect(result.length).toBeGreaterThan(0);
});
it("should handle command that produces version info", async () => {
// Node.js should be available on all platforms in CI
const command = "node --version";
const result = await runTerminalCommandTool.run({ command });
// Should contain version number (starts with v)
expect(result.trim()).toMatch(/^v\d+\.\d+\.\d+/);
});
});
describe("basic error handling", () => {
it("should handle non-existent commands", async () => {
const command = "definitely-not-a-real-command-xyz123";
await expect(runTerminalCommandTool.run({ command })).rejects.toMatch(
/Error \(exit code|Command timed out|not found|not recognized/,
);
});
});
describe("platform-specific features", () => {
if (isWindows) {
it("should work with Windows commands", async () => {
const result = await runTerminalCommandTool.run({
command: "Write-Output $env:OS",
});
expect(result.trim()).toBe("Windows_NT");
});
}
if (isMac) {
it("should work with macOS commands", async () => {
const result = await runTerminalCommandTool.run({
command: "uname -s",
});
expect(result.trim()).toBe("Darwin");
});
}
if (isLinux) {
it("should work with Linux commands", async () => {
const result = await runTerminalCommandTool.run({
command: "uname -s",
});
expect(result.trim()).toBe("Linux");
});
}
});
describe("WSL detection", () => {
it("should cache the WSL detection result", () => {
const firstResult = isRunningInWsl();
const secondResult = isRunningInWsl();
expect(firstResult).toBe(secondResult);
});
if (!isLinux) {
it("should return false on non-Linux platforms", () => {
expect(isRunningInWsl()).toBe(false);
});
}
});
describe("shell selection", () => {
it.runIf(!isWindows)(
"does not pass login-shell flag to csh-family shells",
() => {
const previousShell = process.env.SHELL;
process.env.SHELL = "/bin/tcsh";
try {
const { shell, args } = getShellCommand("pwd");
expect(shell).toBe("/bin/tcsh");
expect(args).toEqual(["-c", "pwd"]);
} finally {
if (previousShell === undefined) {
delete process.env.SHELL;
} else {
process.env.SHELL = previousShell;
}
}
},
);
});
});