|
1 | 1 | import * as fs from 'fs-extra'; |
2 | 2 | import * as os from 'os'; |
3 | 3 | import * as path from 'path'; |
| 4 | +import asar from '@electron/asar'; |
4 | 5 |
|
5 | 6 | import { Fiddle, FiddleFactory } from '../src/index'; |
6 | 7 |
|
@@ -35,6 +36,9 @@ describe('FiddleFactory', () => { |
35 | 36 | const dirname = path.dirname(fiddle!.mainPath); |
36 | 37 | expect(dirname).not.toEqual(sourceDir); |
37 | 38 |
|
| 39 | + // test that main.js file is created (not app.asar) |
| 40 | + expect(path.basename(fiddle!.mainPath)).toBe('main.js'); |
| 41 | + |
38 | 42 | // test that the fiddle is kept in the fiddle cache |
39 | 43 | expect(path.dirname(dirname)).toBe(fiddleDir); |
40 | 44 |
|
@@ -93,6 +97,41 @@ describe('FiddleFactory', () => { |
93 | 97 | expect(fiddle).toBe(fiddleIn); |
94 | 98 | }); |
95 | 99 |
|
| 100 | + it('packages fiddle into ASAR archive', async () => { |
| 101 | + const sourceDir = fiddleFixture('642fa8daaebea6044c9079e3f8a46390'); |
| 102 | + const fiddle = await fiddleFactory.create(sourceDir, { |
| 103 | + packAsAsar: true, |
| 104 | + }); |
| 105 | + |
| 106 | + function normalizeAsarFiles(files: string[]): string[] { |
| 107 | + return files.map( |
| 108 | + (f) => f.replace(/^\//, ''), // Remove leading slash |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + // test that app.asar file is created |
| 113 | + expect(fiddle).toBeTruthy(); |
| 114 | + expect(path.basename(fiddle!.mainPath)).toBe('app.asar'); |
| 115 | + |
| 116 | + // test that the file list is identical |
| 117 | + const dirname: string = fiddle!.mainPath; |
| 118 | + const sourceFiles = fs.readdirSync(sourceDir); |
| 119 | + const asarFiles = normalizeAsarFiles( |
| 120 | + asar.listPackage(dirname, { isPack: false }), |
| 121 | + ); |
| 122 | + expect(asarFiles).toStrictEqual(sourceFiles); |
| 123 | + |
| 124 | + // test that the files' contents are identical |
| 125 | + for (const file of sourceFiles) { |
| 126 | + const sourceFileContent = fs.readFileSync( |
| 127 | + path.join(sourceDir, file), |
| 128 | + 'utf-8', |
| 129 | + ); |
| 130 | + const asarFileContent = asar.extractFile(dirname, file).toString(); |
| 131 | + expect(asarFileContent).toStrictEqual(sourceFileContent); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
96 | 135 | it.todo('reads fiddles from git repositories'); |
97 | 136 | it.todo('refreshes the cache if given a previously-cached git repository'); |
98 | 137 |
|
|
0 commit comments