|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +import { initGitRepository } from "./init-git.js"; |
| 9 | + |
| 10 | +const git = (args: string[], cwd: string): string => |
| 11 | + execFileSync("git", args, { cwd, encoding: "utf8" }).trim(); |
| 12 | + |
| 13 | +describe("initGitRepository", () => { |
| 14 | + let root = ""; |
| 15 | + |
| 16 | + beforeEach(async () => { |
| 17 | + root = await mkdtemp(join(tmpdir(), "create-vitnode-git-")); |
| 18 | + await writeFile(join(root, "package.json"), "{}\n"); |
| 19 | + process.env.GIT_AUTHOR_NAME = "VitNode"; |
| 20 | + process.env.GIT_AUTHOR_EMAIL = "test@vitnode.com"; |
| 21 | + process.env.GIT_COMMITTER_NAME = "VitNode"; |
| 22 | + process.env.GIT_COMMITTER_EMAIL = "test@vitnode.com"; |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(async () => { |
| 26 | + await rm(root, { force: true, recursive: true }); |
| 27 | + }); |
| 28 | + |
| 29 | + it("creates a repository on the main branch with an initial commit", async () => { |
| 30 | + await expect(initGitRepository({ root })).resolves.toBe("created"); |
| 31 | + |
| 32 | + expect(existsSync(join(root, ".git"))).toBe(true); |
| 33 | + expect(git(["rev-parse", "--abbrev-ref", "HEAD"], root)).toBe("main"); |
| 34 | + expect(git(["rev-list", "--count", "HEAD"], root)).toBe("1"); |
| 35 | + expect(git(["show", "--name-only", "--format=", "HEAD"], root)).toContain( |
| 36 | + "package.json", |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("honours a custom branch name", async () => { |
| 41 | + await expect(initGitRepository({ root, branch: "trunk" })).resolves.toBe( |
| 42 | + "created", |
| 43 | + ); |
| 44 | + |
| 45 | + expect(git(["rev-parse", "--abbrev-ref", "HEAD"], root)).toBe("trunk"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("leaves an existing repository alone", async () => { |
| 49 | + git(["init"], root); |
| 50 | + |
| 51 | + await expect(initGitRepository({ root })).resolves.toBe( |
| 52 | + "already-in-repository", |
| 53 | + ); |
| 54 | + expect(git(["rev-list", "--count", "--all"], root)).toBe("0"); |
| 55 | + }); |
| 56 | +}); |
0 commit comments