Skip to content

Commit

Permalink
adding unit test by programmatically create symlinks during test case…
Browse files Browse the repository at this point in the history
… (same approach as has been taken for filesystem UT already)
  • Loading branch information
mmaietta committed Dec 25, 2024
1 parent 2ba954c commit 5f886a8
Show file tree
Hide file tree
Showing 25 changed files with 59 additions and 265 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"mocha": "xvfb-maybe electron-mocha --reporter spec && mocha --reporter spec",
"test": "yarn lint && yarn mocha",
"lint": "yarn prettier:check",
"prettier": "prettier \"src/**/*.ts\" \"test/**/*.ts\" \"test/**/*.js\"",
"prettier": "prettier \"src/**/*.ts\" \"test/**/*.js\"",
"prettier:check": "yarn prettier --check",
"prettier:write": "yarn prettier --write",
"prepare": "tsc"
Expand Down
6 changes: 3 additions & 3 deletions src/asar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export async function createPackageFromFiles(
const file = metadata[filename];

const shouldUnpackPath = function (
relativeDirPath: string,
relativePath: string,
unpack: string | undefined,
unpackDir: string | undefined,
) {
Expand All @@ -151,7 +151,7 @@ export async function createPackageFromFiles(
shouldUnpack = minimatch(filename, unpack, { matchBase: true });
}
if (!shouldUnpack && unpackDir) {
shouldUnpack = isUnpackedDir(relativeDirPath, unpackDir, unpackDirs);
shouldUnpack = isUnpackedDir(relativePath, unpackDir, unpackDirs);
}
return shouldUnpack;
};
Expand All @@ -172,7 +172,7 @@ export async function createPackageFromFiles(
return filesystem.insertFile(filename, shouldUnpack, file, options);
case 'link':
shouldUnpack = shouldUnpackPath(
path.relative(src, path.dirname(filename)),
path.relative(src, filename),
options.unpack,
options.unpackDir,
);
Expand Down
12 changes: 11 additions & 1 deletion src/disk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ const writeFileListToStream = async function (
// the symlink needs to be recreated outside in .unpacked
const filename = path.relative(filesystem.getRootPath(), file.filename);
const link = await fs.readlink(file.filename);
await fs.symlink(link, path.join(`${dest}.unpacked`, filename));
// if symlink is within subdirectories, then we need to recreate dir structure
await fs.mkdirp(path.join(`${dest}.unpacked`, path.dirname(filename)));
// create symlink within unpacked dir
await fs.symlink(link, path.join(`${dest}.unpacked`, filename)).catch(async (error) => {
if (error.code === 'EPERM' && error.syscall === 'symlink') {
throw new Error(
'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.',
);
}
throw error;
});
}
return out.end();
};
Expand Down
43 changes: 12 additions & 31 deletions test/cli-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const rimraf = require('rimraf');
const compDirs = require('./util/compareDirectories');
const compFileLists = require('./util/compareFileLists');
const compFiles = require('./util/compareFiles');
const createSymlinkApp = require('./util/createSymlinkApp');

const exec = promisify(childProcess.exec);

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

// unpacked symlinks
assert.equal(
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/real.txt'),
'Current/real.txt',
assert.ok(fs.existsSync('tmp/packthis-with-symlink.asar.unpacked/private/var/file.txt'));
assert.ok(fs.existsSync('tmp/packthis-with-symlink.asar.unpacked/private/var/app/file.txt'));
assert.strictEqual(
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/private/var/app/file.txt'),
path.join('..', 'file.txt'),
);

assert.equal(
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Hello'),
'Versions/Current/Hello',
);
assert.ok(
fs
.realpathSync(
'tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Versions/Current/Hello',
)
.endsWith('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Versions/A/Hello'),
);

assert.equal(
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Headers'),
'Versions/Current/Headers',
);
assert.ok(
fs
.realpathSync('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Headers')
.endsWith('tmp/packthis-with-symlink.asar.unpacked/Hello.framework/Versions/A/Headers'),
assert.strictEqual(
fs.readlinkSync('tmp/packthis-with-symlink.asar.unpacked/var'),
path.join('private', 'var'),
);
assert.ok(fs.existsSync('tmp/packthis-with-symlink.asar.unpacked/var/file.txt'));
});
});
24 changes: 2 additions & 22 deletions test/filesystem-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert');
const fs = require('../lib/wrapped-fs').default;
const path = require('path');
const rimraf = require('rimraf');
const createSymlinkedApp = require('./util/createSymlinkApp');

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

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

it('should does not throw an error when the src path includes a symbol link', async () => {
/**
* Directory structure:
* tmp
* ├── private
* │ └── var
* │ ├── app
* │ │ └── file.txt -> ../file.txt
* │ └── file.txt
* └── var -> private/var
*/
const tmpPath = path.join(__dirname, '..', 'tmp');
const privateVarPath = path.join(tmpPath, 'private', 'var');
const varPath = path.join(tmpPath, 'var');
fs.mkdirSync(privateVarPath, { recursive: true });
fs.symlinkSync(path.relative(tmpPath, privateVarPath), varPath);

const originFilePath = path.join(varPath, 'file.txt');
fs.writeFileSync(originFilePath, 'hello world');
const appPath = path.join(varPath, 'app');
fs.mkdirpSync(appPath);
fs.symlinkSync('../file.txt', path.join(appPath, 'file.txt'));

const { appPath, varPath } = createSymlinkedApp('filesystem');
const filesystem = new Filesystem(varPath);
assert.doesNotThrow(() => {
filesystem.insertLink(path.join(appPath, 'file.txt'));
Expand Down
42 changes: 0 additions & 42 deletions test/index.test-d.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/input/packthis-with-symlink/Hello.framework/Headers

This file was deleted.

1 change: 0 additions & 1 deletion test/input/packthis-with-symlink/Hello.framework/Hello

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion test/input/packthis-with-symlink/Hello.framework/Modules

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5f886a8

Please sign in to comment.