Skip to content

Commit

Permalink
Support warnOnStderr
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelRobitaille committed Nov 9, 2024
1 parent 1b8a771 commit d458bb3
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [1.13.1] 2024-11-09

- Add support for warning if the command outputs on stderr (warnOnStderr)

## [1.13.0] 2024-11-09

- Fix default value not recorded for useFirstResult / useSingleResult (#117)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Arguments for the extension:
* `allowCustomValues`: If true, it's possible to enter a new value that is not part of the command output. Has no effect with `useFirstResult`.
* `multiselect`: If true, it's possible to select multiple values. They are joined by `multiselectSeparator`. Has no effect with `useFirstResult`.
* `multiselectSeparator`: The string with which to join multiple options when `multiselect` is true (default `" "`). Has no effect without `multiselect`.
* `warnOnStderr`: If true, a warning message is shown if the command outputs anything on stderr (default: true). Has no effect if `stdio` is not `stdout`.
* `taskId`: Unique id to use for storing the last-used value.
* `fieldSeparator`: the string that separates `value`, `label`, `description` and `detail` fields
* `description`: shown as a placeholder in 'Quick Pick', provides context for the input
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Tasks Shell Input",
"description": "Use shell commands as input for your tasks",
"icon": "icon.png",
"version": "1.13.0",
"version": "1.13.1",
"publisher": "augustocdias",
"repository": {
"url": "https://github.com/augustocdias/vscode-shell-command"
Expand Down
2 changes: 2 additions & 0 deletions src/lib/CommandHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const execFileSpy = vi.spyOn(child_process, 'execFile');
beforeEach(() => {
execSpy.mockClear();
execFileSpy.mockClear();
mockVscode.window.resetShowWarningMessageCalls();
});

describe("Simple cases", async () => {
Expand Down Expand Up @@ -340,6 +341,7 @@ describe("Argument parsing", () => {
useFirstResult: false,
useSingleResult: false,
multiselect: false,
warnOnStderr: true,
multiselectSeparator: " ",
stdio: "stdout",
extraTestThing: 42,
Expand Down
37 changes: 28 additions & 9 deletions src/lib/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class CommandHandler {
useSingleResult: CommandHandler.parseBoolean(args.useSingleResult, false),
rememberPrevious: CommandHandler.parseBoolean(args.rememberPrevious, false),
allowCustomValues: CommandHandler.parseBoolean(args.allowCustomValues, false),
warnOnStderr: CommandHandler.parseBoolean(args.warnOnStderr, true),
multiselect: CommandHandler.parseBoolean(args.multiselect, false),
multiselectSeparator: args.multiselectSeparator ?? " ",
stdio: ["stdout", "stderr", "both"].includes(args.stdio as string) ? args.stdio : "stdout",
Expand Down Expand Up @@ -200,23 +201,39 @@ export class CommandHandler {
}
}

protected parseResult({ stdout, stderr }: { stdout: string, stderr: string }): QuickPickItem[] {
let result = "";
protected parseResult(commandOutput: { stdout: string, stderr: string }): QuickPickItem[] {
const stdout = commandOutput.stdout.trim();
const stderr = commandOutput.stderr.trim();
let items: string[] = [];

if (("stdout" == this.args.stdio) || ("both" == this.args.stdio)) {
result += stdout;
items.push(...stdout.split(this.EOL));
}

if (("stderr" == this.args.stdio) || ("both" == this.args.stdio)) {
result += stderr;
items.push(...stderr.split(this.EOL));
}

if ((result.trim().length == 0) && (undefined === this.args.defaultOptions)) {
throw new ShellCommandException(`The command for input '${this.input.id}' returned empty result.`);
items = items.filter(item => item !== "");

if ((items.length == 0) && (undefined === this.args.defaultOptions)) {
let msg = `The command for input '${this.input.id}' returned empty result.`;

if (stderr) {
msg += ` stderr: '${stderr}'`;
}

throw new ShellCommandException(msg);
}

return result
.split(this.EOL)
if ((this.args.warnOnStderr) && ("stdout" == this.args.stdio) && stderr) {
vscode.window.showWarningMessage(
`The command for input '${this.input.id}' might have errors.
stderr: '${stderr}'.
Hint: You can disable this with '"warnOnStderr": false'.`);
}

return items
.map<QuickPickItem>((value: string) => {
const values = value.trim().split(this.args.fieldSeparator as string, 4);
return {
Expand Down Expand Up @@ -347,7 +364,9 @@ export class CommandHandler {

picker.show();
}).finally(() => {
disposable.dispose();
if (disposable) {
disposable.dispose();
}
});
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/ShellCommandOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ShellCommandOptions
rememberPrevious?: boolean;
allowCustomValues?: boolean;
multiselect?: boolean;
warnOnStderr?: boolean;
multiselectSeparator?: string;
fieldSeparator?: string;
description?: string;
Expand Down
4 changes: 4 additions & 0 deletions src/mocks/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export namespace window {
export function getShowWarningMessageCalls() {
return showWarningMessageCalls;
}

export function resetShowWarningMessageCalls() {
showWarningMessageCalls.length = 0;
}
}

export namespace workspace {
Expand Down

0 comments on commit d458bb3

Please sign in to comment.