@@ -173,6 +173,9 @@ describe("Simple Git Hooks tests", () => {
173173 const PROJECT_WITH_CUSTOM_CONF = path . normalize (
174174 path . join ( testsFolder , "project_with_custom_configuration" )
175175 ) ;
176+ const PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV = path . normalize (
177+ path . join ( testsFolder , "project_with_custom_append_file_sync_env" )
178+ )
176179
177180 // Incorrect configurations
178181 const PROJECT_WITH_BAD_CONF_IN_PACKAGE_JSON_ = path . normalize (
@@ -627,6 +630,75 @@ describe("Simple Git Hooks tests", () => {
627630 } )
628631 } ) ;
629632
633+ describe ( "Env var PREFER_APPEND_FILE_SYNC tests" , ( ) => {
634+ const GIT_USER_NAME = "github-actions" ;
635+ 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" ) ;
637+
638+ const initializeGitRepository = ( path ) => {
639+ execSync (
640+ `git init \
641+ && git config user.name ${ GIT_USER_NAME } \
642+ && git config user.email ${ GIT_USER_EMAIL } ` ,
643+ { cwd : path }
644+ ) ;
645+ } ;
646+
647+ // replace pre commit content with 'test pre commit'
648+ const writePreCommitFile = ( ) => {
649+ fs . writeFileSync ( PRE_COMMIT_FILE_PATH , "test pre commit" ) ;
650+ }
651+
652+ beforeEach ( ( ) => {
653+ initializeGitRepository ( PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV ) ;
654+ createGitHooksFolder ( PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV ) ;
655+ } ) ;
656+
657+ describe ( "PREFER_APPEND_FILE_SYNC" , ( ) => {
658+ afterEach ( ( ) => {
659+ delete process . env . PREFER_APPEND_FILE_SYNC ;
660+ } ) ;
661+
662+ it ( "should append to the hook file when PREFER_APPEND_FILE_SYNC is 1" , ( ) => {
663+ process . env . PREFER_APPEND_FILE_SYNC = 1 ;
664+ writePreCommitFile ( ) ;
665+ 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 .`) ;
673+ } )
674+
675+ it ( "should append to the hook file when PREFER_APPEND_FILE_SYNC is true" , ( ) => {
676+ process . env . PREFER_APPEND_FILE_SYNC = "true" ;
677+ writePreCommitFile ( ) ;
678+ 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 .`) ;
686+ } )
687+
688+ it ( "should write to the hook file when PREFER_APPEND_FILE_SYNC is not true" , ( ) => {
689+ writePreCommitFile ( ) ;
690+ 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 .` ) ;
697+ } )
698+ } )
699+
700+ } )
701+
630702 afterEach ( ( ) => {
631703 [
632704 PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT ,
0 commit comments