-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: bump minimum node version to 16 and add tests (#86)
* build: add tests * build: bump deps * sigh * build: install rosetta on m1
- Loading branch information
1 parent
02119d5
commit 8e2842b
Showing
18 changed files
with
3,622 additions
and
1,463 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ dist | |
entry-asar/*.js* | ||
entry-asar/*.ts | ||
*.app | ||
test/fixtures/apps | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
transform: { | ||
'.': [ | ||
'ts-jest', | ||
{ | ||
tsconfig: 'tsconfig.jest.json' | ||
} | ||
] | ||
}, | ||
globalSetup: './jest.setup.ts' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { downloadArtifact } from '@electron/get'; | ||
import * as zip from 'cross-zip'; | ||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
const asarsDir = path.resolve(__dirname, 'test', 'fixtures', 'asars'); | ||
const appsDir = path.resolve(__dirname, 'test', 'fixtures', 'apps'); | ||
|
||
const templateApp = async ( | ||
name: string, | ||
arch: string, | ||
modify: (appPath: string) => Promise<void>, | ||
) => { | ||
const electronZip = await downloadArtifact({ | ||
artifactName: 'electron', | ||
version: '27.0.0', | ||
platform: 'darwin', | ||
arch, | ||
}); | ||
const appPath = path.resolve(appsDir, name); | ||
zip.unzipSync(electronZip, appsDir); | ||
await fs.rename(path.resolve(appsDir, 'Electron.app'), appPath); | ||
await fs.remove(path.resolve(appPath, 'Contents', 'Resources', 'default_app.asar')); | ||
await modify(appPath); | ||
}; | ||
|
||
export default async () => { | ||
await fs.remove(appsDir); | ||
await fs.mkdirp(appsDir); | ||
await templateApp('Asar.app', 'arm64', async (appPath) => { | ||
await fs.copy( | ||
path.resolve(asarsDir, 'app.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 fs.copy( | ||
path.resolve(asarsDir, 'app'), | ||
path.resolve(appPath, 'Contents', 'Resources', 'app'), | ||
); | ||
}); | ||
|
||
await templateApp('X64NoAsar.app', 'x64', async (appPath) => { | ||
await fs.copy( | ||
path.resolve(asarsDir, 'app'), | ||
path.resolve(appPath, 'Contents', 'Resources', 'app'), | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
import * as fs from 'fs-extra'; | ||
import * as crypto from 'crypto'; | ||
import { pipeline } from 'stream/promises'; | ||
|
||
import { d } from './debug'; | ||
|
||
export const sha = async (filePath: string) => { | ||
d('hashing', filePath); | ||
const hash = crypto.createHash('sha256'); | ||
hash.setEncoding('hex'); | ||
const fileStream = fs.createReadStream(filePath); | ||
fileStream.pipe(hash); | ||
await new Promise((resolve, reject) => { | ||
fileStream.on('end', () => resolve()); | ||
fileStream.on('error', (err) => reject(err)); | ||
}); | ||
await pipeline(fs.createReadStream(filePath), hash); | ||
return hash.read(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as path from 'path'; | ||
|
||
import { AsarMode, detectAsarMode, generateAsarIntegrity } from '../src/asar-utils'; | ||
|
||
const asarsPath = path.resolve(__dirname, 'fixtures', 'asars'); | ||
const appsPath = path.resolve(__dirname, 'fixtures', 'apps'); | ||
|
||
describe('asar-utils', () => { | ||
describe('detectAsarMode', () => { | ||
it('should correctly detect an asar enabled app', async () => { | ||
expect(await detectAsarMode(path.resolve(appsPath, 'Asar.app'))).toBe(AsarMode.HAS_ASAR); | ||
}); | ||
|
||
it('should correctly detect an app without an asar', async () => { | ||
expect(await detectAsarMode(path.resolve(appsPath, 'NoAsar.app'))).toBe(AsarMode.NO_ASAR); | ||
}); | ||
}); | ||
|
||
describe('generateAsarIntegrity', () => { | ||
it('should deterministically hash an asar header', async () => { | ||
expect(generateAsarIntegrity(path.resolve(asarsPath, 'app.asar')).hash).toEqual( | ||
'85fff474383bd8df11cd9c5784e8fcd1525af71ff140a8a882e1dc9d5b39fcbf', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as path from 'path'; | ||
|
||
import { AppFile, AppFileType, getAllAppFiles } from '../src/file-utils'; | ||
|
||
const appsPath = path.resolve(__dirname, 'fixtures', 'apps'); | ||
|
||
describe('file-utils', () => { | ||
describe('getAllAppFiles', () => { | ||
let asarFiles: AppFile[]; | ||
let noAsarFiles: AppFile[]; | ||
|
||
beforeAll(async () => { | ||
asarFiles = await getAllAppFiles(path.resolve(appsPath, 'Asar.app')); | ||
noAsarFiles = await getAllAppFiles(path.resolve(appsPath, 'NoAsar.app')); | ||
}); | ||
|
||
it('should correctly identify plist files', async () => { | ||
expect(asarFiles.find((f) => f.relativePath === 'Contents/Info.plist')?.type).toBe( | ||
AppFileType.INFO_PLIST, | ||
); | ||
}); | ||
|
||
it('should correctly identify asar files as app code', async () => { | ||
expect(asarFiles.find((f) => f.relativePath === 'Contents/Resources/app.asar')?.type).toBe( | ||
AppFileType.APP_CODE, | ||
); | ||
}); | ||
|
||
it('should correctly identify non-asar code files as plain text', async () => { | ||
expect( | ||
noAsarFiles.find((f) => f.relativePath === 'Contents/Resources/app/index.js')?.type, | ||
).toBe(AppFileType.PLAIN); | ||
}); | ||
|
||
it('should correctly identify the Electron binary as Mach-O', async () => { | ||
expect(noAsarFiles.find((f) => f.relativePath === 'Contents/MacOS/Electron')?.type).toBe( | ||
AppFileType.MACHO, | ||
); | ||
}); | ||
|
||
it('should correctly identify the Electron Framework as Mach-O', async () => { | ||
expect( | ||
noAsarFiles.find( | ||
(f) => | ||
f.relativePath === | ||
'Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework', | ||
)?.type, | ||
).toBe(AppFileType.MACHO); | ||
}); | ||
|
||
it('should correctly identify the v8 context snapshot', async () => { | ||
expect( | ||
noAsarFiles.find( | ||
(f) => | ||
f.relativePath === | ||
'Contents/Frameworks/Electron Framework.framework/Versions/A/Resources/v8_context_snapshot.arm64.bin', | ||
)?.type, | ||
).toBe(AppFileType.SNAPSHOT); | ||
}); | ||
}); | ||
}); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
console.log('I am an app folder', process.arch); | ||
process.exit(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "app", | ||
"main": "index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello there |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { spawn } from '@malept/cross-spawn-promise'; | ||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
import { makeUniversalApp } from '../src/index'; | ||
|
||
const appsPath = path.resolve(__dirname, 'fixtures', 'apps'); | ||
|
||
async function ensureUniversal(app: string) { | ||
const exe = path.resolve(app, 'Contents', 'MacOS', 'Electron'); | ||
const result = await spawn(exe); | ||
expect(result).toContain('arm64'); | ||
const result2 = await spawn('arch', ['-x86_64', exe]); | ||
expect(result2).toContain('x64'); | ||
} | ||
|
||
describe('makeUniversalApp', () => { | ||
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'), | ||
outAppPath: out, | ||
}); | ||
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); | ||
|
||
// TODO: Add tests for | ||
// * different asar files | ||
// * identical app dirs | ||
// * different app dirs | ||
// * different app dirs with different macho files | ||
// * identical app dirs with universal macho files | ||
}); |
Oops, something went wrong.