-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
190 lines (168 loc) · 6.21 KB
/
Copy pathindex.ts
File metadata and controls
190 lines (168 loc) · 6.21 KB
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
import {
types,
ExecuteWithScope,
createExecuteWithScope,
} from "@rokucommunity/brs";
import fastGlob from "fast-glob";
import * as path from "path";
import * as c from "ansi-colors";
import { ReportOptions } from "istanbul-reports";
import { reportCoverage } from "./coverage";
import { formatInterpreterError, globMatchFiles } from "./util";
import { createTestRunner, ReporterType } from "./runner";
const { isBrsBoolean, isBrsString, RoArray, RoAssociativeArray } = types;
interface CliOptions {
/** The test reporter to use. */
reporter: ReporterType;
/** A path to a file that we should load into global exec scope prior to test run. */
requireFilePath: string | undefined;
/** Whether or not to fail the test run if focused cases are detected. */
forbidFocused?: boolean;
/** The istanbul coverage reporters to use. */
coverageReporters?: (keyof ReportOptions)[];
/** The directory where we should load source files from, if not 'source'. */
sourceDir?: string;
/**
* A list of strings to match files against, specified in the command.
* If empty, we will test/search for all *.test.brs files.
*/
filePatterns: string[];
}
async function findBrsFiles(sourceDir?: string) {
let searchDir = sourceDir || "source";
const pattern = path.join(process.cwd(), searchDir, "**", "*.brs");
return fastGlob(pattern);
}
/**
* Generates an execution scope and runs the tests.
* @param files List of filenames to load into the execution scope
* @param options BRS interpreter options
*/
async function run(brsSourceFiles: string[], options: CliOptions) {
let {
reporter,
requireFilePath,
forbidFocused,
coverageReporters = [],
filePatterns,
} = options;
let coverageEnabled = coverageReporters.length > 0;
// Get the list of files that we should load into the execution scope.
// Loading them here ensures that they only get lexed/parsed once.
let inScopeFiles = [
"roca_lib.brs",
"assert_lib.brs",
path.join("tap", "tap.brs"),
].map((basename) => path.join(__dirname, "..", "resources", basename));
if (requireFilePath) {
inScopeFiles.push(requireFilePath);
}
inScopeFiles.push(...brsSourceFiles);
let testRunner = await createTestRunner(reporter);
// Create an execution scope using the project source files and roca files.
let execute: ExecuteWithScope;
try {
execute = await createExecuteWithScope(inScopeFiles, {
root: process.cwd(),
stdout: testRunner.reporterStream,
stderr: process.stderr,
generateCoverage: coverageEnabled,
componentDirs: ["test", "tests"],
noColor: true,
});
} catch (e) {
console.error(
`Stopping execution. Interpreter encountered errors:\n\t${formatInterpreterError(
e
)}`
);
process.exit(1);
}
let { testFiles, focusedCasesDetected } = await getTestFiles(
execute,
filePatterns
);
// Fail if we find focused test cases and there weren't supposed to be any.
if (forbidFocused && focusedCasesDetected) {
let formattedList = testFiles
.map((filename) => `\t${filename}`)
.join("\n");
console.error(
c.red(
`Error: used command line arg ${c.cyan(
"--forbid-focused"
)} but found focused tests in these files:\n${formattedList}`
)
);
process.exit(1);
}
testRunner.run(execute, testFiles, focusedCasesDetected);
testRunner.reporterStream.end();
if (coverageEnabled) {
reportCoverage(coverageReporters);
}
return testRunner.reporterStream.runner?.testResults || {};
}
/**
* Returns the appropriate set of *.test.brs files, depending on whether it detects any focused tests.
* Runs through the entire test suite (in non-exec mode) to determine this.
* Also returns a boolean indicating whether focused tests were found.
* @param execute The scoped execution function to run with each file
* @param filePatterns A list of strings to match files against
*/
async function getTestFiles(execute: ExecuteWithScope, filePatterns: string[]) {
let testFiles = await globMatchFiles(filePatterns);
let focusedSuites: string[] = [];
let emptyRunArgs = new RoAssociativeArray([]);
testFiles.forEach((filename) => {
try {
// Run the file in non-exec mode.
let suite = execute([filename], [emptyRunArgs]);
// Keep track of which files have focused cases.
let subSuites =
suite instanceof RoArray ? suite.getElements() : [suite];
if (hasFocusedCases(subSuites)) {
focusedSuites.push(filename);
}
} catch {
// This is the pre-execution phase; report interpreter errors during execution instead.
}
});
let focusedCasesDetected = focusedSuites.length > 0;
return {
focusedCasesDetected,
testFiles: focusedCasesDetected ? focusedSuites : testFiles,
};
}
/**
* Checks to see if any suites in a given array of suites are focused.
* @param subSuites An array of Roca suite objects to check
*/
function hasFocusedCases(subSuites: types.BrsType[]): boolean {
for (let subSuite of subSuites) {
if (!(subSuite instanceof RoAssociativeArray)) continue;
let mode = subSuite.elements.get("mode");
if (mode && isBrsString(mode) && mode.value === "focus") {
return true;
}
let state = subSuite.elements.get("__state");
if (state instanceof RoAssociativeArray) {
let hasFocusedDescendants = state.elements.get(
"hasfocuseddescendants"
);
if (
hasFocusedDescendants &&
isBrsBoolean(hasFocusedDescendants) &&
hasFocusedDescendants.toBoolean()
) {
return true;
}
}
}
return false;
}
module.exports = async function (args: CliOptions) {
let { sourceDir, ...options } = args;
let files = await findBrsFiles(sourceDir);
return await run(files, options);
};