-
Notifications
You must be signed in to change notification settings - Fork 0
Add test suites for core tools #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,245 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { afterEach, beforeEach, describe, expect, it } from "bun:test"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { realpathSync } from "node:fs"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { tmpdir } from "node:os"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { dirname, join, resolve } from "node:path"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Path to the script to be tested | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const scriptPath = resolve(__dirname, "./index.ts"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface ExecResult { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdout: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stderr: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exitCode: number | null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async function runScript(args: string[], cwd?: string): Promise<ExecResult> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const proc = Bun.spawn({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cmd: ["bun", "run", scriptPath, ...args], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cwd: cwd || process.cwd(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| env: { ...process.env }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdio: ["ignore", "pipe", "pipe"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const stdout = await new Response(proc.stdout as ReadableStream<Uint8Array>).text(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const stderr = await new Response(proc.stderr as ReadableStream<Uint8Array>).text(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const exitCode = await proc.exited; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { stdout, stderr, exitCode }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Helper to run git commands in a specific directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async function runGit(args: string[], cwd: string): Promise<ExecResult> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const proc = Bun.spawn({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cmd: ["git", ...args], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cwd, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdio: ["ignore", "pipe", "pipe"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const stdout = await new Response(proc.stdout as ReadableStream<Uint8Array>).text(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const stderr = await new Response(proc.stderr as ReadableStream<Uint8Array>).text(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const exitCode = await proc.exited; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (exitCode !== 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error(`Git command [git ${args.join(" ")}] failed in ${cwd}:\nSTDOUT: ${stdout}\nSTDERR: ${stderr}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { stdout, stderr, exitCode }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fail fast when a git setup step fails.
🛠️ Suggested change const stdout = await new Response(proc.stdout as ReadableStream<Uint8Array>).text();
const stderr = await new Response(proc.stderr as ReadableStream<Uint8Array>).text();
const exitCode = await proc.exited;
if (exitCode !== 0) {
- console.error(`Git command [git ${args.join(" ")}] failed in ${cwd}:\nSTDOUT: ${stdout}\nSTDERR: ${stderr}`);
+ throw new Error(
+ `git ${args.join(" ")} failed in ${cwd}\nstdout: ${stdout}\nstderr: ${stderr}`
+ );
}
return { stdout, stderr, exitCode };As per coding guidelines: Use 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe("git-last-commits-diff", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let testRepoDir: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let originalCwd: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| beforeEach(async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| originalCwd = process.cwd(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const baseTmpDir = realpathSync(tmpdir()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testRepoDir = await mkdtemp(join(baseTmpDir, "test-git-diff-")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.chdir(testRepoDir); // Change CWD to the repo for script execution context | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Initialize Git repository | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await runGit(["init", "-b", "main"], testRepoDir); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await runGit(["config", "user.name", "Test User"], testRepoDir); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await runGit(["config", "user.email", "test@example.com"], testRepoDir); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await runGit(["config", "commit.gpgsign", "false"], testRepoDir); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| afterEach(async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.chdir(originalCwd); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (testRepoDir) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await rm(testRepoDir, { recursive: true, force: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Ensure env var is cleaned up if a test fails before deleting it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete process.env.TEST_MODE_CLIPBOARD_OUTPUT_FILE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const setupCommits = async (commitDetails: Array<{ files: Record<string, string>; message: string }>) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const commit of commitDetails) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const [file, content] of Object.entries(commit.files)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dir = dirname(file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (dir !== ".") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await mkdir(join(testRepoDir, dir), { recursive: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await writeFile(join(testRepoDir, file), content); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await runGit(["add", file], testRepoDir); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await runGit(["commit", "-m", commit.message], testRepoDir); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { stdout: _log } = await runGit(["log", "--oneline"], testRepoDir); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // console.log("Repo log after setup:\n", log); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it("should show help with --help flag", async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { stdout, exitCode } = await runScript(["--help"], originalCwd); // Run from original CWD if script expects repo path as arg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(stdout).toContain("Usage: tools git-last-commits-diff <directory>"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it("should show help and exit with 1 if no directory is provided", async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { stdout, exitCode } = await runScript([], originalCwd); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(stdout).toContain("Usage: tools git-last-commits-diff <directory>"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it("should exit with error for invalid --commits value", async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await setupCommits([{ files: { "a.txt": "1" }, message: "c1" }]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let result = await runScript([testRepoDir, "--commits", "0"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.exitCode).toBe(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.stderr).toContain("Error: --commits value must be a positive integer."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = await runScript([testRepoDir, "--commits", "abc"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.exitCode).toBe(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.stderr).toContain("Error: --commits value must be a positive integer."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+105
to
+109
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.stderr).toContain("Error: --commits value must be a positive integer."); | |
| result = await runScript([testRepoDir, "--commits", "abc"]); | |
| expect(result.exitCode).toBe(1); | |
| expect(result.stderr).toContain("Error: --commits value must be a positive integer."); | |
| expect(result.stdout).toContain("✖ Error: --commits value must be a positive integer."); | |
| result = await runScript([testRepoDir, "--commits", "abc"]); | |
| expect(result.exitCode).toBe(1); | |
| expect(result.stdout).toContain("✖ Error: --commits value must be a positive integer."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__dirnameis used to buildscriptPath, but this repo runs as ESM and__dirnameis not defined at runtime. Compute the directory fromimport.meta.url(as done insrc/logger.ts) before resolvingindex.ts.