-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
report-test-status.js
176 lines (157 loc) · 4.7 KB
/
report-test-status.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const argv = require('minimist')(process.argv.slice(2));
const log = require('fancy-log');
const requestPromise = require('request-promise');
const {cyan, green, yellow} = require('ansi-colors');
const {gitCommitHash} = require('../git');
const {travisJobUrl, isTravisPullRequestBuild} = require('../travis');
const reportBaseUrl = 'https://amp-test-status-bot.appspot.com/v0/tests';
const IS_GULP_INTEGRATION = argv._[0] === 'integration';
const IS_GULP_UNIT = argv._[0] === 'unit';
const IS_GULP_E2E = argv._[0] === 'e2e';
const IS_LOCAL_CHANGES = !!argv.local_changes;
const IS_SAUCELABS = !!argv.saucelabs;
const IS_SAUCELABS_STABLE = !!argv.saucelabs && !!argv.stable;
const IS_SAUCELABS_BETA = !!argv.saucelabs && !!argv.beta;
const IS_SINGLE_PASS = !!argv.single_pass;
const TEST_TYPE_SUBTYPES = new Map([
[
'integration',
['local', 'single-pass', 'saucelabs-beta', 'saucelabs-stable'],
],
['unit', ['local', 'local-changes', 'saucelabs']],
['e2e', ['local']],
]);
const TEST_TYPE_BUILD_TARGETS = new Map([
['integration', ['RUNTIME', 'FLAG_CONFIG', 'INTEGRATION_TEST']],
['unit', ['RUNTIME', 'UNIT_TEST']],
['e2e', ['RUNTIME', 'FLAG_CONFIG', 'E2E_TEST']],
]);
function inferTestType() {
if (IS_GULP_E2E) {
return 'e2e/local';
}
let type;
if (IS_GULP_UNIT) {
type = 'unit';
} else if (IS_GULP_INTEGRATION) {
type = 'integration';
} else {
return null;
}
if (IS_LOCAL_CHANGES) {
return `${type}/local-changes`;
}
if (IS_SAUCELABS_BETA) {
return `${type}/saucelabs-beta`;
} else if (IS_SAUCELABS_STABLE) {
return `${type}/saucelabs-stable`;
} else if (IS_SAUCELABS) {
return `${type}/saucelabs`;
}
if (IS_SINGLE_PASS) {
return `${type}/single-pass`;
}
return `${type}/local`;
}
function postReport(type, action) {
if (type !== null && isTravisPullRequestBuild()) {
const commitHash = gitCommitHash();
return requestPromise({
method: 'POST',
uri: `${reportBaseUrl}/${commitHash}/${type}/${action}`,
body: JSON.stringify({
travisJobUrl: travisJobUrl(),
}),
headers: {
'Content-Type': 'application/json',
},
// Do not use `json: true` because the response is a string, not JSON.
})
.then(body => {
log(
green('INFO:'),
'reported',
cyan(`${type}/${action}`),
'to the test-status GitHub App'
);
if (body.length > 0) {
log(
green('INFO:'),
'response from test-status was',
cyan(body.substr(0, 100))
);
}
})
.catch(error => {
log(
yellow('WARNING:'),
'failed to report',
cyan(`${type}/${action}`),
'to the test-status GitHub App:\n',
error.message.substr(0, 100)
);
return;
});
}
return Promise.resolve();
}
function reportTestErrored() {
return postReport(inferTestType(), 'report/errored');
}
function reportTestFinished(success, failed) {
return postReport(inferTestType(), `report/${success}/${failed}`);
}
function reportTestSkipped() {
return postReport(inferTestType(), 'skipped');
}
function reportTestStarted() {
return postReport(inferTestType(), 'started');
}
async function reportAllExpectedTests(buildTargets) {
for (const [type, subTypes] of TEST_TYPE_SUBTYPES) {
const testTypeBuildTargets = TEST_TYPE_BUILD_TARGETS.get(type);
const action = testTypeBuildTargets.some(target => buildTargets.has(target))
? 'queued'
: 'skipped';
for (const subType of subTypes) {
await postReport(`${type}/${subType}`, action);
}
}
}
/**
* Callback to the Karma.Server on('run_complete') event for simple test types.
*
* @param {!any} browsers
* @param {!Karma.TestResults} results
*/
function reportTestRunComplete(browsers, results) {
if (results.error) {
reportTestErrored();
} else {
reportTestFinished(results.success, results.failed);
}
}
module.exports = {
reportAllExpectedTests,
reportTestErrored,
reportTestFinished,
reportTestRunComplete,
reportTestSkipped,
reportTestStarted,
};