Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experiment]Expose coverlet.core.dll as nuget package #628

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Reference Include="coverlet.core">
<HintPath>..\..\..\..\src\coverlet.core\bin\Debug\netstandard2.0\coverlet.core.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Calculator
{
public class CalculatorRuntime
{
public double Add(double a, double b)
{
return a + b;
}

public double Subtract(double a, double b)
{
return a - b;
}

public double Divide(double a, double b)
{
return a / b;
}

public double Multiply(double a, double b)
{
return a * b;
}
}
}
101 changes: 101 additions & 0 deletions Documentation/Examples/StandaloneUsage/Calculator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#nullable enable

using System;
using System.IO;

namespace Calculator
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0 || !File.Exists(args[0]))
{
Console.WriteLine("Instrumentation result file not found");
}


RealTimeCoverageAnalysis inProc = new RealTimeCoverageAnalysis(args[0]);
CalculatorRuntime runtime = new CalculatorRuntime();
double? operanda = null;
double? operandb = null;
for (; ; )
{
if (operanda is null)
{
Console.WriteLine("Insert operand a");
double operand;
while (!double.TryParse(Console.ReadLine(), out operand))
{
Console.WriteLine("Invalid value, insert operand a");
}
operanda = operand;
}
if (operandb is null)
{
Console.WriteLine("Insert operand b");
double operand;
while (!double.TryParse(Console.ReadLine(), out operand))
{
Console.WriteLine("Invalid value, insert operand b");
}
operandb = operand;
}


for (; ; )
{
Console.WriteLine("Insert operation");
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
if (consoleKeyInfo.Key != ConsoleKey.Add && consoleKeyInfo.Key != ConsoleKey.Subtract &&
consoleKeyInfo.Key != ConsoleKey.Divide && consoleKeyInfo.Key != ConsoleKey.Multiply)
{
Console.WriteLine("Invalid operation, allowed operation, +-*/");
}
else
{
Console.WriteLine();
switch (consoleKeyInfo.Key)
{
case ConsoleKey.Add:
{
Console.WriteLine($"Result: {runtime.Add(operanda.Value, operandb.Value)}");
break;
}
case ConsoleKey.Subtract:
{
Console.WriteLine($"Result: {runtime.Subtract(operanda.Value, operandb.Value)}");
break;
}
case ConsoleKey.Multiply:
{
Console.WriteLine($"Result: {runtime.Multiply(operanda.Value, operandb.Value)}");
break;
}
case ConsoleKey.Divide:
{
Console.WriteLine($"Result: {runtime.Divide(operanda.Value, operandb.Value)}");
break;
}
default:
break;
}
operanda = operandb = null;

inProc.PrintCoverageCurrentState();

Console.WriteLine();
Console.WriteLine("Exit(press E)? Any other button to another loop");
if (Console.ReadKey().Key == ConsoleKey.E)
{
return;
}
Console.Clear();
break;
}
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.IO;
using System.Reflection;

using Coverlet.Core.Abstracts;
using Coverlet.Core.Instrumentation;
using Coverlet.Core.ObjectModel;

namespace Calculator
{
public class RealTimeCoverageAnalysis
{
readonly IInProcessCoverageEngine inProcessEngine;

public RealTimeCoverageAnalysis(string instrumentationResult)
{
ICoverageEngineFactory coverageFactory = new BuildInCoverageEngineFactory();
inProcessEngine = coverageFactory.CreateInProcessEngine(File.OpenRead(instrumentationResult));
}

public void PrintCoverageCurrentState()
{
Console.WriteLine();
Console.WriteLine("***Start live coverage analysis***");
Console.WriteLine("---List of instrumented assemblies---");
foreach (Assembly asm in inProcessEngine.GetInstrumentedAssemblies())
{
Console.WriteLine(asm);
}
Console.WriteLine("---Method lines coverage---");
CoverageResult? coverageResult = inProcessEngine.ReadCurrentCoverage();
if (coverageResult != null)
{
CoverageSummary summary = new CoverageSummary();
foreach (var module in coverageResult.Modules)
{
foreach (var document in module.Value)
{
foreach (var @class in document.Value)
{
foreach (var method in @class.Value)
{
var methodLineDetails = summary.CalculateMethodCoverage(method.Value.Lines);
Console.WriteLine($"Method '{method.Key}' {methodLineDetails.Percent}%");
}
}
}
}
ConsoleColor tmp = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Total modules method lines covered '{summary.CalculateMethodCoverage(coverageResult.Modules).Percent}'");
Console.ForegroundColor = tmp;
}
}
}
}
10 changes: 10 additions & 0 deletions Documentation/Examples/StandaloneUsage/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.core" Version="$(coverletCoreVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Reference Include="coverlet.core">
<HintPath>..\..\..\..\src\coverlet.core\bin\Debug\netstandard2.0\coverlet.core.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
34 changes: 34 additions & 0 deletions Documentation/Examples/StandaloneUsage/Instrumentor/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.IO;
using Coverlet.Core.Abstracts;
using Coverlet.Core.Instrumentation;

namespace Instrumentor
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0 || !File.Exists(args[0]))
{
Console.WriteLine("Invalid lib to instrument");
return;
}

ICoverageEngineFactory coverageFactory = new BuildInCoverageEngineFactory();
InstrumentationOptions options = new InstrumentationOptions();
options.Module = args[0];
options.IncludeTestAssembly = true;
ICoverageEngine coverageEngine = coverageFactory.CreateEngine(options);
using (Stream stream = coverageEngine.PrepareModules(),
fs = File.OpenWrite("instrumentationResult"))
{
stream.CopyTo(fs);
}

Console.WriteLine($"Instrumentation result saved '{Path.GetFullPath("instrumentationResult")}'");
Console.WriteLine("Instrumentor active, click any button to restore instrumented libraries");
Console.ReadKey();
}
}
}
69 changes: 69 additions & 0 deletions Documentation/Examples/StandaloneUsage/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
### Run sample

