-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
65 lines (55 loc) · 2.1 KB
/
server.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
var assert = require('assert')
var child_process = require('child_process')
var execSync = child_process.execSync
var spawn = child_process.spawn
var bracery = require('../index')
var initJson = { abc: 'def',
hello: '[hello|hi]',
world: ['world', 'planet'],
test1: 'testing',
test2: '~TEST1',
test3: 'x~test3',
test4: '"e{~TEST1}' }
var binPath = 'bin/bracery'
var port = 8001
var clientDelay = 250 // number of milliseconds client will wait before attempting to connect
var serverTimeout = 4000 // number of milliseconds after that before test fails
describe('client/server tests (' + binPath + ')', function() {
// client/server test
testServer ('~abc', 'def')
testServer ('&cap&lc&eval~test2', 'Testing')
testServer ('&cap{~test3}', 'Xxx')
})
function makeCmdLine (config, initJson, opts, lhs) {
var cmdline = [process.argv[0],
__dirname + '/../' + binPath]
.concat (config ? ['-c',JSON.stringify(config || {})] : [])
.concat (initJson ? ['-s', JSON.stringify(initJson)] : [])
.concat (opts ? opts : [])
.concat (lhs ? ["'" + lhs + "'"] : [])
return cmdline
}
function execCmd (cmd) {
var text = execSync(cmd.join(' '),{stdio:['pipe','pipe',process.env.TRAVIS ? 'pipe' : 'ignore']}).toString()
text = text.substr (0, text.length - 1) // chop off newline
return text
}
function spawnCmd (cmd) {
return spawn (cmd[0], cmd.slice(1))
}
function testServer (lhs, rhs) {
it('should expand ' + lhs + ' to ' + rhs
+ ' by connecting to a local server',
function (done) {
this.timeout (clientDelay + serverTimeout) // generous timeout for server calls
var serverCmd = makeCmdLine (null, initJson, ['-S', port], null)
var proc = spawnCmd (serverCmd)
setTimeout (function() {
var clientCmd = makeCmdLine (null, null, ['-C', 'http://localhost:' + port, '-e', "'" + lhs + "'"])
var text = execCmd (clientCmd)
proc.kill('SIGINT')
assert.equal (text, rhs)
done()
}, clientDelay)
})
}