Skip to content

Commit ec2165f

Browse files
Mohammad DehghanMohammad Dehghan
authored andcommitted
Create recording controller as a result of instrumentation
This successfully encapsulates creating the recording controller and hides implementation details about Runtime Config file.
1 parent 43ca130 commit ec2165f

File tree

8 files changed

+44
-42
lines changed

8 files changed

+44
-42
lines changed

SG.CodeCoverage.TestConsole/Program.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,9 @@ static void Main()
1616
var tester = new InstrumenterTester();
1717
tester.InstrumentSampleProject();
1818
tester.RunSomeCode();
19-
var visitedFiles = tester.GetVisitedFiles();
20-
Console.WriteLine(visitedFiles.Count);
21-
}
22-
23-
static void InstrumentPath(string dir, string backupFolder, string filePattern)
24-
{
25-
var mapFilePath = Path.Combine(dir, "map.json");
26-
var filesToInstrument = Directory
27-
.GetFiles(dir, filePattern)
28-
.ToList();
29-
var options = new InstrumentationOptions(filesToInstrument, new[] { dir }, dir, 12398);
30-
var instrumenter = new Instrumenter(options, mapFilePath, null)
31-
{
32-
BackupFolder = backupFolder,
33-
RecorderLogFilePath = Path.Combine(dir, "CodeCoverageRecorderLog.txt")
34-
};
35-
Directory.CreateDirectory(instrumenter.BackupFolder);
36-
var map = instrumenter.Instrument();
19+
var result = tester.GetCoverageResult();
20+
Console.WriteLine($"Visited {result.GetVisitedSources().Count()} source files" +
21+
$" and {result.GetVisitedMethods().Count()} methods.");
3722
}
3823
}
3924
}

SG.CodeCoverage.Tests.NetFx/InstrumenterTester.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,14 @@ namespace SG.CodeCoverage.Tests
1414
{
1515
public class InstrumenterTester
1616
{
17-
public const int PortNumber = 61238;
18-
public static string DefaultOutputPath { get; }
17+
public int PortNumber { get; set; } = 61238;
18+
public static string DefaultOutputPath { get; } = Path.Combine(Path.GetTempPath(), "SG.CodeCoverage");
1919
public string OutputPath { get; }
2020
public string MapFilePath { get; }
21-
public InstrumentationMap Map { get; private set; }
21+
public IRecordingController RecordingController { get; private set; }
2222
public string InstrumentedAssemblyPath { get; private set; }
2323
private readonly ILogger _logger;
2424

25-
static InstrumenterTester()
26-
{
27-
DefaultOutputPath = Path.Combine(Path.GetTempPath(), "SG.CodeCoverage");
28-
}
29-
3025
public InstrumenterTester(ILogger logger = null)
3126
{
3227
OutputPath = DefaultOutputPath;
@@ -61,7 +56,7 @@ public void InstrumentSampleProject()
6156
Instrumenter instrumenter = new Instrumenter(options, _logger);
6257
instrumenter.BackupFolder = Path.Combine(OutputPath, "backup");
6358
Directory.CreateDirectory(instrumenter.BackupFolder);
64-
Map = instrumenter.Instrument();
59+
RecordingController = instrumenter.Instrument();
6560
InstrumentedAssemblyPath = assemblyFileName;
6661
}
6762

@@ -77,19 +72,17 @@ public void RunSomeCode()
7772
{
7873
if (InstrumentedAssemblyPath == null)
7974
throw new InvalidOperationException("Sample assembly is not instrumented.");
80-
var client = RecordingController.ForEndPoint("localhost", PortNumber, Map, _logger);
8175
Assembly.LoadFrom(Path.Combine(OutputPath, "SG.CodeCoverage.Recorder.dll"));
8276
var assembly = Assembly.LoadFrom(InstrumentedAssemblyPath);
8377
var calc = assembly.DefinedTypes.Where(x => x.Name == nameof(PrimeCalculator)).FirstOrDefault();
8478
var res = calc.GetMethod("IsPrime").Invoke(calc.DeclaredConstructors.First().Invoke(null), new object[] { 7 });
8579
}
8680

87-
public List<string> GetVisitedFiles()
81+
public CoverageResult GetCoverageResult()
8882
{
89-
if (InstrumentedAssemblyPath == null)
83+
if (InstrumentedAssemblyPath == null || RecordingController == null)
9084
throw new InvalidOperationException("Sample assembly is not instrumented.");
91-
var client = RecordingController.ForEndPoint("localhost", PortNumber, Map, _logger);
92-
return client.CollectResultAndReset().GetVisitedSources().ToList(); ;
85+
return RecordingController.CollectResultAndReset();
9386
}
9487
}
9588
}

