Skip to content

Commit 289004b

Browse files
committed
add helper test.if, ifWindows, and ifNotWindows function to retain snapshots when running on different OS's
1 parent dcb8387 commit 289004b

File tree

11 files changed

+167
-74
lines changed

11 files changed

+167
-74
lines changed

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
},
1313
],
1414
},
15-
globalSetup: './test/jest.setup.ts',
15+
globalSetup: './test/setup/jest.global.setup.ts',
16+
setupFilesAfterEnv: ['<rootDir>/test/setup/jest.env.setup.ts'],
1617
testTimeout: 10000,
1718
};

test/__snapshots__/cli-spec.ts.snap

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,87 @@ exports[`command line interface should create archive from directory with unpack
842842
]
843843
`;
844844

845+
exports[`command line interface should create archive from directory with windows-style path separators 1`] = `
846+
{
847+
"files": {
848+
".hiddenfile.txt": {
849+
"integrity": {
850+
"algorithm": "SHA256",
851+
"blockSize": 4194304,
852+
"blocks": [
853+
"d400d9a74f67e724a4793173d73e1403bd6b734930632f4eee50a3a5a1e5a478",
854+
],
855+
"hash": "d400d9a74f67e724a4793173d73e1403bd6b734930632f4eee50a3a5a1e5a478",
856+
},
857+
"size": 19,
858+
},
859+
"dir1": {
860+
"files": {
861+
"file1.txt": {
862+
"integrity": {
863+
"algorithm": "SHA256",
864+
"blockSize": 4194304,
865+
"blocks": [
866+
"420149d3f852894ba7f32e9d3ec7d8919ee2451724bf2580b0186cd373bd6d82",
867+
],
868+
"hash": "420149d3f852894ba7f32e9d3ec7d8919ee2451724bf2580b0186cd373bd6d82",
869+
},
870+
"size": 9,
871+
},
872+
},
873+
},
874+
"dir2": {
875+
"files": {
876+
"file2.png": {
877+
"integrity": {
878+
"algorithm": "SHA256",
879+
"blockSize": 4194304,
880+
"blocks": [
881+
"cc402b796dc92b2b1f3a6d09515003d8400e63d8acaffc967e49c0cf015fcffe",
882+
],
883+
"hash": "cc402b796dc92b2b1f3a6d09515003d8400e63d8acaffc967e49c0cf015fcffe",
884+
},
885+
"size": 182,
886+
},
887+
"file3.txt": {
888+
"integrity": {
889+
"algorithm": "SHA256",
890+
"blockSize": 4194304,
891+
"blocks": [
892+
"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3",
893+
],
894+
"hash": "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3",
895+
},
896+
"size": 3,
897+
},
898+
},
899+
},
900+
"emptyfile.txt": {
901+
"integrity": {
902+
"algorithm": "SHA256",
903+
"blockSize": 4194304,
904+
"blocks": [
905+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
906+
],
907+
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
908+
},
909+
"size": 0,
910+
},
911+
"file0.txt": {
912+
"integrity": {
913+
"algorithm": "SHA256",
914+
"blockSize": 4194304,
915+
"blocks": [
916+
"41a978be88ff87a57308bf234106a57ebf9aa0971e2101e75113547a815be72b",
917+
],
918+
"hash": "41a978be88ff87a57308bf234106a57ebf9aa0971e2101e75113547a815be72b",
919+
},
920+
"size": 13,
921+
},
922+
},
923+
}
924+
`;
925+
845926
exports[`command line interface should create archive from directory without hidden files 1`] = `
846927
{
847928
"files": {

test/api-spec.ts

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ describe('api', function () {
2020
await asar.createPackage('test/input/packthis/', 'tmp/packthis-api.asar');
2121
return compFiles('tmp/packthis-api.asar', 'test/expected/packthis.asar');
2222
});
23-
if (os.platform() === 'win32') {
24-
it('should create archive with windows-style path separators', async () => {
25-
await asar.createPackage('test\\input\\packthis\\', 'tmp\\packthis-api.asar');
26-
return compFiles('tmp/packthis-api.asar', 'test/expected/packthis.asar');
27-
});
28-
}
23+
it.ifWindows('should create archive with windows-style path separators', async () => {
24+
await asar.createPackage('test\\input\\packthis\\', 'tmp\\packthis-api.asar');
25+
return compFiles('tmp/packthis-api.asar', 'test/expected/packthis.asar');
26+
});
2927
it('should create archive from directory (without hidden files)', async () => {
3028
await asar.createPackageWithOptions(
3129
'test/input/packthis/',
@@ -103,45 +101,43 @@ describe('api', function () {
103101
});
104102

105103
// We don't extract symlinks on Windows, so skip these tests
106-
if (os.platform() !== 'win32') {
107-
it('should extract an archive with symlink', async () => {
108-
assert.strictEqual(isSymbolicLinkSync('test/input/packthis-with-symlink/real.txt'), true);
109-
await asar.createPackageWithOptions(
110-
'test/input/packthis-with-symlink/',
111-
'tmp/packthis-with-symlink.asar',
112-
{ dot: false },
113-
);
114-
asar.extractAll('tmp/packthis-with-symlink.asar', 'tmp/packthis-with-symlink/');
115-
return compFiles(
116-
'tmp/packthis-with-symlink/real.txt',
117-
'test/input/packthis-with-symlink/real.txt',
118-
);
119-
});
120-
it('should extract an archive with symlink having the same prefix', async () => {
121-
assert.strictEqual(
122-
isSymbolicLinkSync('test/input/packthis-with-symlink-same-prefix/real.txt'),
123-
true,
124-
);
125-
await asar.createPackageWithOptions(
126-
'test/input/packthis-with-symlink-same-prefix/',
127-
'tmp/packthis-with-symlink-same-prefix.asar',
128-
{ dot: false },
129-
);
130-
asar.extractAll(
131-
'tmp/packthis-with-symlink-same-prefix.asar',
132-
'tmp/packthis-with-symlink-same-prefix/',
133-
);
134-
return compFiles(
135-
'tmp/packthis-with-symlink-same-prefix/real.txt',
136-
'test/input/packthis-with-symlink-same-prefix/real.txt',
137-
);
138-
});
139-
it('should not extract an archive with a bad symlink', async () => {
140-
assert.throws(() => {
141-
asar.extractAll('test/input/bad-symlink.asar', 'tmp/bad-symlink/');
142-
});
104+
it.ifNotWindows('should extract an archive with symlink', async () => {
105+
assert.strictEqual(isSymbolicLinkSync('test/input/packthis-with-symlink/real.txt'), true);
106+
await asar.createPackageWithOptions(
107+
'test/input/packthis-with-symlink/',
108+
'tmp/packthis-with-symlink.asar',
109+
{ dot: false },
110+
);
111+
asar.extractAll('tmp/packthis-with-symlink.asar', 'tmp/packthis-with-symlink/');
112+
return compFiles(
113+
'tmp/packthis-with-symlink/real.txt',
114+
'test/input/packthis-with-symlink/real.txt',
115+
);
116+
});
117+
it.ifNotWindows('should extract an archive with symlink having the same prefix', async () => {
118+
assert.strictEqual(
119+
isSymbolicLinkSync('test/input/packthis-with-symlink-same-prefix/real.txt'),
120+
true,
121+
);
122+
await asar.createPackageWithOptions(
123+
'test/input/packthis-with-symlink-same-prefix/',
124+
'tmp/packthis-with-symlink-same-prefix.asar',
125+
{ dot: false },
126+
);
127+
asar.extractAll(
128+
'tmp/packthis-with-symlink-same-prefix.asar',
129+
'tmp/packthis-with-symlink-same-prefix/',
130+
);
131+
return compFiles(
132+
'tmp/packthis-with-symlink-same-prefix/real.txt',
133+
'test/input/packthis-with-symlink-same-prefix/real.txt',
134+
);
135+
});
136+
it.ifNotWindows('should not extract an archive with a bad symlink', async () => {
137+
assert.throws(() => {
138+
asar.extractAll('test/input/bad-symlink.asar', 'tmp/bad-symlink/');
143139
});
144-
}
140+
});
145141
it('should handle multibyte characters in paths', async () => {
146142
await asar.createPackageWithOptions(
147143
'test/input/packthis-unicode-path/',

test/cli-spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import fs from '../lib/wrapped-fs';
77

88
import compFileLists from './util/compareFileLists';
99
import { compFiles } from './util/compareFiles';
10-
import createSymlinkApp from './util/createSymlinkApp';
10+
import createSymlinkApp from './util/createTestApp';
1111
import { verifyFileTree, verifySmartUnpack } from './util/verifySmartUnpack';
1212

1313
const exec = promisify(childProcess.exec);
@@ -29,12 +29,13 @@ describe('command line interface', function () {
2929
await execAsar('p test/input/packthis/ tmp/packthis-cli.asar');
3030
await verifySmartUnpack('tmp/packthis-cli.asar');
3131
});
32-
if (os.platform() === 'win32') {
33-
it('should create archive from directory with windows-style path separators', async () => {
32+
it.ifWindows(
33+
'should create archive from directory with windows-style path separators',
34+
async () => {
3435
await execAsar('p test\\input\\packthis\\ tmp\\packthis-cli.asar');
3536
await verifySmartUnpack('tmp/packthis-cli.asar');
36-
});
37-
}
37+
},
38+
);
3839
it('should create archive from directory without hidden files', async () => {
3940
await execAsar('p test/input/packthis/ tmp/packthis-without-hidden-cli.asar --exclude-hidden');
4041
await verifySmartUnpack('tmp/packthis-without-hidden-cli.asar');

test/filesystem-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assert';
22
import path from 'path';
3-
import createSymlinkedApp from './util/createSymlinkApp';
3+
import createSymlinkedApp from './util/createTestApp';
44

55
import { Filesystem } from '../lib/filesystem';
66

test/jest-ex.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare namespace jest {
2+
interface It {
3+
if: (condition: boolean) => jest.It;
4+
ifWindows: jest.It;
5+
ifNotWindows: jest.It;
6+
}
7+
}

test/jest.setup.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/setup/jest.env.setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { platform } from 'os';
2+
3+
test.if = (condition: boolean) => (condition ? test : test.skip);
4+
test.ifWindows = test.if(platform() === 'win32');
5+
test.ifNotWindows = test.if(platform() !== 'win32');

test/setup/jest.global.setup.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import rimraf from 'rimraf';
2+
import * as path from 'path';
3+
import fs from '../../lib/wrapped-fs';
4+
5+
export default () => {
6+
rimraf.sync(path.join(__dirname, '..', '..', 'tmp'), fs);
7+
};
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import fs from '../../lib/wrapped-fs';
33
import rimraf from 'rimraf';
4+
45
/**
56
* Directory structure:
67
* testName
@@ -12,19 +13,20 @@ import rimraf from 'rimraf';
1213
* └── var -> private/var
1314
*/
1415
const appsDir = path.join(__dirname, '../..', 'tmp');
15-
let counter = 0;
16-
const createTestApp = async (testName: string, additionalFiles: Record<string, string> = {}) => {
17-
const outDir = testName || 'app-' + counter++;
18-
const testPath = path.join(appsDir, outDir);
19-
if (fs.existsSync(testPath)) {
20-
fs.rmdirSync(testPath);
21-
}
2216

17+
const createTestApp = async (
18+
testName: string | undefined,
19+
additionalFiles: Record<string, string> = {},
20+
) => {
21+
const outDir = testName || 'app' + Math.floor(Math.random() * 100);
22+
const testPath = path.join(appsDir, outDir);
2323
const privateVarPath = path.join(testPath, 'private', 'var');
2424
const varPath = path.join(testPath, 'var');
2525

26-
await fs.mkdirp(privateVarPath);
27-
await fs.symlink(path.relative(testPath, privateVarPath), varPath);
26+
rimraf.sync(testPath, fs);
27+
28+
fs.mkdirSync(privateVarPath, { recursive: true });
29+
fs.symlinkSync(path.relative(testPath, privateVarPath), varPath);
2830

2931
const files = {
3032
'file.txt': 'hello world',
@@ -34,15 +36,12 @@ const createTestApp = async (testName: string, additionalFiles: Record<string, s
3436
const originFilePath = path.join(varPath, filename);
3537
await fs.writeFile(originFilePath, fileData);
3638
}
39+
3740
const appPath = path.join(varPath, 'app');
38-
await fs.mkdirp(appPath);
39-
await fs.symlink('../file.txt', path.join(appPath, 'file.txt'));
41+
fs.mkdirpSync(appPath);
42+
fs.symlinkSync('../file.txt', path.join(appPath, 'file.txt'));
4043

41-
return {
42-
testPath,
43-
varPath,
44-
appPath,
45-
};
44+
return { testPath, varPath, appPath };
4645
};
4746

4847
export default createTestApp;

test/util/verifySmartUnpack.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export function removeUnstableProperties(data: any) {
4040
if (name === 'offset') {
4141
return undefined;
4242
}
43+
if (name === 'link') {
44+
return toSystemIndependentPath(value);
45+
}
4346
return value;
4447
}),
4548
);

0 commit comments

Comments
 (0)