forked from fewlines-education/camp2_exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customTesting.js
119 lines (95 loc) · 3.55 KB
/
customTesting.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 { exec } = require('child_process');
const { Socket } = require('phoenix-channels');
const path = require('path');
const util = require('util');
const execAsPromise = util.promisify(exec);
const { SPARTA_GITHUB_WEBHOOK, SPARTA_API_KEY } = process.env;
const PAYLOADS = {
testsuiteStarted: (build_number, timestamp) => ({
build_number,
timestamp
}),
testsuiteFinished: (build_number, timestamp) => ({
build_number,
timestamp
}),
startTest: (build_number, timestamp, exercise) => ({
build_number,
timestamp,
exercise
}),
testFailed: (build_number, timestamp, exercise, stack_trace) => ({
build_number,
timestamp,
exercise,
stack_trace
}),
testPassed: (build_number, timestamp, exercise) => ({
build_number,
timestamp,
exercise
})
};
const buildNumber = timestamp => timestamp.toString().substr(timestamp.toString().length - 10);
const getTimestamp = () => Date.now();
const isSet = variable => variable !== undefined && variable !== null && variable !== '';
const formatExerciseName = (pathName) =>
execAsPromise('git rev-parse --show-toplevel')
.then(({stdout: rootDir}) => {
const relativePath = path.relative(rootDir.trim(), pathName);
return relativePath.replace(/((.+)\/\.?__tests__\/(.*?)(\.test)?(\.js)|(.*?)(\.test)?(\.js))/, "$2/$3$5$6$8");
})
if (isSet(SPARTA_GITHUB_WEBHOOK) && isSet(SPARTA_API_KEY)) {
const socket = new Socket(SPARTA_GITHUB_WEBHOOK, { params: { api_key: SPARTA_API_KEY } });
socket.connect();
const channel = socket.channel('progress', {});
channel.join();
const onSuccess = value => {
const timestamp = getTimestamp();
formatExerciseName(value.testResults[0].name)
.then(exerciseName =>
channel.push('test_passed', PAYLOADS.testPassed(buildNumber(timestamp), timestamp, exerciseName)));
return value;
};
const onFailure = error => {
const timestamp = getTimestamp();
const value = JSON.parse(error.stdout);
formatExerciseName(value.testResults[0].name)
.then(exerciseName =>
channel.push('test_failed', PAYLOADS.testFailed(buildNumber(timestamp),
timestamp,
exerciseName,
value.testResults[0].message)));
return JSON.parse(error.stdout);
};
const reflect = promise => promise.then(onSuccess, onFailure);
async function listFiles() {
const { stdout, stderr } = await execAsPromise('jest --listTests --json');
return JSON.parse(stdout);
}
async function runTest(path) {
const timestamp = getTimestamp();
channel.push('test_started', PAYLOADS.startTest(buildNumber(timestamp), timestamp, path));
const { stdout, stderr } = await execAsPromise(`jest --json ${path}`);
return JSON.parse(stdout);
}
async function startTestsuite() {
const timestamp = getTimestamp();
channel.push('testsuite_started', PAYLOADS.testsuiteStarted(buildNumber(timestamp), timestamp));
const files = await listFiles();
const tests = files.map(async file => await runTest(file));
return Promise.all(tests.map(reflect));
}
startTestsuite()
.then(results => {
const timestamp = getTimestamp();
channel.push('testuite_finished', PAYLOADS.testsuiteFinished(buildNumber(timestamp), timestamp));
process.exit();
})
.catch(error => {
console.log('error', error);
process.exit();
});
} else {
console.log('error', 'Sparta URL or/and Sparta API key is/are missing');
}