9
9
using System . Reflection . PortableExecutable ;
10
10
using System . Text . RegularExpressions ;
11
11
using Coverlet . Core . Abstracts ;
12
- using Microsoft . Extensions . FileSystemGlobbing ;
13
- using Microsoft . Extensions . DependencyInjection ;
14
- using Microsoft . Extensions . FileSystemGlobbing . Abstractions ;
15
12
16
13
namespace Coverlet . Core . Helpers
17
14
{
18
- internal static class InstrumentationHelper
15
+ internal class InstrumentationHelper : IInstrumentationHelper
19
16
{
20
- private static readonly ConcurrentDictionary < string , string > _backupList = new ConcurrentDictionary < string , string > ( ) ;
17
+ private readonly ConcurrentDictionary < string , string > _backupList = new ConcurrentDictionary < string , string > ( ) ;
18
+ private readonly IRetryHelper _retryHelper ;
21
19
22
- static InstrumentationHelper ( )
20
+ public InstrumentationHelper ( IProcessExitHandler processExitHandler , IRetryHelper retryHelper )
23
21
{
24
- DependencyInjection . Current . GetService < IProcessExitHandler > ( ) . Add ( ( s , e ) => RestoreOriginalModules ( ) ) ;
22
+ processExitHandler . Add ( ( s , e ) => RestoreOriginalModules ( ) ) ;
23
+ _retryHelper = retryHelper ;
25
24
}
26
25
27
- public static string [ ] GetCoverableModules ( string module , string [ ] directories , bool includeTestAssembly )
26
+ public string [ ] GetCoverableModules ( string module , string [ ] directories , bool includeTestAssembly )
28
27
{
29
28
Debug . Assert ( directories != null ) ;
30
29
@@ -68,7 +67,7 @@ public static string[] GetCoverableModules(string module, string[] directories,
68
67
. ToArray ( ) ;
69
68
}
70
69
71
- public static bool HasPdb ( string module , out bool embedded )
70
+ public bool HasPdb ( string module , out bool embedded )
72
71
{
73
72
embedded = false ;
74
73
using ( var moduleStream = File . OpenRead ( module ) )
@@ -94,7 +93,7 @@ public static bool HasPdb(string module, out bool embedded)
94
93
}
95
94
}
96
95
97
- public static bool EmbeddedPortablePdbHasLocalSource ( string module )
96
+ public bool EmbeddedPortablePdbHasLocalSource ( string module )
98
97
{
99
98
using ( FileStream moduleStream = File . OpenRead ( module ) )
100
99
using ( var peReader = new PEReader ( moduleStream ) )
@@ -129,7 +128,7 @@ public static bool EmbeddedPortablePdbHasLocalSource(string module)
129
128
return true ;
130
129
}
131
130
132
- public static void BackupOriginalModule ( string module , string identifier )
131
+ public void BackupOriginalModule ( string module , string identifier )
133
132
{
134
133
var backupPath = GetBackupPath ( module , identifier ) ;
135
134
var backupSymbolPath = Path . ChangeExtension ( backupPath , ".pdb" ) ;
@@ -150,7 +149,7 @@ public static void BackupOriginalModule(string module, string identifier)
150
149
}
151
150
}
152
151
153
- public static void RestoreOriginalModule ( string module , string identifier )
152
+ public void RestoreOriginalModule ( string module , string identifier )
154
153
{
155
154
var backupPath = GetBackupPath ( module , identifier ) ;
156
155
var backupSymbolPath = Path . ChangeExtension ( backupPath , ".pdb" ) ;
@@ -159,14 +158,14 @@ public static void RestoreOriginalModule(string module, string identifier)
159
158
// See: https://github.com/tonerdo/coverlet/issues/25
160
159
var retryStrategy = CreateRetryStrategy ( ) ;
161
160
162
- DependencyInjection . Current . GetService < IRetryHelper > ( ) . Retry ( ( ) =>
161
+ _retryHelper . Retry ( ( ) =>
163
162
{
164
163
File . Copy ( backupPath , module , true ) ;
165
164
File . Delete ( backupPath ) ;
166
165
_backupList . TryRemove ( module , out string _ ) ;
167
166
} , retryStrategy , 10 ) ;
168
167
169
- DependencyInjection . Current . GetService < IRetryHelper > ( ) . Retry ( ( ) =>
168
+ _retryHelper . Retry ( ( ) =>
170
169
{
171
170
if ( File . Exists ( backupSymbolPath ) )
172
171
{
@@ -178,7 +177,7 @@ public static void RestoreOriginalModule(string module, string identifier)
178
177
} , retryStrategy , 10 ) ;
179
178
}
180
179
181
- public static void RestoreOriginalModules ( )
180
+ public void RestoreOriginalModules ( )
182
181
{
183
182
// Restore the original module - retry up to 10 times, since the destination file could be locked
184
183
// See: https://github.com/tonerdo/coverlet/issues/25
@@ -187,7 +186,7 @@ public static void RestoreOriginalModules()
187
186
foreach ( string key in _backupList . Keys . ToList ( ) )
188
187
{
189
188
string backupPath = _backupList [ key ] ;
190
- DependencyInjection . Current . GetService < IRetryHelper > ( ) . Retry ( ( ) =>
189
+ _retryHelper . Retry ( ( ) =>
191
190
{
192
191
File . Copy ( backupPath , key , true ) ;
193
192
File . Delete ( backupPath ) ;
@@ -196,15 +195,15 @@ public static void RestoreOriginalModules()
196
195
}
197
196
}
198
197
199
- public static void DeleteHitsFile ( string path )
198
+ public void DeleteHitsFile ( string path )
200
199
{
201
200
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
202
201
// See: https://github.com/tonerdo/coverlet/issues/25
203
202
var retryStrategy = CreateRetryStrategy ( ) ;
204
- DependencyInjection . Current . GetService < IRetryHelper > ( ) . Retry ( ( ) => File . Delete ( path ) , retryStrategy , 10 ) ;
203
+ _retryHelper . Retry ( ( ) => File . Delete ( path ) , retryStrategy , 10 ) ;
205
204
}
206
205
207
- public static bool IsValidFilterExpression ( string filter )
206
+ public bool IsValidFilterExpression ( string filter )
208
207
{
209
208
if ( filter == null )
210
209
return false ;
@@ -236,7 +235,7 @@ public static bool IsValidFilterExpression(string filter)
236
235
return true ;
237
236
}
238
237
239
- public static bool IsModuleExcluded ( string module , string [ ] excludeFilters )
238
+ public bool IsModuleExcluded ( string module , string [ ] excludeFilters )
240
239
{
241
240
if ( excludeFilters == null || excludeFilters . Length == 0 )
242
241
return false ;
@@ -264,7 +263,7 @@ public static bool IsModuleExcluded(string module, string[] excludeFilters)
264
263
return false ;
265
264
}
266
265
267
- public static bool IsModuleIncluded ( string module , string [ ] includeFilters )
266
+ public bool IsModuleIncluded ( string module , string [ ] includeFilters )
268
267
{
269
268
if ( includeFilters == null || includeFilters . Length == 0 )
270
269
return true ;
@@ -291,7 +290,7 @@ public static bool IsModuleIncluded(string module, string[] includeFilters)
291
290
return false ;
292
291
}
293
292
294
- public static bool IsTypeExcluded ( string module , string type , string [ ] excludeFilters )
293
+ public bool IsTypeExcluded ( string module , string type , string [ ] excludeFilters )
295
294
{
296
295
if ( excludeFilters == null || excludeFilters . Length == 0 )
297
296
return false ;
@@ -303,7 +302,7 @@ public static bool IsTypeExcluded(string module, string type, string[] excludeFi
303
302
return IsTypeFilterMatch ( module , type , excludeFilters ) ;
304
303
}
305
304
306
- public static bool IsTypeIncluded ( string module , string type , string [ ] includeFilters )
305
+ public bool IsTypeIncluded ( string module , string type , string [ ] includeFilters )
307
306
{
308
307
if ( includeFilters == null || includeFilters . Length == 0 )
309
308
return true ;
@@ -315,10 +314,10 @@ public static bool IsTypeIncluded(string module, string type, string[] includeFi
315
314
return IsTypeFilterMatch ( module , type , includeFilters ) ;
316
315
}
317
316
318
- public static bool IsLocalMethod ( string method )
317
+ public bool IsLocalMethod ( string method )
319
318
=> new Regex ( WildcardToRegex ( "<*>*__*|*" ) ) . IsMatch ( method ) ;
320
319
321
- private static bool IsTypeFilterMatch ( string module , string type , string [ ] filters )
320
+ private bool IsTypeFilterMatch ( string module , string type , string [ ] filters )
322
321
{
323
322
Debug . Assert ( module != null ) ;
324
323
Debug . Assert ( filters != null ) ;
@@ -338,15 +337,15 @@ private static bool IsTypeFilterMatch(string module, string type, string[] filte
338
337
return false ;
339
338
}
340
339
341
- private static string GetBackupPath ( string module , string identifier )
340
+ private string GetBackupPath ( string module , string identifier )
342
341
{
343
342
return Path . Combine (
344
343
Path . GetTempPath ( ) ,
345
344
Path . GetFileNameWithoutExtension ( module ) + "_" + identifier + ".dll"
346
345
) ;
347
346
}
348
347
349
- private static Func < TimeSpan > CreateRetryStrategy ( int initialSleepSeconds = 6 )
348
+ private Func < TimeSpan > CreateRetryStrategy ( int initialSleepSeconds = 6 )
350
349
{
351
350
TimeSpan retryStrategy ( )
352
351
{
@@ -358,14 +357,14 @@ TimeSpan retryStrategy()
358
357
return retryStrategy ;
359
358
}
360
359
361
- private static string WildcardToRegex ( string pattern )
360
+ private string WildcardToRegex ( string pattern )
362
361
{
363
362
return "^" + Regex . Escape ( pattern ) .
364
363
Replace ( "\\ *" , ".*" ) .
365
364
Replace ( "\\ ?" , "?" ) + "$" ;
366
365
}
367
366
368
- private static bool IsAssembly ( string filePath )
367
+ private bool IsAssembly ( string filePath )
369
368
{
370
369
Debug . Assert ( filePath != null ) ;
371
370
@@ -383,5 +382,4 @@ private static bool IsAssembly(string filePath)
383
382
}
384
383
}
385
384
}
386
- }
387
-
385
+ }
0 commit comments