-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility to read output from stderr
Fix #86
- Loading branch information
1 parent
f1cb44a
commit 26272f9
Showing
8 changed files
with
173 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,12 @@ const mockExtensionContext = { | |
vi.mock("child_process", async (importOriginal) => ({ | ||
...await importOriginal<typeof import("child_process")>(), | ||
})); | ||
const execSyncSpy = vi.spyOn(child_process, 'execSync'); | ||
const execFileSyncSpy = vi.spyOn(child_process, 'execFileSync'); | ||
const execSpy = vi.spyOn(child_process, 'exec'); | ||
const execFileSpy = vi.spyOn(child_process, 'execFile'); | ||
|
||
beforeEach(() => { | ||
execSyncSpy.mockClear(); | ||
execFileSyncSpy.mockClear(); | ||
execSpy.mockClear(); | ||
execFileSpy.mockClear(); | ||
}); | ||
|
||
describe("Simple cases", async () => { | ||
|
@@ -46,9 +46,9 @@ describe("Simple cases", async () => { | |
|
||
await handler.handle(); | ||
|
||
expect(execFileSyncSpy).toHaveBeenCalledTimes(0); | ||
expect(execSyncSpy).toHaveBeenCalledTimes(1); | ||
expect(execSyncSpy).toHaveBeenCalledWith( | ||
expect(execFileSpy).toHaveBeenCalledTimes(0); | ||
expect(execSpy).toHaveBeenCalledTimes(1); | ||
expect(execSpy).toHaveBeenCalledWith( | ||
`cat ${filePath}`, | ||
{ | ||
cwd: testDataPath, | ||
|
@@ -60,6 +60,7 @@ describe("Simple cases", async () => { | |
}, | ||
maxBuffer: undefined, | ||
}, | ||
expect.anything(), | ||
); | ||
}); | ||
|
||
|
@@ -75,16 +76,17 @@ describe("Simple cases", async () => { | |
|
||
await handler.handle(); | ||
|
||
expect(execFileSyncSpy).toHaveBeenCalledTimes(0); | ||
expect(execSyncSpy).toHaveBeenCalledTimes(1); | ||
expect(execSyncSpy).toHaveBeenCalledWith( | ||
expect(execFileSpy).toHaveBeenCalledTimes(0); | ||
expect(execSpy).toHaveBeenCalledTimes(1); | ||
expect(execSpy).toHaveBeenCalledWith( | ||
`cat ${filePath}`, | ||
{ | ||
cwd: testDataPath, | ||
encoding: "utf8", | ||
env: undefined, | ||
maxBuffer: undefined, | ||
}, | ||
expect.anything(), | ||
); | ||
}); | ||
}); | ||
|
@@ -125,16 +127,17 @@ describe("Multiple workspaces", async () => { | |
|
||
await handler.handle(); | ||
|
||
expect(execFileSyncSpy).toHaveBeenCalledTimes(0); | ||
expect(execSyncSpy).toHaveBeenCalledTimes(1); | ||
expect(execSyncSpy).toHaveBeenCalledWith( | ||
expect(execFileSpy).toHaveBeenCalledTimes(0); | ||
expect(execSpy).toHaveBeenCalledTimes(1); | ||
expect(execSpy).toHaveBeenCalledWith( | ||
`echo ${expectedResult}`, | ||
{ | ||
cwd: taskId.startsWith("a") ? `${testDataPath}/a` : `${testDataPath}/b`, | ||
encoding: "utf8", | ||
env: undefined, | ||
maxBuffer: undefined, | ||
}, | ||
expect.anything(), | ||
); | ||
}); | ||
} | ||
|
@@ -158,16 +161,17 @@ test("Command variable interop", async () => { | |
|
||
await handler.handle(); | ||
|
||
expect(execFileSyncSpy).toHaveBeenCalledTimes(0); | ||
expect(execSyncSpy).toHaveBeenCalledTimes(1); | ||
expect(execSyncSpy).toHaveBeenCalledWith( | ||
expect(execFileSpy).toHaveBeenCalledTimes(0); | ||
expect(execSpy).toHaveBeenCalledTimes(1); | ||
expect(execSpy).toHaveBeenCalledWith( | ||
"echo 'ItWorked'", | ||
{ | ||
cwd: testDataPath, | ||
encoding: "utf8", | ||
env: undefined, | ||
maxBuffer: undefined, | ||
}, | ||
expect.anything(), | ||
); | ||
}); | ||
|
||
|
@@ -189,9 +193,9 @@ test("commandArgs", async () => { | |
|
||
await handler.handle(); | ||
|
||
expect(execSyncSpy).toHaveBeenCalledTimes(0); | ||
expect(execFileSyncSpy).toHaveBeenCalledTimes(1); | ||
expect(execFileSyncSpy).toHaveBeenCalledWith( | ||
expect(execSpy).toHaveBeenCalledTimes(0); | ||
expect(execFileSpy).toHaveBeenCalledTimes(1); | ||
expect(execFileSpy).toHaveBeenCalledWith( | ||
`${testDataPath}/command with spaces.sh`, | ||
[filePath], | ||
{ | ||
|
@@ -200,9 +204,50 @@ test("commandArgs", async () => { | |
env: undefined, | ||
maxBuffer: undefined, | ||
}, | ||
expect.anything(), | ||
); | ||
}); | ||
|
||
test("stdio", async () => { | ||
const testDataPath = path.join(__dirname, "../test/testData/stdio"); | ||
|
||
const tasksJson = await import(path.join(testDataPath, ".vscode/tasks.json")); | ||
const mockData = (await import(path.join(testDataPath, "mockData.ts"))).default; | ||
|
||
mockVscode.setMockData(mockData); | ||
const input = tasksJson.inputs[0].args; | ||
const expectationStdout = expect.objectContaining({ value: "this is on stdout" }) | ||
const expectationStderr = expect.objectContaining({ value: "this is on stderr" }) | ||
|
||
for (const { setting, expectation } of [ | ||
{ setting: "stdout", expectation: [ expectationStdout ] }, | ||
{ setting: "stderr", expectation: [ expectationStderr ] }, | ||
{ setting: "both", expectation: [ expectationStdout, expectationStderr ] }, | ||
]) { | ||
execSpy.mockClear() | ||
execFileSpy.mockClear() | ||
|
||
const handler = new CommandHandler( | ||
{ ...input, stdio: setting }, | ||
new UserInputContext(), | ||
mockExtensionContext as unknown as vscode.ExtensionContext, | ||
child_process, | ||
); | ||
|
||
// @ts-ignore | ||
Check failure on line 237 in src/lib/CommandHandler.test.ts
|
||
handler.quickPick = vi.fn() | ||
|
||
await handler.handle(); | ||
|
||
expect(execSpy).toHaveBeenCalledTimes(1); | ||
expect(execFileSpy).toHaveBeenCalledTimes(0); | ||
// @ts-ignore | ||
Check failure on line 244 in src/lib/CommandHandler.test.ts
|
||
expect(handler.quickPick).toHaveBeenCalledTimes(1) | ||
// @ts-ignore | ||
Check failure on line 246 in src/lib/CommandHandler.test.ts
|
||
expect(handler.quickPick).toHaveBeenCalledWith(expectation) | ||
} | ||
}) | ||
|
||
describe("Errors", async () => { | ||
test("It should trigger an error for an empty result", async () => { | ||
const testDataPath = path.join(__dirname, "../test/testData/errors"); | ||
|
@@ -226,11 +271,12 @@ describe("Errors", async () => { | |
|
||
describe("Argument parsing", () => { | ||
test("Test defaults and that all boolean properties use parseBoolean", () => { | ||
expect(CommandHandler.resolveBooleanArgs({ extraTestThing: 42 })) | ||
expect(CommandHandler.resolveArgs({ extraTestThing: 42 })) | ||
.toStrictEqual({ | ||
rememberPrevious: false, | ||
useFirstResult: false, | ||
useSingleResult: false, | ||
stdio: "stdout", | ||
extraTestThing: 42, | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "Echo Project File", | ||
"type": "shell", | ||
"command": "echo ${input:inputTest}", | ||
"problemMatcher": [] | ||
} | ||
], | ||
"inputs": [ | ||
{ | ||
"id": "inputTest", | ||
"type": "command", | ||
"command": "shellCommand.execute", | ||
"args": { | ||
"command": "echo this is on stdout && echo this is on stderr >&2", | ||
"stdio": "stderr" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export default { | ||
"calls": { | ||
"workspace.getConfiguration().inspect()": { | ||
"[\"tasks\",null,\"inputs\"]": { | ||
"key": "tasks.inputs", | ||
"workspaceValue": [ | ||
{ | ||
"id": "inputTest", | ||
"type": "command", | ||
"command": "shellCommand.execute", | ||
"args": { | ||
"command": "echo this is on stdout && echo this is on stderr >&2", | ||
"cwd": "${workspaceFolder}", | ||
"env": { | ||
"WORKSPACE": "${workspaceFolder[0]}", | ||
"FILE": "${file}", | ||
"PROJECT": "${workspaceFolderBasename}" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"staticData": { | ||
"window.activeTextEditor.document.fileName": `${__dirname}/.vscode/tasks.json`, | ||
"workspace.workspaceFolders": [ | ||
{ | ||
"uri": { | ||
"$mid": 1, | ||
"fsPath": `${__dirname}`, | ||
"external": `file://${__dirname}}`, | ||
"path": `${__dirname}`, | ||
"scheme": "file" | ||
}, | ||
"name": "vscode-shell-command", | ||
"index": 0 | ||
} | ||
] | ||
} | ||
}; |