9
9
using System . Text . RegularExpressions ;
10
10
using Newtonsoft . Json . Linq ;
11
11
12
- [ assembly: AssemblyVersion ( "1.0.1 .*" ) ]
12
+ [ assembly: AssemblyVersion ( "1.0.2 .*" ) ]
13
13
14
14
namespace Gaillard . SharpCover
15
15
{
16
16
public static class Program
17
17
{
18
- public const string RESULTS_FILENAME = "coverageResults.txt" , MISS_PREFIX = "MISS ! " ;
19
- private const string KNOWNS_FILENAME = "coverageKnowns" , HITS_FILENAME = "coverageHits" ;
18
+ public const string RESULTS_FILENAME = "coverageResults.txt" , MISS_PREFIX = "MISS ! " , HITS_FILENAME_PREFIX = "coverageHits" ;
19
+ private const string KNOWNS_FILENAME = "coverageKnowns" ;
20
20
private static readonly MethodInfo countMethodInfo = typeof ( Counter ) . GetMethod ( "Count" ) ;
21
21
22
22
//immutable
@@ -27,7 +27,7 @@ private sealed class InstrumentConfig
27
27
TypeExclude ,
28
28
MethodInclude ,
29
29
MethodExclude ,
30
- HitsPath = Path . Combine ( Directory . GetCurrentDirectory ( ) , HITS_FILENAME ) ;
30
+ HitsPathPrefix = Path . Combine ( Directory . GetCurrentDirectory ( ) , HITS_FILENAME_PREFIX ) ;
31
31
private readonly IDictionary < string , IEnumerable < int > > methodOffsetExcludes = new Dictionary < string , IEnumerable < int > > ( ) ;
32
32
private readonly IDictionary < string , IEnumerable < string > > methodLineExcludes = new Dictionary < string , IEnumerable < string > > ( ) ;
33
33
@@ -100,7 +100,7 @@ private static void Instrument(Instruction instruction,
100
100
101
101
writer . WriteLine ( line ) ;
102
102
103
- var pathParamLoadInstruction = worker . Create ( OpCodes . Ldstr , config . HitsPath ) ;
103
+ var pathParamLoadInstruction = worker . Create ( OpCodes . Ldstr , config . HitsPathPrefix ) ;
104
104
var lineParamLoadInstruction = worker . Create ( OpCodes . Ldc_I4 , instrumentIndex ) ;
105
105
var registerInstruction = worker . Create ( OpCodes . Call , countReference ) ;
106
106
@@ -211,11 +211,15 @@ private static void Instrument(string assemblyPath, InstrumentConfig config, Tex
211
211
212
212
private static int Check ( )
213
213
{
214
+ var currentDirectory = Directory . GetCurrentDirectory ( ) ;
215
+
214
216
var hits = new HashSet < int > ( ) ;
215
- using ( var hitsStream = File . OpenRead ( HITS_FILENAME ) )
216
- using ( var hitsReader = new BinaryReader ( hitsStream ) ) {
217
- while ( hitsStream . Position < hitsStream . Length )
218
- hits . Add ( hitsReader . ReadInt32 ( ) ) ;
217
+ foreach ( var hitsPath in Directory . GetFiles ( currentDirectory , HITS_FILENAME_PREFIX + "*" ) ) {
218
+ using ( var hitsStream = File . OpenRead ( hitsPath ) )
219
+ using ( var hitsReader = new BinaryReader ( hitsStream ) ) {
220
+ while ( hitsStream . Position < hitsStream . Length )
221
+ hits . Add ( hitsReader . ReadInt32 ( ) ) ;
222
+ }
219
223
}
220
224
221
225
var missCount = 0 ;
@@ -234,7 +238,9 @@ private static int Check()
234
238
}
235
239
}
236
240
237
- File . Delete ( HITS_FILENAME ) ;
241
+ //cleanup to leave only results file
242
+ foreach ( var hitsPath in Directory . GetFiles ( currentDirectory , HITS_FILENAME_PREFIX + "*" ) )
243
+ File . Delete ( hitsPath ) ;
238
244
File . Delete ( KNOWNS_FILENAME ) ;
239
245
240
246
var missRatio = ( double ) missCount / ( double ) knownIndex ;
@@ -251,14 +257,14 @@ public static int Main(string[] args)
251
257
if ( args [ 0 ] == "instrument" ) {
252
258
var config = new InstrumentConfig ( args [ 1 ] ) ;
253
259
254
- //so it exists regardless and is deleted
255
- File . WriteAllText ( KNOWNS_FILENAME , string . Empty ) ;
256
- File . WriteAllText ( config . HitsPath , string . Empty ) ;
260
+ //delete existing hit files generatig during program exercising
261
+ foreach ( var hitsPath in Directory . GetFiles ( Directory . GetCurrentDirectory ( ) , HITS_FILENAME_PREFIX + "*" ) )
262
+ File . Delete ( hitsPath ) ;
257
263
258
264
//used to track the line index of the instrumented instruction in the knowns file
259
265
var instrumentIndex = 0 ;
260
266
261
- using ( var writer = new StreamWriter ( KNOWNS_FILENAME , true ) ) {
267
+ using ( var writer = new StreamWriter ( KNOWNS_FILENAME ) ) { //overwrites
262
268
foreach ( var assemblyPath in config . AssemblyPaths )
263
269
Instrument ( assemblyPath , config , writer , ref instrumentIndex ) ;
264
270
}
0 commit comments