Skip to content

Commit

Permalink
feat(cli): add autodiscoverUrlBlocklist option (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnEidum committed Oct 14, 2020
1 parent 01e397b commit bf6a83d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/configuration.md
Expand Up @@ -207,6 +207,8 @@ Options:
multiple URLs.
--staticDistDir The build directory where your HTML files to run Lighthouse on are
located.
--autodiscoverUrlBlocklist A URL to not include when autodiscovering urls from staticDistDir.
Use this flag multiple times to filter multiple URLs.
--isSinglePageApplication If the application is created by Single Page Application, enable
redirect to index.html.
--chromePath The path to the Chrome or Chromium executable to use for collection.
Expand Down
20 changes: 19 additions & 1 deletion packages/cli/src/collect/collect.js
Expand Up @@ -39,6 +39,10 @@ function buildCommand(yargs) {
description:
'A URL to run Lighthouse on. Use this flag multiple times to evaluate multiple URLs.',
},
autodiscoverUrlBlocklist: {
description:
'A URL to not include when autodiscovering urls from staticDistDir. Use this flag multiple times to filter multiple URLs.',
},
psiApiKey: {
description: '[psi only] The API key to use for PageSpeed Insights runner method.',
},
Expand Down Expand Up @@ -174,7 +178,21 @@ async function startServerAndDetermineUrls(options) {
const urls = urlsAsArray;
if (!urls.length) {
const maxNumberOfUrls = options.maxAutodiscoverUrls || Infinity;
urls.push(...server.getAvailableUrls().slice(0, maxNumberOfUrls));
const autodiscoverUrlBlocklistAsArray = Array.isArray(options.autodiscoverUrlBlocklist)
? options.autodiscoverUrlBlocklist
: options.autodiscoverUrlBlocklist
? [options.autodiscoverUrlBlocklist]
: [];
const availableUrls = server.getAvailableUrls();
const normalizedBlocklist = autodiscoverUrlBlocklistAsArray.map(rawUrl => {
const url = new URL(rawUrl, 'http://localhost');
url.port = server.port.toString();
return url.href;
});
const urlsToUse = availableUrls
.filter(url => !normalizedBlocklist.includes(url))
.slice(0, maxNumberOfUrls);
urls.push(...urlsToUse);
}

if (!urls.length) {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/test/collect-puppeteer.test.js
Expand Up @@ -90,9 +90,9 @@ describe('Lighthouse CI collect CLI with puppeteer', () => {
const chromePathHelp = stdout.match(/--chromePath.*\n.*\n.*/);
expect(chromePathHelp).toMatchInlineSnapshot(`
Array [
"--chromePath The path to the Chrome or Chromium executable to use for collection.
--puppeteerScript The path to a script that manipulates the browser with puppeteer before running Lighthouse, used for auth.
--puppeteerLaunchOptions The object of puppeteer launch options",
"--chromePath The path to the Chrome or Chromium executable to use for collection.
--puppeteerScript The path to a script that manipulates the browser with puppeteer before running Lighthouse, used for auth.
--puppeteerLaunchOptions The object of puppeteer launch options",
]
`);
expect(stderr).toMatchInlineSnapshot(`""`);
Expand Down
84 changes: 84 additions & 0 deletions packages/cli/test/collect-static-dir.test.js
Expand Up @@ -82,6 +82,90 @@ describe('Lighthouse CI collect CLI', () => {
expect(status).toEqual(0);
}, 180000);
});

describe('with autodiscoverUrlBlocklist', () => {
const staticDistDir = path.join(__dirname, 'fixtures/collect-static-dir-autodiscover-limit');
it('should not filter any files because it was not tested', async () => {
const {stdout, stderr, status} = await runCLI(
[
'collect',
'-n=1',
'--staticDistDir=./',
'--maxAutodiscoverUrls=1',
'--autodiscoverUrlBlocklist=/team.html',
],
{
cwd: staticDistDir,
}
);
expect(stdout).toMatchInlineSnapshot(`
"Started a web server on port XXXX...
Running Lighthouse 1 time(s) on http://localhost:XXXX/board.html
Run #1...done.
Done running Lighthouse!
"
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);

it('should filter index file', async () => {
const {stdout, stderr, status} = await runCLI(
['collect', '-n=1', '--staticDistDir=./', '--autodiscoverUrlBlocklist=/index.html'],
{
cwd: staticDistDir,
}
);
expect(stdout).toMatchInlineSnapshot(`
"Started a web server on port XXXX...
Running Lighthouse 1 time(s) on http://localhost:XXXX/board.html
Run #1...done.
Running Lighthouse 1 time(s) on http://localhost:XXXX/checkout.html
Run #1...done.
Running Lighthouse 1 time(s) on http://localhost:XXXX/jobs.html
Run #1...done.
Running Lighthouse 1 time(s) on http://localhost:XXXX/shop.html
Run #1...done.
Running Lighthouse 1 time(s) on http://localhost:XXXX/team.html
Run #1...done.
Done running Lighthouse!
"
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);

it('should filter index file and board file', async () => {
const {stdout, stderr, status} = await runCLI(
[
'collect',
'-n=1',
'--staticDistDir=./',
'--autodiscoverUrlBlocklist=/index.html',
'--autodiscoverUrlBlocklist=/board.html',
],
{
cwd: staticDistDir,
}
);
expect(stdout).toMatchInlineSnapshot(`
"Started a web server on port XXXX...
Running Lighthouse 1 time(s) on http://localhost:XXXX/checkout.html
Run #1...done.
Running Lighthouse 1 time(s) on http://localhost:XXXX/jobs.html
Run #1...done.
Running Lighthouse 1 time(s) on http://localhost:XXXX/shop.html
Run #1...done.
Running Lighthouse 1 time(s) on http://localhost:XXXX/team.html
Run #1...done.
Done running Lighthouse!
"
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});

describe('by default', () => {
const staticDistDir = path.join(__dirname, 'fixtures/collect-static-dir-autodiscover-limit');

Expand Down
1 change: 1 addition & 0 deletions types/collect.d.ts
Expand Up @@ -44,6 +44,7 @@ declare global {

export interface Options {
url?: string | string[];
autodiscoverUrlBlocklist?: string | string[];
psiApiKey?: string;
psiApiEndpoint?: string;
staticDistDir?: string;
Expand Down

0 comments on commit bf6a83d

Please sign in to comment.