Skip to content

Commit e1061c8

Browse files
glentakahashiNimaSoroush
authored andcommitted
Sanitize test names to not include dashes (#146)
1 parent ca0b099 commit e1061c8

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/compareImage.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ describe('Compare Image', () => {
132132
});
133133
});
134134

135+
it('sanitizes paths', async () => {
136+
await compareImage(Object, mockConfig, {
137+
testName: 'check /test.html',
138+
testPath: '/src/test.js',
139+
imageType: 'png',
140+
});
141+
expect(fs.writeFileSync)
142+
.toHaveBeenCalledWith(
143+
'./differencify_report/__image_snapshots__/check -test.html.snap.png',
144+
Object,
145+
);
146+
});
147+
135148
it('throws correct error if it cannot read image', async () => {
136149
expect.assertions(3);
137150
Jimp.read.mockReturnValueOnce(Promise.reject(new Error('error1')));

src/utils/paths.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const getTestRoot = (testConfig, globalConfig) => {
1515
return testRoot;
1616
};
1717

18+
const getSanitizedName = name => name.replace(/\//g, '-');
19+
1820
export const getSnapshotsDir = (testConfig, globalConfig) => path.join(
1921
getTestRoot(testConfig, globalConfig),
2022
'__image_snapshots__',
@@ -26,14 +28,16 @@ export const getSnapshotPath = (snapshotsDir, testConfig) => {
2628
if (!snapshotsDir || !testConfig) {
2729
throw new Error('Incorrect arguments passed to getSnapshotPath');
2830
}
29-
return path.join(snapshotsDir, `${testConfig.testName}.snap.${testConfig.imageType || 'png'}`);
31+
const testName = getSanitizedName(testConfig.testName);
32+
return path.join(snapshotsDir, `${testName}.snap.${testConfig.imageType || 'png'}`);
3033
};
3134

3235
export const getDiffPath = (diffDir, testConfig) => {
3336
if (!diffDir || !testConfig) {
3437
throw new Error('Incorrect arguments passed to getDiffPath');
3538
}
36-
return path.join(diffDir, `${testConfig.testName}.differencified.${testConfig.imageType || 'png'}`);
39+
const testName = getSanitizedName(testConfig.testName);
40+
return path.join(diffDir, `${testName}.differencified.${testConfig.imageType || 'png'}`);
3741
};
3842

3943
export const getCurrentImageDir = snapshotsDir => path.join(snapshotsDir, '__current_output__');
@@ -42,5 +46,6 @@ export const getCurrentImagePath = (currentImageDir, testConfig) => {
4246
if (!currentImageDir || !testConfig) {
4347
throw new Error('Incorrect arguments passed to getDiffPath');
4448
}
45-
return path.join(currentImageDir, `${testConfig.testName}.current.${testConfig.imageType || 'png'}`);
49+
const testName = getSanitizedName(testConfig.testName);
50+
return path.join(currentImageDir, `${testName}.current.${testConfig.imageType || 'png'}`);
4651
};

0 commit comments

Comments
 (0)