Skip to content

Commit

Permalink
feat(cli): add collect option to set number of pages to autodiscover (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Aug 28, 2020
1 parent 12619d2 commit 90c8635
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 37 deletions.
3 changes: 3 additions & 0 deletions docs/configuration.md
Expand Up @@ -223,6 +223,9 @@ Options:
continuing [number] [default: 10000]
--settings The Lighthouse settings and flags to use when collecting
--numberOfRuns, -n The number of times to run Lighthouse. [number] [default: 3]
--maxAutodiscoverUrls The maximum number of pages to collect when using the staticDistDir
option with no specified URL. Disable this limit by setting to 0.
[number] [default: 5]
```

#### `method`
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/collect/collect.js
Expand Up @@ -80,6 +80,12 @@ function buildCommand(yargs) {
default: 3,
type: 'number',
},
maxAutodiscoverUrls: {
description:
'The maximum number of pages to collect when using the staticDistDir option with no specified URLs. Disable this limit by setting to 0.',
default: 5,
type: 'number',
},
});
}

Expand Down Expand Up @@ -167,7 +173,8 @@ async function startServerAndDetermineUrls(options) {

const urls = urlsAsArray;
if (!urls.length) {
urls.push(...server.getAvailableUrls().slice(0, 5));
const maxNumberOfUrls = options.maxAutodiscoverUrls || Infinity;
urls.push(...server.getAvailableUrls().slice(0, maxNumberOfUrls));
}

if (!urls.length) {
Expand Down
36 changes: 0 additions & 36 deletions packages/cli/test/collect-static-dir-with-urls.test.js

This file was deleted.

111 changes: 111 additions & 0 deletions packages/cli/test/collect-static-dir.test.js
@@ -0,0 +1,111 @@
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

jest.retryTimes(3);

/* eslint-env jest */

const path = require('path');
const {runCLI} = require('./test-utils.js');

describe('Lighthouse CI collect CLI', () => {
describe('with URLs', () => {
const staticDistDir = path.join(__dirname, 'fixtures/collect-static-dir-with-urls');
it('should collect a static dir with explicit URLs', async () => {
const {stdout, stderr, status} = await runCLI(
['collect', '-n=1', '--staticDistDir=./', '--url=/child/grandchild.html'],
{
cwd: staticDistDir,
}
);
expect(stdout).toMatchInlineSnapshot(`
"Started a web server on port XXXX...
Running Lighthouse 1 time(s) on http://localhost:XXXX/child/grandchild.html
Run #1...done.
Done running Lighthouse!
"
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});

describe('with autodiscover limit', () => {
const staticDistDir = path.join(__dirname, 'fixtures/collect-static-dir-autodiscover-limit');
it('should collect a single page from static dir', async () => {
const {stdout, stderr, status} = await runCLI(
['collect', '-n=1', '--staticDistDir=./', '--maxAutodiscoverUrls=1'],
{
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 collect all available pages from static dir', async () => {
const {stdout, stderr, status} = await runCLI(
['collect', '-n=1', '--staticDistDir=./', '--maxAutodiscoverUrls=0'],
{
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/index.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');

it('should collect 5 pages from static dir', async () => {
const {stdout, stderr, status} = await runCLI(['collect', '-n=1', '--staticDistDir=./'], {
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/index.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.
Done running Lighthouse!
"
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});
});
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>board test page for staticDistDir usage</title>
</head>
<body>
test
</body>
</html>
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>checkout test page for staticDistDir usage</title>
</head>
<body>
test
</body>
</html>
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>index test page for staticDistDir usage</title>
</head>
<body>
test
</body>
</html>
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jobs test page for staticDistDir usage</title>
</head>
<body>
test
</body>
</html>
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>shop test page for staticDistDir usage</title>
</head>
<body>
test
</body>
</html>
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>team test page for staticDistDir usage</title>
</head>
<body>
test
</body>
</html>
1 change: 1 addition & 0 deletions types/collect.d.ts
Expand Up @@ -60,6 +60,7 @@ declare global {
headful: boolean;
additive: boolean;
settings?: LighthouseSettings;
maxAutodiscoverUrls?: number;
}
}
}
Expand Down

0 comments on commit 90c8635

Please sign in to comment.