Skip to content

Commit 8abbbb1

Browse files
authored
test: Test script improvements (#932)
1 parent 69e2c48 commit 8abbbb1

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

scripts/test.js

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env -S deno run --unstable --no-check --allow-run --allow-read --allow-write --allow-env --allow-net
22

3-
import { join } from "std/path/mod.ts";
3+
import { join, resolve } from "std/path/mod.ts";
44
import { setCwd } from "chdir-anywhere";
55
import { dev } from "./dev.js";
66
import { parseArgs } from "../test/shared/testArgs.js";
@@ -9,6 +9,7 @@ setCwd();
99
Deno.chdir("..");
1010

1111
const DENO_COVERAGE_DIR = ".coverage/denoCoverage";
12+
const DENO_HTML_COVERAGE_DIR = ".coverage/html";
1213
const FAKE_IMPORTS_COVERAGE_DIR = ".coverage/fakeImportsCoverageMap";
1314

1415
/**
@@ -29,7 +30,7 @@ async function removeMaybeDirectory(path) {
2930
* @type {string?}
3031
*/
3132
let filteredTests = null;
32-
if (Deno.args.length > 0 && !Deno.args[0].startsWith("--")) {
33+
if (Deno.args.length > 0 && !Deno.args[0].startsWith("-")) {
3334
filteredTests = Deno.args[0];
3435
}
3536

@@ -42,11 +43,7 @@ await dev({
4243

4344
const { inspect } = parseArgs();
4445

45-
let needsCoverage = Deno.args.includes("--coverage") || Deno.args.includes("-c");
46-
const needsHtmlCoverageReport = Deno.args.includes("--html");
47-
if (needsHtmlCoverageReport) {
48-
needsCoverage = true;
49-
}
46+
const needsCoverage = Deno.args.includes("--coverage") || Deno.args.includes("-c");
5047

5148
/** @type {string[][]} */
5249
const testCommands = [];
@@ -116,41 +113,36 @@ if (needsCoverage) {
116113

117114
console.log("Generating cov.lcov...");
118115
const includeRegex = `^file://${Deno.cwd()}/(src|studio/src|studio/devSocket/src)`;
119-
const coverageProcess = new Deno.Command(Deno.execPath(), {
116+
const lcovCommand = new Deno.Command(Deno.execPath(), {
120117
args: ["coverage", DENO_COVERAGE_DIR, "--lcov", `--include=${includeRegex}`],
121118
stdout: "piped",
119+
stderr: "piped",
122120
});
123-
const coverageOutput = await coverageProcess.output();
124-
if (!coverageOutput.success) {
125-
Deno.exit(coverageOutput.code);
121+
const lcovOutput = await lcovCommand.output();
122+
if (!lcovOutput.success) {
123+
Deno.exit(lcovOutput.code);
126124
}
127-
await Deno.writeFile(".coverage/cov.lcov", coverageOutput.stdout);
128-
129-
if (needsHtmlCoverageReport) {
130-
console.log("Generating HTML coverage report...");
131-
let genHtmlCommand = null;
132-
try {
133-
genHtmlCommand = new Deno.Command("genhtml", {
134-
args: ["-o", ".coverage/html", ".coverage/cov.lcov"],
135-
stdout: "inherit",
136-
stderr: "inherit",
137-
});
138-
} catch {
139-
console.error("%cERROR%c Failed to generate html report, is lcov not installed?", "color: red", "");
140-
let installCmd = null;
141-
if (Deno.build.os == "darwin") {
142-
installCmd = "brew install lcov";
143-
}
144-
// I'm not sure how to install it on other platforms, feel free to add more here.
145-
if (installCmd) {
146-
console.log(`Try installing it with: %c${installCmd}`, "color: black; background-color: grey");
147-
}
148-
}
149-
if (genHtmlCommand) {
150-
const genHtmlOutput = await genHtmlCommand.output();
151-
if (!genHtmlOutput.success) {
152-
Deno.exit(genHtmlOutput.code);
153-
}
125+
await Deno.writeFile(".coverage/cov.lcov", lcovOutput.stdout);
126+
127+
console.log("Generating HTML coverage report...");
128+
const htmlCoverageCommand = new Deno.Command(Deno.execPath(), {
129+
args: ["coverage", DENO_COVERAGE_DIR, "--html", `--include=${includeRegex}`],
130+
stdout: "piped",
131+
stderr: "piped",
132+
});
133+
const htmlOutput = await htmlCoverageCommand.output();
134+
if (!htmlOutput.success) {
135+
Deno.exit(htmlOutput.code);
136+
}
137+
138+
try {
139+
await Deno.remove(DENO_HTML_COVERAGE_DIR, { recursive: true });
140+
} catch (e) {
141+
if (e instanceof Deno.errors.NotFound) {
142+
// Already removed
143+
} else {
144+
throw e;
154145
}
155146
}
147+
await Deno.rename(resolve(DENO_COVERAGE_DIR, "html"), DENO_HTML_COVERAGE_DIR);
156148
}

test/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ The test script takes some optional parameters:
1414
- `-i`, `--inspect` to wait for a debugger to connect, this also automatically disables headless mode for e2e tests, disables e2e test timeouts, and forces e2e tests to run only once.
1515
- `-h`, `--headless` toggles the default headless behaviour. Headless mode is disabled by default unless `-i` or `--inspect` have been specified.
1616
- `-c`, `--coverage` generates a coverage file in `.lcov` format. This is useful if your IDE supports it.
17-
- `--html` generates a coverage file in `.html` format. `genhtml` needs to be installed for this to work. The generated html can be found at `.coverage/html`.
1817

1918
## Unit tests
2019

0 commit comments

Comments
 (0)