From d458bb3ef67513302714632afb560b1df18817a0 Mon Sep 17 00:00:00 2001 From: Marcel Robitaille Date: Sat, 9 Nov 2024 13:04:30 +0100 Subject: [PATCH] Support warnOnStderr --- CHANGELOG.md | 4 ++++ README.md | 1 + package-lock.json | 4 ++-- package.json | 2 +- src/lib/CommandHandler.test.ts | 2 ++ src/lib/CommandHandler.ts | 37 +++++++++++++++++++++++++--------- src/lib/ShellCommandOptions.ts | 1 + src/mocks/vscode.ts | 4 ++++ 8 files changed, 43 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9516176..942c414 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index 0759395..609ead7 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 9a708f5..a3ac9a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "tasks-shell-input", - "version": "1.13.0", + "version": "1.13.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "tasks-shell-input", - "version": "1.13.0", + "version": "1.13.1", "devDependencies": { "@types/glob": "8.1.0", "@types/node": "20.11.30", diff --git a/package.json b/package.json index 1cc9f78..1a7b16c 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/lib/CommandHandler.test.ts b/src/lib/CommandHandler.test.ts index b20f564..f35840d 100644 --- a/src/lib/CommandHandler.test.ts +++ b/src/lib/CommandHandler.test.ts @@ -25,6 +25,7 @@ const execFileSpy = vi.spyOn(child_process, 'execFile'); beforeEach(() => { execSpy.mockClear(); execFileSpy.mockClear(); + mockVscode.window.resetShowWarningMessageCalls(); }); describe("Simple cases", async () => { @@ -340,6 +341,7 @@ describe("Argument parsing", () => { useFirstResult: false, useSingleResult: false, multiselect: false, + warnOnStderr: true, multiselectSeparator: " ", stdio: "stdout", extraTestThing: 42, diff --git a/src/lib/CommandHandler.ts b/src/lib/CommandHandler.ts index 97b5702..6597959 100755 --- a/src/lib/CommandHandler.ts +++ b/src/lib/CommandHandler.ts @@ -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", @@ -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((value: string) => { const values = value.trim().split(this.args.fieldSeparator as string, 4); return { @@ -347,7 +364,9 @@ export class CommandHandler { picker.show(); }).finally(() => { - disposable.dispose(); + if (disposable) { + disposable.dispose(); + } }); } diff --git a/src/lib/ShellCommandOptions.ts b/src/lib/ShellCommandOptions.ts index 741b197..ae08d5b 100755 --- a/src/lib/ShellCommandOptions.ts +++ b/src/lib/ShellCommandOptions.ts @@ -9,6 +9,7 @@ export interface ShellCommandOptions rememberPrevious?: boolean; allowCustomValues?: boolean; multiselect?: boolean; + warnOnStderr?: boolean; multiselectSeparator?: string; fieldSeparator?: string; description?: string; diff --git a/src/mocks/vscode.ts b/src/mocks/vscode.ts index 30c21c0..62bc18c 100644 --- a/src/mocks/vscode.ts +++ b/src/mocks/vscode.ts @@ -92,6 +92,10 @@ export namespace window { export function getShowWarningMessageCalls() { return showWarningMessageCalls; } + + export function resetShowWarningMessageCalls() { + showWarningMessageCalls.length = 0; + } } export namespace workspace {