This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
158 lines (143 loc) · 3.7 KB
/
index.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
'use strict'
/*
* adonis-vow
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const _ = require('lodash')
const path = require('path')
const globby = require('globby')
const debug = require('debug')('adonis:vow:cli')
/**
* The Cli class is used to load the test files
* to be executed when running tests. It allows
* user to specific different test directories
* for unit and functional tests and even
* ignore few files.
*
* @class Cli
* @static
*/
class Cli {
constructor (Env, Helpers) {
this.projectRoot = Helpers.appRoot()
this._testGroups = {
unit: 'test/unit/**/*.spec.js',
functional: 'test/functional/**/*.spec.js'
}
this._ignoreTests = Env.get('IGNORE_TESTS', [])
this._filterCallback = null
}
/**
* Returns a glob pattern to be used for loading
* the test files.
*
* @method _getGlob
*
* @param {Array} includes
* @param {Array} excludes
*
* @return {Array}
*
* @private
*/
_getGlob (includes, excludes = []) {
const absIncludes = includes.map((glob) => path.join(this.projectRoot, glob))
const absExcludes = excludes.map((glob) => `!${path.join(this.projectRoot, glob)}`)
return absIncludes.concat(absExcludes)
}
/**
* Returns a reference to all the registered groups
*
* @method getGroups
*
* @return {Object}
*/
getGroups () {
return this._testGroups
}
/**
* Reset groups to a custom map
*
* @method setGroups
*/
setGroups (groups) {
this._testGroups = groups
}
/**
* Define glob for a group of tests
*
* @method group
*
* @param {String} name
* @param {String} glob
*
* @chainable
*/
group (name, glob) {
debug('setting %s tests glob as %s', name, glob)
this._testGroups[name] = glob
return this
}
/**
* Define a custom filter callback to be used for
* filtering the test files
*
* @method filter
*
* @param {String|Function|Array} patternOrCallback
*
* @chainable
*/
filter (patternOrCallback) {
if (typeof (patternOrCallback) === 'function') {
this._filterCallback = patternOrCallback
} else if (typeof (patternOrCallback) === 'string' || patternOrCallback instanceof Array === true) {
this._ignoreTests = patternOrCallback
} else {
throw new Error('cli.filter accepts an array/string of globs or a callback function')
}
return this
}
/**
* Returns an array of test files with their absolute
* path. These files should be loaded directly and
* then tests can be executed
*
* @method getTestFiles
*
* @param {Array} filesToPick
* @return {Array}
*/
async getTestFiles (filesToPick = []) {
const includes = _.filter(this._testGroups, (test) => !!test)
const excludes = typeof (this._ignoreTests) === 'string' ? [this._ignoreTests] : this._ignoreTests
let testFiles = await globby(this._getGlob(includes, excludes), {
realpath: true
})
/**
* If there is no filter callback, all files are returned
* Otherwise user is given a chance to filter test files.
*/
if (typeof (this._filterCallback) === 'function') {
testFiles = testFiles.filter(this._filterCallback)
}
/**
* If there are specific files defined, then grep on
* them to pick only those files
*/
if (_.size(filesToPick)) {
testFiles = _.filter(testFiles, (file) => {
return _.some(filesToPick, (selectedFile) => {
return file.includes(selectedFile.trim())
})
})
}
debug('test files %j', testFiles)
return testFiles
}
}
module.exports = Cli