@@ -165,6 +165,9 @@ describe("Simple Git Hooks tests", () => {
165165 const PROJECT_WITH_CUSTOM_CONF = path . normalize (
166166 path . join ( testsFolder , "project_with_custom_configuration" )
167167 ) ;
168+ const PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV = path . normalize (
169+ path . join ( testsFolder , "project_with_custom_append_file_sync_env" )
170+ )
168171
169172 // Incorrect configurations
170173 const PROJECT_WITH_BAD_CONF_IN_PACKAGE_JSON_ = path . normalize (
@@ -589,6 +592,75 @@ describe("Simple Git Hooks tests", () => {
589592 } )
590593 } ) ;
591594
595+ describe ( "Env var PREFER_APPEND_FILE_SYNC tests" , ( ) => {
596+ const GIT_USER_NAME = "github-actions" ;
597+ 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" ) ;
599+
600+ const initializeGitRepository = ( path ) => {
601+ execSync (
602+ `git init \
603+ && git config user.name ${ GIT_USER_NAME } \
604+ && git config user.email ${ GIT_USER_EMAIL } ` ,
605+ { cwd : path }
606+ ) ;
607+ } ;
608+
609+ // replace pre commit content with 'test pre commit'
610+ const writePreCommitFile = ( ) => {
611+ fs . writeFileSync ( PRE_COMMIT_FILE_PATH , "test pre commit" ) ;
612+ }
613+
614+ beforeEach ( ( ) => {
615+ initializeGitRepository ( PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV ) ;
616+ createGitHooksFolder ( PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV ) ;
617+ } ) ;
618+
619+ describe ( "PREFER_APPEND_FILE_SYNC" , ( ) => {
620+ afterEach ( ( ) => {
621+ delete process . env . PREFER_APPEND_FILE_SYNC ;
622+ } ) ;
623+
624+ it ( "should append to the hook file when PREFER_APPEND_FILE_SYNC is 1" , ( ) => {
625+ process . env . PREFER_APPEND_FILE_SYNC = 1 ;
626+ writePreCommitFile ( ) ;
627+ 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 .`) ;
635+ } )
636+
637+ it ( "should append to the hook file when PREFER_APPEND_FILE_SYNC is true" , ( ) => {
638+ process . env . PREFER_APPEND_FILE_SYNC = "true" ;
639+ writePreCommitFile ( ) ;
640+ 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 .`) ;
648+ } )
649+
650+ it ( "should write to the hook file when PREFER_APPEND_FILE_SYNC is not true" , ( ) => {
651+ writePreCommitFile ( ) ;
652+ 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 .` ) ;
659+ } )
660+ } )
661+
662+ } )
663+
592664 afterEach ( ( ) => {
593665 [
594666 PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT ,
0 commit comments