This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tapon.js
119 lines (103 loc) · 3.2 KB
/
tapon.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
const tapParser = require('tap-parser');
const through = require('through2');
const duplexer = require('duplexer');
const format = require('chalk');
const symbols = require('figures');
const fetch = require('node-fetch');
const nullLogger = () => {};
const createAppVeyorLogger = (baseUrl) => {
console.log('Using AppVeyor test logger');
let chain = Promise.resolve();
const translateOutcome = (outcome) => {
switch (outcome) {
case 'passed': return 'Passed';
case 'failed': return 'Failed';
case 'todo': return 'Ignored';
case 'skipped': return 'Skipped';
default: return 'None';
}
};
return (name, outcome, error = null) => {
const parts = name.indexOf('»') > -1 ? name.split('»') : name.split('›');
let file = 'test';
if (parts.length > 1) {
file = parts[0].trim();
name = parts.slice(1).join('›');
}
chain = chain.then(async () => {
try {
const body = {
testName: name,
fileName: file,
testFramework: 'ava',
outcome: translateOutcome(outcome)
};
if (error) {
body.ErrorMessage = error.message;
body.ErrorStackTrace = error.stack;
}
const result = await fetch(`${baseUrl}api/tests`, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
const text = await result.text();
} catch (e) {
}
});
};
}
const logger = process.env.APPVEYOR_API_URL
? createAppVeyorLogger(process.env.APPVEYOR_API_URL)
: nullLogger;
const makeStream = () => {
const output = through();
const parser = tapParser();
const stream = duplexer(parser, output);
const printTest = (symbol, format, name) =>
output.push(format(` ${symbol} ${name}\n`));
parser.on('assert', assert => {
output.push(' ' + symbols.pointer + ' ');
if (assert.skip) {
printTest(symbols.circlePipe, format.dim, assert.name);
logger(assert.name, 'skipped');
} else if (assert.ok) {
printTest(symbols.circleCircle, format.green, assert.name);
logger(assert.name, 'passed');
} else if (assert.todo) {
printTest(symbols.circleQuestionMark, format.yellow, assert.name);
logger(assert.name, 'todo');
} else {
printTest(symbols.circleCross, format.red.bold, assert.name);
const message = `${assert.diag.name}: ${assert.diag.message}`;
const stack = `${message}\n at ${assert.diag.at}`;
logger(assert.name, 'failed', { message, stack });
}
});
parser.on('complete', results => {
output.push('\n\n');
if (results.failures.length > 0) {
results.failures.forEach(failure => {
output.push(' ' + format.red(failure.name) + ':\n');
output.push(` ${failure.diag.name}: ${failure.diag.message}\n`);
output.push(` at ${failure.diag.at}\n\n`);
});
output.push('\n\n');
}
if (!results.ok) {
handler.failed = true;
}
});
return stream;
};
const handler = makeStream();
process.stdin
.pipe(handler)
.pipe(process.stdout);
process.on('exit', status => {
if (status > 0 || handler.failed) {
process.exit(1);
}
});