Skip to content

Commit

Permalink
Add isValidUrl() check to run, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
justinribeiro committed Aug 30, 2018
1 parent 24a855d commit d1f2ba8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lighthouse-cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ function handleError(err) {
}
}

/**
* @param {string} url
*/
function isValidUrl(url) {
const urlRegex = /^(https?|chrome):\/\/[^\s$.?#].[^\s]*$/gi;
return urlRegex.test(url);
}

/**
* @param {LH.RunnerResult} runnerResult
* @param {LH.CliFlags} flags
Expand Down Expand Up @@ -143,6 +151,15 @@ async function saveResults(runnerResult, flags) {
* @return {Promise<LH.RunnerResult|void>}
*/
function runLighthouse(url, flags, config) {
if (url && !isValidUrl(url)) {
return Promise.resolve()
.then(_ => handleError({
name: 'Invalid URL',
code: 'INVALID_URL',
message: 'The URL you have provided appears to be invalid.',
}));
}

/** @type {ChromeLauncher.LaunchedChrome|undefined} */
let launchedChrome;
const shouldGather = flags.gatherMode || flags.gatherMode === flags.auditMode;
Expand Down
12 changes: 11 additions & 1 deletion lighthouse-cli/test/cli/run-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,20 @@ describe('CLI run', function() {
Object.keys(results.audits).length,
Object.keys(lhr.audits).length);
assert.deepStrictEqual(results.timing, lhr.timing);

fs.unlinkSync(filename);
});
}, 20 * 1000);

it('runLighthouse fails on file:// URL', () => {
const exit = jest.spyOn(process, 'exit').mockImplementation(number => number);
const url = 'file:///fake/file/path/index.html';
const filename = path.join(process.cwd(), 'run.ts.results.json');
const timeoutFlag = `--max-wait-for-load=${9000}`;
const flags = getFlags(`--output=json --output-path=${filename} ${timeoutFlag} ${url}`);
return run.runLighthouse(url, flags, fastConfig).then(_ => {
expect(exit).toHaveBeenCalledWith(1);
});
});
});

describe('flag coercing', () => {
Expand Down

0 comments on commit d1f2ba8

Please sign in to comment.