Skip to content

Commit cfadfe2

Browse files
committed
Correctly handle files with a single test
Also, notify the user of empty files.
1 parent e9e1729 commit cfadfe2

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/commands/tests.sidebar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export function registerRunAll(testsDataProvider: TestsDataProvider) {
6666
"The tests' outcomes are unknown.";
6767
dissatisfactoryOutcomeNotificationRequest.body =
6868
"This feature is fragile, and relies on the extension's compatibility with your particular Deno version. If you're using an outdated version of Deno, or an outdated version of the extension, try updating.";
69+
dissatisfactoryOutcomeNotificationRequest.actions = ["OK"];
6970
nova.notifications.add(dissatisfactoryOutcomeNotificationRequest);
7071
} else {
7172
// shown as a Nova message regardless; doesn't produce a crash

src/tests/TestsDataProvider.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class Test implements Element {
4242
export class UnexpectedLogError extends Error {}
4343
export class TestFile implements Element {
4444
path: string;
45-
children: Test[];
45+
children: Element[];
4646
shouldDisambiguate: boolean;
4747

48-
constructor(path: string, children: [], shouldDisambiguate: boolean) {
48+
constructor(path: string, children: Element[], shouldDisambiguate: boolean) {
4949
this.path = path;
5050
this.children = children;
5151
this.shouldDisambiguate = shouldDisambiguate;
@@ -165,29 +165,35 @@ export default class TestsDataProvider implements TreeDataProvider<Element> {
165165
const denoProcess = new Process("/usr/bin/env", options);
166166

167167
// This must be very fragile!
168-
const INTRODUCTION_REGEXP = /running \d+ tests from ([^]+)/;
168+
const INTRODUCTION_REGEXP = /running (\d+) tests? from ([^]+)/;
169169
const TEST_REGEXP = /([^]+) \.\.\. (FAILED|ok) \(\d+[A-Za-z]+\)$/;
170+
const CONTROL_REGEXP =
171+
// deno-lint-ignore no-control-regex
172+
/[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g;
173+
170174
const output: TestFile[] = [];
175+
176+
let loggingError: UnexpectedLogError | null = null;
171177
denoProcess.onStdout((line) => {
172178
// remove newline
173179
line = line.slice(0, -1);
180+
console.log(line);
174181
// remove control (?) characters that make output colorful
175182
line = line.replace(
176-
// deno-lint-ignore no-control-regex
177-
/[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g,
183+
CONTROL_REGEXP,
178184
"",
179185
);
180186
const introduction = line.match(INTRODUCTION_REGEXP);
181187
const test = line.match(TEST_REGEXP);
182188

183189
if (introduction) {
184-
const [, relativePath] = introduction;
190+
const [, count, relativePath] = introduction;
185191
output.push(
186192
new TestFile(
187193
nova.path.normalize(
188194
nova.path.join(nova.workspace.path!, relativePath),
189195
),
190-
[],
196+
count == "0" ? [new Header("No tests")] : [],
191197
false, // TODO
192198
),
193199
);
@@ -197,27 +203,26 @@ export default class TestsDataProvider implements TreeDataProvider<Element> {
197203

198204
const currentFile = output.at(-1);
199205
if (!currentFile) {
200-
throw new UnexpectedLogError("Unexpected logging. Oops.");
206+
if (!loggingError) {
207+
loggingError = new UnexpectedLogError(
208+
"Unexpected logging. Oops.",
209+
);
210+
}
211+
return;
201212
}
202213
currentFile.children.push(new Test(name, passed));
203214
}
204215
});
205216

206-
const onExit = new Promise((resolve) => {
217+
const onExit = new Promise((resolve, reject) => {
207218
denoProcess.onDidExit(() => {
208219
// TODO: explore the dangers regarding tests that take long to execute
209-
console.log(
210-
JSON.stringify(
211-
output.map((file) => ({
212-
path: file.path,
213-
tests: file.children.map((test) => test.passed),
214-
})),
215-
undefined,
216-
"\t",
217-
),
218-
);
219220
this.files = output;
220-
resolve(output);
221+
if (loggingError) {
222+
reject(loggingError);
223+
} else {
224+
resolve(output);
225+
}
221226
});
222227
});
223228

0 commit comments

Comments
 (0)