-
Notifications
You must be signed in to change notification settings - Fork 3
/
testkit.ts
53 lines (49 loc) · 1.66 KB
/
testkit.ts
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
// biome-ignore lint/nursery/noRestrictedImports: <explanation>
import { readFileSync } from "node:fs";
import { cp, mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join as pathJoin } from "node:path";
import toml from "toml";
import { test } from "vitest";
import { $ as $$, type Shell, cd } from "zx/core";
export function run({
name,
afterInstall,
}: {
name: string;
afterInstall?: ($: Shell) => Promise<void>;
}) {
const tomlPathSource = pathJoin(import.meta.dirname, name, "plugin.toml");
const content = readFileSync(tomlPathSource, { encoding: "utf-8" });
const data = toml.parse(content) as Plugin;
const platform = getPlatform();
const supportPlatforms = Object.keys(data.platform);
const skip = !supportPlatforms.includes(platform);
console.log(`[${name}] supports: [${supportPlatforms}], current: ${platform}, skip: ${skip}`);
const $ = $$({ verbose: true });
test.skipIf(skip)(`proto install ${name}`, { timeout: Number.POSITIVE_INFINITY }, async () => {
const dir = await mkdtemp(`${tmpdir()}/proto-plugin-test-${name}`);
const tomlPathDist = pathJoin(dir, "plugin.toml");
await cp(tomlPathSource, tomlPathDist);
cd(dir);
await $`pwd`;
await $`proto plugin add ${name} source:./plugin.toml`;
await $`proto install ${name} latest`;
if (afterInstall) {
await afterInstall($);
}
});
}
type Plugin = {
name: string;
platform: Partial<Record<"linux" | "macos" | "windows", object>>;
};
function getPlatform(): "linux" | "macos" | "windows" | "unknown" {
return process.platform === "linux"
? "linux"
: process.platform === "darwin"
? "macos"
: process.platform === "win32"
? "windows"
: "unknown";
}