Skip to content

Commit 08ec628

Browse files
committed
refactor: optimize code
1 parent 0090a01 commit 08ec628

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
@@ -633,7 +633,10 @@ describe("Simple Git Hooks tests", () => {
633633
describe("Env var PREFER_APPEND_FILE_SYNC tests", () => {
634634
const GIT_USER_NAME = "github-actions";
635635
const GIT_USER_EMAIL = "github-actions@github.com";
636-
const PRE_COMMIT_FILE_PATH = path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks", "pre-commit");
636+
const EXISTING_PRE_COMMIT_FILE_CONTENT = "test pre commit";
637+
const GIT_HOOKS_PATH = path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks");
638+
const PRE_COMMIT_FILE_PATH = path.join(GIT_HOOKS_PATH, "pre-commit");
639+
const ADD_PRE_COMMIT_FILE_CONTENT = "prettier --write";
637640

638641
const initializeGitRepository = (path) => {
639642
execSync(
@@ -646,7 +649,7 @@ describe("Simple Git Hooks tests", () => {
646649

647650
// replace pre commit content with 'test pre commit'
648651
const writePreCommitFile = () => {
649-
fs.writeFileSync(PRE_COMMIT_FILE_PATH, "test pre commit");
652+
fs.writeFileSync(PRE_COMMIT_FILE_PATH, EXISTING_PRE_COMMIT_FILE_CONTENT);
650653
}
651654

652655
beforeEach(() => {
@@ -663,37 +666,25 @@ describe("Simple Git Hooks tests", () => {
663666
process.env.PREFER_APPEND_FILE_SYNC = 1;
664667
writePreCommitFile();
665668
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
666-
const installedHooks = getInstalledGitHooks(
667-
path.normalize(
668-
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
669-
)
670-
);
671-
expect(installedHooks["pre-commit"]).toBe(`test pre commit
672-
${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
669+
const installedHooks = getInstalledGitHooks(GIT_HOOKS_PATH);
670+
expect(installedHooks["pre-commit"]).toBe(`${EXISTING_PRE_COMMIT_FILE_CONTENT}
671+
${simpleGitHooks.PREPEND_SCRIPT}${ADD_PRE_COMMIT_FILE_CONTENT}`);
673672
})
674673

675674
it("should append to the hook file when PREFER_APPEND_FILE_SYNC is true", () => {
676675
process.env.PREFER_APPEND_FILE_SYNC = "true";
677676
writePreCommitFile();
678677
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
679-
const installedHooks = getInstalledGitHooks(
680-
path.normalize(
681-
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
682-
)
683-
);
684-
expect(installedHooks["pre-commit"]).toBe(`test pre commit
685-
${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
678+
const installedHooks = getInstalledGitHooks(GIT_HOOKS_PATH);
679+
expect(installedHooks["pre-commit"]).toBe(`${EXISTING_PRE_COMMIT_FILE_CONTENT}
680+
${simpleGitHooks.PREPEND_SCRIPT}${ADD_PRE_COMMIT_FILE_CONTENT}`);
686681
})
687682

688683
it("should write to the hook file when PREFER_APPEND_FILE_SYNC is not true", () => {
689684
writePreCommitFile();
690685
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
691-
const installedHooks = getInstalledGitHooks(
692-
path.normalize(
693-
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
694-
)
695-
);
696-
expect(installedHooks["pre-commit"]).toBe(`${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
686+
const installedHooks = getInstalledGitHooks(GIT_HOOKS_PATH);
687+
expect(installedHooks["pre-commit"]).toBe(`${simpleGitHooks.PREPEND_SCRIPT}${ADD_PRE_COMMIT_FILE_CONTENT}`);
697688
})
698689
})
699690

0 commit comments

Comments
 (0)