1) Go to root coverlet folder and generate packages
```
dotnet pack
...
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.core.1.0.4-gac3e48b424.nupkg'
```
2) Update `run_instrumentor.cmd` with correct package version(in this case *1.0.4-gac3e48b424*)
```
...
dotnet build /p:coverletCoreVersion=1.0.4-gac3e48b424
...
```
3) Open fresh new command line and run `run_instrumentor.cmd`
```
run_instrumentor.cmd

dotnet build /p:coverletCoreVersion=1.0.2-g1df3e82a5b
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

Restore completed in 54,96 ms for C:\git\coverlet\Documentation\Examples\StandaloneUsage\Calculator\Calculator.csproj.
Restore completed in 54,96 ms for C:\git\coverlet\Documentation\Examples\StandaloneUsage\Instrumentor\Instrumentor.csproj.
Instrumentor -> C:\git\coverlet\Documentation\Examples\StandaloneUsage\Instrumentor\bin\Debug\netcoreapp2.2\Instrumentor.dll
Calculator -> C:\git\coverlet\Documentation\Examples\StandaloneUsage\Calculator\bin\Debug\netcoreapp2.2\Calculator.dll

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:01.25

dotnet Instrumentor/bin/Debug/netcoreapp2.2/Instrumentor.dll Calculator/bin/Debug/netcoreapp2.2/Calculator.dll
[LogVerbose] Instrumented module: 'Calculator\bin\Debug\netcoreapp2.2\Calculator.dll'
Instrumentation result saved 'C:\git\coverlet\Documentation\Examples\StandaloneUsage\instrumentationResult'
Instrumentor active, click any button to restore instrumented libraries
```
This process instruments "Calculator" app dll, you need to keep this process alive until end of app usage because when
instrumentor process shutdown coverlet restores old "non instrumented" libraries.

4) Open another fresh new command line and run `run_app.cmd` and play with it
```
run_app.cmd

dotnet Calculator/bin/Debug/netcoreapp2.2/Calculator.dll instrumentationResult
Insert operand a
10
Insert operand b
20
Insert operation
+
Result: 30

***Start live coverage analysis***
---List of instrumented assemblies---
Calculator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
---Method lines coverage---
Method 'System.Double Calculator.CalculatorRuntime::Add(System.Double,System.Double)' 100%
Method 'System.Double Calculator.CalculatorRuntime::Subtrac(System.Double,System.Double)' 0%
Method 'System.Double Calculator.CalculatorRuntime::Divide(System.Double,System.Double)' 0%
Method 'System.Double Calculator.CalculatorRuntime::Multiply(System.Double,System.Double)' 0%
Method 'System.Void Calculator.Program::Main(System.String[])' 100%
Method 'System.Void Calculator.RealTimeCoverageAnalysis::PrintCoverageCurrentState()' 100%
Method 'System.Void Calculator.RealTimeCoverageAnalysis::.ctor(System.String)' 100%
Total modules method lines covered '57,14'

Exit(press E)? Any other button to another loop
```
60 changes: 60 additions & 0 deletions Documentation/Examples/StandaloneUsage/StandaloneUsage.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.161
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{280AB950-6C74-493D-B4BD-0BA355511561}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
nuget.config = nuget.config
Readme.md = Readme.md
run_app.cmd = run_app.cmd
run_instrumentor.cmd = run_instrumentor.cmd
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator", "Calculator\Calculator.csproj", "{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Instrumentor", "Instrumentor\Instrumentor.csproj", "{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Debug|x64.ActiveCfg = Debug|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Debug|x64.Build.0 = Debug|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Debug|x86.ActiveCfg = Debug|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Debug|x86.Build.0 = Debug|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Release|Any CPU.Build.0 = Release|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Release|x64.ActiveCfg = Release|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Release|x64.Build.0 = Release|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Release|x86.ActiveCfg = Release|Any CPU
{06FD3D7A-AD30-4FF1-8E39-CEA6384B33AD}.Release|x86.Build.0 = Release|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Debug|x64.ActiveCfg = Debug|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Debug|x64.Build.0 = Debug|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Debug|x86.ActiveCfg = Debug|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Debug|x86.Build.0 = Debug|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Release|Any CPU.Build.0 = Release|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Release|x64.ActiveCfg = Release|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Release|x64.Build.0 = Release|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Release|x86.ActiveCfg = Release|Any CPU
{DCD64A1A-8BE9-4475-AE56-3BA44AB2ED2A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {91E885C2-56D5-41E4-AA91-015650029ECA}
EndGlobalSection
EndGlobal
8 changes: 8 additions & 0 deletions Documentation/Examples/StandaloneUsage/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="localPackage" value="../../../bin/Debug/Packages" />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
Loading