mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
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 { 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) => {
|
||||
return execFileSync(cmd, args, {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
env: env ? { ...process.env, ...env } : process.env,
|
||||
env: { ...process.env, ...baseGitEnv, ...env },
|
||||
}).trim();
|
||||
};
|
||||
|
||||
describe("git-hooks/pre-commit (integration)", () => {
|
||||
it("does not treat staged filenames as git-add flags (e.g. --all)", () => {
|
||||
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.
|
||||
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";
|
||||
let fixtureRoot = "";
|
||||
let sharedBinDir = "";
|
||||
let caseId = 0;
|
||||
let sharedHomeDir = "";
|
||||
let sharedHomeBinDir = "";
|
||||
let sharedFakePythonPath = "";
|
||||
|
||||
async function writeExecutable(filePath: string, body: string): Promise<void> {
|
||||
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-"));
|
||||
sharedBinDir = path.join(fixtureRoot, "shared-bin");
|
||||
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(
|
||||
path.join(sharedBinDir, "plutil"),
|
||||
`#!/usr/bin/env bash
|
||||
@@ -94,6 +104,13 @@ PLIST
|
||||
fi
|
||||
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 () => {
|
||||
@@ -103,33 +120,15 @@ exit 1`,
|
||||
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 () => {
|
||||
const { homeDir, binDir } = await createHomeDir();
|
||||
await writeExecutable(
|
||||
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"),
|
||||
const fallbackResult = runScript(sharedHomeDir, {
|
||||
IOS_PYTHON_BIN: sharedFakePythonPath,
|
||||
});
|
||||
expect(fallbackResult.ok).toBe(true);
|
||||
expect(fallbackResult.stdout).toBe("AAAAA11111");
|
||||
|
||||
const crlfResult = runScript(homeDir, {
|
||||
IOS_PYTHON_BIN: path.join(binDir, "fake-python"),
|
||||
const crlfResult = runScript(sharedHomeDir, {
|
||||
IOS_PYTHON_BIN: sharedFakePythonPath,
|
||||
IOS_PREFERRED_TEAM_ID: "BBBBB22222",
|
||||
});
|
||||
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 () => {
|
||||
const { homeDir } = await createHomeDir();
|
||||
|
||||
const result = runScript(homeDir);
|
||||
const result = runScript(sharedHomeDir);
|
||||
expect(result.ok).toBe(false);
|
||||
expect(
|
||||
result.stderr.includes("An Apple account is signed in to Xcode") ||
|
||||
|
||||
Reference in New Issue
Block a user