Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Respect unpack minimatch for symlinks within previously unpacked directories #341

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding unit test by programmatically create symlinks during test case…
… (same approach as has been taken for filesystem UT already)
  • Loading branch information
mmaietta committed Dec 25, 2024
commit 5f886a8ac3e47c4b0da4fca343f3707e7ddf3ee9
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 3 additions & 3 deletions src/asar.ts
Original file line number Diff line number Diff line change
@@ -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,
) {
@@ -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;
};
@@ -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,
);
12 changes: 11 additions & 1 deletion src/disk.ts
Original file line number Diff line number Diff line change
@@ -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();
};
43 changes: 12 additions & 31 deletions test/cli-spec.js
Original file line number Diff line number Diff line change
@@ -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);

@@ -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
@@ -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;

@@ -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'));
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.

30 changes: 30 additions & 0 deletions test/util/createSymlinkApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const path = require('path');
const fs = require('../../lib/wrapped-fs').default;
const rimraf = require('rimraf');
/**
* Directory structure:
* tmp
* ├── private
* │ └── var
* │ ├── app
* │ │ └── file.txt -> ../file.txt
* │ └── file.txt
* └── var -> private/var
*/
module.exports = (testName) => {
const tmpPath = path.join(__dirname, '../..', 'tmp', testName || 'app');
const privateVarPath = path.join(tmpPath, 'private', 'var');
const varPath = path.join(tmpPath, 'var');

rimraf.sync(tmpPath, fs);

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'));
return { appPath, tmpPath, varPath };
};