Skip to content

Commit

Permalink
Merge pull request #68 from jakubch1/main
Browse files Browse the repository at this point in the history
Scenarios 22 and 23
  • Loading branch information
jakubch1 authored Nov 14, 2023
2 parents aea22cf + be3b15a commit 8b479f5
Show file tree
Hide file tree
Showing 24 changed files with 550 additions and 18 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/Calculator_Scenario22.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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: "Calculator Scenario 22"

on:
push:
branches: [ "main" ]
paths: [ 'samples/Calculator/tests/**', 'samples/Calculator/src/**', '.github/workflows/Calculator_Scenario22.yml', 'samples/Calculator/scenarios/scenario22/coverage.runsettings' ]

env:
STATIC_INSTRUMENTATION_DIR: "${{ github.workspace }}/samples/Calculator/src/Calculator.Console"

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./samples/Calculator/tests/Calculator.Console.Tests
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Restore dependencies (Console project)
run: dotnet restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Build
run: dotnet build --no-restore
- name: Build (Console project)
run: dotnet build --no-restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Test
run: dotnet test --settings ../../scenarios/scenario22/coverage.runsettings --no-build --verbosity normal
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Convert .coverage report to cobertura
run: dotnet-coverage merge -r $GITHUB_WORKSPACE/samples/Calculator/tests/Calculator.Console.Tests/TestResults/*.coverage -f cobertura -o $GITHUB_WORKSPACE/report.cobertura.xml
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
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: ./**/TestResults/**/*.coverage
53 changes: 53 additions & 0 deletions .github/workflows/Calculator_Scenario23.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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: "Calculator Scenario 23"

on:
push:
branches: [ "main" ]
paths: [ 'samples/Calculator/tests/**', 'samples/Calculator/src/**', '.github/workflows/Calculator_Scenario23.yml', 'samples/Calculator/scenarios/scenario23/coverage.runsettings' ]

env:
STATIC_INSTRUMENTATION_DIR: "${{ github.workspace }}/samples/Calculator/src/Calculator.Console"

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./samples/Calculator/tests/Calculator.Console.Tests
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Restore dependencies (Console project)
run: dotnet restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Build
run: dotnet build --no-restore
- name: Build (Console project)
run: dotnet build --no-restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Test
run: dotnet test --settings ../../scenarios/scenario23/coverage.runsettings --no-build --verbosity normal
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Convert .coverage report to cobertura
run: dotnet-coverage merge -r $GITHUB_WORKSPACE/samples/Calculator/tests/Calculator.Console.Tests/TestResults/*.coverage -f cobertura -o $GITHUB_WORKSPACE/report.cobertura.xml
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
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: ./**/TestResults/**/*.coverage
4 changes: 3 additions & 1 deletion samples/Calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ Solution contains seven projects:
18. [***Scenario 18*** Code coverage for whole solution](scenarios/scenario18/README.md)
19. [***Scenario 19*** Code coverage for whole solution with test projects excluded](scenarios/scenario19/README.md)
20. [***Scenario 20*** Code coverage for whole solution with logs](scenarios/scenario20/README.md)
21. [***Scenario 21*** Code coverage for whole solution with script to run tests](scenarios/scenario21/README.md)
21. [***Scenario 21*** Code coverage for whole solution with script to run tests](scenarios/scenario21/README.md)
22. [***Scenario 22*** Code coverage for child processes using static instrumentation without binaries restore](scenarios/scenario22/README.md)
23. [***Scenario 23*** Code coverage for child processes using static instrumentation with binaries restore](scenarios/scenario23/README.md)
2 changes: 1 addition & 1 deletion samples/Calculator/scenarios/scenario11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This example shows that dynamic code coverage by default is collecting code cove
git clone https://github.com/microsoft/codecoverage.git
cd codecoverage/samples/Calculator/src/Calculator.Console/
dotnet build
cd codecoverage/samples/Calculator/tests/Calculator.Console.Tests/
cd ../../tests/Calculator.Console.Tests/
dotnet test --settings ../../scenarios/scenario11/coverage.runsettings
```

Expand Down
2 changes: 1 addition & 1 deletion samples/Calculator/scenarios/scenario12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This example shows dynamic code coverage with child processes collection disable
git clone https://github.com/microsoft/codecoverage.git
cd codecoverage/samples/Calculator/src/Calculator.Console/
dotnet build
cd codecoverage/samples/Calculator/tests/Calculator.Console.Tests/
cd ../../tests/Calculator.Console.Tests/
dotnet test --settings ../../scenarios/scenario12/coverage.runsettings
```

Expand Down
138 changes: 138 additions & 0 deletions samples/Calculator/scenarios/scenario22/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Scenario Description

This example shows that code coverage by default is collecting code coverage also for all child processes. `Calculator.Console.Tests` run tests by spawning `Calculator.Console` as child process. Static instrumentation is used here, which requres specifying what files should be instrumented by `ModulePaths.IncludeDirectories` inside runsettings. Setting `EnableStaticManagedInstrumentationRestore` to `False` means our system will not restore instrumented binaries after the run. Default format is binary (`.coverage` extension) which can be opened in Visual Studio Enterprise.

# Configuration

