-
Notifications
You must be signed in to change notification settings - Fork 280
/
Dredd.js
259 lines (237 loc) · 7.5 KB
/
Dredd.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import async from 'async';
import parse from 'dredd-transactions/parse';
import compile from 'dredd-transactions/compile';
import configureReporters from './configureReporters';
import resolveLocations from './resolveLocations';
import readLocation from './readLocation';
import resolveModule from './resolveModule';
import logger from './logger';
import TransactionRunner from './TransactionRunner';
import { applyConfiguration } from './configuration';
import annotationToLoggerInfo from './annotationToLoggerInfo';
function prefixError(error, prefix) {
error.message = `${prefix}: ${error.message}`;
return error;
}
function prefixErrors(decoratedCallback, prefix) {
return (error, ...args) => {
if (error) {
prefixError(error, prefix);
}
decoratedCallback(error, ...args);
};
}
function readLocations(locations, options, callback) {
const usesOptions = typeof options !== 'function';
const resolvedOptions = usesOptions ? options : {};
const resolvedCallback = usesOptions ? callback : options;
async.map(
locations,
(location, next) => {
const decoratedNext = prefixErrors(
next,
`Unable to load API description document from '${location}'`,
);
readLocation(location, resolvedOptions, decoratedNext);
},
(error, contents) => {
if (error) {
resolvedCallback(error);
return;
}
const apiDescriptions = locations.map((location, i) => ({
location,
content: contents[i],
}));
resolvedCallback(null, apiDescriptions);
},
);
}
function parseContent(apiDescriptions, callback) {
async.map(
apiDescriptions,
({ location, content }, next) => {
const decoratedNext = prefixErrors(
next,
`Unable to parse API description document '${location}'`,
);
parse(content, decoratedNext);
},
(error, parseResults) => {
if (error) {
callback(error);
return;
}
const parsedAPIdescriptions = apiDescriptions.map((apiDescription, i) =>
Object.assign({}, parseResults[i], apiDescription),
);
callback(null, parsedAPIdescriptions);
},
);
}
function compileTransactions(apiDescriptions) {
return apiDescriptions
.map(({ mediaType, apiElements, location }) => {
try {
return compile(mediaType, apiElements, location);
} catch (error) {
throw prefixError(
error,
'Unable to compile HTTP transactions from ' +
`API description document '${location}': ${error.message}`,
);
}
})
.map((compileResult, i) =>
Object.assign({}, compileResult, apiDescriptions[i]),
);
}
function toTransactions(apiDescriptions) {
return (
apiDescriptions
// produce an array of transactions for each API description,
// where each transaction object gets an extra 'apiDescription'
// property with details about the API description it comes from
.map((apiDescription) =>
apiDescription.transactions.map((transaction) =>
Object.assign(
{
apiDescription: {
location: apiDescription.location,
mediaType: apiDescription.mediaType,
},
},
transaction,
),
),
)
// flatten array of arrays
.reduce((flatArray, array) => flatArray.concat(array), [])
);
}
function toLoggerInfos(apiDescriptions) {
return apiDescriptions
.map((apiDescription) =>
apiDescription.annotations.map((annotation) =>
annotationToLoggerInfo(apiDescription.location, annotation),
),
)
.reduce(
(flatAnnotations, annotations) => flatAnnotations.concat(annotations),
[],
);
}
class Dredd {
constructor(config) {
this.configuration = applyConfiguration(config);
this.stats = {
tests: 0,
failures: 0,
errors: 0,
passes: 0,
skipped: 0,
start: 0,
end: 0,
duration: 0,
};
this.transactionRunner = new TransactionRunner(this.configuration);
this.logger = logger;
}
prepareAPIdescriptions(callback) {
this.logger.debug('Resolving locations of API description documents');
let locations;
try {
locations = resolveLocations(
this.configuration.custom.cwd,
this.configuration.path,
);
} catch (error) {
process.nextTick(() => callback(error));
return;
}
async.waterfall(
[
(next) => {
this.logger.debug('Reading API description documents');
readLocations(locations, { http: this.configuration.http }, next);
},
(apiDescriptions, next) => {
const allAPIdescriptions = this.configuration.apiDescriptions.concat(
apiDescriptions,
);
this.logger.debug('Parsing API description documents');
parseContent(allAPIdescriptions, next);
},
],
(error, apiDescriptions) => {
if (error) {
callback(error);
return;
}
this.logger.debug(
'Compiling HTTP transactions from API description documents',
);
let apiDescriptionsWithTransactions;
try {
apiDescriptionsWithTransactions = compileTransactions(
apiDescriptions,
);
} catch (compileErr) {
callback(compileErr);
return;
}
callback(null, apiDescriptionsWithTransactions);
},
);
}
run(callback) {
this.logger.debug('Resolving --require');
if (this.configuration.require) {
const requirePath = resolveModule(
this.configuration.custom.cwd,
this.configuration.require,
);
try {
require(requirePath); // eslint-disable-line global-require, import/no-dynamic-require
} catch (error) {
callback(error, this.stats);
return;
}
}
this.logger.debug('Configuring reporters');
configureReporters(this.configuration, this.stats, this.transactionRunner);
// FIXME: 'configureReporters()' pollutes the 'stats' object with
// this property. Which is unfortunate, as the 'stats' object is
// a part of Dredd's public interface. This line cleans it up for now, but
// ideally the property wouldn't be needed at all.
delete this.stats.fileBasedReporters;
this.logger.debug('Preparing API description documents');
this.prepareAPIdescriptions((error, apiDescriptions) => {
if (error) {
callback(error, this.stats);
return;
}
const loggerInfos = toLoggerInfos(apiDescriptions);
// FIXME: Winston 3.x supports calling .log() directly with the loggerInfo
// object as it's sole argument, but that's not the case with Winston 2.x
// Once we upgrade Winston, the line below can be simplified to .log(loggerInfo)
//
// Watch https://github.com/apiaryio/dredd/issues/1225 for updates
loggerInfos.forEach(({ level, message }) =>
this.logger.log(level, message),
);
if (loggerInfos.find((loggerInfo) => loggerInfo.level === 'error')) {
callback(new Error('API description processing error'), this.stats);
return;
}
this.logger.debug('Starting the transaction runner');
this.configuration.apiDescriptions = apiDescriptions;
this.transactionRunner.config(this.configuration);
const transactions = toTransactions(apiDescriptions);
this.transactionRunner.run(transactions, (runError) => {
callback(runError, this.stats);
});
});
}
}
export default Dredd;