Skip to content

Commit ef3279e

Browse files
committed
package: swap out tape for custom tap and add -o flag
Semver: minor Fixes: #3
1 parent 8dd826d commit ef3279e

File tree

4 files changed

+160
-34
lines changed

4 files changed

+160
-34
lines changed

bin/cmd.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33
'use strict'
44

55
const exec = require('child_process').exec
6+
const fs = require('fs')
67
const http = require('http')
78
const https = require('https')
89
const url = require('url')
910
const nopt = require('nopt')
10-
11+
const path = require('path')
1112
const pretty = require('../lib/format-pretty')
12-
const tap = require('../lib/format-tap')
13+
const formatTap = require('../lib/format-tap')
1314
const Validator = require('../lib')
14-
15+
const Tap = require('../lib/tap')
1516
const knownOpts = { help: Boolean
1617
, version: Boolean
1718
, 'validate-metadata': Boolean
1819
, tap: Boolean
20+
, out: path
1921
}
2022
const shortHand = { h: ['--help']
2123
, v: ['--version']
2224
, V: ['--validate-metadata']
2325
, t: ['--tap']
26+
, o: ['--out']
2427
}
2528

2629
const parsed = nopt(knownOpts, shortHand)
@@ -77,14 +80,28 @@ function loadPatch(uri, cb) {
7780
const v = new Validator(parsed)
7881

7982
if (parsed.tap) {
80-
var errCount = 0
83+
const stream = parsed.out
84+
? fs.createWriteStream(parsed.out)
85+
: process.stdout
86+
87+
const tap = new Tap(stream)
88+
var isEnding = false
89+
8190
v.on('commit', (c) => {
82-
errCount += c.commit.errors
83-
tap(c.commit, c.messages, v)
91+
const test = tap.test(c.commit.sha)
92+
formatTap(test, c.commit, c.messages, v)
93+
if (isEnding) {
94+
setImmediate(() => {
95+
tap.end()
96+
if (tap.status === 'fail')
97+
process.exitCode = 1
98+
})
99+
}
84100
})
85101

86102
function run() {
87103
if (!args.length) {
104+
isEnding = true
88105
return
89106
}
90107
const sha = args.shift()

lib/format-tap.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
'use strict'
22

3-
const tap = require('tape')
4-
5-
module.exports = function formatTap(context, msgs, validator) {
6-
tap.test(context.sha, (t) => {
7-
t.plan(msgs.length)
8-
for (const m of msgs) {
9-
switch (m.level) {
10-
case 'pass':
11-
const a = m.string
12-
? ` [${m.string}]`
13-
: ''
14-
t.pass(`${m.id}: ${m.message}${a}`)
15-
break
16-
case 'skip':
17-
t.skip(`${m.id}: ${m.message}`)
18-
break
19-
case 'fail':
20-
onFail(context, m, validator, t)
21-
break
22-
}
3+
module.exports = function formatTap(t, context, msgs, validator) {
4+
for (const m of msgs) {
5+
switch (m.level) {
6+
case 'pass':
7+
const a = m.string
8+
? ` [${m.string}]`
9+
: ''
10+
t.pass(`${m.id}: ${m.message}${a}`)
11+
break
12+
case 'skip':
13+
t.skip(`${m.id}: ${m.message}`)
14+
break
15+
case 'fail':
16+
onFail(context, m, validator, t)
17+
break
2318
}
24-
})
19+
}
2520
}
2621

2722
function onFail(context, m, validator, t) {
@@ -50,8 +45,7 @@ function lengthFail(context, m, validator, t) {
5045
, at: {
5146
line: m.line || 0
5247
, column: m.column || 0
53-
, file: body
54-
, function: ''
48+
, body: body
5549
}
5650
})
5751
}
@@ -61,7 +55,11 @@ function subsystemFail(context, m, validator, t) {
6155
found: m.string
6256
, compare: 'indexOf() !== -1'
6357
, wanted: m.wanted || ''
64-
, at: `${m.line || 0}:${m.column || 0}`
58+
, at: {
59+
line: m.line || 0
60+
, column: m.column || 0
61+
, body: m.title
62+
}
6563
})
6664
}
6765

@@ -73,8 +71,7 @@ function defaultFail(context, m, validator, t) {
7371
, at: {
7472
line: m.line || 0
7573
, column: m.column || 0
76-
, file: context.body
77-
, function: ''
74+
, body: context.body
7875
}
7976
})
8077
}

lib/tap.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict'
2+
3+
const util = require('util')
4+
5+
class Test {
6+
constructor(tap, name) {
7+
this.tap = tap
8+
this.name = name
9+
this._count = 0
10+
this._passes = 0
11+
this._failures = 0
12+
this._skips = 0
13+
this.begin()
14+
}
15+
16+
pass(msg) {
17+
this.tap.write(`ok ${++this._count} ${msg}`)
18+
this._passes++
19+
this.tap.pass()
20+
}
21+
22+
fail(msg, extra) {
23+
this.tap.write(`not ok ${++this._count} ${msg}`)
24+
if (extra) {
25+
this.tap.write(' ---')
26+
if (typeof extra === 'object') {
27+
extra = util.inspect(extra)
28+
}
29+
30+
if (typeof extra === 'string') {
31+
extra.split(/\n/).forEach((item) => {
32+
this.tap.write(` ${item}`)
33+
})
34+
}
35+
this.tap.write(' ...')
36+
}
37+
this._failures++
38+
this.tap.fail()
39+
}
40+
41+
skip(msg) {
42+
this.tap.write(`ok ${++this._count} ${msg} # SKIP`)
43+
this._skips++
44+
this.tap.skip()
45+
}
46+
47+
begin() {
48+
this.tap.write(`# ${this.name}`)
49+
}
50+
}
51+
52+
module.exports = class Tap {
53+
constructor(stream) {
54+
this.stream = stream
55+
this._wroteVersion = false
56+
this._count = 0
57+
this._passes = 0
58+
this._failures = 0
59+
this._skips = 0
60+
}
61+
62+
get status() {
63+
if (this._failures) {
64+
return 'fail'
65+
}
66+
67+
return 'pass'
68+
}
69+
70+
writeVersion() {
71+
if (!this._wroteVersion) {
72+
this.write('TAP version 13')
73+
this._wroteVersion = true
74+
}
75+
}
76+
77+
pass() {
78+
this._passes++
79+
this._count++
80+
}
81+
82+
fail() {
83+
this._failures++
84+
this._count++
85+
}
86+
87+
skip() {
88+
this._skips++
89+
this._count++
90+
}
91+
92+
write(str = '') {
93+
this.stream.write(`${str}\n`)
94+
}
95+
96+
test(name) {
97+
const t = new Test(this, name)
98+
return t
99+
}
100+
101+
end() {
102+
this.write()
103+
this.write(`0..${this._count}`)
104+
this.write(`# tests ${this._count}`)
105+
if (this._passes) {
106+
this.write(`# pass ${this._passes}`)
107+
}
108+
109+
if (this._failures) {
110+
this.write(`# fail ${this._failures}`)
111+
}
112+
}
113+
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"chalk": "~1.1.1",
1212
"gitlint-parser-node": "^1.0.1",
1313
"help": "^2.1.3",
14-
"nopt": "^3.0.6",
15-
"tape": "^4.6.0"
14+
"nopt": "^3.0.6"
1615
},
1716
"devDependencies": {
1817
"lintit": "~1.0.1",

0 commit comments

Comments
 (0)