Merge pull request #65 from sjp/fix-file-locking

Avoid leaving file handles open
This commit is contained in:
Toni Solarin-Sodara
2018-04-28 10:58:07 +01:00
committed by GitHub
2 changed files with 21 additions and 21 deletions
@@ -19,7 +19,8 @@ namespace Coverlet.Core.Helpers
public static bool HasPdb(string module)
{
using (var peReader = new PEReader(File.OpenRead(module)))
using (var moduleStream = File.OpenRead(module))
using (var peReader = new PEReader(moduleStream))
{
foreach (var entry in peReader.ReadDebugDirectory())
{
@@ -1,9 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Coverlet.Core.Helpers;
using Coverlet.Core.Extensions;
using Mono.Cecil;
using Mono.Cecil.Cil;
@@ -13,8 +11,8 @@ namespace Coverlet.Core.Instrumentation
{
internal class Instrumenter
{
private string _module;
private string _identifier;
private readonly string _module;
private readonly string _identifier;
private InstrumenterResult _result;
public Instrumenter(string module, string identifier)
@@ -48,25 +46,26 @@ namespace Coverlet.Core.Instrumentation
private void InstrumentModule()
{
using (var stream = new FileStream(_module, FileMode.Open, FileAccess.ReadWrite))
using (var resolver = new DefaultAssemblyResolver())
{
var resolver = new DefaultAssemblyResolver();
resolver.AddSearchDirectory(Path.GetDirectoryName(_module));
var parameters = new ReaderParameters { ReadSymbols = true, AssemblyResolver = resolver };
ModuleDefinition module = ModuleDefinition.ReadModule(stream, parameters);
foreach (var type in module.GetTypes())
using (var module = ModuleDefinition.ReadModule(stream, parameters))
{
if (type.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
continue;
foreach (var method in type.Methods)
foreach (var type in module.GetTypes())
{
if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
InstrumentMethod(method);
}
}
if (type.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
continue;
module.Write(stream);
foreach (var method in type.Methods)
{
if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
InstrumentMethod(method);
}
}
module.Write(stream);
}
}
}
@@ -138,7 +137,7 @@ namespace Coverlet.Core.Instrumentation
return pathInstr;
}
private bool IsBranchTarget(ILProcessor processor, Instruction instruction)
private static bool IsBranchTarget(ILProcessor processor, Instruction instruction)
{
foreach (var _instruction in processor.Body.Instructions)
{
@@ -155,7 +154,7 @@ namespace Coverlet.Core.Instrumentation
return false;
}
private void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget)
private static void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget)
{
if (instruction.Operand is Instruction _instruction)
{
@@ -175,7 +174,7 @@ namespace Coverlet.Core.Instrumentation
}
}
private void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, Instruction oldTarget, Instruction newTarget)
private static void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, Instruction oldTarget, Instruction newTarget)
{
if (handler.FilterStart == oldTarget)
handler.FilterStart = newTarget;