11using System ;
2- using System . Collections ;
32using System . Collections . Generic ;
43using System . Diagnostics ;
5- using System . Globalization ;
64using System . IO ;
75using System . Linq ;
86using System . Reflection ;
97using System . Reflection . PortableExecutable ;
10- using System . Security . Cryptography ;
11- using System . Text ;
128using System . Text . RegularExpressions ;
139
1410using Microsoft . Extensions . FileSystemGlobbing ;
@@ -20,15 +16,45 @@ internal static class InstrumentationHelper
2016 {
2117 public static string [ ] GetCoverableModules ( string module , string [ ] includeDirectories )
2218 {
23- var moduleDirectory = Path . GetDirectoryName ( module ) ;
24- var files = new List < string > ( Directory . GetFiles ( moduleDirectory ) ) ;
19+ Debug . Assert ( includeDirectories != null , "Parameter " + nameof ( includeDirectories ) + " in method " +
20+ nameof ( InstrumentationHelper ) + "." + nameof ( GetCoverableModules ) + " must not be null" ) ;
2521
26- if ( includeDirectories != null )
27- foreach ( var includeDirectory in ExpandIncludeDirectories ( includeDirectories , moduleDirectory ) )
28- files . AddRange ( Directory . GetFiles ( includeDirectory ) ) ;
22+ string moduleDirectory = Path . GetDirectoryName ( module ) ;
23+ if ( moduleDirectory == string . Empty )
24+ {
25+ moduleDirectory = Directory . GetCurrentDirectory ( ) ;
26+ }
27+
28+ var dirs = new List < string > ( 1 + includeDirectories . Length )
29+ {
30+ // Add the test assembly's directory.
31+ moduleDirectory
32+ } ;
2933
30- return files . Where ( m => IsAssembly ( m ) && Path . GetFileName ( m ) != Path . GetFileName ( module ) )
31- . Distinct ( ) . ToArray ( ) ;
34+ // Prepare all the directories in which we probe for modules.
35+ foreach ( var includeDirectory in includeDirectories . Where ( d => d != null ) )
36+ {
37+ var fullPath = ( ! Path . IsPathRooted ( includeDirectory )
38+ ? Path . GetFullPath ( Path . Combine ( moduleDirectory , includeDirectory ) )
39+ : includeDirectory ) . TrimEnd ( '*' ) ;
40+
41+ if ( ! Directory . Exists ( fullPath ) ) continue ;
42+
43+ if ( includeDirectory . EndsWith ( "*" , StringComparison . Ordinal ) )
44+ dirs . AddRange ( Directory . GetDirectories ( fullPath ) ) ;
45+ else
46+ dirs . Add ( fullPath ) ;
47+ }
48+
49+ // The test module's name must be unique.
50+ var uniqueModules = new HashSet < string >
51+ {
52+ Path . GetFileName ( module )
53+ } ;
54+
55+ return dirs . SelectMany ( d => Directory . EnumerateFiles ( d ) )
56+ . Where ( m => IsAssembly ( m ) && uniqueModules . Add ( Path . GetFileName ( m ) ) )
57+ . ToArray ( ) ;
3258 }
3359
3460 public static bool HasPdb ( string module )
@@ -254,32 +280,11 @@ private static bool IsTypeFilterMatch(string module, string type, string[] filte
254280 return false ;
255281 }
256282
257- private static IEnumerable < string > ExpandIncludeDirectories ( string [ ] includeDirectories , string moduleDirectory )
258- {
259- var result = new List < string > ( includeDirectories . Length ) ;
260-
261- foreach ( var includeDirectory in includeDirectories . Where ( d => d != null ) )
262- {
263- var fullPath = ( ! Path . IsPathRooted ( includeDirectory )
264- ? Path . GetFullPath ( Path . Combine ( moduleDirectory , includeDirectory ) )
265- : includeDirectory ) . TrimEnd ( '*' ) ;
266-
267- if ( ! Directory . Exists ( fullPath ) ) continue ;
268-
269- if ( includeDirectory . EndsWith ( "*" , StringComparison . Ordinal ) )
270- result . AddRange ( Directory . GetDirectories ( fullPath ) ) ;
271- else
272- result . Add ( fullPath ) ;
273- }
274-
275- return result ;
276- }
277-
278283 private static string GetBackupPath ( string module , string identifier )
279284 {
280285 return Path . Combine (
281286 Path . GetTempPath ( ) ,
282- Path . GetFileNameWithoutExtension ( module ) + "_" + GetPathHash ( Path . GetDirectoryName ( module ) ) + "_" + identifier + ".dll"
287+ Path . GetFileNameWithoutExtension ( module ) + "_" + identifier + ".dll"
283288 ) ;
284289 }
285290
@@ -304,11 +309,14 @@ private static string WildcardToRegex(string pattern)
304309
305310 private static bool IsAssembly ( string filePath )
306311 {
312+ Debug . Assert ( filePath != null , "Parameter " + nameof ( filePath ) + " in " + nameof ( InstrumentationHelper ) +
313+ "." + nameof ( IsAssembly ) + " must not be null." ) ;
314+
315+ if ( ! ( filePath . EndsWith ( ".exe" ) || filePath . EndsWith ( ".dll" ) ) )
316+ return false ;
317+
307318 try
308319 {
309- if ( ! ( filePath . EndsWith ( ".exe" ) || filePath . EndsWith ( ".dll" ) ) )
310- return false ;
311-
312320 AssemblyName . GetAssemblyName ( filePath ) ;
313321 return true ;
314322 }
@@ -317,13 +325,6 @@ private static bool IsAssembly(string filePath)
317325 return false ;
318326 }
319327 }
320-
321- private static string GetPathHash ( string path )
322- {
323- using ( var md5 = MD5 . Create ( ) )
324- return BitConverter . ToString ( md5 . ComputeHash ( Encoding . Unicode . GetBytes ( path ) ) )
325- . Replace ( "-" , string . Empty ) ;
326- }
327328 }
328329}
329330
0 commit comments