-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtask.hook.ts
196 lines (171 loc) · 5.54 KB
/
task.hook.ts
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
import fs from "fs";
import { PNG } from "pngjs";
import pixelmatch, { PixelmatchOptions } from "pixelmatch";
import moveFile from "move-file";
import path from "path";
import { FILE_SUFFIX, TASK } from "./constants";
import {
cleanupUnused,
alignImagesToSameSize,
scaleImageAndWrite,
isImageCurrentVersion,
writePNG,
} from "./image.utils";
import {
generateScreenshotPath,
resetScreenshotNameCache,
} from "./screenshotPath.utils";
import type { CompareImagesTaskReturn } from "./types";
export type CompareImagesCfg = {
scaleFactor: number;
title: string;
imgNew: string;
imgOld: string;
updateImages: boolean;
maxDiffThreshold: number;
diffConfig: PixelmatchOptions;
};
const round = (n: number) => Math.ceil(n * 1000) / 1000;
const unlinkSyncSafe = (path: string) =>
fs.existsSync(path) && fs.unlinkSync(path);
const moveSyncSafe = (pathFrom: string, pathTo: string) =>
fs.existsSync(pathFrom) && moveFile.sync(pathFrom, pathTo);
export const getScreenshotPathInfoTask = (cfg: {
titleFromOptions: string;
imagesPath: string;
specPath: string;
currentRetryNumber: number;
}) => {
const screenshotPath = generateScreenshotPath(cfg);
return { screenshotPath, title: path.basename(screenshotPath, ".png") };
};
export const cleanupImagesTask = (config: Cypress.PluginConfigOptions) => {
if (config.env["pluginVisualRegressionCleanupUnusedImages"]) {
cleanupUnused(config);
}
resetScreenshotNameCache();
return null;
};
export const approveImageTask = ({ img }: { img: string }) => {
const oldImg = img.replace(FILE_SUFFIX.actual, "");
unlinkSyncSafe(oldImg);
const diffImg = img.replace(FILE_SUFFIX.actual, FILE_SUFFIX.diff);
unlinkSyncSafe(diffImg);
moveSyncSafe(img, oldImg);
return null;
};
export const compareImagesTask = async (
cypressConfig: { testingType: string },
cfg: CompareImagesCfg
): Promise<CompareImagesTaskReturn> => {
const messages = [] as string[];
const rawImgNewBuffer = await scaleImageAndWrite({
scaleFactor: cfg.scaleFactor,
path: cfg.imgNew,
});
let imgDiff: number | undefined;
let imgNewBase64: string, imgOldBase64: string, imgDiffBase64: string;
let error = false;
if (fs.existsSync(cfg.imgOld) && !cfg.updateImages) {
const rawImgNew = PNG.sync.read(rawImgNewBuffer);
const rawImgOldBuffer = fs.readFileSync(cfg.imgOld);
const rawImgOld = PNG.sync.read(rawImgOldBuffer);
const isImgSizeDifferent =
rawImgNew.height !== rawImgOld.height ||
rawImgNew.width !== rawImgOld.width;
const [imgNew, imgOld] = isImgSizeDifferent
? alignImagesToSameSize(rawImgNew, rawImgOld)
: [rawImgNew, rawImgOld];
const { width, height } = imgNew;
const diff = new PNG({ width, height });
const diffConfig = Object.assign({ includeAA: true }, cfg.diffConfig);
const diffPixels = pixelmatch(
imgNew.data,
imgOld.data,
diff.data,
width,
height,
diffConfig
);
imgDiff = diffPixels / (width * height);
if (isImgSizeDifferent) {
messages.push(
`Warning: Images size mismatch - new screenshot is ${rawImgNew.width}px by ${rawImgNew.height}px while old one is ${rawImgOld.width}px by ${rawImgOld.height} (width x height).`
);
}
if (imgDiff > cfg.maxDiffThreshold) {
messages.unshift(
`Image diff factor (${round(
imgDiff
)}%) is bigger than maximum threshold option ${cfg.maxDiffThreshold}.`
);
error = true;
}
const diffBuffer = PNG.sync.write(diff);
imgNewBase64 = PNG.sync.write(imgNew).toString("base64");
imgDiffBase64 = diffBuffer.toString("base64");
imgOldBase64 = PNG.sync.write(imgOld).toString("base64");
if (error) {
writePNG(
cypressConfig,
cfg.imgNew.replace(FILE_SUFFIX.actual, FILE_SUFFIX.diff),
diffBuffer
);
return {
error,
message: messages.join("\n"),
imgDiff,
imgNewBase64,
imgDiffBase64,
imgOldBase64,
maxDiffThreshold: cfg.maxDiffThreshold,
};
} else {
if (rawImgOld && !isImageCurrentVersion(rawImgOldBuffer)) {
writePNG(cypressConfig, cfg.imgNew, rawImgNewBuffer);
moveFile.sync(cfg.imgNew, cfg.imgOld);
} else {
// don't overwrite file if it's the same (imgDiff < cfg.maxDiffThreshold && !isImgSizeDifferent)
fs.unlinkSync(cfg.imgNew);
}
}
} else {
// there is no "old screenshot" or screenshots should be immediately updated
imgDiff = 0;
imgNewBase64 = "";
imgDiffBase64 = "";
imgOldBase64 = "";
writePNG(cypressConfig, cfg.imgNew, rawImgNewBuffer);
moveFile.sync(cfg.imgNew, cfg.imgOld);
}
if (typeof imgDiff !== "undefined") {
messages.unshift(
`Image diff factor (${round(
imgDiff
)}%) is within boundaries of maximum threshold option ${
cfg.maxDiffThreshold
}.`
);
return {
message: messages.join("\n"),
imgDiff,
imgNewBase64,
imgDiffBase64,
imgOldBase64,
maxDiffThreshold: cfg.maxDiffThreshold,
};
}
/* c8 ignore next */
return null;
};
export const doesFileExistTask = ({ path }: { path: string }) =>
fs.existsSync(path);
/* c8 ignore start */
export const initTaskHook = (config: Cypress.PluginConfigOptions) => ({
[TASK.getScreenshotPathInfo]: getScreenshotPathInfoTask,
[TASK.cleanupImages]: cleanupImagesTask.bind(undefined, config),
[TASK.doesFileExist]: doesFileExistTask,
[TASK.approveImage]: approveImageTask,
[TASK.compareImages]: compareImagesTask.bind(undefined, config),
});
/* c8 ignore stop */