1+ using System ;
2+ using System . IO ;
3+ using FluentAssertions ;
4+ using NUnit . Framework ;
5+
6+ namespace Assent . Tests ;
7+
8+ public static class DirPathTests
9+ {
10+ public class NoSubDirs
11+ {
12+ [ Test ]
13+ public void SuccessWithValueIfEnvVarSetAndDirExists ( )
14+ {
15+ var value = DirPath . GetFromEnvironmentOrNull ( "LOCALAPPDATA" , [ ] ,
16+ getEnvironmentVariable : _ => "localAppDataFolder" ,
17+ directoryExists : _ => true ) ;
18+
19+ value . Should ( ) . Be ( "localAppDataFolder" ) ;
20+ }
21+
22+ [ Test ]
23+ public void FailIfEnvVarNotSet ( )
24+ {
25+ var value = DirPath . GetFromEnvironmentOrNull ( "LOCALAPPDATA" , [ ] ,
26+ getEnvironmentVariable : _ => null ,
27+ directoryExists : _ => throw new InvalidOperationException ( "should not get here" ) ) ;
28+
29+ value . Should ( ) . BeNull ( ) ;
30+ }
31+
32+ [ Test ]
33+ public void FailIfEnvVarIsEmpty ( )
34+ {
35+ var value = DirPath . GetFromEnvironmentOrNull ( "LOCALAPPDATA" , [ ] ,
36+ getEnvironmentVariable : _ => "" ,
37+ directoryExists : _ => throw new InvalidOperationException ( "should not get here" ) ) ;
38+
39+ value . Should ( ) . BeNull ( ) ;
40+ }
41+
42+ [ Test ]
43+ public void FailIfEnvVarSetAndDirDoesNotExist ( )
44+ {
45+ var value = DirPath . GetFromEnvironmentOrNull ( "LOCALAPPDATA" , [ ] ,
46+ getEnvironmentVariable : _ => "localAppDataFolder" ,
47+ directoryExists : _ => false ) ;
48+
49+ value . Should ( ) . BeNull ( ) ;
50+ }
51+ }
52+
53+ public class CombiningSubDirs
54+ {
55+ [ Test ]
56+ public void SuccessWithValueIfEnvVarSetAndDirExists ( )
57+ {
58+ var value = DirPath . GetFromEnvironmentOrNull ( "LOCALAPPDATA" , [ "foo" , "bar" ] ,
59+ getEnvironmentVariable : _ => "localAppDataFolder" ,
60+ directoryExists : _ => true ) ;
61+
62+ value . Should ( ) . Be ( Path . Combine ( "localAppDataFolder" , "foo" , "bar" ) ) ;
63+ }
64+
65+ [ Test ]
66+ public void FailIfEnvVarNotSet ( )
67+ {
68+ var value = DirPath . GetFromEnvironmentOrNull ( "LOCALAPPDATA" , [ "foo" , "bar" ] ,
69+ getEnvironmentVariable : _ => null ,
70+ directoryExists : _ => throw new InvalidOperationException ( "should not get here" ) ) ;
71+
72+ value . Should ( ) . BeNull ( ) ;
73+ }
74+
75+ [ Test ]
76+ public void FailIfEnvVarIsEmpty ( )
77+ {
78+ var value = DirPath . GetFromEnvironmentOrNull ( "LOCALAPPDATA" , [ "foo" , "bar" ] ,
79+ getEnvironmentVariable : _ => "" ,
80+ directoryExists : _ => throw new InvalidOperationException ( "should not get here" ) ) ;
81+
82+ value . Should ( ) . BeNull ( ) ;
83+ }
84+
85+ [ Test ]
86+ public void FailIfEnvVarSetAndDirDoesNotExist ( )
87+ {
88+ var value = DirPath . GetFromEnvironmentOrNull ( "LOCALAPPDATA" , [ "foo" , "bar" ] ,
89+ getEnvironmentVariable : _ => "localAppDataFolder" ,
90+ directoryExists : d =>
91+ {
92+ // it should check the existence of the combined subdir, NOT the base dir
93+ d . Should ( ) . Be ( Path . Combine ( "localAppDataFolder" , "foo" , "bar" ) ) ;
94+ return false ;
95+ } ) ;
96+
97+ value . Should ( ) . BeNull ( ) ;
98+ }
99+ }
100+ }
0 commit comments