-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
cli-flags-test.js
253 lines (215 loc) · 9.36 KB
/
cli-flags-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* @license Copyright 2016 The Lighthouse Authors. 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';
/* eslint-env jest */
import {strict as assert} from 'assert';
import fs from 'fs';
import yargs from 'yargs';
import {getFlags} from '../../cli-flags.js';
import {LH_ROOT} from '../../../root.js';
/**
* @param {LH.CliFlags} flags
*/
function snapshot(flags) {
flags = {...flags};
// `path` properties will have different values based on the filesystem,
// so normalize.
for (const [k, v] of Object.entries(flags)) {
if (typeof v === 'string') {
// @ts-expect-error
flags[k] = v
.replace(process.cwd(), '__REPLACED__')
.replace(/\\/g, '/');
}
}
expect(flags).toMatchSnapshot();
}
describe('CLI flags', function() {
it('all options should have descriptions', () => {
getFlags('chrome://version');
// @ts-expect-error - getGroups is private
const optionGroups = yargs.getGroups();
/** @type {string[]} */
const allOptions = [];
Object.keys(optionGroups).forEach(key => {
allOptions.push(...optionGroups[key]);
});
const optionsWithDescriptions =
// @ts-expect-error - getUsageInstance is private
Object.keys(yargs.getInternalMethods().getUsageInstance().getDescriptions());
allOptions.forEach(opt => {
assert.ok(optionsWithDescriptions.includes(opt), `cli option '${opt}' has no description`);
});
});
it('settings are accepted from a file path', () => {
const flags = getFlags([
'http://www.example.com',
`--cli-flags-path="${LH_ROOT}/lighthouse-cli/test/fixtures/cli-flags-path.json"`,
'--budgets-path=path/to/my/budget-from-command-line.json', // this should override the config value
].join(' '));
expect(flags).toMatchObject({
budgetsPath: 'path/to/my/budget-from-command-line.json',
onlyCategories: ['performance', 'seo'],
chromeFlags: '--window-size 800,600',
extraHeaders: {'X-Men': 'wolverine'},
throttlingMethod: 'devtools',
throttling: {
requestLatencyMs: 700,
cpuSlowdownMultiplier: 6,
},
});
snapshot(flags);
});
it('settings are accepted from a file path (inlined budgets)', () => {
const flags = getFlags([
'http://www.example.com',
// eslint-disable-next-line max-len
`--cli-flags-path="${LH_ROOT}/lighthouse-cli/test/fixtures/cli-flags-path-inline-budgets.json"`,
].join(' '));
expect(flags.budgets).toMatchObject([{'anything': 'works'}]);
snapshot(flags);
});
it('array values support csv when appropriate', () => {
const flags = getFlags([
'http://www.example.com',
'--only-categories=performance,seo',
'--skipAudits=unused-javascript,redirects',
'--skipAudits=bootup-time',
].join(' '));
expect(flags.onlyCategories).toEqual(['performance', 'seo']);
expect(flags.skipAudits).toEqual(['unused-javascript', 'redirects', 'bootup-time']);
snapshot(flags);
});
it('array values do not support csv when appropriate', () => {
const flags = getFlags([
'http://www.example.com',
'--chrome-flags="--window-size 800,600"',
'--chrome-flags="--enabled-features=NetworkService,VirtualTime"',
'--blockedUrlPatterns=.*x,y\\.png',
].join(' '));
expect(flags.chromeFlags).toEqual([
'--window-size 800,600',
'--enabled-features=NetworkService,VirtualTime',
]);
expect(flags.blockedUrlPatterns).toEqual(['.*x,y\\.png']);
snapshot(flags);
});
describe('extraHeaders', () => {
it('should convert extra headers to object', async () => {
const flags = getFlags([
'http://www.example.com',
'--extra-headers="{"foo": "bar"}"',
].join(' '));
expect(flags).toHaveProperty('extraHeaders', {foo: 'bar'});
snapshot(flags);
});
it('should read extra headers from file', () => {
const headersFile = `${LH_ROOT}/lighthouse-cli/test/fixtures/extra-headers/valid.json`;
const headers = JSON.parse(fs.readFileSync(headersFile, 'utf-8'));
const flags = getFlags([
'http://www.example.com',
`--extra-headers=${headersFile}`,
].join(' '));
expect(flags).toHaveProperty('extraHeaders', headers);
snapshot(flags);
});
});
describe('screenEmulation', () => {
const url = 'http://www.example.com';
describe('width', () => {
it('parses a number value', () => {
const flags = getFlags(`${url} --screenEmulation.width=500`, {noExitOnFailure: true});
expect(flags.screenEmulation).toEqual({width: 500});
snapshot(flags);
});
it('throws on a non-number', () => {
expect(() => getFlags(`${url} --screenEmulation.width=yah`, {noExitOnFailure: true}))
.toThrow(`Invalid value: 'screenEmulation.width' must be a number`);
});
it('throws with no flag value', () => {
expect(() => getFlags(`${url} --screenEmulation.width`, {noExitOnFailure: true}))
.toThrow(`Invalid value: 'screenEmulation.width' must be a number`);
});
});
describe('height', () => {
it('parses a number value', () => {
const flags = getFlags(`${url} --screenEmulation.height=123`, {noExitOnFailure: true});
expect(flags.screenEmulation).toEqual({height: 123});
});
it('throws on a non-number', () => {
expect(() => getFlags(`${url} --screenEmulation.height=false`, {noExitOnFailure: true}))
.toThrow(`Invalid value: 'screenEmulation.height' must be a number`);
});
it('throws with no flag value', () => {
expect(() => getFlags(`${url} --screenEmulation.height`, {noExitOnFailure: true}))
.toThrow(`Invalid value: 'screenEmulation.height' must be a number`);
});
});
describe('deviceScaleFactor', () => {
it('parses a non-integer numeric value', () => {
const flags = getFlags(`${url} --screenEmulation.deviceScaleFactor=1.325`,
{noExitOnFailure: true});
expect(flags.screenEmulation).toEqual({deviceScaleFactor: 1.325});
snapshot(flags);
});
it('throws on a non-number', () => {
expect(() => getFlags(`${url} --screenEmulation.deviceScaleFactor=12px`,
{noExitOnFailure: true}))
.toThrow(`Invalid value: 'screenEmulation.deviceScaleFactor' must be a number`);
});
it('throws with no flag value', () => {
expect(() => getFlags(`${url} --screenEmulation.deviceScaleFactor`,
{noExitOnFailure: true}))
.toThrow(`Invalid value: 'screenEmulation.deviceScaleFactor' must be a number`);
});
});
describe('mobile', () => {
it('parses the flag with no value as true', () => {
const flags = getFlags(`${url} --screenEmulation.mobile`, {noExitOnFailure: true});
expect(flags.screenEmulation).toEqual({mobile: true});
snapshot(flags);
});
it('parses the --no-mobile flag as false', () => {
const flags = getFlags(`${url} --no-screenEmulation.mobile`, {noExitOnFailure: true});
expect(flags.screenEmulation).toEqual({mobile: false});
});
it('parses the flag with a boolean value', () => {
const flagsTrue = getFlags(`${url} --screenEmulation.mobile=true`, {noExitOnFailure: true});
expect(flagsTrue.screenEmulation).toEqual({mobile: true});
const flagsFalse = getFlags(`${url} --screenEmulation.mobile=false`,
{noExitOnFailure: true});
expect(flagsFalse.screenEmulation).toEqual({mobile: false});
});
it('throws on a non-boolean value', () => {
expect(() => getFlags(`${url} --screenEmulation.mobile=2`, {noExitOnFailure: true}))
.toThrow(`Invalid value: 'screenEmulation.mobile' must be a boolean`);
});
});
describe('disabled', () => {
it('parses the flag with no value as true', () => {
const flags = getFlags(`${url} --screenEmulation.disabled`, {noExitOnFailure: true});
expect(flags.screenEmulation).toEqual({disabled: true});
snapshot(flags);
});
it('parses the --no-disabled flag as false', () => {
const flags = getFlags(`${url} --no-screenEmulation.disabled`, {noExitOnFailure: true});
expect(flags.screenEmulation).toEqual({disabled: false});
});
it('parses the flag with a boolean value', () => {
const flagsTrue = getFlags(`${url} --screenEmulation.disabled=true`,
{noExitOnFailure: true});
expect(flagsTrue.screenEmulation).toEqual({disabled: true});
const flagsFalse = getFlags(`${url} --screenEmulation.disabled=false`,
{noExitOnFailure: true});
expect(flagsFalse.screenEmulation).toEqual({disabled: false});
});
it('throws on a non-boolean value', () => {
expect(() => getFlags(`${url} --screenEmulation.disabled=str`, {noExitOnFailure: true}))
.toThrow(`Invalid value: 'screenEmulation.disabled' must be a boolean`);
});
});
});
});