Skip to content

Commit

Permalink
feat(cli): add --lhr to assert command to load LHRs from anywhere (#1024
Browse files Browse the repository at this point in the history
)
  • Loading branch information
connorjclark authored Apr 9, 2024
1 parent 083d639 commit 19c7ca6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
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));
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

0 comments on commit 19c7ca6

Please sign in to comment.