-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): implement compare reports functions
- Loading branch information
1 parent
72d6c14
commit 5493cc4
Showing
7 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { writeFile } from 'node:fs/promises'; | ||
import { Report, ReportsDiff, reportSchema } from '@code-pushup/models'; | ||
import { | ||
Diff, | ||
calcDuration, | ||
readJsonFile, | ||
scoreReport, | ||
} from '@code-pushup/utils'; | ||
import { name as packageName, version } from '../../package.json'; | ||
import { | ||
ReportsToCompare, | ||
compareAudits, | ||
compareCategories, | ||
compareGroups, | ||
} from './implementation/compare-scorables'; | ||
|
||
export function compareReports(reports: Diff<Report>): ReportsDiff { | ||
const start = performance.now(); | ||
const date = new Date().toISOString(); | ||
|
||
const commits: ReportsDiff['commits'] = | ||
reports.before.commit != null && reports.after.commit != null | ||
? { before: reports.before.commit, after: reports.after.commit } | ||
: null; | ||
|
||
const scoredReports: ReportsToCompare = { | ||
before: scoreReport(reports.before), | ||
after: scoreReport(reports.after), | ||
}; | ||
|
||
const categories = compareCategories(scoredReports); | ||
const groups = compareGroups(scoredReports); | ||
const audits = compareAudits(scoredReports); | ||
|
||
const duration = calcDuration(start); | ||
|
||
return { | ||
commits, | ||
categories, | ||
groups, | ||
audits, | ||
packageName, | ||
version, | ||
date, | ||
duration, | ||
}; | ||
} | ||
|
||
export async function compareReportFiles( | ||
inputPaths: Diff<string>, | ||
outputPath: string, | ||
): Promise<void> { | ||
const [reportBefore, reportAfter] = await Promise.all([ | ||
readJsonFile(inputPaths.before), | ||
readJsonFile(inputPaths.after), | ||
]); | ||
const reports: Diff<Report> = { | ||
before: reportSchema.parse(reportBefore), | ||
after: reportSchema.parse(reportAfter), | ||
}; | ||
|
||
const reportsDiff = compareReports(reports); | ||
|
||
await writeFile(outputPath, JSON.stringify(reportsDiff, null, 2)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { vol } from 'memfs'; | ||
import { join } from 'node:path'; | ||
import { Commit, Report, reportsDiffSchema } from '@code-pushup/models'; | ||
import { | ||
COMMIT_ALT_MOCK, | ||
COMMIT_MOCK, | ||
MEMFS_VOLUME, | ||
MINIMAL_REPORT_MOCK, | ||
REPORT_MOCK, | ||
reportMock, | ||
} from '@code-pushup/test-utils'; | ||
import { Diff, readJsonFile } from '@code-pushup/utils'; | ||
import { compareReportFiles, compareReports } from './compare'; | ||
|
||
describe('compareReports', () => { | ||
const mockCommits: Diff<Commit> = { | ||
before: COMMIT_MOCK, | ||
after: COMMIT_ALT_MOCK, | ||
}; | ||
|
||
describe('unchanged reports', () => { | ||
const mockReport = reportMock(); | ||
const mockReports: Diff<Report> = { | ||
before: { ...mockReport, commit: mockCommits.before }, | ||
after: { ...mockReport, commit: mockCommits.after }, | ||
}; | ||
|
||
it('should create valid report diff', () => { | ||
const reportsDiff = compareReports(mockReports); | ||
expect(() => reportsDiffSchema.parse(reportsDiff)).not.toThrow(); | ||
}); | ||
|
||
it('should include commits from both reports', () => { | ||
const reportsDiff = compareReports(mockReports); | ||
expect(reportsDiff.commits).toEqual(mockCommits); | ||
}); | ||
|
||
it('should have no changes, additions or removals', () => { | ||
const reportsDiff = compareReports(mockReports); | ||
expect(reportsDiff.categories.changed).toHaveLength(0); | ||
expect(reportsDiff.categories.added).toHaveLength(0); | ||
expect(reportsDiff.categories.removed).toHaveLength(0); | ||
expect(reportsDiff.groups.changed).toHaveLength(0); | ||
expect(reportsDiff.groups.added).toHaveLength(0); | ||
expect(reportsDiff.groups.removed).toHaveLength(0); | ||
expect(reportsDiff.audits.changed).toHaveLength(0); | ||
expect(reportsDiff.audits.added).toHaveLength(0); | ||
expect(reportsDiff.audits.removed).toHaveLength(0); | ||
}); | ||
|
||
it('should contain all categories/groups/audits in unchanged arrays', () => { | ||
const reportsDiff = compareReports(mockReports); | ||
expect(reportsDiff.categories.unchanged).toHaveLength( | ||
mockReport.categories.length, | ||
); | ||
expect(reportsDiff.groups.unchanged).toHaveLength( | ||
mockReport.plugins.reduce((acc, { groups }) => acc + groups!.length, 0), | ||
); | ||
expect(reportsDiff.audits.unchanged).toHaveLength( | ||
mockReport.plugins.reduce((acc, { audits }) => acc + audits.length, 0), | ||
); | ||
}); | ||
}); | ||
|
||
describe('changed reports', () => { | ||
const mockReports: Diff<Report> = { | ||
before: { ...MINIMAL_REPORT_MOCK, commit: mockCommits.before }, | ||
after: { ...REPORT_MOCK, commit: mockCommits.after }, | ||
}; | ||
|
||
it('should create valid report diff', () => { | ||
const reportsDiff = compareReports(mockReports); | ||
expect(() => reportsDiffSchema.parse(reportsDiff)).not.toThrow(); | ||
}); | ||
|
||
it('should include commits from both reports', () => { | ||
const reportsDiff = compareReports(mockReports); | ||
expect(reportsDiff.commits).toEqual(mockCommits); | ||
}); | ||
|
||
it('should only have added categories (minimal report has none)', () => { | ||
const reportsDiff = compareReports(mockReports); | ||
expect(reportsDiff.categories.added).toHaveLength( | ||
REPORT_MOCK.categories.length, | ||
); | ||
expect(reportsDiff.categories.removed).toHaveLength(0); | ||
expect(reportsDiff.categories.changed).toHaveLength(0); | ||
expect(reportsDiff.categories.unchanged).toHaveLength(0); | ||
}); | ||
|
||
it('should only have added groups (minimal report has none)', () => { | ||
const reportsDiff = compareReports(mockReports); | ||
expect(reportsDiff.groups.added.length).toBeGreaterThan(0); | ||
expect(reportsDiff.groups.removed).toHaveLength(0); | ||
expect(reportsDiff.groups.changed).toHaveLength(0); | ||
expect(reportsDiff.groups.unchanged).toHaveLength(0); | ||
}); | ||
|
||
it('should mark multiple audits as added and 1 as removed (single audit from minimal report unmatched)', () => { | ||
const reportsDiff = compareReports(mockReports); | ||
expect(reportsDiff.audits.added.length).toBeGreaterThan(1); | ||
expect(reportsDiff.audits.removed).toHaveLength(1); | ||
expect(reportsDiff.audits.changed).toHaveLength(0); | ||
expect(reportsDiff.audits.unchanged).toHaveLength(0); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('compareReportFiles', () => { | ||
beforeEach(() => { | ||
vol.fromJSON( | ||
{ | ||
'source-report.json': JSON.stringify(MINIMAL_REPORT_MOCK), | ||
'target-report.json': JSON.stringify(REPORT_MOCK), | ||
}, | ||
MEMFS_VOLUME, | ||
); | ||
}); | ||
|
||
it('should create valid reports-diff.json from report.json files', async () => { | ||
await compareReportFiles( | ||
{ | ||
before: join(MEMFS_VOLUME, 'source-report.json'), | ||
after: join(MEMFS_VOLUME, 'target-report.json'), | ||
}, | ||
join(MEMFS_VOLUME, 'reports-diff.json'), | ||
); | ||
|
||
const reportsDiffPromise = readJsonFile( | ||
join(MEMFS_VOLUME, 'reports-diff.json'), | ||
); | ||
await expect(reportsDiffPromise).resolves.toBeTruthy(); | ||
|
||
const reportsDiff = await reportsDiffPromise; | ||
expect(() => reportsDiffSchema.parse(reportsDiff)).not.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters