forked from OverlayPlugin/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_data_files.ts
92 lines (79 loc) · 3.14 KB
/
test_data_files.ts
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
import path from 'path';
import Mocha from 'mocha';
import { walkDirSync } from '../util/file_utils';
import testOopsyFiles from './helper/test_oopsy';
import testTimelineFiles from './helper/test_timeline';
import testTriggerFiles from './helper/test_trigger';
export type TestMochaGlobal = typeof global & {
triggerFiles?: string[];
manifestFiles?: string[];
timelineFiles?: string[];
oopsyFiles?: string[];
};
// This file runs in one of two ways:
// (1) As a part of Mocha's normal execution, running all the files in test...
// In this case, this file will call all of the testXFiles functions
// itself so that there is only run() of Mocha.
// (2) Called directly via node with optional filenames being passed via argv...
// In this case, this is for something like lint-staged. This file will
// pass all of the filenames it finds into globals and add
// test_data_runner.ts as a test, which will take those globals and call
// all of the same testXFiles functions.
//
// This weird dance allows for both partial testing of data files for lint-staged
// while only having a single Mocha execution when running implicitly as a part
// of Mocha.
const mocha = new Mocha();
const timelineFiles: string[] = [];
const triggerFiles: string[] = [];
const oopsyFiles: string[] = [];
const processInputs = (inputPath: string[]) => {
inputPath.forEach((path: string) => {
walkDirSync(path, (filepath) => {
if (/\/(?:raidboss|oopsy)_manifest.txt/.test(filepath)) {
return;
}
if (/\/raidboss\/data\/.*\.txt/.test(filepath)) {
timelineFiles.push(filepath);
return;
}
if (/\/raidboss\/data\/.*\.[jt]s/.test(filepath)) {
triggerFiles.push(filepath);
return;
}
if (/\/oopsyraidsy\/data\/.*\.[jt]s/.test(filepath)) {
oopsyFiles.push(filepath);
return;
}
});
});
};
const insideMocha = typeof global.describe === 'function';
// Run automatically via mocha, but also allow for running individual
// directories / files via the command-line.
// TODO: use this with lint-staged to run on individual file changes.
const defaultInput = ['ui/raidboss/data', 'ui/oopsyraidsy/data'];
const inputs: string[] = !insideMocha && process.argv.length > 2
? process.argv.slice(1)
: defaultInput;
processInputs(inputs);
if (insideMocha) {
testTriggerFiles(triggerFiles);
testTimelineFiles(timelineFiles);
testOopsyFiles(oopsyFiles);
} else {
const annotatedGlobal: TestMochaGlobal = global;
// Globals are the only way to pass additional fields to the test files below.
// Because we are running mocha programmatically here, the file names must be
// passed via globals. We can't add files after Mocha has started, unfortunately.
annotatedGlobal.timelineFiles = timelineFiles;
annotatedGlobal.triggerFiles = triggerFiles;
annotatedGlobal.oopsyFiles = oopsyFiles;
mocha.addFile(path.posix.join(path.relative(process.cwd(), './test/helper/test_data_runner.ts')));
mocha.loadFilesAsync()
.then(() => mocha.run((failures) => process.exitCode = failures ? 1 : 0))
.catch((error) => {
console.error(error);
process.exitCode = 1;
});
}