Make a globalSetup for the httpd-client tests

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2023-07-04 09:56:23 +02:00
parent 78663937d1
commit 3e797a2d08
4 changed files with 116 additions and 58 deletions
+59
View File
@@ -0,0 +1,59 @@
import * as FsSync from "node:fs";
import * as Path from "node:path";
import {
assertRadInstalled,
promptWorkspaceRemoval,
removeWorkspace,
tmpDir,
} from "@tests/support/support.js";
import { createPeerManager } from "@tests/support/peerManager";
import {
createSourceBrowsingFixture,
gitOptions,
startPalmHttpd,
} from "@tests/support/fixtures";
import { killAllProcesses } from "@tests/support/process";
export default async function globalSetup(): Promise<() => void> {
try {
await assertRadInstalled();
} catch (error) {
console.error(error);
console.log("");
console.log("To download the required test binaries, run:");
console.log(" 👉 ./scripts/install-binaries");
console.log("");
process.exit(1);
}
if (!process.env.SKIP_FIXTURE_CREATION) {
console.log("Setting up global test environment");
if (!process.env.CI) {
await promptWorkspaceRemoval();
}
await removeWorkspace();
const peerManager = await createPeerManager({
dataDir: Path.resolve(Path.join(tmpDir, "peers")),
outputLog: FsSync.createWriteStream(
Path.join(tmpDir, "peerManager.log"),
).setMaxListeners(16),
});
const palm = await peerManager.startPeer({
name: "palm",
gitOptions: gitOptions["alice"],
});
await palm.startHttpd(8080);
await palm.startNode({ trackingPolicy: "track", trackingScope: "all" });
console.log("Creating source-browsing fixture");
await createSourceBrowsingFixture(peerManager, palm);
console.log("Running tests");
await palm.stopNode();
} else {
await startPalmHttpd();
}
return () => killAllProcesses();
}
+1 -1
View File
@@ -6,7 +6,7 @@ export default defineConfig({
environment: "node",
include: ["httpd-client/tests/*.test.ts"],
reporters: "verbose",
globalSetup: "./tests/support/globalSetup",
globalSetup: "./httpd-client/tests/support/globalSetup",
},
resolve: {
alias: {
+7 -57
View File
@@ -1,11 +1,11 @@
import type { FullConfig } from "@playwright/test";
import * as Fs from "node:fs/promises";
import * as FsSync from "node:fs";
import * as Path from "node:path";
import * as readline from "node:readline/promises";
import { execa } from "execa";
import {
assertRadInstalled,
promptWorkspaceRemoval,
removeWorkspace,
tmpDir,
} from "@tests/support/support.js";
import {
createCobsFixture,
createMarkdownFixture,
@@ -14,14 +14,9 @@ import {
startPalmHttpd,
} from "@tests/support/fixtures.js";
import { createPeerManager } from "@tests/support/peerManager.js";
import { heartwoodShortSha, tmpDir } from "@tests/support/support.js";
import { killAllProcesses } from "@tests/support/process.js";
const workspacePaths = [Path.join(tmpDir, "peers"), Path.join(tmpDir, "repos")];
export default async function globalSetup(
_config: FullConfig,
): Promise<() => void> {
export default async function globalSetup(): Promise<() => void> {
try {
await assertRadInstalled();
} catch (error) {
@@ -71,48 +66,3 @@ export default async function globalSetup(
return () => killAllProcesses();
}
// Assert that the `rad` CLI is installed and has the correct version.
async function assertRadInstalled(): Promise<void> {
const { stdout: version } = await execa("rad", ["--version"]);
if (!version.includes(heartwoodShortSha)) {
throw new Error(
`rad version ${version} does not satisfy ${heartwoodShortSha}`,
);
}
}
async function promptWorkspaceRemoval(): Promise<void> {
console.log("");
console.log("This will irrevocably destroy the following directories:");
console.log("");
workspacePaths.forEach(path => console.log(path));
console.log("");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const result = await rl.question(
"Are you sure you want to continue? [yes/no]: ",
);
rl.close();
if (result.toLowerCase() === "yes") {
console.log("Done");
return;
}
console.log("Ok, I won't touch your data.");
process.exit(1);
}
async function removeWorkspace(): Promise<void> {
for (const path of workspacePaths) {
await Fs.rm(path, {
recursive: true,
force: true,
});
}
}
+49
View File
@@ -1,8 +1,11 @@
import type { Options } from "execa";
import { execa } from "execa";
import * as Crypto from "node:crypto";
import { fileURLToPath } from "node:url";
import * as Path from "node:path";
import * as Fs from "node:fs/promises";
import * as readline from "node:readline/promises";
// Generate string of 12 random characters with 8 bits of entropy.
export function randomTag(): string {
@@ -21,6 +24,7 @@ const filename = fileURLToPath(import.meta.url);
export const supportDir = Path.dirname(filename);
export const tmpDir = Path.resolve(supportDir, "..", "./tmp");
export const fixturesDir = Path.resolve(supportDir, "..", "./fixtures");
const workspacePaths = [Path.join(tmpDir, "peers"), Path.join(tmpDir, "repos")];
export const heartwoodShortSha = (
await Fs.readFile(`${supportDir}/heartwood-version`, "utf8")
@@ -30,3 +34,48 @@ process.env.PATH = [
Path.join(tmpDir, "bin", heartwoodShortSha),
process.env.PATH,
].join(Path.delimiter);
// Assert that the `rad` CLI is installed and has the correct version.
export async function assertRadInstalled(): Promise<void> {
const { stdout: version } = await execa("rad", ["--version"]);
if (!version.includes(heartwoodShortSha)) {
throw new Error(
`rad version ${version} does not satisfy ${heartwoodShortSha}`,
);
}
}
export async function promptWorkspaceRemoval(): Promise<void> {
console.log("");
console.log("This will irrevocably destroy the following directories:");
console.log("");
workspacePaths.forEach(path => console.log(path));
console.log("");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const result = await rl.question(
"Are you sure you want to continue? [yes/no]: ",
);
rl.close();
if (result.toLowerCase() === "yes") {
console.log("Done");
return;
}
console.log("Ok, I won't touch your data.");
process.exit(1);
}
export async function removeWorkspace(): Promise<void> {
for (const path of workspacePaths) {
await Fs.rm(path, {
recursive: true,
force: true,
});
}
}