Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: error early if --output-path is invalid #14367

Merged
merged 4 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/* eslint-disable max-len */

import fs from 'fs';
import path from 'path';

import yargs from 'yargs';
import * as yargsHelpers from 'yargs/helpers';
Expand Down Expand Up @@ -227,6 +228,7 @@ function getYargsParser(manualArgv) {
},
'output-path': {
type: 'string',
coerce: coerceOutputPath,
describe: `The file path to output the results. Use 'stdout' to write to stdout.
If using JSON output, default is stdout.
If using HTML or CSV output, default is a file in the working directory with a name based on the test URL and date.
Expand Down Expand Up @@ -408,6 +410,21 @@ function coerceOutput(values) {
return validValues;
}

/**
* Verifies outputPath is something we can actually write to.
* @param {unknown=} value
* @return {string=}
*/
function coerceOutputPath(value) {
if (value === undefined) return;

if (typeof value !== 'string' || !value || !fs.existsSync(path.dirname(value))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what situation will value === undefined.

If it's the case where --output-path is provided without any value, could we do a different error message for that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what situation will value === undefined.

when not provided. coerce is still called when param is not specified (or at least, it is for the "flags in a json path" case).

If it's the case where --output-path is provided without any value, could we do a different error message for that?

I think the current error is sufficient.

throw new Error(`--output-path (${value}) cannot be written to`);
}

return value;
}

/**
* Verifies value is a string, then coerces type to LH.Locale for convenience. However, don't
* allowlist specific locales. Why? So we can support the user who requests 'es-MX' (unsupported)
Expand Down
9 changes: 9 additions & 0 deletions cli/test/cli/cli-flags-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,14 @@ describe('CLI flags', function() {
.toThrow(`Invalid value: 'screenEmulation.disabled' must be a boolean`);
});
});

describe('outputPath', () => {
it('throws when path cannot be written to', () => {
expect(() => getFlags(`${url} --output-path=i/do/not/exist.json`, {noExitOnFailure: true}))
.toThrow('--output-path (i/do/not/exist.json) cannot be written to');
expect(() => getFlags(`${url} --output-path=ok.json`, {noExitOnFailure: true}))
.not.toThrow();
});
});
});
});