2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
4
using System . Linq ;
5
+ using System . Reflection ;
5
6
using System . Reflection . PortableExecutable ;
6
- using Microsoft . Extensions . FileSystemGlobbing ;
7
7
8
- using Coverlet . Core . Instrumentation ;
8
+ using Microsoft . Extensions . FileSystemGlobbing ;
9
9
using Microsoft . Extensions . FileSystemGlobbing . Abstractions ;
10
10
11
11
namespace Coverlet . Core . Helpers
@@ -15,7 +15,7 @@ internal static class InstrumentationHelper
15
15
public static string [ ] GetDependencies ( string module )
16
16
{
17
17
IEnumerable < string > modules = Directory . GetFiles ( Path . GetDirectoryName ( module ) , "*.dll" ) ;
18
- modules = modules . Where ( a => Path . GetFileName ( a ) != Path . GetFileName ( module ) ) ;
18
+ modules = modules . Where ( a => IsAssembly ( a ) && Path . GetFileName ( a ) != Path . GetFileName ( module ) ) ;
19
19
return modules . ToArray ( ) ;
20
20
}
21
21
@@ -41,39 +41,29 @@ public static bool HasPdb(string module)
41
41
public static void CopyCoverletDependency ( string module )
42
42
{
43
43
var directory = Path . GetDirectoryName ( module ) ;
44
- if ( Path . GetFileNameWithoutExtension ( module ) == "coverlet.core" )
45
- return ;
44
+ var moduleFileName = Path . GetFileName ( module ) ;
46
45
47
46
var assembly = typeof ( Coverage ) . Assembly ;
48
47
string name = Path . GetFileName ( assembly . Location ) ;
48
+ if ( name == moduleFileName )
49
+ return ;
50
+
49
51
File . Copy ( assembly . Location , Path . Combine ( directory , name ) , true ) ;
50
52
}
51
53
52
54
public static void BackupOriginalModule ( string module , string identifier )
53
55
{
54
- var backupPath = Path . Combine (
55
- Path . GetTempPath ( ) ,
56
- Path . GetFileNameWithoutExtension ( module ) + "_" + identifier + ".dll"
57
- ) ;
58
-
56
+ var backupPath = GetBackupPath ( module , identifier ) ;
59
57
File . Copy ( module , backupPath ) ;
60
58
}
61
59
62
60
public static void RestoreOriginalModule ( string module , string identifier )
63
61
{
64
- var backupPath = Path . Combine (
65
- Path . GetTempPath ( ) ,
66
- Path . GetFileNameWithoutExtension ( module ) + "_" + identifier + ".dll"
67
- ) ;
62
+ var backupPath = GetBackupPath ( module , identifier ) ;
68
63
69
64
// Restore the original module - retry up to 10 times, since the destination file could be locked
70
65
// See: https://github.com/tonerdo/coverlet/issues/25
71
- var currentSleep = 6 ;
72
- Func < TimeSpan > retryStrategy = ( ) => {
73
- var sleep = TimeSpan . FromMilliseconds ( currentSleep ) ;
74
- currentSleep *= 2 ;
75
- return sleep ;
76
- } ;
66
+ var retryStrategy = CreateRetryStrategy ( ) ;
77
67
78
68
RetryHelper . Retry ( ( ) => {
79
69
File . Copy ( backupPath , module , true ) ;
@@ -85,13 +75,7 @@ public static IEnumerable<string> ReadHitsFile(string path)
85
75
{
86
76
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
87
77
// See: https://github.com/tonerdo/coverlet/issues/25
88
- var currentSleep = 6 ;
89
- Func < TimeSpan > retryStrategy = ( ) =>
90
- {
91
- var sleep = TimeSpan . FromMilliseconds ( currentSleep ) ;
92
- currentSleep *= 2 ;
93
- return sleep ;
94
- } ;
78
+ var retryStrategy = CreateRetryStrategy ( ) ;
95
79
96
80
return RetryHelper . Do ( ( ) => File . ReadLines ( path ) , retryStrategy , 10 ) ;
97
81
}
@@ -100,39 +84,33 @@ public static void DeleteHitsFile(string path)
100
84
{
101
85
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
102
86
// See: https://github.com/tonerdo/coverlet/issues/25
103
- var currentSleep = 6 ;
104
- Func < TimeSpan > retryStrategy = ( ) => {
105
- var sleep = TimeSpan . FromMilliseconds ( currentSleep ) ;
106
- currentSleep *= 2 ;
107
- return sleep ;
108
- } ;
87
+ var retryStrategy = CreateRetryStrategy ( ) ;
109
88
110
89
RetryHelper . Retry ( ( ) => File . Delete ( path ) , retryStrategy , 10 ) ;
111
90
}
112
-
113
- public static IEnumerable < string > GetExcludedFiles ( IEnumerable < string > excludeRules ,
114
- string parentDir = null )
91
+
92
+ public static IEnumerable < string > GetExcludedFiles ( IEnumerable < string > excludeRules ,
93
+ string parentDir = null )
115
94
{
116
95
const string RELATIVE_KEY = nameof ( RELATIVE_KEY ) ;
117
96
parentDir = string . IsNullOrWhiteSpace ( parentDir ) ? Directory . GetCurrentDirectory ( ) : parentDir ;
118
-
97
+
119
98
if ( excludeRules == null || ! excludeRules . Any ( ) ) return Enumerable . Empty < string > ( ) ;
120
-
99
+
121
100
var matcherDict = new Dictionary < string , Matcher > ( ) { { RELATIVE_KEY , new Matcher ( ) } } ;
122
101
foreach ( var excludeRule in excludeRules )
123
102
{
124
103
if ( Path . IsPathRooted ( excludeRule ) ) {
125
104
var root = Path . GetPathRoot ( excludeRule ) ;
126
105
if ( ! matcherDict . ContainsKey ( root ) ) {
127
106
matcherDict . Add ( root , new Matcher ( ) ) ;
128
- }
107
+ }
129
108
matcherDict [ root ] . AddInclude ( excludeRule . Substring ( root . Length ) ) ;
130
109
} else {
131
110
matcherDict [ RELATIVE_KEY ] . AddInclude ( excludeRule ) ;
132
111
}
133
-
134
112
}
135
-
113
+
136
114
var files = new List < string > ( ) ;
137
115
foreach ( var entry in matcherDict )
138
116
{
@@ -144,9 +122,42 @@ public static IEnumerable<string> GetExcludedFiles(IEnumerable<string> excludeRu
144
122
. Select ( f => Path . GetFullPath ( Path . Combine ( directoryInfo . ToString ( ) , f . Path ) ) ) ;
145
123
files . AddRange ( currentFiles ) ;
146
124
}
147
-
125
+
148
126
return files . Distinct ( ) ;
149
127
}
128
+
129
+ private static bool IsAssembly ( string filePath )
130
+ {
131
+ try
132
+ {
133
+ AssemblyName . GetAssemblyName ( filePath ) ;
134
+ return true ;
135
+ }
136
+ catch
137
+ {
138
+ return false ;
139
+ }
140
+ }
141
+
142
+ private static string GetBackupPath ( string module , string identifier )
143
+ {
144
+ return Path . Combine (
145
+ Path . GetTempPath ( ) ,
146
+ Path . GetFileNameWithoutExtension ( module ) + "_" + identifier + ".dll"
147
+ ) ;
148
+ }
149
+
150
+ private static Func < TimeSpan > CreateRetryStrategy ( int initialSleepSeconds = 6 )
151
+ {
152
+ TimeSpan retryStrategy ( )
153
+ {
154
+ var sleep = TimeSpan . FromMilliseconds ( initialSleepSeconds ) ;
155
+ initialSleepSeconds *= 2 ;
156
+ return sleep ;
157
+ }
158
+
159
+ return retryStrategy ;
160
+ }
150
161
}
151
162
}
152
163
0 commit comments