```xml
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<EnableDynamicManagedInstrumentation>False</EnableDynamicManagedInstrumentation>
<EnableStaticManagedInstrumentation>True</EnableStaticManagedInstrumentation>
<EnableStaticManagedInstrumentationRestore>False</EnableStaticManagedInstrumentationRestore>
<ModulePaths>
<IncludeDirectories>
<Directory Recursive="true">%STATIC_INSTRUMENTATION_DIR%</Directory>
</IncludeDirectories>
</ModulePaths>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
```

# Collect code coverage using command line

```shell
git clone https://github.com/microsoft/codecoverage.git
cd codecoverage/samples/Calculator/src/Calculator.Console/
dotnet build
export STATIC_INSTRUMENTATION_DIR=`pwd`
cd ../../tests/Calculator.Console.Tests/
dotnet test --settings ../../scenarios/scenario22/coverage.runsettings
```

> **_NOTE:_** You don't have to use `--collect "Code Coverage"` when you specify runsettings with code coverage configuration.
You can also use [run.ps1](run.ps1) to collect code coverage.

# Collect code coverage inside github workflow

```yml
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Restore dependencies (Console project)
run: dotnet restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Build
run: dotnet build --no-restore
- name: Build (Console project)
run: dotnet build --no-restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Test
run: dotnet test --settings ../../scenarios/scenario22/coverage.runsettings --no-build --verbosity normal
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Convert .coverage report to cobertura
run: dotnet-coverage merge -r $GITHUB_WORKSPACE/samples/Calculator/tests/Calculator.Console.Tests/TestResults/*.coverage -f cobertura -o $GITHUB_WORKSPACE/report.cobertura.xml
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
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: ./**/TestResults/**/*.coverage
```
[Full source example](../../../../.github/workflows/Calculator_Scenario22.yml)
[Run example](../../../../../../actions/workflows/Calculator_Scenario22.yml)
# Collect code coverage inside Azure DevOps Pipelines
```yml
steps:
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet restore'

- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '$(testProjectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet restore (tests)'

- task: DotNetCoreCLI@2
inputs:
command: 'build'
arguments: '--no-restore --configuration $(buildConfiguration)'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build'

- task: DotNetCoreCLI@2
inputs:
command: 'build'
arguments: '--no-restore --configuration $(buildConfiguration)'
projects: '$(testProjectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build (tests)'

- task: DotNetCoreCLI@2
inputs:
command: 'test'
arguments: '--no-build --configuration $(buildConfiguration) --settings samples/Calculator/scenarios/scenario22/coverage.runsettings'
projects: '$(testProjectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet test'
env:
STATIC_INSTRUMENTATION_DIR: '$(Build.SourcesDirectory)/samples/Calculator/src/Calculator.Console'
```
> **_NOTE:_** Azure DevOps pipelines automatically recognize binary coverage report format. Code coverage results are automatically processed and published to Azure DevOps. No additional steps needed.
[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.coverage)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions samples/Calculator/scenarios/scenario22/azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: "Calculator Scenario 22"

pool:
vmImage: 'ubuntu-latest'

variables:
buildConfiguration: 'Debug'
testProjectPath: './samples/Calculator/tests/Calculator.Console.Tests/Calculator.Console.Tests.csproj' # this is specific to example - in most cases not needed
projectPath: './samples/Calculator/src/Calculator.Console/Calculator.Console.csproj' # this is specific to example - in most cases not needed

steps:
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet restore'

- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '$(testProjectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet restore (tests)'

- task: DotNetCoreCLI@2
inputs:
command: 'build'
arguments: '--no-restore --configuration $(buildConfiguration)'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build'

- task: DotNetCoreCLI@2
inputs:
command: 'build'
arguments: '--no-restore --configuration $(buildConfiguration)'
projects: '$(testProjectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build (tests)'

- task: DotNetCoreCLI@2
inputs:
command: 'test'
arguments: '--no-build --configuration $(buildConfiguration) --settings samples/Calculator/scenarios/scenario22/coverage.runsettings'
projects: '$(testProjectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet test'
env:
STATIC_INSTRUMENTATION_DIR: '$(Build.SourcesDirectory)/samples/Calculator/src/Calculator.Console'
21 changes: 21 additions & 0 deletions samples/Calculator/scenarios/scenario22/coverage.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<EnableDynamicManagedInstrumentation>False</EnableDynamicManagedInstrumentation>
<EnableStaticManagedInstrumentation>True</EnableStaticManagedInstrumentation>
<EnableStaticManagedInstrumentationRestore>False</EnableStaticManagedInstrumentationRestore>
<ModulePaths>
<IncludeDirectories>
<Directory Recursive="true">%STATIC_INSTRUMENTATION_DIR%</Directory>
</IncludeDirectories>
</ModulePaths>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions samples/Calculator/scenarios/scenario22/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cd $PSScriptRoot/../../src/Calculator.Console
dotnet build
$env:STATIC_INSTRUMENTATION_DIR="$PSScriptRoot/../../src/Calculator.Console"
cd $PSScriptRoot/../../tests/Calculator.Console.Tests
dotnet test --settings ../../scenarios/scenario22/coverage.runsettings
Loading

0 comments on commit 8b479f5

Please sign in to comment.