1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | 'use strict';
|
13 |
|
14 | const T = require('../lib/tools'),
|
15 | Mocha = require('mocha'),
|
16 | mochaOptions = require('mocha/bin/options'),
|
17 | vsf = require('vinyl-fs'),
|
18 | mps = require('map-stream'),
|
19 | fsrr = require('fs-readdir-recursive');
|
20 |
|
21 | const cwd = process.cwd();
|
22 | const packageConfig = require(T.Path.resolve(cwd, 'package.json'));
|
23 | const testFolder = (packageConfig['test'] && T.Path.resolve(cwd, packageConfig['test'])) || T.Path.resolve(cwd, 'test');
|
24 | const files = fsrr(testFolder, f => f);
|
25 | const argfiles = T.argv._.slice(1);
|
26 |
|
27 | class Testor {
|
28 | constructor(files) {
|
29 | this.files = files || [];
|
30 | this.mca = new Mocha(mochaOptions);
|
31 | }
|
32 |
|
33 | test() {
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | if (argfiles && argfiles.length > 0) {
|
39 | this.files = argfiles.map(file => file.replace(/^([\s\S]+?)(.js)?$/g, '$1.js'));
|
40 | }
|
41 | this.testAll();
|
42 | }
|
43 |
|
44 | testAll() {
|
45 | if (Array.isArray(this.files) && this.files.length > 0) {
|
46 | this.files = this.files.filter(file => {
|
47 | let info = T.Path.parse(file);
|
48 | return info.ext === '.js' && /[.-](test|spec)/i.test(info.name);
|
49 | });
|
50 | this.files.map(file => {
|
51 | this.mca.addFile(T.Path.resolve(testFolder, file));
|
52 | });
|
53 | this.mca.run();
|
54 | } else {
|
55 | T.log.error('× Not found test files');
|
56 | }
|
57 | }
|
58 | }
|
59 |
|
60 | module.exports = new Testor(files);
|