33
44using System ;
55using System . IO ;
6+ using System . Linq ;
67using System . Reflection ;
78using System . Runtime . CompilerServices ;
89
@@ -21,10 +22,12 @@ public static class TestData
2122 /// This returns the filepath of the file found by the searchPattern. If more than one file found that throws and exception
2223 /// </summary>
2324 /// <param name="searchPattern">If the search pattern starts with a @"\", then it will look in a subdirectory for the file</param>
25+ /// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
2426 /// <returns>The absolute filepath to the found file</returns>
25- public static string GetFilePath ( string searchPattern )
27+ public static string GetFilePath ( string searchPattern , Assembly callingAssembly = null )
2628 {
27- string [ ] fileList = GetFilePaths ( searchPattern ) ;
29+ callingAssembly = callingAssembly ?? Assembly . GetCallingAssembly ( ) ;
30+ string [ ] fileList = GetFilePaths ( searchPattern , callingAssembly ) ;
2831
2932 if ( fileList . Length != 1 )
3033 throw new Exception ( string . Format ( "GetTestDataFilePath: The searchString {0} found {1} file. Either not there or ambiguous" ,
@@ -36,17 +39,23 @@ public static string GetFilePath(string searchPattern)
3639 /// <summary>
3740 /// This returns all the filepaths of file that fit the search pattern
3841 /// </summary>
39- /// <param name="searchPattern">If the search pattern starts with a @"\", then it will look in a subdirectory for the file</param>
42+ /// <param name="searchPattern">If the search pattern starts with a @"\", then it will look for an alternate testdata directory</param>
43+ /// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
4044 /// <returns>array of absolute filepaths that match the filepath</returns>
41- public static string [ ] GetFilePaths ( string searchPattern = "" )
45+ public static string [ ] GetFilePaths ( string searchPattern = "" , Assembly callingAssembly = null )
4246 {
43- var directory = GetTestDataDir ( ) ;
44- if ( searchPattern . Contains ( @"\" ) )
47+ callingAssembly = callingAssembly ?? Assembly . GetCallingAssembly ( ) ;
48+ string testDir = null ;
49+ if ( searchPattern . Any ( ) && searchPattern [ 0 ] == Path . DirectorySeparatorChar )
4550 {
46- //Has subdirectory in search pattern, so change directory
47- directory = Path . Combine ( directory , searchPattern . Substring ( 0 , searchPattern . LastIndexOf ( '\\ ' ) ) ) ;
48- searchPattern = searchPattern . Substring ( searchPattern . LastIndexOf ( '\\ ' ) + 1 ) ;
51+ //has alternate test directory name, so set that and remove from search string
52+ var indexNextDirSep = searchPattern . Substring ( 1 ) . IndexOf ( Path . DirectorySeparatorChar ) ;
53+ if ( indexNextDirSep < 0 )
54+ throw new InvalidOperationException ( @"Your search pattern started with an alternative directory, but did have a closing directory separator." ) ;
55+ testDir = searchPattern . Substring ( 1 , indexNextDirSep ) ;
56+ searchPattern = searchPattern . Substring ( indexNextDirSep + 2 ) ;
4957 }
58+ var directory = GetTestDataDir ( testDir , callingAssembly ) ;
5059
5160 string [ ] fileList = Directory . GetFiles ( directory , searchPattern ) ;
5261
@@ -57,21 +66,25 @@ public static string[] GetFilePaths(string searchPattern = "")
5766 /// This returns the content of the file found by the searchPattern. If more than one file found that throws and exception
5867 /// </summary>
5968 /// <param name="searchPattern">If the search pattern starts with a @"\", then it will look in a subdirectory for the file</param>
69+ /// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
6070 /// <returns>The content of the file as text of the found file</returns>
61- public static string GetFileContent ( string searchPattern )
71+ public static string GetFileContent ( string searchPattern , Assembly callingAssembly = null )
6272 {
63- var filePath = GetFilePath ( searchPattern ) ;
73+ callingAssembly = callingAssembly ?? Assembly . GetCallingAssembly ( ) ;
74+ var filePath = GetFilePath ( searchPattern , callingAssembly ) ;
6475 return File . ReadAllText ( filePath ) ;
6576 }
6677
6778 /// <summary>
6879 /// This will ensure that a file in the TestData directory is deleted
6980 /// </summary>
7081 /// <param name="searchPattern"></param>
82+ /// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
7183 /// <returns></returns>
72- public static bool EnsureFileDeleted ( string searchPattern )
84+ public static bool EnsureFileDeleted ( string searchPattern , Assembly callingAssembly = null )
7385 {
74- var fileList = GetFilePaths ( searchPattern ) ;
86+ callingAssembly = callingAssembly ?? Assembly . GetCallingAssembly ( ) ;
87+ var fileList = GetFilePaths ( searchPattern , callingAssembly ) ;
7588 if ( fileList . Length == 0 ) return false ;
7689 if ( fileList . Length != 1 )
7790 throw new Exception ( string . Format ( "TestFileDeleteIfPresent: The searchString {0} found {1} files!" ,
@@ -119,13 +132,14 @@ public static void DeleteAllFilesAndSubDirsInDir(string topDirPath)
119132 /// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
120133 /// <returns></returns>
121134 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
122- public static string GetTestDataDir ( string alternateTestDir = TestFileDirectoryName , Assembly callingAssembly = null )
135+ public static string GetTestDataDir ( string alternateTestDir = null , Assembly callingAssembly = null )
123136 {
137+ alternateTestDir = alternateTestDir ?? TestFileDirectoryName ;
124138 //see https://stackoverflow.com/questions/670566/path-combine-absolute-with-relative-path-strings
125139 return Path . Combine (
126140 Path . GetFullPath (
127141 GetCallingAssemblyTopLevelDir ( callingAssembly ?? Assembly . GetCallingAssembly ( ) )
128- + " \\ " + alternateTestDir ) ) ;
142+ + Path . DirectorySeparatorChar + alternateTestDir ) ) ;
129143 }
130144
131145 /// <summary>
@@ -136,7 +150,7 @@ public static string GetTestDataDir(string alternateTestDir = TestFileDirectoryN
136150 [ MethodImpl ( MethodImplOptions . NoInlining ) ] //see https://docs.microsoft.com/en-gb/dotnet/api/system.reflection.assembly.getcallingassembly?view=netstandard-2.0#System_Reflection_Assembly_GetCallingAssembly
137151 public static string GetCallingAssemblyTopLevelDir ( Assembly callingAssembly = null )
138152 {
139- const string binDir = @"\ bin\ ";
153+ var binDir = $ " { Path . DirectorySeparatorChar } bin{ Path . DirectorySeparatorChar } ";
140154 var pathToManipulate = ( callingAssembly ?? Assembly . GetCallingAssembly ( ) ) . Location ;
141155
142156 var indexOfPart = pathToManipulate . IndexOf ( binDir , StringComparison . OrdinalIgnoreCase ) ;
0 commit comments