Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions _tests/project_with_custom_append_file_sync_env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"simple-git-hooks": {
"pre-commit": "prettier --write"
},
"devDependencies": {
"simple-pre-commit": "1.0.0"
}
}
8 changes: 7 additions & 1 deletion simple-git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
fs.mkdirSync(normalizedHookDirectory)
}

fs.writeFileSync(hookPath, hookCommand)
// use PREFER_APPEND_FILE_SYNC env to append hook command into file
if (['1', 'true'].includes(process.env.PREFER_APPEND_FILE_SYNC)) {
fs.appendFileSync(hookPath, `
${hookCommand}`)
} else {
fs.writeFileSync(hookPath, hookCommand)
}
fs.chmodSync(hookPath, 0o0755)

console.info(`[INFO] Successfully set the ${hook} with command: ${command}`)
Expand Down
63 changes: 63 additions & 0 deletions simple-git-hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ describe("Simple Git Hooks tests", () => {
const PROJECT_WITH_CUSTOM_CONF = path.normalize(
path.join(testsFolder, "project_with_custom_configuration")
);
const PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV = path.normalize(
path.join(testsFolder, "project_with_custom_append_file_sync_env")
)

// Incorrect configurations
const PROJECT_WITH_BAD_CONF_IN_PACKAGE_JSON_ = path.normalize(
Expand Down Expand Up @@ -627,6 +630,66 @@ describe("Simple Git Hooks tests", () => {
})
});

describe("Env var PREFER_APPEND_FILE_SYNC tests", () => {
const GIT_USER_NAME = "github-actions";
const GIT_USER_EMAIL = "github-actions@github.com";
const EXISTING_PRE_COMMIT_FILE_CONTENT = "test pre commit";
const GIT_HOOKS_PATH = path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks");
const PRE_COMMIT_FILE_PATH = path.join(GIT_HOOKS_PATH, "pre-commit");
const ADD_PRE_COMMIT_FILE_CONTENT = "prettier --write";

const initializeGitRepository = (path) => {
execSync(
`git init \
&& git config user.name ${GIT_USER_NAME} \
&& git config user.email ${GIT_USER_EMAIL}`,
{ cwd: path }
);
};

// replace pre commit content with 'test pre commit'
const writePreCommitFile = () => {
fs.writeFileSync(PRE_COMMIT_FILE_PATH, EXISTING_PRE_COMMIT_FILE_CONTENT);
}

beforeEach(() => {
initializeGitRepository(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
createGitHooksFolder(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
});

describe("PREFER_APPEND_FILE_SYNC", () => {
afterEach(() => {
delete process.env.PREFER_APPEND_FILE_SYNC;
});

it("should append to the hook file when PREFER_APPEND_FILE_SYNC is 1", () => {
process.env.PREFER_APPEND_FILE_SYNC = 1;
writePreCommitFile();
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
const installedHooks = getInstalledGitHooks(GIT_HOOKS_PATH);
expect(installedHooks["pre-commit"]).toBe(`${EXISTING_PRE_COMMIT_FILE_CONTENT}
${simpleGitHooks.PREPEND_SCRIPT}${ADD_PRE_COMMIT_FILE_CONTENT}`);
})

it("should append to the hook file when PREFER_APPEND_FILE_SYNC is true", () => {
process.env.PREFER_APPEND_FILE_SYNC = "true";
writePreCommitFile();
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
const installedHooks = getInstalledGitHooks(GIT_HOOKS_PATH);
expect(installedHooks["pre-commit"]).toBe(`${EXISTING_PRE_COMMIT_FILE_CONTENT}
${simpleGitHooks.PREPEND_SCRIPT}${ADD_PRE_COMMIT_FILE_CONTENT}`);
})

it("should write to the hook file when PREFER_APPEND_FILE_SYNC is not true", () => {
writePreCommitFile();
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
const installedHooks = getInstalledGitHooks(GIT_HOOKS_PATH);
expect(installedHooks["pre-commit"]).toBe(`${simpleGitHooks.PREPEND_SCRIPT}${ADD_PRE_COMMIT_FILE_CONTENT}`);
})
})

})

afterEach(() => {
[
PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT,
Expand Down