-
Notifications
You must be signed in to change notification settings - Fork 21
feat: adds combinePages option to diff per-page #98
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
Open
EvHaus
wants to merge
1
commit into
moshensky:master
Choose a base branch
from
EvHaus:97-per-page-diffing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||
| import * as path from 'node:path' | ||||||
| import { access, mkdir, unlink } from 'node:fs/promises' | ||||||
| import { access, mkdir, unlink, readdir } from 'node:fs/promises' | ||||||
| import { pdf2png } from './pdf2png/pdf2png' | ||||||
| import { compareImages } from './compare-images' | ||||||
| import { Jimp, JimpInstance } from 'jimp' | ||||||
|
|
@@ -90,6 +90,12 @@ const maskImgWithRegions = | |||||
| * fields is inlined. | ||||||
| */ | ||||||
| export type CompareOptions = { | ||||||
| /** | ||||||
| * Whether to combine all pages into a single image. | ||||||
| * | ||||||
| * @defaultValue true | ||||||
| */ | ||||||
| combinePages?: boolean | ||||||
| /** | ||||||
| * Number value for error tolerance in the range [0, 1]. | ||||||
| * | ||||||
|
|
@@ -142,10 +148,25 @@ export async function comparePdfToSnapshot( | |||||
| options?: CompareOptions, | ||||||
| ): Promise<boolean> { | ||||||
| const mergedOptions = mergeOptionsWithDefaults(options) | ||||||
| const snapshotContext = await createSnapshotContext(snapshotDir, snapshotName) | ||||||
| const snapshotContext = await createSnapshotContext(snapshotDir, snapshotName, mergedOptions) | ||||||
|
|
||||||
| // When combinePages is false, we need to process each page as a separate context | ||||||
| if (Array.isArray(snapshotContext)) { | ||||||
| try { | ||||||
| // Ensure snapshots exists for all pages. If any are missing, we | ||||||
| // should re-generate all of them. | ||||||
| for (const context of snapshotContext) { | ||||||
| await access(context.path) | ||||||
| } | ||||||
|
|
||||||
| return compareWithSnapshot(pdf, snapshotContext, mergedOptions) | ||||||
| } catch { | ||||||
| return handleMissingSnapshot(pdf, snapshotContext[0], mergedOptions) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Check if snapshot exits and handle accordingly | ||||||
| try { | ||||||
| // Check if snapshot exits and handle accordingly | ||||||
| await access(snapshotContext.path) | ||||||
| return compareWithSnapshot(pdf, snapshotContext, mergedOptions) | ||||||
| } catch { | ||||||
|
|
@@ -163,6 +184,7 @@ type SnapshotContext = { | |||||
|
|
||||||
| function mergeOptionsWithDefaults(options?: CompareOptions): Required<CompareOptions> { | ||||||
| return { | ||||||
| combinePages: options?.combinePages ?? true, | ||||||
| maskRegions: options?.maskRegions ?? (() => []), | ||||||
| pdf2PngOptions: options?.pdf2PngOptions ?? { dpi: Dpi.High }, | ||||||
| failOnMissingSnapshot: options?.failOnMissingSnapshot ?? false, | ||||||
|
|
@@ -180,7 +202,8 @@ export const snapshotsDirName = SNAPSHOTS_DIR_NAME | |||||
| async function createSnapshotContext( | ||||||
| snapshotDir: string, | ||||||
| snapshotName: string, | ||||||
| ): Promise<SnapshotContext> { | ||||||
| options: Required<CompareOptions>, | ||||||
| ): Promise<SnapshotContext | Array<SnapshotContext>> { | ||||||
| const dirPath = path.join(snapshotDir, SNAPSHOTS_DIR_NAME) | ||||||
| try { | ||||||
| await access(dirPath) | ||||||
|
|
@@ -190,6 +213,21 @@ async function createSnapshotContext( | |||||
|
|
||||||
| const basePath = path.join(dirPath, snapshotName) | ||||||
|
|
||||||
| // When combinePages is false, we need to create a separate snapshot for each page | ||||||
| if (options.combinePages === false) { | ||||||
| const files = await readdir(dirPath) | ||||||
| return files.filter((file: string) => file.startsWith(snapshotName)).map((file: string) => { | ||||||
| const fileNameWithoutExt = file.substring(0, file.lastIndexOf('.')) | ||||||
| return ({ | ||||||
| name: snapshotName, | ||||||
| dirPath, | ||||||
| path: path.join(dirPath, file), | ||||||
| diffPath: path.join(dirPath, `${fileNameWithoutExt}.diff.png`), | ||||||
| newPath: path.join(dirPath, `${fileNameWithoutExt}.new.png`), | ||||||
| }) | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| return { | ||||||
| name: snapshotName, | ||||||
| dirPath, | ||||||
|
|
@@ -202,40 +240,58 @@ async function createSnapshotContext( | |||||
| async function handleMissingSnapshot( | ||||||
| pdf: string | Buffer, | ||||||
| snapshotContext: SnapshotContext, | ||||||
| { failOnMissingSnapshot, maskRegions, pdf2PngOptions }: Required<CompareOptions>, | ||||||
| { combinePages, failOnMissingSnapshot, maskRegions, pdf2PngOptions }: Required<CompareOptions>, | ||||||
| ): Promise<boolean> { | ||||||
| if (failOnMissingSnapshot) { | ||||||
| return false | ||||||
| } | ||||||
|
|
||||||
| // Generate snapshot if missing | ||||||
| const images = await pdf2png(pdf, pdf2PngOptions).then(maskImgWithRegions(maskRegions)) | ||||||
| await writeImages(snapshotContext.path)(images) | ||||||
| await writeImages(snapshotContext.path, combinePages)(images) | ||||||
|
|
||||||
| return true | ||||||
| } | ||||||
|
|
||||||
| async function compareWithSnapshot( | ||||||
| pdf: string | Buffer, | ||||||
| async function compareContext( | ||||||
| snapshotContext: SnapshotContext, | ||||||
| { maskRegions, pdf2PngOptions, tolerance }: Required<CompareOptions>, | ||||||
| ): Promise<boolean> { | ||||||
| const images = await pdf2png(pdf, pdf2PngOptions).then(maskImgWithRegions(maskRegions)) | ||||||
| images: ReadonlyArray<JimpInstance>, | ||||||
| { combinePages, tolerance }: Required<CompareOptions> | ||||||
| ) { | ||||||
| const result = await compareImages(snapshotContext.path, images, { tolerance }) | ||||||
|
|
||||||
| if (result.equal) { | ||||||
| await removeIfExists(snapshotContext.diffPath) | ||||||
| await removeIfExists(snapshotContext.newPath) | ||||||
|
|
||||||
| return true | ||||||
| } | ||||||
|
|
||||||
| await writeImages(snapshotContext.newPath)(images) | ||||||
| await writeImages(snapshotContext.diffPath)(result.diffs.map((x) => x.diff)) | ||||||
| await writeImages(snapshotContext.newPath, combinePages)(images) | ||||||
| await writeImages(snapshotContext.diffPath, combinePages)(result.diffs.map((x) => x.diff)) | ||||||
|
|
||||||
| return false | ||||||
| } | ||||||
|
|
||||||
| async function compareWithSnapshot( | ||||||
| pdf: string | Buffer, | ||||||
| snapshotContext: SnapshotContext | Array<SnapshotContext>, | ||||||
| options: Required<CompareOptions>, | ||||||
| ): Promise<boolean> { | ||||||
| const { maskRegions, pdf2PngOptions } = options; | ||||||
| const images = await pdf2png(pdf, pdf2PngOptions).then(maskImgWithRegions(maskRegions)) | ||||||
|
|
||||||
| if (Array.isArray(snapshotContext)) { | ||||||
| let results: Array<Promise<boolean>> = []; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. results isn't reassigned, can be const
Suggested change
|
||||||
| for (let i = 0, l = snapshotContext.length; i < l; i++) { | ||||||
| results.push(compareContext(snapshotContext[i], [images[i]], options)); | ||||||
| } | ||||||
|
|
||||||
| return (await Promise.all(results)).every((result) => result) | ||||||
| } | ||||||
|
|
||||||
| return await compareContext(snapshotContext, images, options) | ||||||
| } | ||||||
|
|
||||||
| async function removeIfExists(filePath: string): Promise<void> { | ||||||
| try { | ||||||
| await unlink(filePath) | ||||||
|
|
||||||
Binary file added
BIN
+20.7 KB
src/test-data/pdf2png-expected/__snapshots__/two-page-separate-pages_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+21 KB
src/test-data/pdf2png-expected/__snapshots__/two-page-separate-pages_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default for this option is kept as
trueto ensure this is a non-breaking change.