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

feat(cli): add --lhr to assert command to load LHRs from anywhere #1024

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion packages/cli/src/assert/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function buildCommand(yargs) {
type: 'boolean',
description: 'Whether to include the results of passed assertions in the output.',
},
lhr: {
description:
'Path to LHRs (either a folder or a single file path). Not recursive. If not provided, .lighthouseci is used',
},
});
}

Expand All @@ -53,7 +57,7 @@ async function runCommand(options) {
// If we have a budgets file, convert it to our assertions format.
if (budgetsFile) options = await convertBudgetsToAssertions(readBudgets(budgetsFile));

const lhrs = loadSavedLHRs().map(json => JSON.parse(json));
const lhrs = loadSavedLHRs(options.lhr).map(json => JSON.parse(json));
Comment on lines 57 to +60
Copy link
Contributor

Choose a reason for hiding this comment

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

it looks like if we pass options.lhr it will be gone cause it was replaced by the convertBudgetsToAssertions returned value.

if my understanding is correct & this is a vallid issue, I would love to create PR to fix this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's absolutely right, nice catch. A PR would be appreciated!

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks! fixes coming right up

Copy link
Contributor

Choose a reason for hiding this comment

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

The fixes are at #1062, @connorjclark.
Could you kindly review it at your convenience?

const uniqueUrls = new Set(lhrs.map(lhr => lhr.finalUrl));
const allResults = getAllAssertionResults(options, lhrs);
const groupedResults = _.groupBy(allResults, result => result.url);
Expand Down
15 changes: 12 additions & 3 deletions packages/utils/src/saved-reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ function ensureDirectoryExists(baseDir = LHCI_DIR) {
}

/**
* @param {string} [directoryOrPath]
* @return {string[]}
*/
function loadSavedLHRs() {
ensureDirectoryExists();
function loadSavedLHRs(directoryOrPath) {
directoryOrPath = directoryOrPath || LHCI_DIR;

if (directoryOrPath === LHCI_DIR) {
ensureDirectoryExists();
}

if (fs.lstatSync(directoryOrPath).isFile()) {
return [fs.readFileSync(directoryOrPath, 'utf8')];
}

/** @type {string[]} */
const lhrs = [];
for (const file of fs.readdirSync(LHCI_DIR)) {
for (const file of fs.readdirSync(directoryOrPath)) {
if (!LHR_REGEX.test(file)) continue;

const filePath = path.join(LHCI_DIR, file);
Expand Down
1 change: 1 addition & 0 deletions types/assert.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare global {
includePassedAssertions?: boolean;
budgetsFile?: string;
assertMatrix?: BaseOptions[];
lhr?: string;
}

/**
Expand Down
Loading