/
_util.js
143 lines (133 loc) · 4.94 KB
/
_util.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
'use strict'
// Keeping this module because it handles non-buffers gracefully
const bufferEqual = require('buffer-equal')
const common = require('../common')
const config = require('./config.json')
const fs = require('fs-extra')
const packager = require('../index')
const path = require('path')
const plist = require('plist')
const setup = require('./_setup')
const sinon = require('sinon')
const tempy = require('tempy')
const test = require('ava')
const ORIGINAL_CWD = process.cwd()
test.before(t => {
if (!process.env.CI) {
return setup.setupTestsuite()
.then(() => process.chdir(setup.WORK_CWD))
}
return Promise.resolve(process.chdir(setup.WORK_CWD))
})
test.after.always(t => {
process.chdir(ORIGINAL_CWD)
return fs.remove(setup.WORK_CWD)
})
test.beforeEach(t => {
t.context.workDir = tempy.directory()
t.context.tempDir = tempy.directory()
if (!console.warn.restore) {
sinon.spy(console, 'warn')
}
})
test.afterEach.always(t => {
if (console.warn.restore) {
console.warn.restore()
}
return fs.remove(t.context.workDir)
.then(() => fs.remove(t.context.tempDir))
})
function testSinglePlatform (name, testFunction, testFunctionArgs, parallel) {
module.exports.packagerTest(name, (t, opts) => {
Object.assign(opts, module.exports.singlePlatformOptions())
return testFunction.apply(null, [t, opts].concat(testFunctionArgs))
}, parallel)
}
module.exports = {
allPlatformArchCombosCount: 9,
assertDirectory: function assertDirectory (t, pathToCheck, message) {
return fs.stat(pathToCheck)
.then(stats => t.true(stats.isDirectory(), message))
},
assertFile: function assertFile (t, pathToCheck, message) {
return fs.stat(pathToCheck)
.then(stats => t.true(stats.isFile(), message))
},
assertFilesEqual: function assertFilesEqual (t, file1, file2, message) {
return Promise.all([fs.readFile(file1), fs.readFile(file2)])
.then(([buffer1, buffer2]) => bufferEqual(buffer1, buffer2))
.then(equal => t.true(equal, message))
},
assertPathExistsCustom: function assertPathExistsCustom (t, pathToCheck, exists, message) {
return fs.pathExists(pathToCheck)
.then(result => t.is(exists, result, message))
},
assertPathExists: function assertPathExists (t, pathToCheck, message) {
return module.exports.assertPathExistsCustom(t, pathToCheck, true, message)
},
assertPathNotExists: function assertPathNotExists (t, pathToCheck, message) {
return module.exports.assertPathExistsCustom(t, pathToCheck, false, message)
},
assertSymlink: function assertFile (t, pathToCheck, message) {
return fs.lstat(pathToCheck)
.then(stats => t.true(stats.isSymbolicLink(), message))
},
assertWarning: function assertWarning (t, message) {
t.true(console.warn.calledWithExactly(message), `console.warn should be called with: ${message}`)
},
fixtureSubdir: setup.fixtureSubdir,
generateResourcesPath: function generateResourcesPath (opts) {
return common.isPlatformMac(opts.platform)
? path.join(opts.name + '.app', 'Contents', 'Resources')
: 'resources'
},
invalidOptionTest: function invalidOptionTest (opts, err, message) {
return t => t.throws(packager(opts), err, message)
},
packageAndEnsureResourcesPath: function packageAndEnsureResourcesPath (t, opts) {
let resourcesPath
return packager(opts)
.then(paths => {
resourcesPath = path.join(paths[0], module.exports.generateResourcesPath(opts))
return module.exports.assertDirectory(t, resourcesPath, 'The output directory should contain the expected resources subdirectory')
}).then(() => resourcesPath)
},
packagerTest: function packagerTest (name, testFunction, parallel) {
const testDefinition = parallel ? test : test.serial
testDefinition(name, t => {
return testFunction(t, {
name: 'packagerTest',
out: t.context.workDir,
tmpdir: t.context.tempDir
})
})
},
parsePlist: function parsePlist (t, appPath) {
const plistPath = path.join(appPath, 'Contents', 'Info.plist')
return module.exports.assertFile(t, plistPath, `The expected Info.plist should exist in ${path.basename(appPath)}`)
.then(() => fs.readFile(plistPath, 'utf8'))
.then(file => plist.parse(file))
},
singlePlatformOptions: function singlePlatformOptions () {
return {
platform: 'linux',
arch: 'x64',
electronVersion: config.version
}
},
testSinglePlatform: function (name, testFunction, ...testFunctionArgs) {
return testSinglePlatform(name, testFunction, testFunctionArgs, false)
},
testSinglePlatformParallel: function (name, testFunction, ...testFunctionArgs) {
return testSinglePlatform(name, testFunction, testFunctionArgs, true)
},
verifyPackageExistence: function verifyPackageExistence (finalPaths) {
return Promise.all(finalPaths.map(finalPath => {
return fs.stat(finalPath)
.then(
stats => stats.isDirectory(),
() => false
)
}))
}
}