|
| 1 | +import { existsSync, readFileSync, writeFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | + |
| 4 | +import { |
| 5 | + createProjectForTestFile, |
| 6 | + deleteTestProject, |
| 7 | +} from "../tests/test-project"; |
| 8 | +import { GitService } from "./GitService"; |
| 9 | +import { HooksService } from "./HooksService"; |
| 10 | + |
| 11 | +// Set to false to avoid using the cache |
| 12 | +const useCache = true; |
| 13 | +// Set to false to inspect the test project directory after the test |
| 14 | +const shouldCleanupAfterTest = true; |
| 15 | + |
| 16 | +describe("HooksService", () => { |
| 17 | + let testProjectDir: string; |
| 18 | + |
| 19 | + beforeAll(() => { |
| 20 | + if (!useCache) { |
| 21 | + console.warn("Cache is disabled. Enable it one dev is done."); |
| 22 | + } |
| 23 | + if (!shouldCleanupAfterTest) { |
| 24 | + console.warn("Cleanup is disabled. Enable it one dev is done."); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + beforeEach(async () => { |
| 29 | + testProjectDir = await createProjectForTestFile(__filename, useCache); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(async () => { |
| 33 | + if (shouldCleanupAfterTest) { |
| 34 | + await deleteTestProject(__filename); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + describe("consolidateManagedGitHooks", () => { |
| 39 | + it("should keep the previous managed command as legacy when a hook changes", () => { |
| 40 | + const managedGitHooks = new Map(); |
| 41 | + |
| 42 | + HooksService.consolidateManagedGitHooks( |
| 43 | + testProjectDir, |
| 44 | + [{ name: "pre-commit", command: "echo old;" }], |
| 45 | + managedGitHooks, |
| 46 | + ); |
| 47 | + HooksService.consolidateManagedGitHooks( |
| 48 | + testProjectDir, |
| 49 | + [{ name: "pre-commit", command: "echo new;" }], |
| 50 | + managedGitHooks, |
| 51 | + ); |
| 52 | + |
| 53 | + expect(Array.from(managedGitHooks.values())).toEqual([ |
| 54 | + { |
| 55 | + name: "pre-commit", |
| 56 | + command: "echo new;", |
| 57 | + legacyCommands: ["echo old;"], |
| 58 | + }, |
| 59 | + ]); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe("applyManagedGitHooks", () => { |
| 64 | + it("should update a managed hook from a legacy command", async () => { |
| 65 | + vi.spyOn(GitService, "isGitRepository").mockResolvedValue(true); |
| 66 | + |
| 67 | + const gitHookFilePath = join( |
| 68 | + testProjectDir, |
| 69 | + ".git", |
| 70 | + "hooks", |
| 71 | + "pre-commit", |
| 72 | + ); |
| 73 | + writeFileSync( |
| 74 | + gitHookFilePath, |
| 75 | + GitService.GIT_HOOK_TEMPLATE.replace("%gitHookCommand%", "echo old;"), |
| 76 | + ); |
| 77 | + |
| 78 | + await HooksService.applyManagedGitHooks(testProjectDir, [ |
| 79 | + { |
| 80 | + name: "pre-commit", |
| 81 | + command: "echo new;", |
| 82 | + legacyCommands: ["echo old;"], |
| 83 | + }, |
| 84 | + ]); |
| 85 | + |
| 86 | + expect(existsSync(gitHookFilePath)).toBe(true); |
| 87 | + expect(readFileSync(gitHookFilePath, "utf-8")).toBe(`#!/bin/sh |
| 88 | +
|
| 89 | +# Created by ts-dev-tools (https://escemi-tech.github.io/ts-dev-tools/) |
| 90 | +
|
| 91 | +echo new;`); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments