@@ -14,7 +14,6 @@ public class ReaderTests : IDisposable
1414
1515 private readonly string _testRootPath ;
1616 private readonly string _startPath ;
17- private readonly string _originalBaseDirectory ;
1817
1918 public ReaderTests ( )
2019 {
@@ -24,24 +23,14 @@ public ReaderTests()
2423 _testRootPath = Path . Combine ( Path . GetTempPath ( ) , "DotEnvTests_" + Guid . NewGuid ( ) . ToString ( "N" ) ) ;
2524 Directory . CreateDirectory ( _testRootPath ) ;
2625
27- // Create a nested structure to simulate parent directories.
28- // StartPath will be our simulated AppContext.BaseDirectory.
29- _startPath = Path . Combine ( _testRootPath , "level1" , "level2" ) ;
30- Directory . CreateDirectory ( _startPath ) ;
31-
32- // HACK: To robustly test the method without changing its source code,
33- // we temporarily set the AppContext.BaseDirectory to our controlled test path.
34- _originalBaseDirectory = ( string ) AppContext . GetData ( "APP_CONTEXT_BASE_DIRECTORY" ) ! ;
35- AppDomain . CurrentDomain . SetData ( "APP_CONTEXT_BASE_DIRECTORY" , _startPath ) ;
26+ _startPath = AppContext . BaseDirectory ;
3627 }
3728
3829 public void Dispose ( )
3930 {
4031 if ( File . Exists ( _tempFilePath ) )
4132 File . Delete ( _tempFilePath ) ;
4233
43- AppDomain . CurrentDomain . SetData ( "APP_CONTEXT_BASE_DIRECTORY" , _originalBaseDirectory ) ;
44-
4534 if ( Directory . Exists ( _testRootPath ) )
4635 Directory . Delete ( _testRootPath , true ) ;
4736 }
@@ -72,7 +61,7 @@ public void ReadFileLines_InvalidPathAndIgnoreExceptionsTrue_ShouldReturnEmptySp
7261 [ Fact ]
7362 public void ReadFileLines_NonExistentFileAndIgnoreExceptionsFalse_ShouldThrowFileNotFoundException ( )
7463 {
75- var path = "nonexistent.env" ;
64+ const string path = "nonexistent.env" ;
7665 Action act = ( ) => Reader . ReadFileLines ( path , false , null ) ;
7766 act . ShouldThrow < FileNotFoundException > ( ) . Message . ShouldContain ( path ) ;
7867 }
@@ -97,7 +86,7 @@ public void ReadFileLines_ValidFile_ShouldReturnLines()
9786 [ Fact ]
9887 public void ReadFileLines_WithCustomEncoding_ShouldReturnCorrectContent ( )
9988 {
100- var content = "KEY=üñîçø∂é" ;
89+ const string content = "KEY=üñîçø∂é" ;
10190 File . WriteAllText ( _tempFilePath , content , Encoding . UTF32 ) ;
10291 var result = Reader . ReadFileLines ( _tempFilePath , false , Encoding . UTF32 ) ;
10392 result [ 0 ] . ShouldBe ( content ) ;
@@ -134,7 +123,8 @@ public void MergeEnvKeyValues_SingleArray_ShouldReturnAllItems()
134123 {
135124 new [ ]
136125 {
137- new KeyValuePair < string , string > ( "KEY1" , "value1" ) , new KeyValuePair < string , string > ( "KEY2" , "value2" )
126+ new KeyValuePair < string , string > ( "KEY1" , "value1" ) ,
127+ new KeyValuePair < string , string > ( "KEY2" , "value2" )
138128 }
139129 } ;
140130 var result = Reader . MergeEnvKeyValues ( input , false ) ;
@@ -191,84 +181,43 @@ public void MergeEnvKeyValues_ComplexMerge_ShouldHandleAllCases()
191181 [ Fact ]
192182 public void GetProbedEnvPath_FileNotFoundAndIgnoreExceptionsFalse_ShouldThrow ( )
193183 {
194- Action act = ( ) => Reader . GetProbedEnvPath ( levelsToSearch : 2 , ignoreExceptions : false ) ;
195- act . ShouldThrow < FileNotFoundException > ( )
196- . Message . ShouldContain ( DotEnvOptions . DefaultEnvFileName ) ;
197- }
198-
199- [ Fact ]
200- public void GetProbedEnvPath_ShouldFindFile_InCurrentDirectory ( )
201- {
202- var expectedPath = Path . Combine ( _startPath , DotEnvOptions . DefaultEnvFileName ) ;
203- CreateEnvFileAt ( _startPath ) ;
204-
205- var result = Reader . GetProbedEnvPath ( levelsToSearch : 0 , ignoreExceptions : true ) . ToList ( ) ;
184+ var levelsToSearch = 2 ;
185+ var exception = Should . Throw < FileNotFoundException > ( ( ) =>
186+ {
187+ Reader . GetProbedEnvPath ( levelsToSearch , ignoreExceptions : false ) ;
188+ } ) ;
206189
207- result . ShouldHaveSingleItem ( ) ;
208- result . First ( ) . ShouldBe ( expectedPath ) ;
190+ exception . Message . ShouldContain ( $ "Could not find '{ DotEnvOptions . DefaultEnvFileName } '") ;
191+ exception . Message . ShouldContain ( $ "after searching { levelsToSearch } directory level(s) upwards.") ;
192+ exception . Message . ShouldContain ( "Searched paths:" ) ;
193+ exception . Message . ShouldContain ( _startPath ) ;
194+ exception . Message . ShouldContain ( "net9.0" ) ;
195+ exception . Message . ShouldContain ( "Debug" ) ;
209196 }
210197
211198 [ Fact ]
212- public void GetProbedEnvPath_ShouldFindFile_InParentDirectory ( )
199+ public void GetProbedEnvPath_FileNotFoundAndIgnoreExceptionsTrue_ShouldReturnEmpty ( )
213200 {
214- var parentDirectory = Directory . GetParent ( _startPath ) ! . FullName ;
215- var expectedPath = Path . Combine ( parentDirectory , DotEnvOptions . DefaultEnvFileName ) ;
216- CreateEnvFileAt ( parentDirectory ) ;
217-
218- var result = Reader . GetProbedEnvPath ( levelsToSearch : 1 , ignoreExceptions : true ) . ToList ( ) ;
219-
220- result . ShouldHaveSingleItem ( ) ;
221- result . First ( ) . ShouldBe ( expectedPath ) ;
201+ var result = Reader . GetProbedEnvPath ( levelsToSearch : 2 , ignoreExceptions : true ) ;
202+ result . ShouldBeEmpty ( ) ;
222203 }
223204
224205 [ Fact ]
225- public void GetProbedEnvPath_ShouldFindFile_TwoLevelsUp ( )
206+ public void GetProbedEnvPath_ShouldFindFile_ThreeLevelsUp ( )
226207 {
227- var grandParentDirectory = Directory . GetParent ( _startPath ) ! . Parent ! . FullName ;
208+ var grandParentDirectory = Directory . GetParent ( _startPath ) ! . Parent ! . Parent ! . Parent ! . FullName ;
228209 var expectedPath = Path . Combine ( grandParentDirectory , DotEnvOptions . DefaultEnvFileName ) ;
229- CreateEnvFileAt ( grandParentDirectory ) ;
230210
231- var result = Reader . GetProbedEnvPath ( levelsToSearch : 2 , ignoreExceptions : true ) . ToList ( ) ;
211+ var result = Reader . GetProbedEnvPath ( levelsToSearch : 3 , ignoreExceptions : true ) . ToList ( ) ;
232212
233213 result . ShouldHaveSingleItem ( ) ;
234214 result . First ( ) . ShouldBe ( expectedPath ) ;
235215 }
236216
237- [ Fact ]
238- public void GetProbedEnvPath_ShouldReturnEmpty_WhenFileNotFoundAndExceptionsIgnored ( )
239- {
240- var result = Reader . GetProbedEnvPath ( levelsToSearch : 3 , ignoreExceptions : true ) ;
241- result . ShouldBeEmpty ( ) ;
242- }
243-
244- [ Fact ]
245- public void GetProbedEnvPath_ShouldThrow_WhenFileNotFoundAndExceptionsNotIgnored ( )
246- {
247- // No .env file is created.
248- var levelsToSearch = 2 ;
249- var parentPath = Directory . GetParent ( _startPath ) ! . FullName ;
250- var grandParentPath = Directory . GetParent ( parentPath ) ! . FullName ;
251-
252- var exception = Should . Throw < FileNotFoundException > ( ( ) =>
253- {
254- Reader . GetProbedEnvPath ( levelsToSearch , ignoreExceptions : false ) ;
255- } ) ;
256-
257- exception . Message . ShouldContain ( $ "Could not find '{ DotEnvOptions . DefaultEnvFileName } '") ;
258- exception . Message . ShouldContain ( $ "after searching { levelsToSearch } directory level(s) upwards.") ;
259- exception . Message . ShouldContain ( "Searched paths:" ) ;
260- exception . Message . ShouldContain ( _startPath ) ; // Searched level 0
261- exception . Message . ShouldContain ( parentPath ) ; // Searched level 1
262- exception . Message . ShouldContain ( grandParentPath ) ; // Searched level 2
263- }
264-
265217 [ Fact ]
266218 public void GetProbedEnvPath_ShouldReturnEmpty_WhenFileExistsButIsOutOfSearchRange ( )
267219 {
268- // File is at level 2, but we only search up to level 1.
269- var grandParentDirectory = Directory . GetParent ( _startPath ) ! . Parent ! . FullName ;
270- CreateEnvFileAt ( grandParentDirectory ) ;
271-
220+ // File is at level 3, but we only search up to level 1.
272221 var result = Reader . GetProbedEnvPath ( levelsToSearch : 1 , ignoreExceptions : true ) ;
273222
274223 result . ShouldBeEmpty ( ) ;
@@ -277,18 +226,12 @@ public void GetProbedEnvPath_ShouldReturnEmpty_WhenFileExistsButIsOutOfSearchRan
277226 [ Fact ]
278227 public void GetProbedEnvPath_ShouldThrow_WhenFileExistsButIsOutOfSearchRange ( )
279228 {
280- // File is at level 2, but we only search up to level 1.
281- var grandParentDirectory = Directory . GetParent ( _startPath ) ! . Parent ! . FullName ;
282- CreateEnvFileAt ( grandParentDirectory ) ;
283-
229+ // File is at level 3, but we only search up to level 1.
284230 var exception = Should . Throw < FileNotFoundException > ( ( ) =>
285231 {
286232 Reader . GetProbedEnvPath ( levelsToSearch : 1 , ignoreExceptions : false ) ;
287233 } ) ;
288234
289235 exception . Message . ShouldContain ( $ "Could not find '{ DotEnvOptions . DefaultEnvFileName } '") ;
290236 }
291-
292- private void CreateEnvFileAt ( string directoryPath ) =>
293- File . WriteAllText ( Path . Combine ( directoryPath , DotEnvOptions . DefaultEnvFileName ) , "TEST=true" ) ;
294237}
0 commit comments