Skip to content

Commit

Permalink
Merge pull request #95 from jakubch1/main
Browse files Browse the repository at this point in the history
Native AOT example
  • Loading branch information
jakubch1 authored Feb 1, 2024
2 parents 4f0639c + c609732 commit cbdb8cb
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 2 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/Algorithms_Scenario05.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: "Algorithms Scenario 05"

on:
push:
branches: [ "main" ]
paths: [ 'samples/Algorithms/src/**', '.github/workflows/Algorithms_Scenario05.yml' ]

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./samples/Algorithms/src/Algorithms.Console
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Publish
run: dotnet publish -r linux-x64 -c Release /p:AotMsCodeCoverageInstrumentation="true"
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Run
run: dotnet-coverage collect --output $GITHUB_WORKSPACE/report.cobertura.xml --output-format cobertura ./bin/Release/net8.0/linux-x64/publish/Algorithms.Console
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.2.0
with:
reports: '${{ github.workspace }}/report.cobertura.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
name: code-coverage-report
path: ${{ github.workspace }}/report.cobertura.xml
6 changes: 4 additions & 2 deletions samples/Algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

Solution contains two projects:
1. `Algorithms.Core` - contains core logic for solution
2. `Algorithms.Core.Tests` - contains unit tests for `Algorithms.Core`. It is [MSTest runner project](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-runner-intro?tabs=dotnetcli)
2. `Algorithms.Console` - contains Native AOT console app
3. `Algorithms.Core.Tests` - contains unit tests for `Algorithms.Core`. It is [MSTest runner project](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-runner-intro?tabs=dotnetcli)

# Scenarios

1. [***Scenario 01*** Code coverage for MSTest Runner project using dynamic instrumentation](scenarios/scenario01/README.md)
2. [***Scenario 02*** Code coverage for MSTest Runner project using static instrumentation](scenarios/scenario02/README.md)
3. [***Scenario 03*** Code coverage for MSTest Runner project using compile-time instrumentation](scenarios/scenario03/README.md)
4. [***Scenario 04*** Code coverage for MSTest Runner project using `dotnet-coverage` tool](scenarios/scenario04/README.md)
4. [***Scenario 04*** Code coverage for MSTest Runner project using `dotnet-coverage` tool](scenarios/scenario04/README.md)
5. [***Scenario 05*** Code coverage for Native AOT console app](scenarios/scenario05/README.md)
94 changes: 94 additions & 0 deletions samples/Algorithms/scenarios/scenario05/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Scenario Description

Collect code coverage using `dotnet-coverage` tool for Native AOT console application.

> **_NOTE:_** `Microsoft.CodeCoverage.MSBuild` package needs to be added as reference of console application. Remember to re-publish your application without coverage instrumentation before deploying to production.
# Collect code coverage using command line

```shell
git clone https://github.com/microsoft/codecoverage.git
cd codecoverage/samples/Algorithms/src/Algorithms.Console/
dotnet publish -r win-x64 -c Release /p:AotMsCodeCoverageInstrumentation="true"
dotnet tool install -g dotnet-coverage
dotnet-coverage collect --output report.cobertura.xml --output-format cobertura bin/Release/net8.0/win-x64/publish/Algorithms.Console.exe
```

You can also use [run.ps1](run.ps1) to collect code coverage.

# Collect code coverage inside github workflow

`reportgenerator` can be used to generate final github summary markdown.

```yml
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Publish
run: dotnet publish -r linux-x64 -c Release /p:AotMsCodeCoverageInstrumentation="true"
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Run
run: dotnet-coverage collect --output $GITHUB_WORKSPACE/report.cobertura.xml --output-format cobertura ./bin/Release/net8.0/linux-x64/publish/Algorithms.Console
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.2.0
with:
reports: '${{ github.workspace }}/report.cobertura.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
name: code-coverage-report
path: ${{ github.workspace }}/report.cobertura.xml
```
[Full source example](../../../../.github/workflows/Algorithms_Scenario05.yml)
[Run example](../../../../../../actions/workflows/Algorithms_Scenario05.yml)
# Collect code coverage inside Azure DevOps Pipelines
```yml
steps:
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
zipAfterPublish: false
arguments: '-r linux-x64 -c Release /p:AotMsCodeCoverageInstrumentation="true"'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'publish'

- task: DotNetCoreCLI@2
inputs:
command: 'custom'
custom: "tool"
arguments: 'install -g dotnet-coverage'
displayName: 'install dotnet-coverage'

- task: Bash@3
inputs:
targetType: 'inline'
script: 'dotnet-coverage collect --output-format cobertura --output $(Agent.TempDirectory)/report.cobertura.xml ./samples/Algorithms/src/Algorithms.Console/bin/Release/net8.0/linux-x64/publish/Algorithms.Console'
displayName: 'run'

- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: $(Agent.TempDirectory)/**/*.cobertura.xml
```
[Full source example](azure-pipelines.yml)
![alt text](azure-pipelines.jpg "Code Coverage tab in Azure DevOps pipelines")
# Report example
![alt text](example.report.jpg "Example report")
[Link](example.report.cobertura.xml)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions samples/Algorithms/scenarios/scenario05/azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "Algorithms Scenario 05"

