Skip to content

Commit 4166ba5

Browse files
committed
refactor: optimize code
1 parent 3261d69 commit 4166ba5

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

simple-git-hooks.test.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,10 @@ describe("Simple Git Hooks tests", () => {
595595
describe("Env var PREFER_APPEND_FILE_SYNC tests", () => {
596596
const GIT_USER_NAME = "github-actions";
597597
const GIT_USER_EMAIL = "github-actions@github.com";
598-
const PRE_COMMIT_FILE_PATH = path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks", "pre-commit");
598+
const EXISTING_PRE_COMMIT_FILE_CONTENT = "test pre commit";
599+
const GIT_HOOKS_PATH = path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks");
600+
const PRE_COMMIT_FILE_PATH = path.join(GIT_HOOKS_PATH, "pre-commit");
601+
const ADD_PRE_COMMIT_FILE_CONTENT = "prettier --write";
599602

600603
const initializeGitRepository = (path) => {
601604
execSync(
@@ -608,7 +611,7 @@ describe("Simple Git Hooks tests", () => {
608611

609612
// replace pre commit content with 'test pre commit'
610613
const writePreCommitFile = () => {
611-
fs.writeFileSync(PRE_COMMIT_FILE_PATH, "test pre commit");
614+
fs.writeFileSync(PRE_COMMIT_FILE_PATH, EXISTING_PRE_COMMIT_FILE_CONTENT);
612615
}
613616

614617
beforeEach(() => {
@@ -625,37 +628,25 @@ describe("Simple Git Hooks tests", () => {
625628
process.env.PREFER_APPEND_FILE_SYNC = 1;
626629
writePreCommitFile();
627630
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
628-
const installedHooks = getInstalledGitHooks(
629-
path.normalize(
630-
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
631-
)
632-
);
633-
expect(installedHooks["pre-commit"]).toBe(`test pre commit
634-
${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
631+
const installedHooks = getInstalledGitHooks(GIT_HOOKS_PATH);
632+
expect(installedHooks["pre-commit"]).toBe(`${EXISTING_PRE_COMMIT_FILE_CONTENT}
633+
${simpleGitHooks.PREPEND_SCRIPT}${ADD_PRE_COMMIT_FILE_CONTENT}`);
635634
})
636635

637636
it("should append to the hook file when PREFER_APPEND_FILE_SYNC is true", () => {
638637
process.env.PREFER_APPEND_FILE_SYNC = "true";
639638
writePreCommitFile();
640639
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
641-
const installedHooks = getInstalledGitHooks(
642-
path.normalize(
643-
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
644-
)
645-
);
646-
expect(installedHooks["pre-commit"]).toBe(`test pre commit
647-
${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
640+
const installedHooks = getInstalledGitHooks(GIT_HOOKS_PATH);
641+
expect(installedHooks["pre-commit"]).toBe(`${EXISTING_PRE_COMMIT_FILE_CONTENT}
642+
${simpleGitHooks.PREPEND_SCRIPT}${ADD_PRE_COMMIT_FILE_CONTENT}`);
648643
})
649644

650645
it("should write to the hook file when PREFER_APPEND_FILE_SYNC is not true", () => {
651646
writePreCommitFile();
652647
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
653-
const installedHooks = getInstalledGitHooks(
654-
path.normalize(
655-
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
656-
)
657-
);
658-
expect(installedHooks["pre-commit"]).toBe(`${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
648+
const installedHooks = getInstalledGitHooks(GIT_HOOKS_PATH);
649+
expect(installedHooks["pre-commit"]).toBe(`${simpleGitHooks.PREPEND_SCRIPT}${ADD_PRE_COMMIT_FILE_CONTENT}`);
659650
})
660651
})
661652

0 commit comments

Comments
 (0)