@@ -409,5 +409,94 @@ public void PassingANonExistingLocalConfigurationFileToBuildFromthrowss()
409409 Assert . Throws < FileNotFoundException > ( ( ) => Configuration . BuildFrom (
410410 Path . Combine ( Path . GetTempPath ( ) , Path . GetRandomFileName ( ) ) ) ) ;
411411 }
412+
413+ [ Fact ]
414+ public void CanSetAndGetSearchPath ( )
415+ {
416+ string globalPath = Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ;
417+ string systemPath = Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ;
418+ string xdgPath = Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ;
419+
420+ GlobalSettings . SetConfigSearchPath ( ConfigurationLevel . Global , globalPath ) ;
421+ GlobalSettings . SetConfigSearchPath ( ConfigurationLevel . System , systemPath ) ;
422+ GlobalSettings . SetConfigSearchPath ( ConfigurationLevel . Xdg , xdgPath ) ;
423+
424+ Assert . Equal ( globalPath , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) . Single ( ) ) ;
425+ Assert . Equal ( systemPath , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . System ) . Single ( ) ) ;
426+ Assert . Equal ( xdgPath , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Xdg ) . Single ( ) ) ;
427+
428+ // reset the search paths to their defaults
429+ GlobalSettings . SetConfigSearchPath ( ConfigurationLevel . Global , null ) ;
430+ GlobalSettings . SetConfigSearchPath ( ConfigurationLevel . System , null ) ;
431+ GlobalSettings . SetConfigSearchPath ( ConfigurationLevel . Xdg , null ) ;
432+ }
433+
434+ [ Fact ]
435+ public void CanSetAndGetMultipleSearchPaths ( )
436+ {
437+ string [ ] paths =
438+ {
439+ Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ,
440+ Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ,
441+ Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) ,
442+ } ;
443+
444+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , paths ) ;
445+
446+ Assert . Equal ( paths , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ) ;
447+
448+ // set back to the defaults
449+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ;
450+ }
451+
452+ [ Fact ]
453+ public void CanResetSearchPaths ( )
454+ {
455+ // set the global search path to its default value
456+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ;
457+ var oldPaths = GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ;
458+ Assert . NotNull ( oldPaths ) ;
459+
460+ // change to something other than the default
461+ var newPaths = new string [ ] { Path . Combine ( Constants . TemporaryReposPath , Path . GetRandomFileName ( ) ) } ;
462+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , newPaths ) ;
463+ Assert . Equal ( newPaths , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ) ;
464+
465+ // set it back to the default
466+ GlobalSettings . SetConfigSearchPaths ( ConfigurationLevel . Global , null ) ;
467+ Assert . Equal ( oldPaths , GlobalSettings . GetConfigSearchPaths ( ConfigurationLevel . Global ) ) ;
468+ }
469+
470+ [ Fact ]
471+ public void CanRedirectConfigAccess ( )
472+ {
473+ var scd1 = BuildSelfCleaningDirectory ( ) ;
474+ var scd2 = BuildSelfCleaningDirectory ( ) ;
475+
476+ Touch ( scd1 . RootedDirectoryPath , ".gitconfig" ) ;
477+ Touch ( scd2 . RootedDirectoryPath , ".gitconfig" ) ;
478+
479+ // redirect global access to the first path
480+ GlobalSettings . SetConfigSearchPath ( ConfigurationLevel . Global , scd1 . RootedDirectoryPath ) ;
481+
482+ // set a value in the first config
483+ using ( var config = Configuration . BuildFrom ( null ) )
484+ {
485+ config . Set ( "luggage.code" , 9876 , ConfigurationLevel . Global ) ;
486+ Assert . Equal ( 9876 , config . Get < int > ( "luggage.code" , ConfigurationLevel . Global ) . Value ) ;
487+ }
488+
489+ // redirect global config access to path2
490+ GlobalSettings . SetConfigSearchPath ( ConfigurationLevel . Global , scd2 . RootedDirectoryPath ) ;
491+
492+ // if the redirect succeeds, the value set in the prior config should not be visible
493+ using ( var config = Configuration . BuildFrom ( null ) )
494+ {
495+ Assert . Equal ( - 1 , config . GetValueOrDefault < int > ( "luggage.code" , ConfigurationLevel . Global , - 1 ) ) ;
496+ }
497+
498+ // reset the search path to the default
499+ GlobalSettings . SetConfigSearchPath ( ConfigurationLevel . Global , null ) ;
500+ }
412501 }
413502}
0 commit comments