Skip to content

Commit 6ed467e

Browse files
committed
adding chai+snapshot functionality. adding snapshot tests to verify app with new ordering file unit tests
1 parent 9b7ccfe commit 6ed467e

File tree

14 files changed

+1337
-36
lines changed

14 files changed

+1337
-36
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ jobs:
1515
test:
1616
name: Test
1717
strategy:
18+
fail-fast: false
1819
matrix:
1920
node-version:
2021
- '20.10'

.mocharc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
"watch-files": [
3+
"test/**/*.js",
4+
"lib/**/*.js"
5+
],
6+
"recursive": true,
7+
"file": "./mocha.setup.js", // setup file before everything else loads
8+
"forbid-only": true
9+
}

mocha.setup.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const chai = require("chai");
2+
const { jestSnapshotPlugin } = require("mocha-chai-jest-snapshot");
3+
4+
chai.use(jestSnapshotPlugin());

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"scripts": {
3030
"build": "tsc",
3131
"mocha": "xvfb-maybe electron-mocha --reporter spec && mocha --reporter spec",
32+
"mocha:watch": "mocha --watch",
3233
"test": "yarn lint && yarn mocha",
3334
"lint": "yarn prettier:check",
3435
"prettier": "prettier \"src/**/*.ts\" \"test/**/*.js\"",
@@ -44,10 +45,12 @@
4445
"devDependencies": {
4546
"@types/minimatch": "^3.0.5",
4647
"@types/node": "^12.0.0",
48+
"chai": "^4",
4749
"electron": "^22.0.0",
4850
"electron-mocha": "^13.0.1",
4951
"lodash": "^4.17.15",
5052
"mocha": "^10.1.0",
53+
"mocha-chai-jest-snapshot": "^1.1.6",
5154
"prettier": "^3.3.3",
5255
"rimraf": "^3.0.2",
5356
"typescript": "^5.5.4",

test/__snapshots__/cli-spec.js.snap

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`command line interface should respect ordering file (format: "\${filepath}") 1`] = `
4+
Object {
5+
"files": Object {
6+
"private": Object {
7+
"files": Object {
8+
"var": Object {
9+
"files": Object {
10+
"app": Object {
11+
"files": Object {
12+
"file.txt": Object {
13+
"link": "private/var/file.txt",
14+
},
15+
},
16+
},
17+
"file.txt": Object {
18+
"integrity": Object {
19+
"algorithm": "SHA256",
20+
"blockSize": 4194304,
21+
"blocks": Array [
22+
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
23+
],
24+
"hash": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
25+
},
26+
"size": 11,
27+
},
28+
},
29+
},
30+
},
31+
},
32+
"var": Object {
33+
"link": "private/var",
34+
},
35+
},
36+
}
37+
`;
38+
39+
exports[`command line interface should respect ordering file (format: "\${random number} : \${filepath}") 1`] = `
40+
Object {
41+
"files": Object {
42+
"private": Object {
43+
"files": Object {
44+
"var": Object {
45+
"files": Object {
46+
"app": Object {
47+
"files": Object {
48+
"file.txt": Object {
49+
"link": "private/var/file.txt",
50+
},
51+
},
52+
},
53+
"file.txt": Object {
54+
"integrity": Object {
55+
"algorithm": "SHA256",
56+
"blockSize": 4194304,
57+
"blocks": Array [
58+
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
59+
],
60+
"hash": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
61+
},
62+
"size": 11,
63+
},
64+
},
65+
},
66+
},
67+
},
68+
"var": Object {
69+
"link": "private/var",
70+
},
71+
},
72+
}
73+
`;
74+
75+
exports[`command line interface should respect ordering file (format: ": \${filepath}") 1`] = `
76+
Object {
77+
"files": Object {
78+
"private": Object {
79+
"files": Object {
80+
"var": Object {
81+
"files": Object {
82+
"app": Object {
83+
"files": Object {
84+
"file.txt": Object {
85+
"link": "private/var/file.txt",
86+
},
87+
},
88+
},
89+
"file.txt": Object {
90+
"integrity": Object {
91+
"algorithm": "SHA256",
92+
"blockSize": 4194304,
93+
"blocks": Array [
94+
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
95+
],
96+
"hash": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
97+
},
98+
"size": 11,
99+
},
100+
},
101+
},
102+
},
103+
},
104+
"var": Object {
105+
"link": "private/var",
106+
},
107+
},
108+
}
109+
`;
110+
111+
exports[`command line interface should unpack static framework with all underlying symlinks unpacked 1`] = `
112+
Object {
113+
"files": Object {
114+
"private": Object {
115+
"files": Object {
116+
"var": Object {
117+
"files": Object {
118+
"app": Object {
119+
"files": Object {
120+
"file.txt": Object {
121+
"link": "private/var/file.txt",
122+
"unpacked": true,
123+
},
124+
},
125+
},
126+
"file.txt": Object {
127+
"integrity": Object {
128+
"algorithm": "SHA256",
129+
"blockSize": 4194304,
130+
"blocks": Array [
131+
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
132+
],
133+
"hash": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
134+
},
135+
"size": 11,
136+
"unpacked": true,
137+
},
138+
},
139+
},
140+
},
141+
},
142+
"var": Object {
143+
"link": "private/var",
144+
"unpacked": true,
145+
},
146+
},
147+
}
148+
`;
149+
150+
exports[`command line interface should unpack static framework with all underlying symlinks unpacked 2`] = `
151+
Array [
152+
Object {
153+
"content": "hello world",
154+
"name": "private/var/app/file.txt",
155+
},
156+
"private/var/app",
157+
Object {
158+
"content": "hello world",
159+
"name": "private/var/file.txt",
160+
},
161+
"private/var",
162+
"private",
163+
"var",
164+
]
165+
`;

test/api-spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const compDirs = require('./util/compareDirectories');
1111
const compFileLists = require('./util/compareFileLists');
1212
const { compFiles, isSymbolicLinkSync } = require('./util/compareFiles');
1313
const transform = require('./util/transformStream');
14+
const { TEST_APPS_DIR } = require('./util/constants');
1415

1516
async function assertPackageListEquals(actualList, expectedFilename) {
1617
const expected = await fs.readFile(expectedFilename, 'utf8');
@@ -19,7 +20,7 @@ async function assertPackageListEquals(actualList, expectedFilename) {
1920

2021
describe('api', function () {
2122
beforeEach(() => {
22-
rimraf.sync(path.join(__dirname, '..', 'tmp'), fs);
23+
rimraf.sync(TEST_APPS_DIR, fs);
2324
});
2425

2526
it('should create archive from directory', async () => {

test/cli-spec.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const compDirs = require('./util/compareDirectories');
1212
const compFileLists = require('./util/compareFileLists');
1313
const { compFiles } = require('./util/compareFiles');
1414
const createSymlinkApp = require('./util/createSymlinkApp');
15+
const { verifySmartUnpack } = require('./util/verifySmartUnpack');
16+
const { TEST_APPS_DIR } = require('./util/constants');
1517

1618
const exec = promisify(childProcess.exec);
1719

@@ -29,7 +31,7 @@ async function assertAsarOutputMatches(args, expectedFilename) {
2931

3032
describe('command line interface', function () {
3133
beforeEach(() => {
32-
rimraf.sync(path.join(__dirname, '..', 'tmp'), fs);
34+
rimraf.sync(TEST_APPS_DIR, fs);
3335
});
3436

3537
it('should create archive from directory', async () => {
@@ -190,21 +192,53 @@ describe('command line interface', function () {
190192
);
191193
});
192194
it('should unpack static framework with all underlying symlinks unpacked', async () => {
193-
const { tmpPath } = createSymlinkApp('app');
195+
const { testPath } = await createSymlinkApp('app');
194196
await execAsar(
195-
`p ${tmpPath} tmp/packthis-with-symlink.asar --unpack *.txt --unpack-dir var --exclude-hidden`,
197+
`p ${testPath} tmp/packthis-with-symlink.asar --unpack *.txt --unpack-dir var --exclude-hidden`,
196198
);
197199

198-
assert.ok(fs.existsSync('tmp/packthis-with-symlink.asar.unpacked/private/var/file.txt'));
199-
assert.ok(fs.existsSync('tmp/packthis-with-symlink.asar.unpacked/private/var/app/file.txt'));
200-
assert.strictEqual(
201-
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/private/var/app/file.txt'),
202-
path.join('..', 'file.txt'),
200+
await verifySmartUnpack('tmp/packthis-with-symlink.asar');
201+
});
202+
it('should respect ordering file (format: "${filepath}")', async () => {
203+
const { testPath, filesOrdering } = await createSymlinkApp('app');
204+
205+
const orderingPath = path.join(testPath, '../ordered-app-ordering1.txt');
206+
const data = filesOrdering.reduce((prev, curr) => {
207+
return `${prev}${curr}\n`;
208+
}, '');
209+
await fs.writeFile(orderingPath, data);
210+
211+
await execAsar(
212+
`p ${testPath} tmp/packthis-with-symlink.asar --ordering=${orderingPath} --exclude-hidden`,
203213
);
204-
assert.strictEqual(
205-
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/var'),
206-
path.join('private', 'var'),
214+
await verifySmartUnpack('tmp/packthis-with-symlink.asar');
215+
});
216+
it('should respect ordering file (format: ": ${filepath}")', async () => {
217+
const { testPath, filesOrdering } = await createSymlinkApp('app');
218+
219+
const orderingPath = path.join(testPath, '../ordered-app-ordering2.txt');
220+
const data = filesOrdering.reduce((prev, curr) => {
221+
return `${prev}: ${curr}\n`;
222+
}, '');
223+
await fs.writeFile(orderingPath, data);
224+
225+
await execAsar(
226+
`p ${testPath} tmp/packthis-with-symlink.asar --ordering=${orderingPath} --exclude-hidden`,
227+
);
228+
await verifySmartUnpack('tmp/packthis-with-symlink.asar');
229+
});
230+
it('should respect ordering file (format: "${random number} : ${filepath}")', async () => {
231+
const { testPath, filesOrdering } = await createSymlinkApp('app');
232+
233+
const orderingPath = path.join(testPath, '../ordered-app-ordering3.txt');
234+
const data = filesOrdering.reduce((prev, curr) => {
235+
return `${prev}${Math.floor(Math.random() * 1000)} : ${curr} \n`;
236+
}, '');
237+
await fs.writeFile(orderingPath, data);
238+
239+
await execAsar(
240+
`p ${testPath} tmp/packthis-with-symlink.asar --ordering=${orderingPath} --exclude-hidden`,
207241
);
208-
assert.ok(fs.existsSync('tmp/packthis-with-symlink.asar.unpacked/var/file.txt'));
242+
await verifySmartUnpack('tmp/packthis-with-symlink.asar');
209243
});
210244
});

test/filesystem-spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ const fs = require('../lib/wrapped-fs').default;
55
const path = require('path');
66
const rimraf = require('rimraf');
77
const createSymlinkedApp = require('./util/createSymlinkApp');
8+
const { TEST_APPS_DIR } = require('./util/constants');
89

910
const Filesystem = require('../lib/filesystem').Filesystem;
1011

1112
describe('filesystem', function () {
1213
beforeEach(() => {
13-
rimraf.sync(path.join(__dirname, '..', 'tmp'), fs);
14+
rimraf.sync(TEST_APPS_DIR, fs);
1415
});
1516

1617
it('should does not throw an error when the src path includes a symbol link', async () => {
17-
const { appPath, varPath } = createSymlinkedApp('filesystem');
18+
const { appPath, varPath } = await createSymlinkedApp('filesystem');
1819
const filesystem = new Filesystem(varPath);
1920
assert.doesNotThrow(() => {
2021
filesystem.insertLink(path.join(appPath, 'file.txt'));

test/util/compareDirectories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = async function (dirA, dirB) {
2929
differentFiles.push(filename);
3030
continue;
3131
}
32-
const [fileContentA, fileContentB] = Promise.all(
32+
const [fileContentA, fileContentB] = await Promise.all(
3333
[dirA, dirB].map((dir) => fs.readFile(path.join(dir, filename), 'utf8')),
3434
);
3535
if (fileContentA !== fileContentB) {

test/util/constants.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const path = require('path');
2+
3+
// root dir of the repo
4+
const ROOT_PROJECT_DIR = path.resolve(__dirname, '..', '..');
5+
6+
// tmp dir we use for test artifacts
7+
const TEST_APPS_DIR = path.join(ROOT_PROJECT_DIR, 'tmp');
8+
9+
module.exports = { ROOT_PROJECT_DIR, TEST_APPS_DIR };

0 commit comments

Comments
 (0)