-
Notifications
You must be signed in to change notification settings - Fork 15
/
suite.js
173 lines (150 loc) · 4.96 KB
/
suite.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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const _ = require('lodash')
const a = require('async')
const EventEmitter = require('events').EventEmitter
const path = require('path')
const util = require('util')
const testUtil = require('../util')
const packager = require('./packager')
const Test = require('./test')
function Suite(testFolders, opts) {
this.testFolders = testFolders
this.pkgsMeta = {}
this.opts = _.extend(
{},
{
limit: 1,
versions: 'minor',
testPatterns: [],
globalSamples: null,
matrixCountOnly: false
},
opts
)
this.failures = []
}
util.inherits(Suite, EventEmitter)
/**
* Builds the metadata for every package in the appropriate test folders.
* This will iterate over every test folder, get the package.json,
* iterate over every tests array and find semver ranges,
* latest flag and static versions for every unique package.
*/
Suite.prototype.prepare = function prepare() {
this.testFolders.forEach((folder) => {
const testPackage = require(path.join(folder, 'package'))
if (testPackage.tests) {
testPackage.tests.forEach((test) => {
const dependencies = Object.keys(test.dependencies)
dependencies.forEach((dep) => {
const versions = getPkgVersions(test.dependencies[dep])
this._buildPkgMeta(dep, versions)
})
})
}
})
}
Suite.prototype.start = async function start() {
this.prepare()
const pkgVersions = await this._mapPackagesToVersions()
if (this.opts.matrixCountOnly === true) {
this.emit('matrixCountReady')
this.emit('end')
return
}
await this._runTests(pkgVersions)
this.emit('end')
}
/**
* The versions for a package can be a string or object
* return the appropriate one
*/
function getPkgVersions(dep) {
return typeof dep === 'object' ? dep.versions : dep
}
/**
* Creates a key in the pkgsMeta for every package. Each package
* will have an array of semver ranges, a latest flag(if a test wants only the latest of the package), and static versions.
*/
Suite.prototype._buildPkgMeta = function _buildPkgMeta(name, versions) {
if (!this.pkgsMeta.hasOwnProperty(name)) {
this.pkgsMeta[name] = { semverRanges: [], latest: false, staticVersions: [] }
}
if (versions === 'latest') {
this.pkgsMeta[name].latest = true
// Static version if it starts with a digit and does not end in `.x`
// i.e - 3.0.0(static), 3.x(not static)
} else if (versions.match(/^\d.*[^\.x]$/)) {
this.pkgsMeta[name].staticVersions.push(versions)
} else {
this.pkgsMeta[name].semverRanges.push(versions)
}
}
Suite.prototype._mapPackagesToVersions = async function _mapPackagesToVersions() {
const packages = Object.keys(this.pkgsMeta)
const self = this
const versionsFinal = await a.mapLimit(packages, this.opts.limit, async function loadPkgs(pkg) {
// Request the package information from NPM's registry.
let versions = await packager.load(pkg)
const pkgMeta = self.pkgsMeta[pkg]
versions = testUtil.maxVersionPerMode(Object.keys(versions), self.opts.versions, pkgMeta)
self.emit('packageResolved', pkg, versions)
return { versions, latest: versions[versions.length - 1] }
})
const pkgInfo = {}
for (let i = 0; i < packages.length; ++i) {
pkgInfo[packages[i]] = versionsFinal[i]
}
// Now we have all of the package versions we'll need in an object looking
// like this: {"package": { "versions": ["1.2", "1.3", "2.0"], "latest": "2.0"}}
return pkgInfo
}
Suite.prototype._runTests = async function _runTests(pkgVersions) {
this.failures = []
const self = this
const queue = a.queue(function done(test, queueCb) {
const testRun = test.run()
if (!testRun) {
self.emit('update', test, 'done')
return queueCb()
}
testRun.on('error', function errorHandler() {
self.failures.push(test)
self.emit('update', test, 'error')
})
testRun.on('end', function endHandler() {
if (testRun.failed) {
self.failures.push(test)
self.emit('update', test, 'failure')
} else {
// The test didn't fail and wants to continue, so update its status and
// then requeue it in the front of the pack.
self.emit('update', test, 'success')
queue.unshift(test)
}
queueCb()
})
if (testRun.needsInstall) {
self.emit('update', test, 'installing')
}
testRun.continue()
testRun.once('completed', () => {
self.emit('update', test, 'running')
testRun.continue()
})
}, this.opts.limit)
// Build and queue all of our test directories. The tests are sorted by number
// of runs required so the longer tests start sooner.
this.tests = this.testFolders
.map((folder) => new Test(folder, pkgVersions, this.opts))
.sort((firstEl, secondEl) => secondEl.matrix.length - firstEl.matrix.length)
this.tests.forEach((test) => {
queue.push(test)
})
await queue.drain()
}
module.exports = Suite