-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
107 lines (87 loc) · 3.18 KB
/
plugin.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
const chalk = require('chalk');
const marked = require('marked');
const Test = require('./src/test');
const Queue = require('./src/queue');
let config;
let queue;
let score;
exports.onHandleTag = function(ev) {
ev.data.tag
.filter(tag => !!tag.description)
.map(({ longname, lineNumber, description }) => {
const renderer = new marked.Renderer();
renderer.code = (code, level) => {
const [ ,lang, options] = level.match(/(\w+)((?:#.+(?:=.*?)?)*)/);
if (['js', 'javascript'].indexOf(lang.toLowerCase()) > -1) {
const parts = longname.split('~');
const [file, item] = parts;
queue.push(new Test(
file,
item,
lineNumber,
code,
config,
(options || '')
.split('#')
.filter(value => !!value)
.reduce((prev, value) => {
const parts = value.split('=');
return Object.assign({}, prev, { [parts[0]]: parts.length > 1 ? parts[1] : true });
}, {})
));
}
};
return { description, renderer };
})
.forEach(({ description, renderer }) => {
marked(description, { renderer });
});
console.log('test: Running tests found in documentation');
queue.run(
(file) => { console.log(chalk.black(` ${file}`)); },
(item, line, file) => { console.log(`${chalk.black(` ${item}`)} ${chalk.gray(`${file}:${line}`)}`); },
(test, index) => {
if (test.options.skip) {
console.log(chalk.yellow(` #${++index} - SKIPPED`));
score.skipped = score.skipped + 1;
} else {
const result = test.run();
if (result instanceof Error) {
console.log(chalk.red(` #${++index} - ${result}`));
score.failure = score.failure + 1;
} else {
console.log(chalk.green(` #${++index} - OK`));
score.success = score.success + 1;
}
}
}
);
const bg = score.failure > 0 ? 'bgRed' : 'bgGreen';
const fg = score.failure > 0 ? 'white' : 'black';
console.log(chalk.bold[bg][fg](` ${score.failure > 0 ? 'Failure' : 'Success'}: ${score.success + score.failure + score.skipped} tests, ${score.success} success, ${score.failure} failures, ${score.skipped} skipped. `));
if (config.exitOnFailure) {
process.exit(1);
}
};
exports.onStart = ev => {
config = Object.assign(
{},
{
assert: 'node',
exitOnFailure: false,
exitWithFailure: true
},
ev.data.option
);
queue = new Queue();
score = {
success: 0,
failure: 0,
skipped: 0,
};
};
exports.onComplete = ev => {
if (config.exitWithFailure && score.failure > 0) {
process.exit(1);
}
};