Skip to content

Commit

Permalink
Refactor detection if assembly was instrumented
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslorentz committed Dec 20, 2020
1 parent 7747615 commit 4fc54d0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
13 changes: 5 additions & 8 deletions src/MiniCover.Core/Instrumentation/AssemblyInstrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ private InstrumentedAssembly InstrumentAssemblyDefinition(
return null;
}

var isInstrumented = false;

var instrumentedAssembly = new InstrumentedAssembly(assemblyDefinition.Name.Name);
var instrumentedAttributeReference = assemblyDefinition.MainModule.ImportReference(instrumentedAttributeConstructor);
assemblyDefinition.CustomAttributes.Add(new CustomAttribute(instrumentedAttributeReference));
Expand All @@ -98,17 +96,16 @@ private InstrumentedAssembly InstrumentAssemblyDefinition(
|| typeDefinition.DeclaringType != null)
continue;

if (_typeInstrumenter.InstrumentType(
_typeInstrumenter.InstrumentType(
context,
typeDefinition,
instrumentedAssembly))
{
isInstrumented = true;
}
instrumentedAssembly);
}

if (!isInstrumented)
if (!instrumentedAssembly.Methods.Any()) {
_logger.LogInformation("Nothing to instrument");
return null;
}

_logger.LogInformation("Assembly instrumented");

Expand Down
2 changes: 1 addition & 1 deletion src/MiniCover.Core/Instrumentation/ITypeInstrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace MiniCover.Core.Instrumentation
{
public interface ITypeInstrumenter
{
bool InstrumentType(IInstrumentationContext context, TypeDefinition typeDefinition, InstrumentedAssembly instrumentedAssembly);
void InstrumentType(IInstrumentationContext context, TypeDefinition typeDefinition, InstrumentedAssembly instrumentedAssembly);
}
}
14 changes: 3 additions & 11 deletions src/MiniCover.Core/Instrumentation/TypeInstrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ public TypeInstrumenter(IMethodInstrumenter methodInstrumenter)
_methodInstrumenter = methodInstrumenter;
}

public bool InstrumentType(
public void InstrumentType(
IInstrumentationContext context,
TypeDefinition typeDefinition,
InstrumentedAssembly instrumentedAssembly)
{
var instrumented = false;

foreach (var methodDefinition in typeDefinition.Methods)
{
if (!methodDefinition.HasBody || !methodDefinition.DebugInformation.HasSequencePoints)
Expand All @@ -34,16 +32,10 @@ public bool InstrumentType(
isSource,
methodDefinition,
instrumentedAssembly);

instrumented = true;
}

foreach (var nestedType in typeDefinition.NestedTypes) {
if (InstrumentType(context, nestedType, instrumentedAssembly))
instrumented = true;
}

return instrumented;
foreach (var nestedType in typeDefinition.NestedTypes)
InstrumentType(context, nestedType, instrumentedAssembly);
}
}
}

0 comments on commit 4fc54d0

Please sign in to comment.