-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathrc-test.js
152 lines (137 loc) · 4.53 KB
/
rc-test.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
var test = require('tape')
var path = require('path')
var exec = require('child_process').exec
var fs = require('fs')
var tempy = require('tempy') // Locked to 0.2.1 for node 6 support
var cleanEnv = require('./util/clean-env')
test('custom config and aliases', function (t) {
var args = [
'--arch ARCH',
'--platform PLATFORM',
'--download https://foo.bar',
'--debug',
'--version',
'--help',
'--path ../some/other/path',
'--target 1.4.10',
'--runtime electron',
'--libc testlibc',
'--token TOKEN'
]
runRc(t, args.join(' '), {}, function (rc) {
t.equal(rc.arch, 'ARCH', 'correct arch')
t.equal(rc.arch, rc.a, 'arch alias')
t.equal(rc.platform, 'PLATFORM', 'correct platform')
t.equal(rc.download, 'https://foo.bar', 'download is set')
t.equal(rc.download, rc.d, 'download alias')
t.equal(rc.debug, true, 'debug is set')
t.equal(rc.version, true, 'version is set')
t.equal(rc.version, rc.v, 'version alias')
t.equal(rc.help, true, 'help is set')
t.equal(rc.help, rc.h, 'help alias')
t.equal(rc.path, '../some/other/path', 'correct path')
t.equal(rc.path, rc.p, 'path alias')
t.equal(rc.target, '1.4.10', 'correct target')
t.equal(rc.target, rc.t, 'target alias')
t.equal(rc.runtime, 'electron', 'correct runtime')
t.equal(rc.runtime, rc.r, 'runtime alias')
t.equal(rc.libc, 'testlibc', 'libc family')
t.equal(rc.abi, '50', 'correct ABI')
t.equal(rc.token, 'TOKEN', 'correct token')
t.equal(rc['tag-prefix'], 'v', 'correct default tag prefix')
t.end()
})
})
// TODO: merge into above test
test('npm args are passed on from npm environment into rc', function (t) {
var args = [
'--build-from-source',
'--download',
'https://foo.bar',
'--debug',
'--verbose'
].join(' ')
runRc(t, args, {}, function (rc) {
t.equal(rc.buildFromSource, true, 'buildFromSource should be true')
t.equal(rc.debug, true, 'debug should be true')
t.equal(rc.verbose, true, 'verbose should be true')
t.equal(rc.download, 'https://foo.bar', 'download is set')
t.end()
})
})
test('npm_config_* are passed on from environment into rc', function (t) {
var env = {
// Note that these are validated by npm
npm_config_proxy: 'http://localhost/',
npm_config_https_proxy: 'https://localhost/',
npm_config_local_address: '127.0.0.1',
npm_config_target: '1.4.0',
npm_config_runtime: 'electron',
npm_config_platform: 'PLATFORM',
npm_config_build_from_source: 'true'
}
runRc(t, '', env, function (rc) {
t.equal(rc.proxy, 'http://localhost/', 'proxy is set')
t.equal(rc['https-proxy'], 'https://localhost/', 'https-proxy is set')
t.equal(rc['local-address'], '127.0.0.1', 'local-address is set')
t.equal(rc.target, '1.4.0', 'target is set')
t.equal(rc.runtime, 'electron', 'runtime is set')
t.equal(rc.platform, 'PLATFORM', 'platform is set')
t.equal(rc.buildFromSource, true, 'build-from-source is set')
t.end()
})
})
test('can pass in external package config to rc', function (t) {
var pkg = {
config: {
target: '1.0.0',
runtime: 'electron',
arch: 'woohoo-arch'
}
}
var rc = require('../rc')(pkg)
t.equal(rc.target, '1.0.0', 'correct target')
t.equal(rc.runtime, 'electron', 'correct runtime')
t.equal(rc.arch, 'woohoo-arch', 'correct arch')
t.end()
})
test('use default ABI', function (t) {
runRc(t, '', {}, function (rc) {
t.equal(rc.abi, process.versions.modules, 'correct default ABI')
t.end()
})
})
test('using --tag-prefix will set the tag prefix', function (t) {
var args = ['--tag-prefix @scoped/package@']
runRc(t, args.join(' '), {}, function (rc) {
t.equal(rc['tag-prefix'], '@scoped/package@', 'tag prefix should be set')
t.end()
})
})
function runRc (t, args, env, cb) {
var pkg = {
name: 'test',
private: true,
scripts: {
install: 'node ' + path.resolve(__dirname, '..', 'rc.js') + ' ' + args
}
}
var tmp = tempy.directory()
var json = JSON.stringify(pkg)
fs.writeFile(path.join(tmp, 'package.json'), json, function (err) {
if (err) throw err
var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'
var cmd = npm + ' run install'
env = Object.assign(cleanEnv(process.env), env)
exec(cmd, { env, cwd: tmp }, function (err, stdout, stderr) {
t.error(err, 'no error')
t.equal(stderr.trim(), '', 'no stderr')
try {
var result = JSON.parse(stdout.slice(stdout.indexOf('{')))
} catch (e) {
return t.fail(e)
}
cb(result)
})
})
}