test(perf): reduce per-case setup in script and git-hook tests
This commit is contained in:
@@ -4,18 +4,23 @@ import os from "node:os";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
const baseGitEnv = {
|
||||||
|
GIT_CONFIG_NOSYSTEM: "1",
|
||||||
|
GIT_TERMINAL_PROMPT: "0",
|
||||||
|
};
|
||||||
|
|
||||||
const run = (cwd: string, cmd: string, args: string[] = [], env?: NodeJS.ProcessEnv) => {
|
const run = (cwd: string, cmd: string, args: string[] = [], env?: NodeJS.ProcessEnv) => {
|
||||||
return execFileSync(cmd, args, {
|
return execFileSync(cmd, args, {
|
||||||
cwd,
|
cwd,
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
env: env ? { ...process.env, ...env } : process.env,
|
env: { ...process.env, ...baseGitEnv, ...env },
|
||||||
}).trim();
|
}).trim();
|
||||||
};
|
};
|
||||||
|
|
||||||
describe("git-hooks/pre-commit (integration)", () => {
|
describe("git-hooks/pre-commit (integration)", () => {
|
||||||
it("does not treat staged filenames as git-add flags (e.g. --all)", () => {
|
it("does not treat staged filenames as git-add flags (e.g. --all)", () => {
|
||||||
const dir = mkdtempSync(path.join(os.tmpdir(), "openclaw-pre-commit-"));
|
const dir = mkdtempSync(path.join(os.tmpdir(), "openclaw-pre-commit-"));
|
||||||
run(dir, "git", ["init", "-q"]);
|
run(dir, "git", ["init", "-q", "--initial-branch=main"]);
|
||||||
|
|
||||||
// Use the real hook script and lightweight helper stubs.
|
// Use the real hook script and lightweight helper stubs.
|
||||||
mkdirSync(path.join(dir, "git-hooks"), { recursive: true });
|
mkdirSync(path.join(dir, "git-hooks"), { recursive: true });
|
||||||
|
|||||||
@@ -12,7 +12,9 @@ const BASE_PATH = process.env.PATH ?? "/usr/bin:/bin";
|
|||||||
const BASE_LANG = process.env.LANG ?? "C";
|
const BASE_LANG = process.env.LANG ?? "C";
|
||||||
let fixtureRoot = "";
|
let fixtureRoot = "";
|
||||||
let sharedBinDir = "";
|
let sharedBinDir = "";
|
||||||
let caseId = 0;
|
let sharedHomeDir = "";
|
||||||
|
let sharedHomeBinDir = "";
|
||||||
|
let sharedFakePythonPath = "";
|
||||||
|
|
||||||
async function writeExecutable(filePath: string, body: string): Promise<void> {
|
async function writeExecutable(filePath: string, body: string): Promise<void> {
|
||||||
await writeFile(filePath, body, "utf8");
|
await writeFile(filePath, body, "utf8");
|
||||||
@@ -57,6 +59,14 @@ describe("scripts/ios-team-id.sh", () => {
|
|||||||
fixtureRoot = await mkdtemp(path.join(os.tmpdir(), "openclaw-ios-team-id-"));
|
fixtureRoot = await mkdtemp(path.join(os.tmpdir(), "openclaw-ios-team-id-"));
|
||||||
sharedBinDir = path.join(fixtureRoot, "shared-bin");
|
sharedBinDir = path.join(fixtureRoot, "shared-bin");
|
||||||
await mkdir(sharedBinDir, { recursive: true });
|
await mkdir(sharedBinDir, { recursive: true });
|
||||||
|
sharedHomeDir = path.join(fixtureRoot, "home");
|
||||||
|
sharedHomeBinDir = path.join(sharedHomeDir, "bin");
|
||||||
|
await mkdir(sharedHomeBinDir, { recursive: true });
|
||||||
|
await mkdir(path.join(sharedHomeDir, "Library", "Preferences"), { recursive: true });
|
||||||
|
await writeFile(
|
||||||
|
path.join(sharedHomeDir, "Library", "Preferences", "com.apple.dt.Xcode.plist"),
|
||||||
|
"",
|
||||||
|
);
|
||||||
await writeExecutable(
|
await writeExecutable(
|
||||||
path.join(sharedBinDir, "plutil"),
|
path.join(sharedBinDir, "plutil"),
|
||||||
`#!/usr/bin/env bash
|
`#!/usr/bin/env bash
|
||||||
@@ -94,6 +104,13 @@ PLIST
|
|||||||
fi
|
fi
|
||||||
exit 1`,
|
exit 1`,
|
||||||
);
|
);
|
||||||
|
sharedFakePythonPath = path.join(sharedHomeBinDir, "fake-python");
|
||||||
|
await writeExecutable(
|
||||||
|
sharedFakePythonPath,
|
||||||
|
`#!/usr/bin/env bash
|
||||||
|
printf 'AAAAA11111\\t0\\tAlpha Team\\r\\n'
|
||||||
|
printf 'BBBBB22222\\t0\\tBeta Team\\r\\n'`,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
@@ -103,33 +120,15 @@ exit 1`,
|
|||||||
await rm(fixtureRoot, { recursive: true, force: true });
|
await rm(fixtureRoot, { recursive: true, force: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
async function createHomeDir(): Promise<{ homeDir: string; binDir: string }> {
|
|
||||||
const homeDir = path.join(fixtureRoot, `case-${caseId++}`);
|
|
||||||
await mkdir(homeDir, { recursive: true });
|
|
||||||
const binDir = path.join(homeDir, "bin");
|
|
||||||
await mkdir(binDir, { recursive: true });
|
|
||||||
await mkdir(path.join(homeDir, "Library", "Preferences"), { recursive: true });
|
|
||||||
await writeFile(path.join(homeDir, "Library", "Preferences", "com.apple.dt.Xcode.plist"), "");
|
|
||||||
return { homeDir, binDir };
|
|
||||||
}
|
|
||||||
|
|
||||||
it("resolves fallback and preferred team IDs from Xcode team listings", async () => {
|
it("resolves fallback and preferred team IDs from Xcode team listings", async () => {
|
||||||
const { homeDir, binDir } = await createHomeDir();
|
const fallbackResult = runScript(sharedHomeDir, {
|
||||||
await writeExecutable(
|
IOS_PYTHON_BIN: sharedFakePythonPath,
|
||||||
path.join(binDir, "fake-python"),
|
|
||||||
`#!/usr/bin/env bash
|
|
||||||
printf 'AAAAA11111\\t0\\tAlpha Team\\r\\n'
|
|
||||||
printf 'BBBBB22222\\t0\\tBeta Team\\r\\n'`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const fallbackResult = runScript(homeDir, {
|
|
||||||
IOS_PYTHON_BIN: path.join(binDir, "fake-python"),
|
|
||||||
});
|
});
|
||||||
expect(fallbackResult.ok).toBe(true);
|
expect(fallbackResult.ok).toBe(true);
|
||||||
expect(fallbackResult.stdout).toBe("AAAAA11111");
|
expect(fallbackResult.stdout).toBe("AAAAA11111");
|
||||||
|
|
||||||
const crlfResult = runScript(homeDir, {
|
const crlfResult = runScript(sharedHomeDir, {
|
||||||
IOS_PYTHON_BIN: path.join(binDir, "fake-python"),
|
IOS_PYTHON_BIN: sharedFakePythonPath,
|
||||||
IOS_PREFERRED_TEAM_ID: "BBBBB22222",
|
IOS_PREFERRED_TEAM_ID: "BBBBB22222",
|
||||||
});
|
});
|
||||||
expect(crlfResult.ok).toBe(true);
|
expect(crlfResult.ok).toBe(true);
|
||||||
@@ -137,9 +136,7 @@ printf 'BBBBB22222\\t0\\tBeta Team\\r\\n'`,
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("prints actionable guidance when Xcode account exists but no Team ID is resolvable", async () => {
|
it("prints actionable guidance when Xcode account exists but no Team ID is resolvable", async () => {
|
||||||
const { homeDir } = await createHomeDir();
|
const result = runScript(sharedHomeDir);
|
||||||
|
|
||||||
const result = runScript(homeDir);
|
|
||||||
expect(result.ok).toBe(false);
|
expect(result.ok).toBe(false);
|
||||||
expect(
|
expect(
|
||||||
result.stderr.includes("An Apple account is signed in to Xcode") ||
|
result.stderr.includes("An Apple account is signed in to Xcode") ||
|
||||||
|
|||||||
Reference in New Issue
Block a user