Skip to content

Commit

Permalink
Merge pull request #138 from lucaslorentz/feature/instrumentation-con…
Browse files Browse the repository at this point in the history
…text-interface

Add IInstrumentationContext interface
  • Loading branch information
lucaslorentz committed Dec 20, 2020
2 parents 3e92fcc + 4fc54d0 commit 1a48051
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 63 deletions.
19 changes: 9 additions & 10 deletions src/MiniCover.Core/Instrumentation/AssemblyInstrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AssemblyInstrumenter(
}

public InstrumentedAssembly InstrumentAssemblyFile(
InstrumentationContext context,
IInstrumentationContext context,
IFileInfo assemblyFile)
{
var assemblyDirectory = assemblyFile.Directory;
Expand All @@ -59,7 +59,7 @@ public InstrumentedAssembly InstrumentAssemblyFile(
}

private InstrumentedAssembly InstrumentAssemblyDefinition(
InstrumentationContext context,
IInstrumentationContext context,
AssemblyDefinition assemblyDefinition)
{
if (assemblyDefinition.CustomAttributes.Any(a => a.AttributeType.Name == "InstrumentedAttribute"))
Expand All @@ -70,12 +70,6 @@ private InstrumentedAssembly InstrumentAssemblyDefinition(

var assemblyDocuments = assemblyDefinition.GetAllDocuments();

if (!assemblyDocuments.Any(d => context.IsSource(d.Url) || context.IsTest(d.Url)))
{
_logger.LogInformation("No link to source files or test files");
return null;
}

var changedDocuments = assemblyDocuments.Where(d => d.FileHasChanged()).ToArray();
if (changedDocuments.Any())
{
Expand All @@ -91,8 +85,6 @@ private InstrumentedAssembly InstrumentAssemblyDefinition(
return null;
}

_logger.LogInformation("Instrumenting");

var instrumentedAssembly = new InstrumentedAssembly(assemblyDefinition.Name.Name);
var instrumentedAttributeReference = assemblyDefinition.MainModule.ImportReference(instrumentedAttributeConstructor);
assemblyDefinition.CustomAttributes.Add(new CustomAttribute(instrumentedAttributeReference));
Expand All @@ -110,6 +102,13 @@ private InstrumentedAssembly InstrumentAssemblyDefinition(
instrumentedAssembly);
}

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

_logger.LogInformation("Assembly instrumented");

var miniCoverTempPath = GetMiniCoverTempPath();

var instrumentedAssemblyFile = _fileSystem.FileInfo.FromFileName(Path.Combine(miniCoverTempPath, $"{Guid.NewGuid()}.dll"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using MiniCover.Core.Extensions;
using Mono.Cecil;

namespace MiniCover.Core.Instrumentation
{
public class FileBasedInstrumentationContext : IInstrumentationContext
{
private int _uniqueId;

public virtual IList<IFileInfo> Assemblies { get; set; }
public virtual IList<IFileInfo> Sources { get; set; }
public virtual IList<IFileInfo> Tests { get; set; }
public virtual string HitsPath { get; set; }
public virtual IDirectoryInfo Workdir { get; set; }

public virtual int NewInstructionId()
{
return ++_uniqueId;
}

public virtual bool IsSource(MethodDefinition methodDefinition)
{
return methodDefinition.GetAllDocuments()
.Any(d => Sources.Any(s => s.FullName == d.Url));
}

public virtual bool IsTest(MethodDefinition methodDefinition)
{
return methodDefinition.GetAllDocuments()
.Any(d => Tests.Any(s => s.FullName == d.Url));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace MiniCover.Core.Instrumentation
{
public interface IAssemblyInstrumenter
{
InstrumentedAssembly InstrumentAssemblyFile(InstrumentationContext context, IFileInfo assemblyFile);
InstrumentedAssembly InstrumentAssemblyFile(IInstrumentationContext context, IFileInfo assemblyFile);
}
}
16 changes: 16 additions & 0 deletions src/MiniCover.Core/Instrumentation/IInstrumentationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.IO.Abstractions;
using Mono.Cecil;

namespace MiniCover.Core.Instrumentation
{
public interface IInstrumentationContext
{
IList<IFileInfo> Assemblies { get; }
string HitsPath { get; }
IDirectoryInfo Workdir { get; }
int NewInstructionId();
bool IsSource(MethodDefinition methodDefinition);
bool IsTest(MethodDefinition methodDefinition);
}
}
2 changes: 1 addition & 1 deletion src/MiniCover.Core/Instrumentation/IInstrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace MiniCover.Core.Instrumentation
{
public interface IInstrumenter
{
InstrumentationResult Instrument(InstrumentationContext context);
InstrumentationResult Instrument(IInstrumentationContext context);
}
}
2 changes: 1 addition & 1 deletion src/MiniCover.Core/Instrumentation/IMethodInstrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace MiniCover.Core.Instrumentation
{
public interface IMethodInstrumenter
{
void InstrumentMethod(InstrumentationContext context, bool instrumentInstructions, MethodDefinition methodDefinition, InstrumentedAssembly instrumentedAssembly);
void InstrumentMethod(IInstrumentationContext context, bool instrumentInstructions, MethodDefinition methodDefinition, InstrumentedAssembly instrumentedAssembly);
}
}
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
{
void InstrumentType(InstrumentationContext context, TypeDefinition typeDefinition, InstrumentedAssembly instrumentedAssembly);
void InstrumentType(IInstrumentationContext context, TypeDefinition typeDefinition, InstrumentedAssembly instrumentedAssembly);
}
}
27 changes: 0 additions & 27 deletions src/MiniCover.Core/Instrumentation/InstrumentationContext.cs

This file was deleted.

6 changes: 2 additions & 4 deletions src/MiniCover.Core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ public Instrumenter(
_logger = logger;
}

public InstrumentationResult Instrument(InstrumentationContext context)
public InstrumentationResult Instrument(IInstrumentationContext context)
{
context.Workdir = context.Workdir;

var result = new InstrumentationResult
{
SourcePath = context.Workdir.FullName,
Expand Down Expand Up @@ -79,7 +77,7 @@ private bool ShouldInstrumentAssemblyFile(IFileInfo assemblyFile)
}

private void VisitAssemblyGroup(
InstrumentationContext context,
IInstrumentationContext context,
InstrumentationResult result,
IEnumerable<IFileInfo> assemblyFiles)
{
Expand Down
13 changes: 7 additions & 6 deletions src/MiniCover.Core/Instrumentation/MethodInstrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public MethodInstrumenter(
}

public void InstrumentMethod(
InstrumentationContext context,
IInstrumentationContext context,
bool instrumentInstructions,
MethodDefinition methodDefinition,
InstrumentedAssembly instrumentedAssembly)
Expand Down Expand Up @@ -96,10 +96,11 @@ public void InstrumentMethod(
}

private void InstrumentInstructions(
InstrumentationContext context,
IInstrumentationContext context,
MethodDefinition methodDefinition,
InstrumentedAssembly instrumentedAssembly,
IList<(SequencePoint sequencePoint, Instruction instruction)> sequencePointsInstructions,
IList<(SequencePoint sequencePoint,
Instruction instruction)> sequencePointsInstructions,
ILProcessor ilProcessor,
VariableDefinition methodContextVariable,
InstrumentedMethod instrumentedMethod)
Expand Down Expand Up @@ -219,11 +220,11 @@ private static int InstrumentInstruction(
ILProcessor ilProcessor,
VariableDefinition methodContextVariable,
MethodReference hitMethodReference,
InstrumentationContext context)
IInstrumentationContext context)
{
return instruction.GetOrAddExtension("Id", () =>
{
var id = ++context.UniqueId;
var id = context.NewInstructionId();
ilProcessor.InsertBefore(instruction, new[]
{
ilProcessor.Create(OpCodes.Ldloc, methodContextVariable),
Expand All @@ -239,7 +240,7 @@ private IEnumerable<Instruction> GetExclusions(IList<Instruction> instructions)
return LambdaInitPattern.FindInstructions(instructions);
}

private static string GetSourceRelativePath(InstrumentationContext context, string path)
private static string GetSourceRelativePath(IInstrumentationContext context, string path)
{
return PathUtils.GetRelativePath(context.Workdir.FullName, path);
}
Expand Down
13 changes: 4 additions & 9 deletions src/MiniCover.Core/Instrumentation/TypeInstrumenter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;
using MiniCover.Core.Extensions;
using MiniCover.Core.Model;
using MiniCover.Core.Model;
using Mono.Cecil;

namespace MiniCover.Core.Instrumentation
Expand All @@ -15,7 +13,7 @@ public TypeInstrumenter(IMethodInstrumenter methodInstrumenter)
}

public void InstrumentType(
InstrumentationContext context,
IInstrumentationContext context,
TypeDefinition typeDefinition,
InstrumentedAssembly instrumentedAssembly)
{
Expand All @@ -24,11 +22,8 @@ public void InstrumentType(
if (!methodDefinition.HasBody || !methodDefinition.DebugInformation.HasSequencePoints)
continue;

var methodDocuments = methodDefinition.GetAllDocuments();

var isSource = methodDocuments.Any(d => context.IsSource(d.Url));
var isTest = methodDocuments.Any(d => context.IsTest(d.Url));

var isTest = context.IsTest(methodDefinition);
var isSource = context.IsSource(methodDefinition);
if (!isSource && !isTest)
continue;

Expand Down
2 changes: 1 addition & 1 deletion src/MiniCover/CommandLine/Commands/InstrumentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Task<int> Execute()

var testFiles = GetFiles(_includeTestsOption.Value, _excludeTestsOption.Value, _parentDirOption.DirectoryInfo);

var instrumentationContext = new InstrumentationContext
var instrumentationContext = new FileBasedInstrumentationContext
{
Assemblies = assemblies,
HitsPath = _hitsDirectoryOption.DirectoryInfo.FullName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public static MethodBase Load(this MethodDefinition methodDefinition)
return type.GetMembers().OfType<MethodBase>().First(m => m.Name == methodDefinition.Name);
}

private static InstrumentationContext CreateInstrumentationContext(IEnumerable<string> documents)
private static FileBasedInstrumentationContext CreateInstrumentationContext(IEnumerable<string> documents)
{
return new InstrumentationContext
return new FileBasedInstrumentationContext
{
HitsPath = "/tmp",
Workdir = _fileSystem.DirectoryInfo.FromDirectoryName("/tmp"),
Expand Down

0 comments on commit 1a48051

Please sign in to comment.