-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathrunTest.ts
More file actions
272 lines (248 loc) · 7.66 KB
/
runTest.ts
File metadata and controls
272 lines (248 loc) · 7.66 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import * as os from "node:os";
import * as vscode from "vscode";
import {
type DebuggeeExited,
type DebuggeeOutput,
trackerFactory,
} from "../debugAdapter";
import { reporter } from "../telemetry";
export type RunTestArgs = {
cwd: string;
filePath?: string;
line?: number;
doctestLine?: number;
module?: string;
workspaceFolder: vscode.WorkspaceFolder;
getTest: (
file: string,
module: string,
describe: string | null,
name: string,
type: string,
) => vscode.TestItem | undefined;
};
// Get the configuration for mix test, if it exists
function getExistingLaunchConfig(
args: RunTestArgs,
debug: boolean,
): vscode.DebugConfiguration | undefined {
const launchJson = vscode.workspace.getConfiguration(
"launch",
args.workspaceFolder,
);
const configurations = launchJson.get<vscode.DebugConfiguration[]>(
"configurations",
);
let testConfig: vscode.DebugConfiguration | undefined;
if (Array.isArray(configurations)) {
for (let i = configurations.length - 1; i >= 0; i--) {
const c = configurations[i];
if (c?.name === "mix test") {
testConfig = c;
break;
}
}
}
if (testConfig === undefined) {
return undefined;
}
// override configuration with sane defaults
testConfig.request = "launch";
testConfig.task = "test";
testConfig.projectDir = args.cwd;
testConfig.env = {
MIX_ENV: "test",
...(testConfig.env ?? {}),
};
// as of vscode 1.78 ANSI is not fully supported
testConfig.taskArgs = buildTestCommandArgs(args, debug);
testConfig.requireFiles = [
"test/**/test_helper.exs",
"apps/*/test/**/test_helper.exs",
args.filePath,
];
testConfig.noDebug = !debug;
return testConfig;
}
// Get the config to use for debugging
function getLaunchConfig(
args: RunTestArgs,
debug: boolean,
): vscode.DebugConfiguration {
const fileConfiguration: vscode.DebugConfiguration | undefined =
getExistingLaunchConfig(args, debug);
const fallbackConfiguration: vscode.DebugConfiguration = {
type: "mix_task",
name: "mix test",
request: "launch",
task: "test",
env: {
MIX_ENV: "test",
},
taskArgs: buildTestCommandArgs(args, debug),
startApps: true,
projectDir: args.cwd,
// we need to require all test helpers and only the file we need to test
// mix test runs tests in all required files even if they do not match
// given path:line
requireFiles: [
"test/**/test_helper.exs",
"apps/*/test/**/test_helper.exs",
args.filePath,
],
noDebug: !debug,
};
const config = fileConfiguration ?? fallbackConfiguration;
console.log("Starting debug session with launch config", config);
return config;
}
export async function runTest(
run: vscode.TestRun,
args: RunTestArgs,
debug: boolean,
): Promise<string> {
reporter.sendTelemetryEvent("run_test", {
"elixir_ls.with_debug": "true",
});
const debugConfiguration: vscode.DebugConfiguration = getLaunchConfig(
args,
debug,
);
return new Promise((resolve, reject) => {
const listeners: Array<vscode.Disposable> = [];
const disposeListeners = () => {
for (const listener of listeners) {
listener.dispose();
}
};
let sessionId = "";
// default to error
// expect DAP `exited` event with mix test exit code
let exitCode = 1;
const output: string[] = [];
listeners.push(
trackerFactory.onOutput((outputEvent: DebuggeeOutput) => {
if (outputEvent.sessionId === sessionId) {
const category = outputEvent.output.body.category;
if (category === "stdout" || category === "stderr") {
output.push(outputEvent.output.body.output);
} else if (category === "ex_unit") {
const exUnitEvent = outputEvent.output.body.data.event;
const data = outputEvent.output.body.data;
const test = args.getTest(
data.file,
data.module,
data.describe,
data.name,
data.type,
);
if (test) {
if (exUnitEvent === "test_started") {
run.started(test);
} else if (exUnitEvent === "test_passed") {
run.passed(test, data.time / 1000);
} else if (exUnitEvent === "test_failed") {
run.failed(
test,
new vscode.TestMessage(data.message),
data.time / 1000,
);
} else if (exUnitEvent === "test_errored") {
// ex_unit does not report duration for invalid tests
run.errored(test, new vscode.TestMessage(data.message));
} else if (
exUnitEvent === "test_skipped" ||
exUnitEvent === "test_excluded"
) {
run.skipped(test);
}
} else {
if (exUnitEvent !== "test_excluded") {
console.warn(
`ElixirLS: Test ${data.file} ${data.module} ${data.describe} ${data.name} not found`,
);
}
}
}
}
}),
);
listeners.push(
trackerFactory.onExited((exit: DebuggeeExited) => {
console.log(
`ElixirLS: Debug session ${exit.sessionId}: debuggee exited with code ${exit.code}`,
);
if (exit.sessionId === sessionId) {
exitCode = exit.code;
}
}),
);
listeners.push(
vscode.debug.onDidStartDebugSession((s) => {
console.log(`ElixirLS: Debug session ${s.id} started`);
sessionId = s.id;
}),
);
listeners.push(
vscode.debug.onDidTerminateDebugSession((s) => {
console.log(`ElixirLS: Debug session ${s.id} terminated`);
disposeListeners();
if (exitCode === 0) {
resolve(output.join(""));
} else {
reject(output.join(""));
}
}),
);
vscode.debug.startDebugging(args.workspaceFolder, debugConfiguration).then(
(debugSessionStarted) => {
if (!debugSessionStarted) {
reporter.sendTelemetryErrorEvent("run_test_error", {
"elixir_ls.with_debug": "true",
});
disposeListeners();
reject("Unable to start debug session");
}
},
(reason) => {
reporter.sendTelemetryErrorEvent("run_test_error", {
"elixir_ls.with_debug": "true",
"elixir_ls.run_test_error": String(reason),
"elixir_ls.run_test_error_stack": reason?.stack ?? "",
});
disposeListeners();
reject("Unable to start debug session");
},
);
});
}
const COMMON_ARGS = ["--formatter", "ElixirLS.DebugAdapter.ExUnitFormatter"];
function buildTestCommandArgs(args: RunTestArgs, debug: boolean): string[] {
let line = "";
if (typeof args.line === "number") {
line = `:${args.line}`;
}
const result = [];
if (args.module) {
result.push("--only");
result.push(`module:${args.module}`);
}
if (args.doctestLine) {
result.push("--only");
result.push(`doctest_line:${args.doctestLine}`);
}
if (args.filePath) {
// workaround for https://github.com/elixir-lang/elixir/issues/13225
// ex_unit file filters with windows path separators are broken on elixir < 1.16.1
// fortunately unix separators work correctly
// TODO remove this when we require elixir 1.17
const path =
os.platform() === "win32"
? args.filePath.replace(/\\/g, "/")
: args.filePath;
result.push(`${path}${line}`);
}
// debug tests in tracing mode to disable timeouts
const maybeTrace = debug ? ["--trace"] : [];
return [...maybeTrace, ...result, ...COMMON_ARGS];
}