SG.CodeCoverage/Collection/MultiRecordingController.cs renamed to SG.CodeCoverage/Collection/DynamicPortRecordingController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace SG.CodeCoverage.Collection
1111
{
12-
internal class MultiRecordingController : IRecordingController
12+
internal class DynamicPortRecordingController : IRecordingController
1313
{
1414
private readonly string _recorderRuntimeConfigFilePath;
1515
private readonly InstrumentationMap _map;
@@ -18,13 +18,15 @@ internal class MultiRecordingController : IRecordingController
1818
private RuntimeConfig _currentRuntimeConfig;
1919
private const string _host = "localhost";
2020

21-
public MultiRecordingController(string recorderRuntimeConfigFilePath, InstrumentationMap map, ILogger logger = null)
21+
public DynamicPortRecordingController(string recorderRuntimeConfigFilePath, InstrumentationMap map, ILogger logger = null)
2222
{
2323
_recorderRuntimeConfigFilePath = recorderRuntimeConfigFilePath;
2424
_map = map;
2525
_logger = logger ?? new ConsoleLogger();
2626
}
2727

28+
public InstrumentationMap InstrumentationMap => _map;
29+
2830
public void ResetHits()
2931
{
3032
if (!LoadRuntimeConfig())

SG.CodeCoverage/Collection/SingleRecordingController.cs renamed to SG.CodeCoverage/Collection/FixedPortRecordingController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44

55
namespace SG.CodeCoverage.Collection
66
{
7-
internal class SingleRecordingController : IRecordingController
7+
internal class FixedPortRecordingController : IRecordingController
88
{
99
private readonly string _host;
1010
private readonly int _port;
1111
private readonly InstrumentationMap _map;
1212
private readonly ILogger _logger;
1313

14-
public SingleRecordingController(string host, int port, InstrumentationMap map, ILogger logger = null)
14+
public FixedPortRecordingController(string host, int port, InstrumentationMap map, ILogger logger = null)
1515
{
1616
_host = host;
1717
_port = port;
1818
_map = map;
1919
_logger = logger ?? new ConsoleLogger();
2020
}
2121

22+
public InstrumentationMap InstrumentationMap => _map;
23+
2224
public CoverageResult CollectResultAndReset()
2325
{
2426
_logger.LogInformation($"Collecting coverage result for {_host}:{_port}");

SG.CodeCoverage/Collection/IRecordingController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using SG.CodeCoverage.Coverage;
2+
using SG.CodeCoverage.Metadata;
23

34
namespace SG.CodeCoverage.Collection
45
{
56
public interface IRecordingController
67
{
8+
InstrumentationMap InstrumentationMap { get; }
79
void ResetHits();
810
CoverageResult CollectResultAndReset();
911
}

SG.CodeCoverage/Collection/RecordingController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public static class RecordingController
77
{
88
public static IRecordingController ForRuntimeConfigFile(string runtimeConfigFilePath, InstrumentationMap map, ILogger logger)
99
{
10-
return new MultiRecordingController(runtimeConfigFilePath, map, logger);
10+
return new DynamicPortRecordingController(runtimeConfigFilePath, map, logger);
1111
}
1212

1313
public static IRecordingController ForEndPoint(string host, int port, InstrumentationMap map, ILogger logger)
1414
{
15-
return new SingleRecordingController(host, port, map, logger);
15+
return new FixedPortRecordingController(host, port, map, logger);
1616
}
1717
}
1818
}

SG.CodeCoverage/Instrumentation/Instrumenter.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using Newtonsoft.Json;
88
using Mono.Cecil.Cil;
9+
using SG.CodeCoverage.Collection;
910
using SG.CodeCoverage.Metadata;
1011

1112
namespace SG.CodeCoverage.Instrumentation
@@ -53,7 +54,7 @@ public Instrumenter(
5354
{
5455
}
5556

56-
public InstrumentationMap Instrument()
57+
public Collection.IRecordingController Instrument()
5758
{
5859
UniqueId = Guid.NewGuid();
5960

@@ -65,7 +66,24 @@ public InstrumentationMap Instrument()
6566
if (!string.IsNullOrEmpty(OutputMapFilePath))
6667
SaveMapFile(map, OutputMapFilePath);
6768

68-
return map;
69+
return CreateRecordingController(map);
70+
}
71+
72+
private IRecordingController CreateRecordingController(InstrumentationMap map)
73+
{
74+
if (Options.ControllerPortNumber > 0)
75+
{
76+
return new FixedPortRecordingController(
77+
"localhost", Options.ControllerPortNumber, map, _logger);
78+
}
79+
else
80+
{
81+
var dir = Options.RuntimeConfigOutputPath;
82+
if (string.IsNullOrEmpty(dir))
83+
dir = Options.RecorderAssemblyCopyPath;
84+
var fullPath = Path.Combine(dir, Options.RuntimeConfigOutputPath);
85+
return new DynamicPortRecordingController(fullPath , map, _logger);
86+
}
6987
}
7088

7189
private InstrumentationMap InstrumentInternal()

SG.CodeCoverage/SG.CodeCoverage.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
</ItemGroup>
4545
<ItemGroup>
4646
<Compile Include="Collection\IRecordingController.cs" />
47-
<Compile Include="Collection\MultiRecordingController.cs" />
47+
<Compile Include="Collection\DynamicPortRecordingController.cs" />
4848
<Compile Include="Collection\RecordingControllerClient.cs" />
4949
<Compile Include="Collection\RecordingController.cs" />
50-
<Compile Include="Collection\SingleRecordingController.cs" />
50+
<Compile Include="Collection\FixedPortRecordingController.cs" />
5151
<Compile Include="Common\ConsoleLogger.cs" />
5252
<Compile Include="Common\ILogger.cs" />
5353
<Compile Include="Common\VersionInfo.cs" />

0 commit comments

Comments
 (0)