diff --git a/sdk/test-utils/perf/CHANGELOG.md b/sdk/test-utils/perf/CHANGELOG.md index afeb283f93f5..88fedc4f879d 100644 --- a/sdk/test-utils/perf/CHANGELOG.md +++ b/sdk/test-utils/perf/CHANGELOG.md @@ -2,6 +2,11 @@ ## 1.0.0 (Unreleased) +### 2021-11-15 + +- Throw an error when an unrecognized command-line argument is encountered + [#18708](https://github.com/Azure/azure-sdk-for-js/pull/18708) + ### 2021-10-29 - Add an elapsed time column to the snapshot log output. diff --git a/sdk/test-utils/perf/src/options.ts b/sdk/test-utils/perf/src/options.ts index a61db08c9b84..6b99e3cd0cae 100644 --- a/sdk/test-utils/perf/src/options.ts +++ b/sdk/test-utils/perf/src/options.ts @@ -167,6 +167,34 @@ export function parsePerfOption( return result as Required>; } +/** + * Validate that the provided command-line options are all recognized, throwing an error if an unrecognized + * option is provided. + * + * @param options A dictionary of options which should be passed. + */ +export function validateOptions(options: PerfOptionDictionary): void { + const minimistResult: MinimistParsedArgs = minimist( + process.argv, + getBooleanOptionDetails(options) + ); + + const longNames = Object.entries>(options).map( + ([optionName, { longName }]) => longName ?? optionName + ); + const shortNames = Object.values>(options) + .map(({ shortName }) => shortName) + .filter(Boolean); + + // include _ and -- as these may be present in the MinimistParsedArgs object + const acceptedOptions = ["_", "--", ...longNames, ...shortNames]; + const unknownOptions = Object.keys(minimistResult).filter((x) => !acceptedOptions.includes(x)); + + if (unknownOptions.length !== 0) { + throw new Error(`Encountered invalid options: ${unknownOptions.join(", ")}`); + } +} + function getBooleanOptionDetails(options: PerfOptionDictionary) { let booleanProps: { boolean: string[]; default: { [key: string]: boolean } } = { boolean: [], diff --git a/sdk/test-utils/perf/src/tests.ts b/sdk/test-utils/perf/src/tests.ts index eb2d12c57461..8c2f118911a9 100644 --- a/sdk/test-utils/perf/src/tests.ts +++ b/sdk/test-utils/perf/src/tests.ts @@ -7,7 +7,8 @@ import { PerfOptionDictionary, parsePerfOption, DefaultPerfOptions, - defaultPerfOptions + defaultPerfOptions, + validateOptions } from "./options"; import { TestProxyHttpClient, @@ -54,6 +55,12 @@ export abstract class PerfTest { } public get parsedOptions(): PerfOptionDictionary { + // Only validate the options if they are defined: if (when) parsedOptions is called + // in the constructor, options will be undefined. + if (this.options) { + validateOptions({ ...this.options, ...defaultPerfOptions }); + } + // This cast is needed because TS thinks // PerfOptionDictionary // is different from