@@ -449,11 +449,16 @@ describe("config reads", () => {
449449 type TestError = GitCommandError | PlatformError . PlatformError ;
450450 interface ConfigChange {
451451 readonly name : string ;
452- // Prepares the repository at `cwd` and returns the directory to read from.
453- readonly setup : ( cwd : string ) => Effect . Effect < string , TestError , Scope . Scope | TestServices > ;
452+ // Prepares the repository at `cwd` and returns the directory to read from. `env` is the
453+ // environment the test runs Git in, which setup and change may extend.
454+ readonly setup : (
455+ cwd : string ,
456+ env : NodeJS . ProcessEnv ,
457+ ) => Effect . Effect < string , TestError , Scope . Scope | TestServices > ;
454458 readonly change : (
455459 cwd : string ,
456460 readCwd : string ,
461+ env : NodeJS . ProcessEnv ,
457462 ) => Effect . Effect < void , TestError , Scope . Scope | TestServices > ;
458463 readonly read : (
459464 driver : GitVcsDriver . GitVcsDriver [ "Service" ] ,
@@ -468,39 +473,26 @@ describe("config reads", () => {
468473 driver . readConfigValue ( cwd , key ) ;
469474 const readPrimaryRemote = ( driver : GitVcsDriver . GitVcsDriver [ "Service" ] , cwd : string ) =>
470475 driver . resolvePrimaryRemoteName ( cwd ) . pipe ( Effect . orElseSucceed ( ( ) => null ) ) ;
471- const setEnv = ( name : string , value : string ) =>
476+ // An empty safe.directory drops any configured exception, so Git checks owners.
477+ const trustNoOtherOwner = ( env : NodeJS . ProcessEnv ) =>
478+ Effect . sync ( ( ) => {
479+ Object . assign ( env , {
480+ GIT_CONFIG_COUNT : "1" ,
481+ GIT_CONFIG_KEY_0 : "safe.directory" ,
482+ GIT_CONFIG_VALUE_0 : "" ,
483+ } ) ;
484+ } ) ;
485+ const changeOwner = ( env : NodeJS . ProcessEnv ) => ( file : string ) =>
472486 Effect . acquireRelease (
473487 Effect . sync ( ( ) => {
474- const previous = process . env [ name ] ;
475- process . env [ name ] = value ;
476- return previous ;
488+ statOwner . path = file ;
489+ env . GIT_TEST_ASSUME_DIFFERENT_OWNER = "1" ;
477490 } ) ,
478- ( previous ) =>
491+ ( ) =>
479492 Effect . sync ( ( ) => {
480- if ( previous === undefined ) Reflect . deleteProperty ( process . env , name ) ;
481- else process . env [ name ] = previous ;
493+ statOwner . path = null ;
482494 } ) ,
483495 ) ;
484- const setGlobalConfig = ( file : string ) => setEnv ( "GIT_CONFIG_GLOBAL" , file ) ;
485- // An empty safe.directory drops any configured exception, so Git checks owners.
486- const trustNoOtherOwner = Effect . all ( [
487- setEnv ( "GIT_CONFIG_COUNT" , "1" ) ,
488- setEnv ( "GIT_CONFIG_KEY_0" , "safe.directory" ) ,
489- setEnv ( "GIT_CONFIG_VALUE_0" , "" ) ,
490- ] ) ;
491- const changeOwner = ( file : string ) =>
492- Effect . all ( [
493- Effect . acquireRelease (
494- Effect . sync ( ( ) => {
495- statOwner . path = file ;
496- } ) ,
497- ( ) =>
498- Effect . sync ( ( ) => {
499- statOwner . path = null ;
500- } ) ,
501- ) ,
502- setEnv ( "GIT_TEST_ASSUME_DIFFERENT_OWNER" , "1" ) ,
503- ] ) . pipe ( Effect . asVoid ) ;
504496 const writeInclude = ( file : string , value : string ) =>
505497 Effect . sync ( ( ) => NodeFS . writeFileSync ( file , `[t3]\n\tvalue = ${ value } \n` ) ) ;
506498 const changes : ReadonlyArray < ConfigChange > = [
@@ -596,14 +588,14 @@ describe("config reads", () => {
596588 } ,
597589 {
598590 name : "an edit to the global config" ,
599- setup : ( cwd ) =>
591+ setup : ( cwd , env ) =>
600592 Effect . gen ( function * ( ) {
601593 const global = NodePath . join ( yield * makeTmpDir ( "git-global-" ) , "gitconfig" ) ;
602594 yield * writeInclude ( global , "first" ) ;
603- yield * setGlobalConfig ( global ) ;
595+ env . GIT_CONFIG_GLOBAL = global ;
604596 return cwd ;
605597 } ) ,
606- change : ( ) => writeInclude ( process . env . GIT_CONFIG_GLOBAL ! , "second" ) ,
598+ change : ( _cwd , _readCwd , env ) => writeInclude ( env . GIT_CONFIG_GLOBAL ! , "second" ) ,
607599 read : readValue ( "t3.value" ) ,
608600 before : "first" ,
609601 after : "second" ,
@@ -679,32 +671,32 @@ describe("config reads", () => {
679671 } ) ) ) ,
680672 {
681673 name : "a new owner of the top level above the directory read from" ,
682- setup : ( cwd ) =>
674+ setup : ( cwd , env ) =>
683675 Effect . gen ( function * ( ) {
684676 yield * git ( cwd , [ "config" , "t3.value" , "first" ] ) ;
685- yield * trustNoOtherOwner ;
677+ yield * trustNoOtherOwner ( env ) ;
686678 NodeFS . mkdirSync ( NodePath . join ( cwd , "sub" ) ) ;
687679 return NodePath . join ( cwd , "sub" ) ;
688680 } ) ,
689- change : ( _cwd , sub ) =>
690- git ( sub , [ "rev-parse" , "--show-toplevel" ] ) . pipe ( Effect . flatMap ( changeOwner ) ) ,
681+ change : ( _cwd , sub , env ) =>
682+ git ( sub , [ "rev-parse" , "--show-toplevel" ] ) . pipe ( Effect . flatMap ( changeOwner ( env ) ) ) ,
691683 read : readValue ( "t3.value" ) ,
692684 before : "first" ,
693685 after : null ,
694686 reused : true ,
695687 } ,
696688 {
697689 name : "a new owner of a linked worktree's git dir" ,
698- setup : ( cwd ) =>
690+ setup : ( cwd , env ) =>
699691 Effect . gen ( function * ( ) {
700692 const worktree = NodePath . join ( yield * makeTmpDir ( "git-worktrees-" ) , "linked" ) ;
701693 yield * git ( cwd , [ "config" , "t3.value" , "first" ] ) ;
702694 yield * git ( cwd , [ "worktree" , "add" , "-b" , "linked" , worktree ] ) ;
703- yield * trustNoOtherOwner ;
695+ yield * trustNoOtherOwner ( env ) ;
704696 return worktree ;
705697 } ) ,
706- change : ( _cwd , worktree ) =>
707- git ( worktree , [ "rev-parse" , "--absolute-git-dir" ] ) . pipe ( Effect . flatMap ( changeOwner ) ) ,
698+ change : ( _cwd , worktree , env ) =>
699+ git ( worktree , [ "rev-parse" , "--absolute-git-dir" ] ) . pipe ( Effect . flatMap ( changeOwner ( env ) ) ) ,
708700 read : readValue ( "t3.value" ) ,
709701 before : "first" ,
710702 after : null ,
@@ -758,23 +750,26 @@ describe("config reads", () => {
758750 ] ;
759751
760752 it . effect . each ( changes ) ( "sees $name on the next read" , ( change ) =>
761- Effect . scoped (
762- Effect . gen ( function * ( ) {
763- const { driver, spawned } = yield * makeCountingDriver ;
764- const cwd = yield * makeTmpDir ( ) ;
765- yield * initRepoWithCommit ( cwd ) ;
766- const readCwd = yield * change . setup ( cwd ) ;
767- assert . equal ( yield * change . read ( driver , readCwd ) , change . before ) ;
768- assert . equal ( yield * change . read ( driver , readCwd ) , change . before ) ;
769- spawned . length = 0 ;
770- assert . equal ( yield * change . read ( driver , readCwd ) , change . before ) ;
771- assert . equal ( spawned . filter ( readsOnlyConfig ) . length === 0 , change . reused ) ;
753+ Effect . flatMap ( HostProcess . Environment , ( hostEnv ) => {
754+ const env = { ...hostEnv } ;
755+ return Effect . scoped (
756+ Effect . gen ( function * ( ) {
757+ const { driver, spawned } = yield * makeCountingDriver ;
758+ const cwd = yield * makeTmpDir ( ) ;
759+ yield * initRepoWithCommit ( cwd ) ;
760+ const readCwd = yield * change . setup ( cwd , env ) ;
761+ assert . equal ( yield * change . read ( driver , readCwd ) , change . before ) ;
762+ assert . equal ( yield * change . read ( driver , readCwd ) , change . before ) ;
763+ spawned . length = 0 ;
764+ assert . equal ( yield * change . read ( driver , readCwd ) , change . before ) ;
765+ assert . equal ( spawned . filter ( readsOnlyConfig ) . length === 0 , change . reused ) ;
772766
773- yield * change . change ( cwd , readCwd ) ;
767+ yield * change . change ( cwd , readCwd , env ) ;
774768
775- assert . equal ( yield * change . read ( driver , readCwd ) , change . after ) ;
776- } ) ,
777- ) . pipe ( Effect . provide ( layerTest ) ) ,
769+ assert . equal ( yield * change . read ( driver , readCwd ) , change . after ) ;
770+ } ) ,
771+ ) . pipe ( Effect . provideService ( HostProcess . Environment , env ) ) ;
772+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
778773 ) ;
779774
780775 it . effect . skipIf ( ! caseInsensitiveFileSystem ) (
@@ -810,18 +805,11 @@ describe("config reads", () => {
810805 const sub = NodePath . join ( cwd , "sub" ) ;
811806 NodeFS . mkdirSync ( sub ) ;
812807 const workTree = yield * makeTmpDir ( "git-work-tree-" ) ;
813- const readWithWorkTree = Effect . acquireUseRelease (
814- Effect . sync ( ( ) => {
815- const previous = process . env . GIT_WORK_TREE ;
816- process . env . GIT_WORK_TREE = workTree ;
817- return previous ;
808+ const readWithWorkTree = driver . readConfigValue ( sub , "t3.value" ) . pipe (
809+ Effect . provideService ( HostProcess . Environment , {
810+ ...( yield * HostProcess . Environment ) ,
811+ GIT_WORK_TREE : workTree ,
818812 } ) ,
819- ( ) => driver . readConfigValue ( sub , "t3.value" ) ,
820- ( previous ) =>
821- Effect . sync ( ( ) => {
822- if ( previous === undefined ) delete process . env . GIT_WORK_TREE ;
823- else process . env . GIT_WORK_TREE = previous ;
824- } ) ,
825813 ) ;
826814 assert . equal ( yield * readWithWorkTree , "parent" ) ;
827815 assert . equal ( yield * readWithWorkTree , "parent" ) ;
@@ -834,6 +822,38 @@ describe("config reads", () => {
834822 ) . pipe ( Effect . provide ( layerTest ) ) ,
835823 ) ;
836824
825+ it . effect ( "answers each read from the config of the environment it runs in" , ( ) =>
826+ Effect . scoped (
827+ Effect . gen ( function * ( ) {
828+ const { driver, spawned } = yield * makeCountingDriver ;
829+ const cwd = yield * makeTmpDir ( ) ;
830+ yield * initRepoWithCommit ( cwd ) ;
831+ const hostEnv = yield * HostProcess . Environment ;
832+ const readWithGlobal = Effect . fnUntraced ( function * ( value : string ) {
833+ const global = NodePath . join ( yield * makeTmpDir ( "git-global-" ) , "gitconfig" ) ;
834+ yield * writeInclude ( global , value ) ;
835+ return driver . readConfigValue ( cwd , "t3.value" ) . pipe (
836+ Effect . provideService ( HostProcess . Environment , {
837+ ...hostEnv ,
838+ GIT_CONFIG_GLOBAL : global ,
839+ } ) ,
840+ ) ;
841+ } ) ;
842+ const readFirst = yield * readWithGlobal ( "first" ) ;
843+ const readSecond = yield * readWithGlobal ( "second" ) ;
844+ for ( const _ of [ 1 , 2 ] ) {
845+ assert . equal ( yield * readFirst , "first" ) ;
846+ assert . equal ( yield * readSecond , "second" ) ;
847+ }
848+ spawned . length = 0 ;
849+
850+ assert . equal ( yield * readFirst , "first" ) ;
851+ assert . equal ( yield * readSecond , "second" ) ;
852+ assert . deepStrictEqual ( spawned . filter ( readsOnlyConfig ) , [ ] ) ;
853+ } ) ,
854+ ) . pipe ( Effect . provide ( layerTest ) ) ,
855+ ) ;
856+
837857 it . effect ( "reads what git config --get and git remote read" , ( ) =>
838858 Effect . scoped (
839859 Effect . gen ( function * ( ) {
0 commit comments