Skip to content

Commit 5f886a8

Browse files
committed
adding unit test by programmatically create symlinks during test case (same approach as has been taken for filesystem UT already)
1 parent 2ba954c commit 5f886a8

25 files changed

+59
-265
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"mocha": "xvfb-maybe electron-mocha --reporter spec && mocha --reporter spec",
2929
"test": "yarn lint && yarn mocha",
3030
"lint": "yarn prettier:check",
31-
"prettier": "prettier \"src/**/*.ts\" \"test/**/*.ts\" \"test/**/*.js\"",
31+
"prettier": "prettier \"src/**/*.ts\" \"test/**/*.js\"",
3232
"prettier:check": "yarn prettier --check",
3333
"prettier:write": "yarn prettier --write",
3434
"prepare": "tsc"

src/asar.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export async function createPackageFromFiles(
142142
const file = metadata[filename];
143143

144144
const shouldUnpackPath = function (
145-
relativeDirPath: string,
145+
relativePath: string,
146146
unpack: string | undefined,
147147
unpackDir: string | undefined,
148148
) {
@@ -151,7 +151,7 @@ export async function createPackageFromFiles(
151151
shouldUnpack = minimatch(filename, unpack, { matchBase: true });
152152
}
153153
if (!shouldUnpack && unpackDir) {
154-
shouldUnpack = isUnpackedDir(relativeDirPath, unpackDir, unpackDirs);
154+
shouldUnpack = isUnpackedDir(relativePath, unpackDir, unpackDirs);
155155
}
156156
return shouldUnpack;
157157
};
@@ -172,7 +172,7 @@ export async function createPackageFromFiles(
172172
return filesystem.insertFile(filename, shouldUnpack, file, options);
173173
case 'link':
174174
shouldUnpack = shouldUnpackPath(
175-
path.relative(src, path.dirname(filename)),
175+
path.relative(src, filename),
176176
options.unpack,
177177
options.unpackDir,
178178
);

src/disk.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ const writeFileListToStream = async function (
6161
// the symlink needs to be recreated outside in .unpacked
6262
const filename = path.relative(filesystem.getRootPath(), file.filename);
6363
const link = await fs.readlink(file.filename);
64-
await fs.symlink(link, path.join(`${dest}.unpacked`, filename));
64+
// if symlink is within subdirectories, then we need to recreate dir structure
65+
await fs.mkdirp(path.join(`${dest}.unpacked`, path.dirname(filename)));
66+
// create symlink within unpacked dir
67+
await fs.symlink(link, path.join(`${dest}.unpacked`, filename)).catch(async (error) => {
68+
if (error.code === 'EPERM' && error.syscall === 'symlink') {
69+
throw new Error(
70+
'Could not create symlinks for unpacked assets. On Windows, consider activating Developer Mode to allow non-admin users to create symlinks by following the instructions at https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development.',
71+
);
72+
}
73+
throw error;
74+
});
6575
}
6676
return out.end();
6777
};

test/cli-spec.js

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const rimraf = require('rimraf');
1111
const compDirs = require('./util/compareDirectories');
1212
const compFileLists = require('./util/compareFileLists');
1313
const compFiles = require('./util/compareFiles');
14+
const createSymlinkApp = require('./util/createSymlinkApp');
1415

1516
const exec = promisify(childProcess.exec);
1617

@@ -189,41 +190,21 @@ describe('command line interface', function () {
189190
);
190191
});
191192
it('should unpack static framework with all underlying symlinks unpacked', async () => {
193+
const { tmpPath } = createSymlinkApp('app');
192194
await execAsar(
193-
'p test/input/packthis-with-symlink/ tmp/packthis-with-symlink.asar --unpack *.txt --unpack-dir "{dir2/subdir,Hello.framework}" --exclude-hidden',
194-
);
195-
// actual files
196-
assert.ok(fs.existsSync('tmp/packthis-with-symlink.asar.unpacked/A/real.txt'));
197-
assert.ok(
198-
fs.existsSync('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Versions/A/Hello'),
195+
`p ${tmpPath} tmp/packthis-with-symlink.asar --unpack *.txt --unpack-dir var --exclude-hidden`,
199196
);
200197

201-
// unpacked symlinks
202-
assert.equal(
203-
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/real.txt'),
204-
'Current/real.txt',
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'),
205203
);
206-
207-
assert.equal(
208-
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Hello'),
209-
'Versions/Current/Hello',
210-
);
211-
assert.ok(
212-
fs
213-
.realpathSync(
214-
'tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Versions/Current/Hello',
215-
)
216-
.endsWith('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Versions/A/Hello'),
217-
);
218-
219-
assert.equal(
220-
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Headers'),
221-
'Versions/Current/Headers',
222-
);
223-
assert.ok(
224-
fs
225-
.realpathSync('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Headers')
226-
.endsWith('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Versions/A/Headers'),
204+
assert.strictEqual(
205+
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/var'),
206+
path.join('private', 'var'),
227207
);
208+
assert.ok(fs.existsSync('tmp/packthis-with-symlink.asar.unpacked/var/file.txt'));
228209
});
229210
});

test/filesystem-spec.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const assert = require('assert');
44
const fs = require('../lib/wrapped-fs').default;
55
const path = require('path');
66
const rimraf = require('rimraf');
7+
const createSymlinkedApp = require('./util/createSymlinkApp');
78

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

@@ -13,28 +14,7 @@ describe('filesystem', function () {
1314
});
1415

1516
it('should does not throw an error when the src path includes a symbol link', async () => {
16-
/**
17-
* Directory structure:
18-
* tmp
19-
* ├── private
20-
* │ └── var
21-
* │ ├── app
22-
* │ │ └── file.txt -> ../file.txt
23-
* │ └── file.txt
24-
* └── var -> private/var
25-
*/
26-
const tmpPath = path.join(__dirname, '..', 'tmp');
27-
const privateVarPath = path.join(tmpPath, 'private', 'var');
28-
const varPath = path.join(tmpPath, 'var');
29-
fs.mkdirSync(privateVarPath, { recursive: true });
30-
fs.symlinkSync(path.relative(tmpPath, privateVarPath), varPath);
31-
32-
const originFilePath = path.join(varPath, 'file.txt');
33-
fs.writeFileSync(originFilePath, 'hello world');
34-
const appPath = path.join(varPath, 'app');
35-
fs.mkdirpSync(appPath);
36-
fs.symlinkSync('../file.txt', path.join(appPath, 'file.txt'));
37-
17+
const { appPath, varPath } = createSymlinkedApp('filesystem');
3818
const filesystem = new Filesystem(varPath);
3919
assert.doesNotThrow(() => {
4020
filesystem.insertLink(path.join(appPath, 'file.txt'));

test/index.test-d.ts

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

test/input/packthis-with-symlink/Hello.framework/Headers

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/input/packthis-with-symlink/Hello.framework/Hello

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/input/packthis-with-symlink/Hello.framework/Info.plist

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/input/packthis-with-symlink/Hello.framework/Modules

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)