UNPKG

4.04 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const Parser = require('../')
4const etoa = require('events-to-array')
5const util = require('util')
6
7const args = process.argv.slice(2)
8let json = null
9let flat = false
10let bail = false
11let preserveWhitespace = true
12let omitVersion = false
13let strict = false
14
15function version () {
16 console.log(require('../package.json').version)
17 process.exit(0)
18}
19
20for (let i = 0; i < args.length; i++) {
21 const arg = args[i]
22 if (arg === '-j') {
23 const val = +args[i + 1]
24 if (val >= 0) {
25 json = val
26 i += 1
27 } else
28 json = 2
29 continue
30 } else {
31 const m = arg.match(/^--json(?:=([0-9]+))?$/)
32 if (m) {
33 if (+m[1] >= 0)
34 json = +m[1]
35 else if (+args[i + 1] >= 0) {
36 json = +args[i + 1]
37 i += 1
38 } else
39 json = 2
40 continue
41 }
42 }
43
44 if (arg === '-v' || arg === '--version')
45 version()
46 else if (arg === '-o' || arg === '--omit-version')
47 omitVersion = true
48 else if (arg === '-w' || arg === '--ignore-all-whitespace')
49 preserveWhitespace = false
50 else if (arg === '-b' || arg === '--bail')
51 bail = true
52 else if (arg === '-B' || arg === '--no-bail')
53 bail = false
54 else if (arg === '-t' || arg === '--tap')
55 json = 'tap'
56 else if (arg === '-l' || arg === '--lines')
57 json = 'lines'
58 else if (arg === '-h' || arg === '--help')
59 usage()
60 else if (arg === '-f' || arg === '--flat')
61 flat = true
62 else if (arg === '-F' || arg === '--no-flat')
63 flat = false
64 else if (arg === '--strict')
65 strict = true
66 else if (arg === '--no-strict')
67 strict = false
68 else if (arg === '-s' || arg === '--silent')
69 json = 'silent'
70 else
71 console.error('Unrecognized arg: %j', arg)
72}
73
74function usage () {
75 console.log(`Usage:
76 tap-parser <options>
77
78Parses TAP data from stdin, and outputs the parsed result
79in the format specified by the options. Default output
80uses node's \`util.inspect()\` method.
81
82Options:
83
84 -j [<indent>] | --json[=indent]
85 Output event data as JSON with the specified indentation (default=2)
86
87 -t | --tap
88 Output data as reconstituted TAP based on parsed results
89
90 -l | --lines
91 Output each parsed line as it is recognized by the parser
92
93 -b | --bail
94 Emit a \`Bail out!\` at the first failed test point encountered
95
96 -B | --no-bail
97 Do not bail out at the first failed test point encountered
98 (Default)
99
100 -f | --flat
101 Flatten all assertions to the top level parser
102
103 -F | --no-flat
104 Do not flatten all assertions to the top level parser
105 (Default)
106
107 -w | --ignore-all-whitespace
108 Skip over blank lines outside of YAML blocks
109
110 -o | --omit-version
111 Ignore the \`TAP version 13\` line at the start of tests
112
113 --strict
114 Run the parser in strict mode
115
116 --no-strict
117 Do not run the parser in strict mode
118
119 -s | --silent
120 Do not print output, just exit success/failure based on TAP stream
121`)
122
123 // prevent the EPIPE upstream when the data drops on the floor
124 /* istanbul ignore else */
125 if (!process.stdin.isTTY)
126 process.stdin.resume()
127
128 process.exit()
129}
130
131const yaml = require('tap-yaml')
132
133function format (msg) {
134 if (json === 'tap')
135 return Parser.stringify(msg, options)
136 else if (json !== null)
137 return JSON.stringify(msg, null, +json)
138 else
139 return util.inspect(msg, null, Infinity)
140}
141
142const options = {
143 bail: bail,
144 preserveWhitespace: preserveWhitespace,
145 omitVersion: omitVersion,
146 strict: strict,
147 flat: flat,
148}
149
150if (json === 'lines' || json === 'silent') {
151 const parser = new Parser(options)
152 if (json === 'lines')
153 parser.on('line', l => process.stdout.write(l))
154 parser.on('complete', () => process.exit(parser.ok ? 0 : 1))
155 process.stdin.pipe(parser)
156} else {
157 const input = []
158 process.stdin.on('data', c => input.push(c)).on('end', () => {
159 const buf = Buffer.concat(input)
160 const result = Parser.parse(buf, options)
161 const summary = result[ result.length - 1 ]
162 console.log(format(result))
163 if (summary[0] !== 'complete' || !summary[1].ok)
164 process.exit(1)
165 })
166}