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

core(audit-mode): do not require a URL #5495

Merged
merged 5 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion lighthouse-cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ function getFlags(manualArgv) {
.check(/** @param {!LH.Flags} argv */ (argv) => {
// Make sure lighthouse has been passed a url, or at least one of --list-all-audits
// or --list-trace-categories. If not, stop the program and ask for a url
Copy link
Member

Choose a reason for hiding this comment

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

update comment too

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

if (!argv.listAllAudits && !argv.listTraceCategories && argv._.length === 0) {
const isListMode = argv.listAllAudits || argv.listTraceCategories;
const isOnlyAuditMode = !!argv.auditMode && !argv.gatherMode;
if (!isListMode && !isOnlyAuditMode && argv._.length === 0) {
throw new Error('Please provide a url');
}

Expand Down
49 changes: 26 additions & 23 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Connection = require('./gather/connections/connection.js'); // eslint-disa
class Runner {
/**
* @param {Connection} connection
* @param {{config: LH.Config, url: string, driverMock?: Driver}} opts
* @param {{config: LH.Config, url?: string, driverMock?: Driver}} opts
* @return {Promise<LH.RunnerResult|undefined>}
*/
static async run(connection, opts) {
Expand All @@ -37,19 +37,6 @@ class Runner {
*/
const lighthouseRunWarnings = [];

// save the requestedUrl provided by the user
const rawRequestedUrl = opts.url;
if (typeof rawRequestedUrl !== 'string' || rawRequestedUrl.length === 0) {
throw new Error(`You must provide a url to the runner. '${rawRequestedUrl}' provided.`);
}

let parsedURL;
try {
parsedURL = new URL(opts.url);
} catch (e) {
throw new Error('The url provided should have a proper protocol and hostname.');
}

const sentryContext = Sentry.getContext();
// @ts-ignore TODO(bckenny): Sentry type checking
Sentry.captureBreadcrumb({
Expand All @@ -59,27 +46,43 @@ class Runner {
data: sentryContext && sentryContext.extra,
});

// If the URL isn't https and is also not localhost complain to the user.
if (parsedURL.protocol !== 'https:' && parsedURL.hostname !== 'localhost') {
log.warn('Lighthouse', 'The URL provided should be on HTTPS');
log.warn('Lighthouse', 'Performance stats will be skewed redirecting from HTTP to HTTPS.');
}

// canonicalize URL with any trailing slashes neccessary
const requestedUrl = parsedURL.href;

// User can run -G solo, -A solo, or -GA together
// -G and -A will run partial lighthouse pipelines,
// and -GA will run everything plus save artifacts to disk

// Gather phase
// Either load saved artifacts off disk or from the browser
let artifacts;
let requestedUrl;
if (settings.auditMode && !settings.gatherMode) {
// No browser required, just load the artifacts from disk.
const path = Runner._getArtifactsPath(settings);
artifacts = await assetSaver.loadArtifacts(path);
requestedUrl = artifacts.URL.requestedUrl;

if (!requestedUrl) {
throw new Error('Cannot run audit mode on empty URL');
}
if (opts.url && opts.url !== requestedUrl) {
Copy link
Member

Choose a reason for hiding this comment

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

might throw a wrench in the type checking works, but opts.url should now be optional in the function params?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

throw new Error('Cannot run audit mode on different URL');
}
} else {
if (typeof opts.url !== 'string' || opts.url.length === 0) {
throw new Error(`You must provide a url to the runner. '${opts.url}' provided.`);
}

try {
// Use canonicalized URL (with trailing slashes and such)
requestedUrl = new URL(opts.url).href;
} catch (e) {
// Prepend https:// if user was lazy and just went with an unqualified URL
try {
requestedUrl = new URL(`https://${opts.url}`).href;
} catch (e) {
throw new Error('The url provided should have a proper protocol and hostname.');
}
}

artifacts = await Runner._gatherArtifactsFromBrowser(requestedUrl, opts, connection);
// -G means save these to ./latest-run, etc.
if (settings.gatherMode) {
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-core/test/runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Runner', () => {

// uses the files on disk from the -G test. ;)
it('-A audits from saved artifacts and doesn\'t gather', () => {
const opts = {url, config: generateConfig({auditMode: artifactsPath}), driverMock};
const opts = {config: generateConfig({auditMode: artifactsPath}), driverMock};
return Runner.run(null, opts).then(_ => {
assert.equal(loadArtifactsSpy.called, true, 'loadArtifacts was not called');
assert.equal(gatherRunnerRunSpy.called, false, 'GatherRunner.run was called');
Expand All @@ -83,7 +83,7 @@ describe('Runner', () => {

it('-A throws if the settings change', async () => {
const settings = {auditMode: artifactsPath, disableDeviceEmulation: true};
const opts = {url, config: generateConfig(settings), driverMock};
const opts = {config: generateConfig(settings), driverMock};
try {
await Runner.run(null, opts);
assert.fail('should have thrown');
Expand Down