forked from nativefier/nativefier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
100 lines (86 loc) · 2.77 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
var fs = require('fs')
var path = require('path')
var test = require('tape')
var download = require('electron-download')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var series = require('run-series')
var ORIGINAL_CWD = process.cwd()
var WORK_CWD = path.join(__dirname, 'work')
var archs = ['ia32', 'x64']
var platforms = ['darwin', 'linux', 'win32']
var slice = Array.prototype.slice
var version = require('./config.json').version
var combinations = []
archs.forEach(function (arch) {
platforms.forEach(function (platform) {
// Electron does not have 32-bit releases for Mac OS X, so skip that combination
// Also skip testing darwin target on Windows since electron-packager itself skips it
// (see https://github.com/maxogden/electron-packager/issues/71)
if (platform === 'darwin' && (arch === 'ia32' || require('os').platform() === 'win32')) return
combinations.push({
arch: arch,
platform: platform,
version: version
})
})
})
exports.areFilesEqual = function areFilesEqual (file1, file2, callback) {
series([
function (cb) {
fs.readFile(file1, cb)
},
function (cb) {
fs.readFile(file2, cb)
}
], function (err, buffers) {
callback(err, slice.call(buffers[0]).every(function (b, i) {
return b === buffers[1][i]
}))
})
}
exports.downloadAll = function downloadAll (version, callback) {
series(combinations.map(function (combination) {
return function (cb) {
download(combination, cb)
}
}), callback)
}
exports.forEachCombination = function forEachCombination (cb) {
combinations.forEach(cb)
}
exports.generateResourcesPath = function generateResourcesPath (opts) {
return opts.platform === 'darwin' ? path.join(opts.name + '.app', 'Contents', 'Resources') : 'resources'
}
exports.getWorkCwd = function getWorkCwd () {
return WORK_CWD
}
// tape doesn't seem to have a provision for before/beforeEach/afterEach/after,
// so run setup/teardown and cleanup tasks as additional "tests" to put them in sequence
// and run them irrespective of test failures
exports.setup = function setup () {
test('setup', function (t) {
mkdirp(WORK_CWD, function (err) {
if (err) t.end(err)
process.chdir(WORK_CWD)
t.end()
})
})
}
exports.teardown = function teardown () {
test('teardown', function (t) {
process.chdir(ORIGINAL_CWD)
rimraf(WORK_CWD, function (err) {
t.end(err)
})
})
}
exports.testAllPlatforms = function testAllPlatforms (name, createTest /*, ...createTestArgs */) {
var args = slice.call(arguments, 2)
exports.setup()
exports.forEachCombination(function (combination) {
test(name + ': ' + combination.platform + '-' + combination.arch,
createTest.apply(null, [combination].concat(args)))
})
exports.teardown()
}