forked from jhs/couchjs-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·266 lines (206 loc) · 6.31 KB
/
cli.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env node
//
// couchjs client
//
// Copyright 2011 Iris Couch
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var fs = require('fs')
var util = require('util')
var optimist = require('optimist')
var child_process = require('child_process')
var client = require('./client')
var LineStream = require('couchjs/stream')
var opts = optimist.usage('$0 </path/to/couchjs> </path/to/main.js>')
.describe('confirm=<path>', 'Path to a log file to test')
.describe('verbose', 'Display messages being passed')
.boolean('verbose')
function toSource() {
if(typeof this == 'function')
return '' + this
if(this instanceof Error)
return this.stack
return util.inspect(this)
}
function main() {
var couchjs_filename = opts.argv._[0]
, main_js = opts.argv._[1]
if(opts.argv.help)
return console.log(opts.help())
if(!main_js || !couchjs_filename)
return console.error(opts.help())
console.log('%j %j', couchjs_filename, main_js)
var child = child_process.spawn(couchjs_filename, [main_js], {'stdio':'pipe'})
child.on('exit', function(code) {
console.log('couchjs %s exit: %s', child.pid, code)
})
child.stderr.on('data', function(body) {
console.error('couchjs %s error: %s', child.pid, body)
})
// Break messages from the child into lines.
child.stdout_lines = new LineStream
//child.stdout_lines.on('data', child_line)
child.stdout.setEncoding('utf8')
child.stdout.pipe(child.stdout_lines)
child.stdout.pause()
//function child_line(line) {
// console.log('couchjs %s: %s', child.pid, line)
//}
if(opts.argv.confirm)
return confirm_log(child, opts.argv.confirm, function(er) {
if(er)
throw er
})
console.log('Done')
child.stdin.write('[]\n')
}
function confirm_log(couchjs, log_filename, callback) {
var log = new LineStream
log.on('data', log_line)
couchjs.stdout_lines.on('data', couchjs_line)
log.on('end', log_end)
couchjs.stdout_lines.on('end', couchjs_end)
var log_data = fs.createReadStream(log_filename)
log_data.setEncoding('utf8')
var expect = 'STDIN'
, io_re = /^(STDIN|STDOUT) (\d+): (.*)$/
, to_couchjs = []
, from_couchjs = []
, from_log = []
, pass = {}
, fail = {}
var start = new Date
, stop = null
couchjs.stdout.resume()
log_data.pipe(log)
function log_line(line) {
var match = line.match(io_re)
if(!match)
return
var direction = match[1]
, message = match[3]
if(direction !== expect) {
couchjs.kill()
return callback(new Error('Expected direction '+expect+' but got ' + JSON.stringify(direction) + ': ' + line))
}
//console.log('Log %s: %j', direction, message)
if(direction === 'STDIN') {
expect = 'STDOUT'
try {
message = JSON.parse(message)
} catch (er) {
return callback(er)
}
if(opts.argv.verbose)
console.log('Send: %j', message)
to_couchjs.push(message)
couchjs.stdin.write(JSON.stringify(message) + '\n')
} else if(direction === 'STDOUT') {
expect = 'STDIN'
from_log.push(message)
check()
}
}
function couchjs_line(line) {
if(opts.argv.verbose)
console.log('Recv: %j', line)
from_couchjs.push(line)
check()
}
function check() {
if(from_couchjs.length === 0 || from_log.length === 0 || to_couchjs.length === 0)
return
var expect_line = from_log.shift()
, couchjs_line = from_couchjs.shift()
, sent_message = to_couchjs.shift()
try {
var expect = JSON.parse(expect_line)
, couchjs = JSON.parse(couchjs_line)
} catch (er) {
return callback(er)
}
var is_same = is_deep_equal(expect, couchjs)
if(is_same)
return add_pass(sent_message)
else
return add_fail(message, expect, couchjs)
}
function add_pass(message) {
var method = message[0]
pass[method] = pass[method] || 0
pass[method] += 1
}
function add_fail(message, expect, couchjs) {
}
var ended = {'log':false, 'couchjs':false}
function log_end() {
couchjs.stdin.end()
ended.log = true
wrapup()
}
function couchjs_end() {
stop = new Date
ended.couchjs = true
wrapup()
}
function wrapup() {
if(!ended.log || !ended.couchjs)
return
var ms = stop - start
, elapsed_s = ms / 1000
, elapsed = (ms > 1000)
? (ms / 1000).toFixed(2) + ' s'
: ms + ' ms'
console.log('Finished in %s s: %s', elapsed, log_filename)
console.log('Passes:')
for (var method in pass)
console.log(' %s: %s', method, pass[method])
console.log('Fails:')
for (var method in fail)
console.log(' %s: %s', method, fail[method])
console.log('')
}
}
function is_deep_equal(expect, obj) {
var expect_type = typeof expect
, obj_type = typeof obj
if(expect_type !== obj_type) console.log('## Bad type')
if(expect_type !== obj_type)
return false
if(Array.isArray(obj)) {
if(obj.length !== expect.length) console.log('## Bad array length')
if(obj.length !== expect.length)
return false
for (var i = 0; i < obj.length; i++)
if(! is_deep_equal(expect[i], obj[i])) console.log('## Arrays do not match: %j %j', expect, obj)
if(! is_deep_equal(expect[i], obj[i]))
return false
return true
}
if(!obj_type || !expect)
return expect === obj
if(obj_type === 'object') {
var keys = Object.keys(obj)
if(Object.keys(expect).length !== keys.length)
return false
for (var key in obj)
if(! is_deep_equal(expect[key], obj[key]))
return false
return true
}
if(~['boolean', 'number', 'string'].indexOf(obj_type))
return expect === obj
throw new Error('Unknown type: ' + obj_type)
}
if(require.main === module)
main()