Skip to content

Commit

Permalink
beep boop
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzhao committed Jun 13, 2024
1 parent a98588c commit ad07ffc
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 11 deletions.
20 changes: 18 additions & 2 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,43 @@ const templateApp = async (
export default async () => {
await fs.remove(appsDir);
await fs.mkdirp(appsDir);
await templateApp('Asar.app', 'arm64', async (appPath) => {
await templateApp('Arm64Asar.app', 'arm64', async (appPath) => {
await fs.copy(
path.resolve(asarsDir, 'app.asar'),
path.resolve(appPath, 'Contents', 'Resources', 'app.asar'),
);
});

// contains `extra-file.txt`
await templateApp('Arm64AsarExtraFile.app', 'arm64', async (appPath) => {
await fs.copy(
path.resolve(asarsDir, 'app2.asar'),
path.resolve(appPath, 'Contents', 'Resources', 'app.asar'),
);
});

await templateApp('X64Asar.app', 'x64', async (appPath) => {
await fs.copy(
path.resolve(asarsDir, 'app.asar'),
path.resolve(appPath, 'Contents', 'Resources', 'app.asar'),
);
});

await templateApp('NoAsar.app', 'arm64', async (appPath) => {
await templateApp('Arm64NoAsar.app', 'arm64', async (appPath) => {
await fs.copy(
path.resolve(asarsDir, 'app'),
path.resolve(appPath, 'Contents', 'Resources', 'app'),
);
});

// contains `extra-file.txt`
await templateApp('Arm64NoAsarExtraFile.app', 'arm64', async (appPath) => {
await fs.copy(
path.resolve(asarsDir, 'app2'),
path.resolve(appPath, 'Contents', 'Resources', 'app'),
);
});

await templateApp('X64NoAsar.app', 'x64', async (appPath) => {
await fs.copy(
path.resolve(asarsDir, 'app'),
Expand Down
Binary file added test/fixtures/asars/app2.asar
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/asars/app2/extra-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
erick was here!
2 changes: 2 additions & 0 deletions test/fixtures/asars/app2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('I am an app.asar', process.arch);
process.exit(0);
4 changes: 4 additions & 0 deletions test/fixtures/asars/app2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app",
"main": "index.js"
}
124 changes: 115 additions & 9 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { spawn } from '@malept/cross-spawn-promise';
import * as fs from 'fs-extra';
import * as path from 'path';

import { makeUniversalApp } from '../src/index';
import { makeUniversalApp } from '../dist/cjs/index';

const appsPath = path.resolve(__dirname, 'fixtures', 'apps');
const appsOutPath = path.resolve(__dirname, 'fixtures', 'apps', 'out');

async function ensureUniversal(app: string) {
const exe = path.resolve(app, 'Contents', 'MacOS', 'Electron');
Expand All @@ -14,16 +15,65 @@ async function ensureUniversal(app: string) {
expect(result2).toContain('x64');
}

// See `jest.setup.ts` for app fixture setup process
describe('makeUniversalApp', () => {
it.todo('`force` works');
afterEach(async () => {
await fs.emptyDir(appsOutPath);
});

it('throws an error if asar is only detected in one arch', async () => {
const out = path.resolve(appsOutPath, 'Error.app');
await expect(
makeUniversalApp({
x64AppPath: path.resolve(appsPath, 'X64Asar.app'),
arm64AppPath: path.resolve(appsPath, 'Arm64NoAsar.app'),
outAppPath: out,
}),
).rejects.toThrow(
'Both the x64 and arm64 versions of your application need to have been built with the same asar settings (enabled vs disabled)',
);
});

it.todo('works for lipo binary resources');

describe('force', () => {
it('throws an error if `out` bundle already exists and `force` is `false`', async () => {
const out = path.resolve(appsOutPath, 'Error.app');
await fs.mkdirp(out);
await expect(
makeUniversalApp({
x64AppPath: path.resolve(appsPath, 'X64Asar.app'),
arm64AppPath: path.resolve(appsPath, 'Arm64Asar.app'),
outAppPath: out,
}),
).rejects.toThrow(/The out path ".*" already exists and force is not set to true/);
});

it('packages successfully if `out` bundle already exists and `force` is `true`', async () => {
const out = path.resolve(appsOutPath, 'Error.app');
await fs.mkdirp(out);
await makeUniversalApp({
x64AppPath: path.resolve(appsPath, 'X64Asar.app'),
arm64AppPath: path.resolve(appsPath, 'Arm64Asar.app'),
outAppPath: out,
force: true,
});
await ensureUniversal(out);
// Only a single asar as they were identical
expect(
(await fs.readdir(path.resolve(out, 'Contents', 'Resources'))).filter((p) =>
p.endsWith('asar'),
),
).toEqual(['app.asar']);
}, 60000);
});

describe('asar mode', () => {
it('should correctly merge two identical asars', async () => {
const out = path.resolve(appsPath, 'MergedAsar.app');
await makeUniversalApp({
x64AppPath: path.resolve(appsPath, 'X64Asar.app'),
arm64AppPath: path.resolve(appsPath, 'Asar.app'),
arm64AppPath: path.resolve(appsPath, 'Arm64Asar.app'),
outAppPath: out,
});
await ensureUniversal(out);
Expand All @@ -34,16 +84,54 @@ describe('makeUniversalApp', () => {
),
).toEqual(['app.asar']);
}, 60000);
it('should create two separate non-identical ASAR files', async () => {

it('should create a shim if asars are different between architectures', async () => {
const out = path.resolve(appsPath, 'TwoAsars.app');
await makeUniversalApp({
x64AppPath: path.resolve(appsPath, 'X64Asar.app'),
arm64AppPath: path.resolve(appsPath, 'Asar.app'),
arm64AppPath: path.resolve(appsPath, 'Arm64AsarExtraFile.app'),
outAppPath: out,
});
await ensureUniversal(out);
// We have three asars including the arch-agnostic shim
expect(
(await fs.readdir(path.resolve(out, 'Contents', 'Resources'))).filter((p) =>
p.endsWith('asar'),
),
).toEqual(['app.asar', 'app-x64.asar', 'app-arm64.asar']);
}, 60000);
it.todo('should correctly merge two non-identical asars when `mergeASARs` is enabled');

it('should merge two different asars when `mergeASARs` is enabled', async () => {
const out = path.resolve(appsPath, 'TwoAsars.app');
await makeUniversalApp({
x64AppPath: path.resolve(appsPath, 'X64Asar.app'),
arm64AppPath: path.resolve(appsPath, 'Arm64AsarExtraFile.app'),
outAppPath: out,
mergeASARs: true,
singleArchFiles: 'extra-file.txt',
});
await ensureUniversal(out);
// Only a single merged asar
expect(
(await fs.readdir(path.resolve(out, 'Contents', 'Resources'))).filter((p) =>
p.endsWith('asar'),
),
).toEqual(['app.asar']);
}, 60000);

it('throws an error if `mergeASARs` is enabled and `singleArchFiles` is missing a unique file', async () => {
const out = path.resolve(appsPath, 'TwoAsars.app');
await expect(
makeUniversalApp({
x64AppPath: path.resolve(appsPath, 'X64Asar.app'),
arm64AppPath: path.resolve(appsPath, 'Arm64AsarExtraFile.app'),
outAppPath: out,
mergeASARs: true,
singleArchFiles: 'bad-rule',
}),
).rejects.toThrow(/Detected unique file "extra-file\.txt"/);
}, 60000);

it.todo('should not inject ElectronAsarIntegrity into `infoPlistsToIgnore`');
});

Expand All @@ -52,16 +140,34 @@ describe('makeUniversalApp', () => {
const out = path.resolve(appsPath, 'MergedNoAsar.app');
await makeUniversalApp({
x64AppPath: path.resolve(appsPath, 'X64NoAsar.app'),
arm64AppPath: path.resolve(appsPath, 'NoAsar.app'),
arm64AppPath: path.resolve(appsPath, 'Arm64NoAsar.app'),
outAppPath: out,
});
await ensureUniversal(out);
// Only a single app folder as they were identical
expect(
(await fs.readdir(path.resolve(out, 'Contents', 'Resources'))).filter((p) => p === 'app'),
(await fs.readdir(path.resolve(out, 'Contents', 'Resources'))).filter((p) =>
p.startsWith('app'),
),
).toEqual(['app']);
}, 60000);
it.todo('should create two separate non-identical app folders');

// getting some kind of mach-o error right now!
it.skip('should create two separate non-identical app folders', async () => {
const out = path.resolve(appsPath, 'TwoFolders.app');
await makeUniversalApp({
x64AppPath: path.resolve(appsPath, 'X64NoAsar.app'),
arm64AppPath: path.resolve(appsPath, 'Arm64NoAsarExtraFile.app'),
outAppPath: out,
});
await ensureUniversal(out);
// We have three folders including the arch-agnostic shim
expect(
(await fs.readdir(path.resolve(out, 'Contents', 'Resources'))).filter((p) =>
p.startsWith('app'),
),
).toEqual(['app', 'app-x64', 'app-arm64']);
}, 60000);
});

// TODO: Add tests for
Expand Down

0 comments on commit ad07ffc

Please sign in to comment.