From e8053bac5fa5b33bc6b0fb5bb4fe9c77b2143a32 Mon Sep 17 00:00:00 2001 From: Chapman Pendery <35637443+cpendery@users.noreply.github.com> Date: Tue, 12 Dec 2023 11:16:01 -0800 Subject: [PATCH] fix: infinite loop when argument is given when spec doesn't have an argument (#113) Signed-off-by: Chapman Pendery --- src/runtime/runtime.ts | 7 +++- .../__snapshots__/runtime.test.ts.snap | 38 +++++++++++++++++++ src/tests/runtime/runtime.test.ts | 6 ++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/runtime/runtime.ts b/src/runtime/runtime.ts index 08a45ea..5770269 100644 --- a/src/runtime/runtime.ts +++ b/src/runtime/runtime.ts @@ -266,5 +266,10 @@ const runSubcommand = async ( return; // not subcommand or option & no args exist } - return runArg(tokens, getArgs(subcommand.args), subcommand, allOptions, acceptedTokens, false, false); + const args = getArgs(subcommand.args); + if (args.length != 0) { + return runArg(tokens, args, subcommand, allOptions, acceptedTokens, false, false); + } + // if the subcommand has no args specified, fallback to the subcommand and ignore this item + return runSubcommand(tokens.slice(1), subcommand, persistentOptions, acceptedTokens.concat(activeToken)); }; diff --git a/src/tests/runtime/__snapshots__/runtime.test.ts.snap b/src/tests/runtime/__snapshots__/runtime.test.ts.snap index 17214a6..2c65993 100644 --- a/src/tests/runtime/__snapshots__/runtime.test.ts.snap +++ b/src/tests/runtime/__snapshots__/runtime.test.ts.snap @@ -432,6 +432,44 @@ exports[`parseCommand loadSpec 1`] = ` } `; +exports[`parseCommand noArgsArgumentGiven 1`] = ` +{ + "charactersToDrop": 0, + "suggestions": [ + { + "allNames": [ + "--analyzer-output", + ], + "description": "Static analyzer report output format", + "icon": "🔗", + "insertValue": undefined, + "name": "--analyzer-output", + "priority": 50, + }, + { + "allNames": [ + "--analyze", + ], + "description": "Run the static analyzer", + "icon": "🔗", + "insertValue": undefined, + "name": "--analyze", + "priority": 50, + }, + { + "allNames": [ + "-arcmt-migrate-emit-errors", + ], + "description": "Emit ARC errors even if the migrator can fix them", + "icon": "🔗", + "insertValue": undefined, + "name": "-arcmt-migrate-emit-errors", + "priority": 50, + }, + ], +} +`; + exports[`parseCommand noOptionsSuggestedAfterVariadicArg 1`] = ` { "argumentDescription": undefined, diff --git a/src/tests/runtime/runtime.test.ts b/src/tests/runtime/runtime.test.ts index 71b8a2c..e6d1a0f 100644 --- a/src/tests/runtime/runtime.test.ts +++ b/src/tests/runtime/runtime.test.ts @@ -18,13 +18,17 @@ const testData = [ { name: "command", command: "sudo git sta" }, { name: "nestedNonCommands", command: "az az ", skip: true }, // TODO: fix skipped test { name: "loadSpec", command: "aws acm add" }, + { name: "noArgsArgumentGiven", command: "gcc lab ", maxSuggestions: 3 }, ]; describe(`parseCommand`, () => { - testData.forEach(({ command, name, skip }) => { + testData.forEach(({ command, name, skip, maxSuggestions }) => { if (skip) return; test(name, async () => { const suggestions = await getSuggestions(command); + if (suggestions != null && suggestions.suggestions != null) { + suggestions.suggestions = suggestions?.suggestions.slice(0, maxSuggestions); + } expect(suggestions).toMatchSnapshot(); }); });