Skip to content

Commit c38de4b

Browse files
committed
refactor: migrate commit and add tests to use shared command helpers
- Replace execFileSync calls with zagi() and git() helpers from shared.ts - Remove duplicate ZAGI_BIN path resolution and runCommand() wrappers - Simplify CommandResult handling by returning output strings directly - Remove runWithEnv() wrapper in favor of env option in shared helpers - Add createTestRepo() and cleanupTestRepo() for isolated test scenarios - Update assertions to check output strings instead of result objects - Ad
1 parent a0cac78 commit c38de4b

7 files changed

Lines changed: 449 additions & 661 deletions

File tree

test/src/add.test.ts

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
11
import { describe, test, expect, beforeEach, afterEach } from "vitest";
2-
import { execFileSync } from "child_process";
3-
import { resolve } from "path";
42
import { rmSync } from "fs";
53
import { createFixtureRepo } from "../fixtures/setup";
4+
import { zagi, git } from "./shared";
65

7-
const ZAGI_BIN = resolve(__dirname, "../../zig-out/bin/zagi");
86
let REPO_DIR: string;
97

10-
interface CommandResult {
11-
output: string;
12-
exitCode: number;
13-
}
14-
15-
function runCommand(cmd: string, args: string[], expectFail = false): CommandResult {
16-
try {
17-
const output = execFileSync(cmd, args, {
18-
cwd: REPO_DIR,
19-
encoding: "utf-8",
20-
});
21-
return { output, exitCode: 0 };
22-
} catch (e: any) {
23-
if (!expectFail) throw e;
24-
return {
25-
output: e.stderr || e.stdout || "",
26-
exitCode: e.status || 1,
27-
};
28-
}
29-
}
30-
318
beforeEach(() => {
329
REPO_DIR = createFixtureRepo();
3310
});
@@ -40,35 +17,34 @@ afterEach(() => {
4017

4118
describe("zagi add", () => {
4219
test("shows confirmation after adding file", () => {
43-
const result = runCommand(ZAGI_BIN, ["add", "src/new-file.ts"]);
20+
const result = zagi(["add", "src/new-file.ts"], { cwd: REPO_DIR });
4421

45-
expect(result.output).toContain("staged:");
46-
expect(result.output).toContain("A ");
47-
expect(result.output).toContain("new-file.ts");
22+
expect(result).toContain("staged:");
23+
expect(result).toContain("A ");
24+
expect(result).toContain("new-file.ts");
4825
});
4926

5027
test("shows count of staged files", () => {
51-
const result = runCommand(ZAGI_BIN, ["add", "src/new-file.ts"]);
28+
const result = zagi(["add", "src/new-file.ts"], { cwd: REPO_DIR });
5229

53-
expect(result.output).toMatch(/staged: \d+ file/);
30+
expect(result).toMatch(/staged: \d+ file/);
5431
});
5532

5633
test("error message is concise for missing file", () => {
57-
const zagi = runCommand(ZAGI_BIN, ["add", "nonexistent.txt"], true);
34+
const result = zagi(["add", "nonexistent.txt"], { cwd: REPO_DIR });
5835

59-
expect(zagi.output).toBe("error: file not found\n");
60-
expect(zagi.exitCode).toBe(128);
36+
expect(result).toBe("error: file not found\n");
6137
});
6238

6339
test("git add is silent on success", () => {
64-
const git = runCommand("git", ["add", "src/new-file.ts"]);
40+
const result = git(["add", "src/new-file.ts"], { cwd: REPO_DIR });
6541

66-
expect(git.output).toBe("");
42+
expect(result).toBe("");
6743
});
6844

6945
test("zagi add provides feedback", () => {
70-
const zagi = runCommand(ZAGI_BIN, ["add", "src/new-file.ts"]);
46+
const result = zagi(["add", "src/new-file.ts"], { cwd: REPO_DIR });
7147

72-
expect(zagi.output.length).toBeGreaterThan(0);
48+
expect(result.length).toBeGreaterThan(0);
7349
});
7450
});

0 commit comments

Comments
 (0)