@@ -14,6 +14,13 @@ namespace Coverlet.Core.Helpers
14
14
{
15
15
internal static class InstrumentationHelper
16
16
{
17
+ private static readonly Dictionary < string , string > _backupList = new Dictionary < string , string > ( ) ;
18
+
19
+ static InstrumentationHelper ( )
20
+ {
21
+ AppDomain . CurrentDomain . ProcessExit += ( s , e ) => RestoreOriginalModules ( ) ;
22
+ }
23
+
17
24
public static string [ ] GetCoverableModules ( string module , string [ ] directories , bool includeTestAssembly )
18
25
{
19
26
Debug . Assert ( directories != null ) ;
@@ -87,11 +94,13 @@ public static void BackupOriginalModule(string module, string identifier)
87
94
var backupPath = GetBackupPath ( module , identifier ) ;
88
95
var backupSymbolPath = Path . ChangeExtension ( backupPath , ".pdb" ) ;
89
96
File . Copy ( module , backupPath , true ) ;
97
+ _backupList . Add ( module , backupPath ) ;
90
98
91
99
var symbolFile = Path . ChangeExtension ( module , ".pdb" ) ;
92
100
if ( File . Exists ( symbolFile ) )
93
101
{
94
102
File . Copy ( symbolFile , backupSymbolPath , true ) ;
103
+ _backupList . Add ( symbolFile , backupSymbolPath ) ;
95
104
}
96
105
}
97
106
@@ -108,18 +117,39 @@ public static void RestoreOriginalModule(string module, string identifier)
108
117
{
109
118
File . Copy ( backupPath , module , true ) ;
110
119
File . Delete ( backupPath ) ;
120
+ _backupList . Remove ( module ) ;
111
121
} , retryStrategy , 10 ) ;
112
122
113
123
RetryHelper . Retry ( ( ) =>
114
124
{
115
125
if ( File . Exists ( backupSymbolPath ) )
116
126
{
117
- File . Copy ( backupSymbolPath , Path . ChangeExtension ( module , ".pdb" ) , true ) ;
127
+ string symbolFile = Path . ChangeExtension ( module , ".pdb" ) ;
128
+ File . Copy ( backupSymbolPath , symbolFile , true ) ;
118
129
File . Delete ( backupSymbolPath ) ;
130
+ _backupList . Remove ( symbolFile ) ;
119
131
}
120
132
} , retryStrategy , 10 ) ;
121
133
}
122
134
135
+ public static void RestoreOriginalModules ( )
136
+ {
137
+ // Restore the original module - retry up to 10 times, since the destination file could be locked
138
+ // See: https://github.com/tonerdo/coverlet/issues/25
139
+ var retryStrategy = CreateRetryStrategy ( ) ;
140
+
141
+ foreach ( string key in _backupList . Keys . ToList ( ) )
142
+ {
143
+ string backupPath = _backupList [ key ] ;
144
+ RetryHelper . Retry ( ( ) =>
145
+ {
146
+ File . Copy ( backupPath , key , true ) ;
147
+ File . Delete ( backupPath ) ;
148
+ _backupList . Remove ( key ) ;
149
+ } , retryStrategy , 10 ) ;
150
+ }
151
+ }
152
+
123
153
public static void DeleteHitsFile ( string path )
124
154
{
125
155
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
0 commit comments