Skip to content

Commit 3e1d2ea

Browse files
authored
feat: Run tests in parallel when executing via tasks, speeding up total execution (commontoolsinc#2194)
1 parent 81d9752 commit 3e1d2ea

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

packages/utils/src/encoding.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const decoder = new TextDecoder();
66
* @param input - The string to encode
77
* @returns The encoded Uint8Array
88
*/
9-
export const encode = (input: string): Uint8Array => encoder.encode(input);
9+
export const encode = (input: string): Uint8Array<ArrayBuffer> =>
10+
encoder.encode(input);
1011

1112
/**
1213
* Decodes a Uint8Array into a string using UTF-8 decoding.

tasks/check.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ if [[ ! " ${DENO_VERSIONS_ALLOWED[@]} " =~ " ${DENO_VERSION} " ]]; then
99
exit 1
1010
fi
1111

12+
deno check tasks/*.ts
1213
deno check recipes/[!_]*.ts*
1314
deno check \
1415
packages/api \

tasks/test.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
#!/usr/bin/env -S deno run --allow-read --allow-run
22
import * as path from "@std/path";
3-
4-
const decoder = new TextDecoder();
3+
import { decode, encode } from "@commontools/utils/encoding";
54

65
export const ALL_DISABLED = [
76
"background-charm-service", // no tests yet
87
"vendor-astral", // no tests yet
98
];
109

11-
export async function testPackage(packagePath: string): Promise<boolean> {
12-
const result = await new Deno.Command(Deno.execPath(), {
13-
args: ["task", "test"],
14-
cwd: packagePath,
15-
stdout: "piped",
16-
}).output();
17-
18-
const stdout = decoder.decode(result.stdout);
19-
if (stdout) {
20-
console.log(stdout);
21-
}
22-
const stderr = decoder.decode(result.stderr);
23-
if (stderr) {
24-
console.error(stderr);
10+
export async function testPackage(
11+
packagePath: string,
12+
): Promise<{ packagePath: string; result: Deno.CommandOutput }> {
13+
try {
14+
return {
15+
packagePath,
16+
result: await new Deno.Command(Deno.execPath(), {
17+
args: ["task", "test"],
18+
cwd: packagePath,
19+
stdout: "piped",
20+
}).output(),
21+
};
22+
} catch (e) {
23+
return {
24+
packagePath,
25+
result: {
26+
success: false,
27+
stdout: new Uint8Array(),
28+
stderr: encode(`${e}`),
29+
code: 1,
30+
signal: null,
31+
},
32+
};
2533
}
26-
return result.success;
2734
}
2835

2936
export async function runTests(disabledPackages: string[]): Promise<boolean> {
3037
const workspaceCwd = Deno.cwd();
3138
const manifest = JSON.parse(await Deno.readTextFile("./deno.json"));
3239
const members: string[] = manifest.workspace;
3340

34-
const failedPackages: string[] = [];
35-
41+
const tests = [];
3642
for (const memberPath of members) {
3743
// Convert "./packages/memory" to "memory"
3844
const packageName = memberPath.substring(2).split("/")[1];
@@ -42,18 +48,21 @@ export async function runTests(disabledPackages: string[]): Promise<boolean> {
4248
}
4349
console.log(`Testing ${packageName}...`);
4450
const packagePath = path.join(workspaceCwd, "packages", packageName);
45-
if (!await testPackage(packagePath)) {
46-
failedPackages.push(packageName);
47-
}
51+
tests.push(testPackage(packagePath));
4852
}
4953

54+
const results = await Promise.all(tests);
55+
const failedPackages = results.filter((result) => !result.result.success);
56+
5057
if (failedPackages.length === 0) {
5158
console.log("All tests passing!");
5259
} else {
5360
console.error("One or more tests failed.");
5461
console.error("Failed packages:");
55-
for (const pkg of failedPackages) {
56-
console.error(`- ${pkg}`);
62+
for (const result of failedPackages) {
63+
console.error(`- ${result.packagePath}`);
64+
console.log(decode(result.result.stdout));
65+
console.error(decode(result.result.stderr));
5766
}
5867
Deno.exit(1);
5968
}

0 commit comments

Comments
 (0)