/
basic.js
331 lines (273 loc) · 14.5 KB
/
basic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
'use strict'
const common = require('../src/common')
const download = require('../src/download')
const fs = require('fs-extra')
const { getHostArch } = require('@electron/get')
const packager = require('..')
const path = require('path')
const sinon = require('sinon')
const test = require('ava')
const util = require('./_util')
// Generates a path to the generated app that reflects the name given in the options.
// Returns the Helper.app location on darwin since the top-level .app is already tested for the
// resources path; on other OSes, returns the executable.
function generateNamePath (opts) {
if (common.isPlatformMac(opts.platform)) {
return path.join(`${opts.name}.app`, 'Contents', 'Frameworks', `${opts.name} Helper.app`)
}
return opts.name + (opts.platform === 'win32' ? '.exe' : '')
}
test('setting the quiet option does not print messages', t => {
util.setupConsoleWarnSpy()
sinon.spy(console, 'error')
common.warning('warning', true)
t.true(console.warn.notCalled, 'quieted common.warning should not call console.warn')
common.info('info', true)
t.true(console.error.notCalled, 'quieted common.info should not call console.error')
})
test('download argument: download.{arch,platform,version,artifactName} does not overwrite {arch,platform,version,artifactName}', t => {
const opts = {
download: {
arch: 'ia32',
platform: 'win32',
version: '0.30.0',
artifactName: 'ffmpeg'
},
electronVersion: '0.36.0'
}
const downloadOpts = download.createDownloadOpts(opts, 'linux', 'x64')
t.deepEqual(downloadOpts, { arch: 'x64', platform: 'linux', version: '0.36.0', artifactName: 'electron' })
})
test('sanitize app name for use in file/directory names', t => {
t.is(common.sanitizeAppName('@username/package'), '@username-package', 'slash should be replaced')
})
test('sanitize app name for use in the out directory name', t => {
const opts = {
arch: 'x64',
name: '@username/package-name',
platform: 'linux'
}
t.is(common.generateFinalBasename(opts), '@username-package-name-linux-x64', 'generateFinalBasename output should be sanitized')
})
test('cannot build apps where the name ends in " Helper"', async t => {
const opts = {
arch: 'x64',
dir: util.fixtureSubdir('basic'),
name: 'Bad Helper',
platform: 'linux'
}
await t.throwsAsync(async () => packager(opts), { message: 'Application names cannot end in " Helper" due to limitations on macOS' })
})
test('deprecatedParameter moves value in deprecated param to new param if new param is not set', (t) => {
const opts = {
old: 'value'
}
common.deprecatedParameter(opts, 'old', 'new', 'new-value')
t.false(Object.prototype.hasOwnProperty.call(opts, 'old'), 'old property is not set')
t.true(Object.prototype.hasOwnProperty.call(opts, 'new'), 'new property is set')
opts.not_overwritten_old = 'another'
common.deprecatedParameter(opts, 'not_overwritten_old', 'new', 'new-value')
t.false(Object.prototype.hasOwnProperty.call(opts, 'not_overwritten_old'), 'not_overwritten_old property is not set')
t.true(Object.prototype.hasOwnProperty.call(opts, 'new'), 'new property is set')
t.is(opts.new, 'value', 'new property is not overwritten')
})
test.serial('defaults', util.testSinglePlatform(async (t, opts) => {
opts.name = 'defaultsTest'
opts.dir = util.fixtureSubdir('basic')
delete opts.platform
delete opts.arch
const defaultOpts = {
arch: getHostArch(),
name: opts.name,
platform: process.platform
}
const paths = await packager(opts)
t.true(Array.isArray(paths), 'packager call should resolve to an array')
t.is(paths.length, 1, 'Single-target run should resolve to a 1-item array')
const finalPath = paths[0]
t.is(finalPath, path.join(t.context.workDir, common.generateFinalBasename(defaultOpts)),
'Path should follow the expected format and be in the cwd')
await util.assertDirectory(t, finalPath, 'The expected output directory should exist')
const resourcesPath = path.join(finalPath, util.generateResourcesPath(defaultOpts))
const appPath = path.join(finalPath, generateNamePath(defaultOpts))
if (common.isPlatformMac(defaultOpts.platform)) {
await util.assertDirectory(t, appPath, 'The Helper.app should reflect opts.name')
} else {
await util.assertFile(t, appPath, 'The executable should reflect opts.name')
}
await util.assertDirectory(t, resourcesPath, 'The output directory should contain the expected resources subdirectory')
await util.assertPathNotExists(t, path.join(resourcesPath, 'app', 'node_modules', 'run-waterfall'), 'The output directory should NOT contain devDependencies by default (prune=true)')
await util.assertFilesEqual(t, path.join(opts.dir, 'main.js'), path.join(resourcesPath, 'app', 'main.js'), 'File under packaged app directory should match source file')
await util.assertFilesEqual(t, path.join(opts.dir, 'ignore', 'this.txt'), path.join(resourcesPath, 'app', 'ignore', 'this.txt'), 'File under subdirectory of packaged app directory should match source file and not be ignored by default')
await util.assertPathNotExists(t, path.join(resourcesPath, 'default_app'), 'The output directory should not contain the Electron default_app directory')
await util.assertPathNotExists(t, path.join(resourcesPath, 'default_app.asar'), 'The output directory should not contain the Electron default_app.asar file')
}))
test.serial('out', util.testSinglePlatform(async (t, opts) => {
opts.name = 'outTest'
opts.dir = util.fixtureSubdir('basic')
opts.out = 'dist'
const finalPath = (await packager(opts))[0]
t.is(finalPath, path.join('dist', common.generateFinalBasename(opts)),
'Path should follow the expected format and be under the folder specified in `out`')
await util.assertDirectory(t, finalPath, 'The expected output directory should exist')
await util.assertDirectory(t, path.join(finalPath, util.generateResourcesPath(opts)), 'The output directory should contain the expected resources subdirectory')
}))
test.serial('overwrite', util.testSinglePlatform(async (t, opts) => {
opts.name = 'overwriteTest'
opts.dir = util.fixtureSubdir('basic')
const finalPath = (await packager(opts))[0]
await util.assertDirectory(t, finalPath, 'The expected output directory should exist')
// Create a dummy file to detect whether the output directory is replaced in subsequent runs
const testPath = path.join(finalPath, 'test.txt')
await fs.writeFile(testPath, 'test')
await packager(opts) // Run again, defaulting to overwrite false
await util.assertFile(t, testPath, 'The existing output directory should exist as before (skipped by default)')
// Run a third time, explicitly setting overwrite to true
await packager({ ...opts, overwrite: true })
await util.assertPathNotExists(t, testPath, 'The output directory should be regenerated when overwrite is true')
}))
test.serial('overwrite sans platform/arch set', util.testSinglePlatform(async (t, opts) => {
delete opts.platform
delete opts.arch
opts.dir = util.fixtureSubdir('basic')
opts.overwrite = true
const roundOnePaths = await packager(opts)
await util.assertPathExists(t, roundOnePaths[0], 'The output directory exists')
const roundTwoPaths = await packager(opts)
await util.assertPathExists(t, roundTwoPaths[0], 'The output directory exists')
}))
test.serial('tmpdir', util.testSinglePlatform(async (t, opts) => {
opts.name = 'tmpdirTest'
opts.dir = path.join(__dirname, 'fixtures', 'basic')
opts.out = 'dist'
opts.tmpdir = path.join(t.context.workDir, 'tmp')
await packager(opts)
await util.assertDirectory(t, path.join(opts.tmpdir, 'electron-packager'), 'The expected temp directory should exist')
}))
test.serial('tmpdir disabled', util.testSinglePlatform(async (t, opts) => {
opts.name = 'disableTmpdirTest'
opts.dir = util.fixtureSubdir('basic')
opts.out = 'dist'
opts.tmpdir = false
const finalPath = (await packager(opts))[0]
await util.assertDirectory(t, finalPath, 'The expected out directory should exist')
}))
test.serial('deref symlink', util.testSinglePlatform(async (t, opts) => {
opts.name = 'disableSymlinkDerefTest'
opts.dir = util.fixtureSubdir('basic')
opts.derefSymlinks = false
const src = path.join(opts.dir, 'main.js')
const dest = path.join(opts.dir, 'main-link.js')
await fs.ensureSymlink(src, dest)
const finalPath = (await packager(opts))[0]
const destLink = path.join(finalPath, 'resources', 'app', 'main-link.js')
await util.assertSymlink(t, destLink, 'The expected file should still be a symlink')
await fs.remove(dest)
}))
async function createExtraResourceStringTest (t, opts, platform) {
const extra1Base = 'data1.txt'
const extra1Path = path.join(__dirname, 'fixtures', extra1Base)
opts.name = 'extraResourceStringTest'
opts.dir = util.fixtureSubdir('basic')
opts.out = 'dist'
opts.platform = platform
opts.extraResource = extra1Path
const resourcesPath = await util.packageAndEnsureResourcesPath(t, opts)
await util.assertFilesEqual(t, extra1Path, path.join(resourcesPath, extra1Base), 'resource file data1.txt should match')
}
async function createExtraResourceArrayTest (t, opts, platform) {
const extra1Base = 'data1.txt'
const extra1Path = path.join(__dirname, 'fixtures', extra1Base)
const extra2Base = 'extrainfo.plist'
const extra2Path = path.join(__dirname, 'fixtures', extra2Base)
opts.name = 'extraResourceArrayTest'
opts.dir = util.fixtureSubdir('basic')
opts.out = 'dist'
opts.platform = platform
opts.extraResource = [extra1Path, extra2Path]
const resourcesPath = await util.packageAndEnsureResourcesPath(t, opts)
const extra1DistPath = path.join(resourcesPath, extra1Base)
const extra2DistPath = path.join(resourcesPath, extra2Base)
await util.assertPathExists(t, extra1DistPath, 'resource file data1.txt exists')
await util.assertFilesEqual(t, extra1Path, extra1DistPath, 'resource file data1.txt should match')
await util.assertPathExists(t, extra2DistPath, 'resource file extrainfo.plist exists')
await util.assertFilesEqual(t, extra2Path, extra2DistPath, 'resource file extrainfo.plist should match')
}
for (const platform of ['darwin', 'linux']) {
test.serial(`extraResource: string (${platform})`, util.testSinglePlatform(createExtraResourceStringTest, platform))
test.serial(`extraResource: array (${platform})`, util.testSinglePlatform(createExtraResourceArrayTest, platform))
}
test.serial('building for Linux target sanitizes binary name', util.testSinglePlatform(async (t, opts) => {
opts.name = '@username/package-name'
opts.dir = util.fixtureSubdir('basic')
const paths = await packager(opts)
t.is(paths.length, 1, '1 bundle created')
await util.assertFile(t, path.join(paths[0], '@username-package-name'), 'The sanitized binary filename should exist')
}))
test.serial('executableName honored when building for Linux target', util.testSinglePlatform(async (t, opts) => {
opts.name = 'PackageName'
opts.executableName = 'my-package'
opts.dir = util.fixtureSubdir('basic')
const paths = await packager(opts)
t.is(paths.length, 1, '1 bundle created')
await util.assertFile(t, path.join(paths[0], 'my-package'), 'The executableName-based filename should exist')
}))
test('fails with invalid version', util.invalidOptionTest({
name: 'invalidElectronTest',
electronVersion: '0.0.1',
arch: 'x64',
platform: 'linux',
download: {
quiet: !!process.env.CI
}
}))
test.serial('dir: relative path', util.testSinglePlatform(async (t, opts) => {
opts.name = 'ElectronTest'
opts.dir = path.join('..', 'fixtures', 'basic')
const finalPath = (await packager(opts))[0]
t.is(path.join(t.context.workDir, 'ElectronTest-linux-x64'), finalPath, 'paths returned')
}))
test.serial('electronZipDir success', util.testSinglePlatform(async (t, opts) => {
const customDir = path.join(t.context.tempDir, 'download')
opts.dir = util.fixtureSubdir('basic')
opts.electronZipDir = customDir
await fs.ensureDir(customDir)
const zipPath = await download.downloadElectronZip(download.createDownloadOpts(opts, 'linux', 'x64'))
await fs.copy(zipPath, path.join(customDir, path.basename(zipPath)))
const paths = await packager(opts)
t.is(paths.length, 1, '1 bundle created')
}))
test.serial('electronZipDir does not exist', util.testSinglePlatform(async (t, opts) => {
const customDir = path.join(t.context.tempDir, 'does-not-exist')
opts.dir = util.fixtureSubdir('basic')
opts.electronZipDir = customDir
await t.throwsAsync(async () => packager(opts), { message: /Electron ZIP directory does not exist/ })
}))
test.serial('electronZipDir: ZIP file does not exist', util.testSinglePlatform(async (t, opts) => {
const customDir = path.join(t.context.tempDir, 'download')
opts.dir = util.fixtureSubdir('basic')
opts.electronZipDir = customDir
await fs.ensureDir(customDir)
await t.throwsAsync(async () => packager(opts), { message: /Electron ZIP file does not exist/ })
}))
test('validateElectronApp succeeds on a well-formed Electron app containing a main field', async t => {
await t.notThrowsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-success-with-main')))
})
test('validateElectronApp succeeds on a well-formed Electron app without a main field', async t => {
await t.notThrowsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-success-without-main')))
})
test('validateElectronApp fails on an Electron app without package.json', async t => {
await t.throwsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-failure-without-package-json')), {
message: `Application manifest was not found. Make sure "${path.join('original-dir', 'package.json')}" exists and does not get ignored by your ignore option`
})
})
test('validateElectronApp fails on an Electron app with a package.json with a main field missing main entry point', async t => {
await t.throwsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-failure-without-main-or-index')), {
message: `The main entry point to your app was not found. Make sure "${path.join('original-dir', 'index.js')}" exists and does not get ignored by your ignore option`
})
})
test('validateElectronApp fails on an Electron app with a package.json without a main field missing main entry point', async t => {
await t.throwsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-failure-with-main-without-entry-point')), {
message: `The main entry point to your app was not found. Make sure "${path.join('original-dir', 'main.js')}" exists and does not get ignored by your ignore option`
})
})