Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
171 lines (124 loc) · 3.71 KB
/
Copy pathindex.js
File metadata and controls
171 lines (124 loc) · 3.71 KB
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
const fs = require('fs')
const path = require('path')
const bareBuild = require('bare-build')
const { find } = require('bare-device')
const TAPResult = require('./lib/tap')
const platforms = {
darwin: require('./lib/platform/darwin'),
ios: require('./lib/platform/ios'),
android: require('./lib/platform/android'),
linux: require('./lib/platform/linux'),
win32: require('./lib/platform/win32')
}
module.exports = async function run(entry, opts = {}) {
const {
cwd = path.resolve('.'),
device: query = null,
runtime = 'bare-native/runtime',
name = 'Tests',
identifier = 'to.holepunch.bare.native.test',
timeout = 5 * 60 * 1000
} = opts
const out = path.resolve(cwd, opts.out || path.join('build', 'test'))
const device = await find({ platform: opts.platform, name: query })
const platform = platforms[device.platform]
if (platform === undefined) throw new Error(`Unsupported platform '${device.platform}'`)
const app = await build(generate(entry, platform, cwd, out), {
app: platform.app(out, name),
name,
identifier,
host: device.host,
runtime,
cwd,
out
})
if (app === null) return 1
await device.install(app)
const result = await report(await device.launch(app, { args: platform.args }), platform, timeout)
return result.ok ? 0 : 1
}
function generate(entry, platform, cwd, out) {
fs.mkdirSync(out, { recursive: true })
const wrapper = path.join(out, 'entry.js')
let source = platform.setup
source += `\nrequire(${JSON.stringify(relative(out, path.resolve(cwd, entry)))})\n`
fs.writeFileSync(wrapper, source)
return wrapper
}
// A specifier is separated by `/` on every platform.
function relative(from, to) {
const result = path.relative(from, to).split(path.sep).join('/')
return result.startsWith('.') ? result : './' + result
}
async function build(entry, opts) {
const { app, name, identifier, host, runtime, cwd, out } = opts
fs.rmSync(app, { recursive: true, force: true })
try {
for await (const _ of bareBuild(entry, {
base: cwd,
name,
identifier,
hosts: [host],
runtime,
out
})) {
//
}
} catch (err) {
console.log('Bail out! The test app did not build')
comment(err.stack)
return null
}
return app
}
// The run is judged by its TAP output, never by how the app exits.
async function report(app, platform, timeout) {
const result = new TAPResult()
let finish
const finished = new Promise((resolve) => {
finish = resolve
})
const ended = lines(app.stdout, (line) => {
console.log(line)
result.push(line)
if (result.done) finish()
})
lines(app.stderr, (line) => {
if (!platform.ignore.some((pattern) => pattern.test(line))) comment(line)
})
Promise.all([app.exited, ended]).then(finish)
// Each case has a timeout of its own, so this only catches a stalled run.
const timer = setTimeout(() => {
console.log(`Bail out! Timed out after ${timeout} ms`)
result.bailed = true
finish()
}, timeout)
await finished
clearTimeout(timer)
if (!result.done) {
console.log('Bail out! The app stopped before the end of its plan')
result.bailed = true
}
await app.close()
return result
}
function lines(stream, online) {
return new Promise((resolve) => {
let buffered = ''
stream
.on('data', (data) => {
const parts = (buffered + data.toString()).split(/\r?\n/)
buffered = parts.pop()
for (const line of parts) online(line)
})
.on('end', () => {
if (buffered !== '') online(buffered)
resolve()
})
})
}
function comment(output) {
for (const line of output.split('\n')) {
if (line !== '') console.log('# ' + line)
}
}