@@ -431,5 +431,127 @@ public void CannotBuildAProperSignatureFromConfigWhenFullIdentityCannotBeFoundIn
431431 Assert . Null ( signature ) ;
432432 }
433433 }
434+
435+ [ Fact ]
436+ public void CanSetAndGetSearchPath ( )
437+ {
438+ string globalPath = Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ;
439+ string systemPath = Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ;
440+ string xdgPath = Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ;
441+
442+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , globalPath ) ;
443+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . System , systemPath ) ;
444+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Xdg , xdgPath ) ;
445+
446+ Assert . Equal ( globalPath , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) . Single ( ) ) ;
447+ Assert . Equal ( systemPath , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . System ) . Single ( ) ) ;
448+ Assert . Equal ( xdgPath , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Xdg ) . Single ( ) ) ;
449+
450+ // reset the search paths to their defaults
451+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ;
452+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . System , null ) ;
453+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Xdg , null ) ;
454+ }
455+
456+ [ Fact ]
457+ public void CanSetAndGetMultipleSearchPaths ( )
458+ {
459+ string [ ] paths =
460+ {
461+ Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ,
462+ Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ,
463+ Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ,
464+ } ;
465+
466+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , paths ) ;
467+
468+ Assert . Equal ( paths , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ) ;
469+
470+ // set back to the defaults
471+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ;
472+ }
473+
474+ [ Fact ]
475+ public void CanResetSearchPaths ( )
476+ {
477+ // all of these calls should reset the config path to the default
478+ Action [ ] resetActions =
479+ {
480+ ( ) => GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global ) ,
481+ ( ) => GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ,
482+ ( ) => GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , string . Empty ) ,
483+ ( ) => GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , new string [ ] { } ) ,
484+ } ;
485+
486+ // record the default search path
487+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ;
488+ var oldPaths = GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ;
489+ Assert . NotNull ( oldPaths ) ;
490+
491+ // generate a non-default path to set
492+ var newPaths = new string [ ] { Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) } ;
493+
494+ foreach ( var tryToReset in resetActions )
495+ {
496+ // change to the non-default path
497+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , newPaths ) ;
498+ Assert . Equal ( newPaths , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ) ;
499+
500+ // set it back to the default
501+ tryToReset ( ) ;
502+ Assert . Equal ( oldPaths , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ) ;
503+ }
504+
505+ // make sure the config paths are reset after the test ends
506+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ;
507+ }
508+
509+ [ Fact ]
510+ public void CanAppendToSearchPaths ( )
511+ {
512+ string appendMe = Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ;
513+ var prevPaths = GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ;
514+
515+ // append using the special name $PATH
516+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , "$PATH" , appendMe ) ;
517+
518+ var currentPaths = GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ;
519+ Assert . Equal ( currentPaths , prevPaths . Concat ( new [ ] { appendMe } ) ) ;
520+
521+ // set it back to the default
522+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ;
523+ }
524+
525+ [ Fact ]
526+ public void CanRedirectConfigAccess ( )
527+ {
528+ var scd1 = BuildSelfCleaningDirectory ( ) ;
529+ var scd2 = BuildSelfCleaningDirectory ( ) ;
530+
531+ Touch ( scd1 . RootedDirectoryPath , ".gitconfig" ) ;
532+ Touch ( scd2 . RootedDirectoryPath , ".gitconfig" ) ;
533+
534+ // redirect global access to the first path
535+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , scd1 . RootedDirectoryPath ) ;
536+
537+ // set a value in the first config
538+ using ( var config = Configuration . BuildFrom ( null ) )
539+ {
540+ config . Set ( "luggage.code" , 9876 , ConfigurationLevel . Global ) ;
541+ Assert . Equal ( 9876 , config . Get < int > ( "luggage.code" , ConfigurationLevel . Global ) . Value ) ;
542+ }
543+
544+ // redirect global config access to path2
545+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , scd2 . RootedDirectoryPath ) ;
546+
547+ // if the redirect succeeds, the value set in the prior config should not be visible
548+ using ( var config = Configuration . BuildFrom ( null ) )
549+ {
550+ Assert . Equal ( - 1 , config . GetValueOrDefault < int > ( "luggage.code" , ConfigurationLevel . Global , - 1 ) ) ;
551+ }
552+
553+ // reset the search path to the default
554+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ;
555+ }
434556 }
435557}
0 commit comments