|
1 | | -"use strict"; |
2 | | - |
3 | | -import * as fs from "fs"; |
4 | | -import * as glob from "glob"; |
5 | | -import * as paths from "path"; |
6 | | - |
7 | | -const istanbul = require("istanbul"); |
8 | | -const Mocha = require("mocha"); |
9 | | -const remapIstanbul = require("remap-istanbul"); |
10 | | - |
11 | | -// Linux: prevent a weird NPE when mocha on Linux requires the window size from the TTY |
12 | | -// Since we are not running in a tty environment, we just implementt he method statically |
13 | | -const tty = require("tty"); |
14 | | -if (!tty.getWindowSize) { |
15 | | - tty.getWindowSize = (): number[] => { |
16 | | - return [80, 75]; |
17 | | - }; |
18 | | -} |
19 | | - |
20 | | -let mocha = new Mocha({ |
21 | | - ui: "tdd", |
22 | | - useColors: true |
| 1 | +// |
| 2 | +// PLEASE DO NOT MODIFY / DELETE UNLESS YOU KNOW WHAT YOU ARE DOING |
| 3 | +// |
| 4 | +// This file is providing the test runner to use when running extension tests. |
| 5 | +// By default the test runner in use is Mocha based. |
| 6 | +// |
| 7 | +// You can provide your own test runner if you want to override it by exporting |
| 8 | +// a function run(testRoot: string, clb: (error:Error) => void) that the extension |
| 9 | +// host can call to run the tests. The test runner is expected to use console.log |
| 10 | +// to report the results back to the caller. When the tests are finished, return |
| 11 | +// a possible error to the callback or null if none. |
| 12 | + |
| 13 | +import * as testRunner from "vscode/lib/testrunner"; |
| 14 | + |
| 15 | +// You can directly control Mocha options by uncommenting the following lines |
| 16 | +// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info |
| 17 | +testRunner.configure({ |
| 18 | + ui: "tdd", // the TDD UI is being used in extension.test.ts (suite, test, etc.) |
| 19 | + useColors: true // colored output from test results |
23 | 20 | }); |
24 | 21 |
|
25 | | -function configure(mochaOpts): void { |
26 | | - mocha = new Mocha(mochaOpts); |
27 | | -} |
28 | | -exports.configure = configure; |
29 | | - |
30 | | -function _mkDirIfExists(dir: string): void { |
31 | | - if (!fs.existsSync(dir)) { |
32 | | - fs.mkdirSync(dir); |
33 | | - } |
34 | | -} |
35 | | - |
36 | | -function _readCoverOptions(testsRoot: string): ITestRunnerOptions { |
37 | | - let coverConfigPath = paths.join(testsRoot, "..", "..", "coverconfig.json"); |
38 | | - let coverConfig: ITestRunnerOptions = undefined; |
39 | | - if (fs.existsSync(coverConfigPath)) { |
40 | | - let configContent = fs.readFileSync(coverConfigPath, "utf-8"); |
41 | | - coverConfig = JSON.parse(configContent); |
42 | | - } |
43 | | - return coverConfig; |
44 | | -} |
45 | | - |
46 | | -function run(testsRoot, clb): any { |
47 | | - // Enable source map support |
48 | | - require("source-map-support").install(); |
49 | | - |
50 | | - // Read configuration for the coverage file |
51 | | - let coverOptions: ITestRunnerOptions = _readCoverOptions(testsRoot); |
52 | | - if (coverOptions && coverOptions.enabled) { |
53 | | - // Setup coverage pre-test, including post-test hook to report |
54 | | - let coverageRunner = new CoverageRunner(coverOptions, testsRoot, clb); |
55 | | - coverageRunner.setupCoverage(); |
56 | | - } |
57 | | - |
58 | | - // Glob test files |
59 | | - glob("**/**.test.js", { cwd: testsRoot }, (error, files): any => { |
60 | | - if (error) { |
61 | | - return clb(error); |
62 | | - } |
63 | | - try { |
64 | | - // Fill into Mocha |
65 | | - files.forEach((f): Mocha => { |
66 | | - return mocha.addFile(paths.join(testsRoot, f)); |
67 | | - }); |
68 | | - // Run the tests |
69 | | - let failureCount = 0; |
70 | | - |
71 | | - mocha |
72 | | - .run() |
73 | | - .on("fail", (test, err): void => { |
74 | | - failureCount++; |
75 | | - }) |
76 | | - .on("end", (): void => { |
77 | | - clb(undefined, failureCount); |
78 | | - }); |
79 | | - } catch (error) { |
80 | | - return clb(error); |
81 | | - } |
82 | | - }); |
83 | | -} |
84 | | -exports.run = run; |
85 | | - |
86 | | -interface ITestRunnerOptions { |
87 | | - enabled?: boolean; |
88 | | - relativeCoverageDir: string; |
89 | | - relativeSourcePath: string; |
90 | | - ignorePatterns: string[]; |
91 | | - includePid?: boolean; |
92 | | - reports?: string[]; |
93 | | - verbose?: boolean; |
94 | | -} |
95 | | - |
96 | | -class CoverageRunner { |
97 | | - private coverageVar: string = "$$cov_" + new Date().getTime() + "$$"; |
98 | | - private transformer: any = undefined; |
99 | | - private matchFn: any = undefined; |
100 | | - private instrumenter: any = undefined; |
101 | | - |
102 | | - constructor( |
103 | | - private options: ITestRunnerOptions, |
104 | | - private testsRoot: string, |
105 | | - private endRunCallback: any |
106 | | - ) { |
107 | | - if (!options.relativeSourcePath) { |
108 | | - return endRunCallback( |
109 | | - "Error - relativeSourcePath must be defined for code coverage to work" |
110 | | - ); |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - public setupCoverage(): void { |
115 | | - // Set up Code Coverage, hooking require so that instrumented code is returned |
116 | | - let self = this; |
117 | | - self.instrumenter = new istanbul.Instrumenter({ |
118 | | - coverageVariable: self.coverageVar |
119 | | - }); |
120 | | - let sourceRoot = paths.join( |
121 | | - self.testsRoot, |
122 | | - self.options.relativeSourcePath |
123 | | - ); |
124 | | - |
125 | | - // Glob source files |
126 | | - let srcFiles = glob.sync("**/**.js", { |
127 | | - cwd: sourceRoot, |
128 | | - ignore: self.options.ignorePatterns |
129 | | - }); |
130 | | - |
131 | | - // Create a match function - taken from the run-with-cover.js in istanbul. |
132 | | - let decache = require("decache"); |
133 | | - let fileMap = {}; |
134 | | - srcFiles.forEach(file => { |
135 | | - let fullPath = paths.join(sourceRoot, file); |
136 | | - fileMap[fullPath] = true; |
137 | | - |
138 | | - // On Windows, extension is loaded pre-test hooks and this mean we lose |
139 | | - // our chance to hook the Require call. In order to instrument the code |
140 | | - // we have to decache the JS file so on next load it gets instrumented. |
141 | | - // This doesn"t impact tests, but is a concern if we had some integration |
142 | | - // tests that relied on VSCode accessing our module since there could be |
143 | | - // some shared global state that we lose. |
144 | | - decache(fullPath); |
145 | | - }); |
146 | | - |
147 | | - self.matchFn = (file): boolean => { |
148 | | - return fileMap[file]; |
149 | | - }; |
150 | | - self.matchFn.files = Object.keys(fileMap); |
151 | | - |
152 | | - // Hook up to the Require function so that when this is called, if any of our source files |
153 | | - // are required, the instrumented version is pulled in instead. These instrumented versions |
154 | | - // write to a global coverage variable with hit counts whenever they are accessed |
155 | | - self.transformer = self.instrumenter.instrumentSync.bind(self.instrumenter); |
156 | | - let hookOpts = { verbose: false, extensions: [".js"] }; |
157 | | - istanbul.hook.hookRequire(self.matchFn, self.transformer, hookOpts); |
158 | | - |
159 | | - // initialize the global variable to stop mocha from complaining about leaks |
160 | | - global[self.coverageVar] = {}; |
161 | | - |
162 | | - // Hook the process exit event to handle reporting |
163 | | - // Only report coverage if the process is exiting successfully |
164 | | - process.on("exit", code => { |
165 | | - self.reportCoverage(); |
166 | | - }); |
167 | | - } |
168 | | - |
169 | | - /** |
170 | | - * Writes a coverage report. Note that as this is called in the process exit callback, all calls must be synchronous. |
171 | | - * |
172 | | - * @returns {void} |
173 | | - * |
174 | | - * @memberOf CoverageRunner |
175 | | - */ |
176 | | - public reportCoverage(): void { |
177 | | - let self = this; |
178 | | - istanbul.hook.unhookRequire(); |
179 | | - let cov: any; |
180 | | - if ( |
181 | | - typeof global[self.coverageVar] === "undefined" || |
182 | | - Object.keys(global[self.coverageVar]).length === 0 |
183 | | - ) { |
184 | | - console.error( |
185 | | - "No coverage information was collected, exit without writing coverage information" |
186 | | - ); |
187 | | - return; |
188 | | - } else { |
189 | | - cov = global[self.coverageVar]; |
190 | | - } |
191 | | - |
192 | | - // TODO consider putting this under a conditional flag |
193 | | - // Files that are not touched by code ran by the test runner is manually instrumented, to |
194 | | - // illustrate the missing coverage. |
195 | | - self.matchFn.files.forEach(file => { |
196 | | - if (!cov[file]) { |
197 | | - self.transformer(fs.readFileSync(file, "utf-8"), file); |
198 | | - |
199 | | - // When instrumenting the code, istanbul will give each FunctionDeclaration a value of 1 in coverState.s, |
200 | | - // presumably to compensate for function hoisting. We need to reset this, as the function was not hoisted, |
201 | | - // as it was never loaded. |
202 | | - Object.keys(self.instrumenter.coverState.s).forEach(key => { |
203 | | - self.instrumenter.coverState.s[key] = 0; |
204 | | - }); |
205 | | - |
206 | | - cov[file] = self.instrumenter.coverState; |
207 | | - } |
208 | | - }); |
209 | | - |
210 | | - // TODO Allow config of reporting directory with |
211 | | - let reportingDir = paths.join( |
212 | | - self.testsRoot, |
213 | | - self.options.relativeCoverageDir |
214 | | - ); |
215 | | - let includePid = self.options.includePid; |
216 | | - let pidExt = includePid ? "-" + process.pid : ""; |
217 | | - let coverageFile = paths.resolve( |
218 | | - reportingDir, |
219 | | - "coverage" + pidExt + ".json" |
220 | | - ); |
221 | | - |
222 | | - _mkDirIfExists(reportingDir); // yes, do this again since some test runners could clean the dir initially created |
223 | | - fs.writeFileSync(coverageFile, JSON.stringify(cov), "utf8"); |
224 | | - |
225 | | - let remappedCollector = remapIstanbul.remap(cov, { |
226 | | - warn: warning => { |
227 | | - // We expect some warnings as any JS file without a typescript mapping will cause this. |
228 | | - // By default, we"ll skip printing these to the console as it clutters it up |
229 | | - if (self.options.verbose) { |
230 | | - console.warn(warning); |
231 | | - } |
232 | | - } |
233 | | - }); |
234 | | - |
235 | | - let reporter = new istanbul.Reporter(undefined, reportingDir); |
236 | | - let reportTypes = |
237 | | - self.options.reports instanceof Array ? self.options.reports : ["lcov"]; |
238 | | - reporter.addAll(reportTypes); |
239 | | - reporter.write(remappedCollector, true, () => { |
240 | | - console.log(`reports written to ${reportingDir}`); |
241 | | - }); |
242 | | - } |
243 | | -} |
| 22 | +module.exports = testRunner; |
0 commit comments