Skip to content

Commit

Permalink
[Perf Framework] Throw on invalid argument (#18708)
Browse files Browse the repository at this point in the history
  • Loading branch information
timovv authored Nov 18, 2021
1 parent 8d6b53d commit 708dee6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions sdk/test-utils/perf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions sdk/test-utils/perf/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,34 @@ export function parsePerfOption<TOptions>(
return result as Required<PerfOptionDictionary<TOptions>>;
}

/**
* 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<TOptions>(options: PerfOptionDictionary<TOptions>): void {
const minimistResult: MinimistParsedArgs = minimist(
process.argv,
getBooleanOptionDetails(options)
);

const longNames = Object.entries<OptionDetails<unknown>>(options).map(
([optionName, { longName }]) => longName ?? optionName
);
const shortNames = Object.values<OptionDetails<unknown>>(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<TOptions>(options: PerfOptionDictionary<TOptions>) {
let booleanProps: { boolean: string[]; default: { [key: string]: boolean } } = {
boolean: [],
Expand Down
9 changes: 8 additions & 1 deletion sdk/test-utils/perf/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
PerfOptionDictionary,
parsePerfOption,
DefaultPerfOptions,
defaultPerfOptions
defaultPerfOptions,
validateOptions
} from "./options";
import {
TestProxyHttpClient,
Expand Down Expand Up @@ -54,6 +55,12 @@ export abstract class PerfTest<TOptions = {}> {
}

public get parsedOptions(): PerfOptionDictionary<TOptions & DefaultPerfOptions> {
// 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<TOptions & DefaultPerfOptions>
// is different from
Expand Down

0 comments on commit 708dee6

Please sign in to comment.