Skip to content

Commit 51c4f02

Browse files
committed
Merge pull request #7 from Hypercubed/feature/markdown
Feature/markdown
2 parents bff696d + d7f2486 commit 51c4f02

File tree

7 files changed

+159
-87
lines changed

7 files changed

+159
-87
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ or in `package.json`
2323
}
2424
```
2525

26+
### CLI Options
27+
28+
```
29+
--no-ansi Disable ANSI formatting
30+
--no-progress Disable progress output during tests
31+
--markdown Format output as markdown
32+
```
33+
2634
## Example
2735

2836
![summary](example/clip.gif)
29-

bin/cmd.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
#!/usr/bin/env node
2+
var minimist = require('minimist')
23

3-
var reporter = require('..')()
4+
var opts = minimist(process.argv.slice(2), {
5+
boolean: true,
6+
alias: {
7+
ansi: 'a',
8+
progress: 'p',
9+
markdown: 'm',
10+
},
11+
default: {
12+
ansi: true,
13+
progress: true,
14+
markdown: false,
15+
},
16+
})
17+
18+
var reporter = require('..')(opts)
419

520
process.stdin
621
.pipe(reporter)

example/run-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ var exec = require('child_process').exec
33
var tapeCmd = require.resolve('../node_modules/task-tape/bin/task-tape')
44
var tapCmd = require.resolve('../bin/cmd')
55
var tests = require.resolve('./test')
6+
var extra = '--no-ansi --no-progress --markdown'
67

7-
exec([tapeCmd, tests, '|', tapCmd].join(' ')).stdout.pipe(process.stdout)
8-
8+
exec([tapeCmd, tests, '|', tapCmd, extra].join(' ')).stdout.pipe(process.stdout)

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
module.exports = require('./lib/summary')
21

2+
module.exports = require('./lib/summary')

lib/no-ansi.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/** Generates a API matching ansi-escape
2+
* but without the escape codes */
3+
4+
var LF = '\n'
5+
6+
var escapes = require('ansi-escape/lib/escapes')
7+
var colors = require('ansi-escape/lib/colors')
8+
9+
var escape = function (x) { return x }
10+
escape.escape = escape;
11+
12+
Object.keys(colors).forEach(function (key) {
13+
escape[key] = escape
14+
})
15+
16+
Object.keys(escapes).forEach(function (key) {
17+
escape[key] = escape
18+
})
19+
20+
escape.eraseLine = {
21+
escape: function (x) { return LF + x }
22+
}
23+
24+
module.exports = escape

lib/summary.js

+107-82
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,53 @@
1-
var format = require('ansi-escape')
1+
var ansi = require('ansi-escape')
2+
var noAnsi = require('./no-ansi');
23
var symbols = require('figures')
34
var prettyMs = require('pretty-ms')
45
var LF = '\n'
56

67
var summarize = require('./summarize')
78

8-
module.exports = function () {
9+
module.exports = function (opts) {
10+
opts = opts || {}
11+
opts.ansi = typeof opts.ansi !== 'undefined' ? opts.ansi : true
12+
opts.progress = typeof opts.progress !== 'undefined' ? opts.progress : true
13+
opts.markdown = typeof opts.markdown !== 'undefined' ? opts.markdown : false
14+
15+
var format = opts.ansi ? ansi : noAnsi
16+
var splitter = opts.markdown ? mdSplitter : barSplitter
17+
18+
var INDENT = opts.markdown ? repeat(' ', 4) : ''
19+
var LIST = opts.markdown ? '- ' : ''
20+
921
var summary = summarize()
1022
var output = summary.output
1123

1224
output.push(LF + splitter(' Tests '))
1325

1426
summary.on('test.start', function (test) {
1527
Object.defineProperty(test, 'title', { get: getTitle })
16-
output.push(LF + format.cha.eraseLine.escape('# ' + test.title))
1728
})
1829

1930
summary.on('test.end', function (test) {
2031
if (test.fail) {
21-
output.push(format.cha.red.eraseLine.escape(symbols.cross + ' ' + test.title))
32+
output.push(format.cha.red.eraseLine.escape(INDENT + symbols.cross + ' ' + test.title))
2233
} else {
23-
output.push(format.cha.green.eraseLine.escape(symbols.tick + ' ' + test.title))
34+
output.push(format.cha.green.eraseLine.escape(INDENT + symbols.tick + ' ' + test.title))
2435
}
2536
})
2637

27-
summary.on('test.pass', function (test) {
28-
output.push(format.cha.eraseLine.escape('# ' + test.title))
29-
})
38+
if (opts.progress) {
39+
summary.on('test.start', function (test) {
40+
output.push(LF + format.cha.eraseLine.escape(INDENT + '# ' + test.title))
41+
})
3042

31-
summary.on('test.fail', function (test) {
32-
output.push(format.cha.eraseLine.escape('# ' + test.title))
33-
})
43+
summary.on('test.pass', function (test) {
44+
output.push(format.cha.eraseLine.escape(INDENT + '# ' + test.title))
45+
})
46+
47+
summary.on('test.fail', function (test) {
48+
output.push(format.cha.eraseLine.escape(INDENT + '# ' + test.title))
49+
})
50+
}
3451

3552
summary.on('summary', function (sum, fails, comments) {
3653
output.push(formatSummary(sum))
@@ -46,86 +63,94 @@ module.exports = function () {
4663
})
4764

4865
return summary
49-
}
5066

51-
function getTitle() {
52-
return this.name + ' [' +
53-
'pass: ' + this.pass + ', fail: ' + this.fail +
54-
(this.duration ? ', duration: ' + prettyMs(this.duration) : '') +
55-
']'
56-
}
67+
function getTitle() {
68+
return this.name + ' [' +
69+
'pass: ' + this.pass + ', fail: ' + this.fail +
70+
(this.duration ? ', duration: ' + prettyMs(this.duration) : '') +
71+
']'
72+
}
5773

58-
function formatSummary(res) {
59-
var output = [LF]
60-
output.push(splitter(' Summary '))
61-
output.push(format.cyan.escape('duration: ' + prettyMs(res.duration)))
62-
output.push(format.cyan.escape('assertions: ' + res.assertions))
63-
if (res.pass) {
64-
output.push(format.green.escape('pass: ' + res.pass))
65-
} else {
66-
output.push(format.cyan.escape('pass: ' + res.pass))
74+
function formatSummary(res) {
75+
var output = [LF]
76+
output.push(splitter(' Summary '))
77+
output.push(format.cyan.escape(LIST + 'duration: ' + prettyMs(res.duration)))
78+
output.push(format.cyan.escape(LIST + 'assertions: ' + res.assertions))
79+
if (res.pass) {
80+
output.push(format.green.escape(LIST + 'pass: ' + res.pass))
81+
} else {
82+
output.push(format.cyan.escape(LIST + 'pass: ' + res.pass))
83+
}
84+
if (res.fail) {
85+
output.push(format.red.escape(LIST + 'fail: ' + res.fail))
86+
} else {
87+
output.push(format.cyan.escape(LIST + 'fail: ' + res.fail))
88+
}
89+
return output.join(LF)
6790
}
68-
if (res.fail) {
69-
output.push(format.red.escape('fail: ' + res.fail))
70-
} else {
71-
output.push(format.cyan.escape('fail: ' + res.fail))
91+
92+
function formatComment(comments) {
93+
var output = [LF]
94+
output.push(splitter(' Comments '))
95+
output.push(Object.keys(comments).map(function (name) {
96+
return format.cyan.underline.escape(name) + LF + comments[name].join(LF)
97+
}).join(LF + LF))
98+
return output.join(LF)
7299
}
73-
return output.join(LF)
74-
}
75100

76-
function formatComment(comments) {
77-
var output = [LF]
78-
output.push(splitter(' Comments '))
79-
output.push(Object.keys(comments).map(function (name) {
80-
return format.cyan.underline.escape(name) + LF + comments[name].join(LF)
81-
}).join(LF + LF))
82-
return output.join(LF)
83-
}
101+
function barSplitter(s) {
102+
var len = s && s.length || 0
103+
var max = 80
104+
var left = max - len >> 1
105+
return format.yellow.escape(
106+
repeat('-', left) + (s || '') + repeat('-', max - len - left)
107+
)
108+
}
84109

85-
function splitter(s) {
86-
var len = s && s.length || 0
87-
var max = 80
88-
var left = max - len >> 1
89-
return format.yellow.escape(
90-
repeat('-', left) + (s || '') + repeat('-', max - len - left)
91-
)
92-
}
110+
function mdSplitter(s, left) {
111+
left = arguments.length > 1 ? left : 1
112+
return format.yellow.escape(
113+
repeat('#', left) + (s || '') + LF
114+
)
115+
}
93116

94-
function repeat(str, n) {
95-
if (str.repeat) {
96-
return str.repeat(n)
117+
function repeat(str, n) {
118+
if (str.repeat) {
119+
return str.repeat(n)
120+
}
121+
return (new Array(n + 1)).join(str)
97122
}
98-
return (new Array(n + 1)).join(str)
99-
}
100123

101-
function formatFail(fail) {
102-
var output = [LF]
103-
output.push(splitter(' Fails '))
104-
output.push(
105-
Object.keys(fail).map(function (name) {
106-
var res = [format.cyan.underline.escape('# ' + name)]
107-
fail[name].forEach(function (assertion) {
108-
res.push(format.red.escape(' ' + symbols.cross + ' ' + assertion.name))
109-
res.push(prettifyError(assertion))
110-
})
111-
return res.join(LF)
112-
}).join(LF + LF)
113-
)
114-
115-
return output.join(LF)
116-
}
124+
function formatFail(fail) {
125+
var output = [LF]
126+
output.push(splitter(' Fails '))
127+
output.push(
128+
Object.keys(fail).map(function (name) {
129+
var res = [format.cyan.underline.escape('# ' + name)]
130+
fail[name].forEach(function (assertion) {
131+
res.push(format.red.escape(INDENT + ' ' + symbols.cross + ' ' + assertion.name))
132+
res.push(prettifyError(assertion))
133+
})
134+
return res.join(LF)
135+
}).join(LF + LF)
136+
)
137+
138+
return output.join(LF)
139+
}
117140

118-
function prettifyError(assertion) {
119-
var rawError = assertion.error.raw
120-
var ret = rawError.split(LF)
121-
var stack = assertion.error.stack
122-
if (stack) {
123-
stack = stack.split(LF)
124-
var padding = repeat(' ', ret[ret.length - 1].length)
125-
ret = ret.concat(stack.map(function (s) {
126-
return padding + s
127-
}))
141+
function prettifyError(assertion) {
142+
var rawError = assertion.error.raw
143+
var ret = rawError.split(LF).map(function (s) {
144+
return INDENT + s
145+
})
146+
var stack = assertion.error.stack
147+
if (stack) {
148+
stack = stack.split(LF)
149+
var padding = repeat(' ', ret[ret.length - 1].length)
150+
ret = ret.concat(stack.map(function (s) {
151+
return padding + s
152+
}))
153+
}
154+
return format.cyan.escape(ret.join(LF))
128155
}
129-
return format.cyan.escape(ret.join(LF))
130156
}
131-

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"ansi-escape": "^1.0.1",
2929
"duplexer2": "^0.1.4",
3030
"figures": "^1.4.0",
31+
"minimist": "^1.2.0",
3132
"pretty-ms": "^2.1.0",
3233
"tap-out": "^1.4.1"
3334
},

0 commit comments

Comments
 (0)