pool:
vmImage: 'ubuntu-latest'

variables:
projectPath: './samples/Algorithms/src/Algorithms.Console/Algorithms.Console.csproj' # this is specific to example - in most cases not needed

steps:
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
zipAfterPublish: false
arguments: '-r linux-x64 -c Release /p:AotMsCodeCoverageInstrumentation="true"'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'publish'

- task: DotNetCoreCLI@2
inputs:
command: 'custom'
custom: "tool"
arguments: 'install -g dotnet-coverage'
displayName: 'install dotnet-coverage'

- task: Bash@3
inputs:
targetType: 'inline'
script: 'dotnet-coverage collect --output-format cobertura --output $(Agent.TempDirectory)/report.cobertura.xml ./samples/Algorithms/src/Algorithms.Console/bin/Release/net8.0/linux-x64/publish/Algorithms.Console'
displayName: 'run'

- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: $(Agent.TempDirectory)/**/*.cobertura.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<coverage line-rate="0.95" branch-rate="1" complexity="3" version="1.9" timestamp="1706778071" lines-covered="19" lines-valid="20">
<packages>
<package line-rate="1" branch-rate="1" complexity="2" name="Algorithms.Console">
<classes>
<class line-rate="1" branch-rate="1" complexity="2" name="Program" filename="/home/runner/work/codecoverage/codecoverage/samples/Algorithms/src/Algorithms.Console/Program.cs">
<methods>
<method line-rate="1" branch-rate="1" complexity="1" name="&lt;Main&gt;$" signature="(string[])">
<lines>
<line number="4" hits="1" branch="False" />
<line number="5" hits="1" branch="False" />
<line number="7" hits="1" branch="False" />
<line number="8" hits="1" branch="False" />
<line number="9" hits="1" branch="False" />
</lines>
</method>
<method line-rate="1" branch-rate="1" complexity="1" name="&lt;&lt;Main&gt;$&gt;g__Print|0_0" signature="(string, int[])">
<lines>
<line number="13" hits="1" branch="False" />
<line number="14" hits="1" branch="False" />
</lines>
</method>
</methods>
<lines>
<line number="4" hits="1" branch="False" />
<line number="5" hits="1" branch="False" />
<line number="7" hits="1" branch="False" />
<line number="8" hits="1" branch="False" />
<line number="9" hits="1" branch="False" />
<line number="13" hits="1" branch="False" />
<line number="14" hits="1" branch="False" />
</lines>
</class>
</classes>
</package>
<package line-rate="0.9230769230769231" branch-rate="1" complexity="1" name="Algorithms.Core">
<classes>
<class line-rate="0.9230769230769231" branch-rate="1" complexity="1" name="Algorithms.Core.Merger" filename="/home/runner/work/codecoverage/codecoverage/samples/Algorithms/src/Algorithms.Core/Merger.cs">
<methods>
<method line-rate="0.9230769230769231" branch-rate="1" complexity="1" name="Merge" signature="(int[], int[])">
<lines>
<line number="7" hits="1" branch="False" />
<line number="8" hits="1" branch="False" />
<line number="9" hits="1" branch="False" />
<line number="11" hits="1" branch="False" />
<line number="13" hits="1" branch="False" />
<line number="15" hits="1" branch="False" />
<line number="16" hits="1" branch="False" />
<line number="18" hits="1" branch="False" />
<line number="21" hits="1" branch="False" />
<line number="22" hits="0" branch="False" />
<line number="24" hits="1" branch="False" />
<line number="25" hits="1" branch="False" />
<line number="27" hits="1" branch="False" />
</lines>
</method>
</methods>
<lines>
<line number="7" hits="1" branch="False" />
<line number="8" hits="1" branch="False" />
<line number="9" hits="1" branch="False" />
<line number="11" hits="1" branch="False" />
<line number="13" hits="1" branch="False" />
<line number="15" hits="1" branch="False" />
<line number="16" hits="1" branch="False" />
<line number="18" hits="1" branch="False" />
<line number="21" hits="1" branch="False" />
<line number="22" hits="0" branch="False" />
<line number="24" hits="1" branch="False" />
<line number="25" hits="1" branch="False" />
<line number="27" hits="1" branch="False" />
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions samples/Algorithms/scenarios/scenario05/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cd $PSScriptRoot/../../src/Algorithms.Console
dotnet publish -r win-x64 -c Release /p:AotMsCodeCoverageInstrumentation="true"
dotnet tool install -g dotnet-coverage
dotnet-coverage collect --output report.cobertura.xml --output-format cobertura bin\Release\net8.0\win-x64\publish\Algorithms.Console.exe
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../Algorithms.Core/Algorithms.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeCoverage.MSBuild" Version="17.10.1" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions samples/Algorithms/src/Algorithms.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// See https://aka.ms/new-console-template for more information
using Algorithms.Core;

int[] a = [1, 5, 10, 15];
int[] b = [0, 3, 4, 6, 7, 20];

Print(nameof(a), a);
Print(nameof(b), b);
Print("merged:", Merger.Merge(a, b));

void Print(string name, int[] array)
{
Console.WriteLine($"{name}: [{string.Join(",", array)}]");
}

0 comments on commit cbdb8cb

Please sign in to comment.