Skip to content

Commit

Permalink
.NET coverage (#20)
Browse files Browse the repository at this point in the history
* Provide example for .NET coverage

* Remove coverlet from refs
  • Loading branch information
hybloid committed Aug 28, 2024
1 parent 974e777 commit 9175799
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/net.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: .NET - coverlet test coverage

on:
workflow_dispatch:
pull_request:
push:
branches:
- main
- 'releases/*'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0

- name: Set up .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0'

- name: Add coverlet dependencies
working-directory: NET/coverlet/UnitTestProject
run: |
dotnet add package coverlet.msbuild; dotnet add package coverlet.collector
- name: Build solution
working-directory: NET/coverlet
run: dotnet build

- name: Run tests with code coverage
run: dotnet test /p:CollectCoverage=true /p:CoverletOutput=../.qodana/code-coverage/ /p:CoverletOutputFormat=lcov
working-directory: NET/coverlet

- name: Archive coverage data
uses: actions/upload-artifact@v2
with:
name: net-coverage-data
path: NET/coverlet/.qodana/code-coverage

- name: Qodana Scan
uses: JetBrains/qodana-action@main
env:
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN_NET }}
with:
args: "-i,NET/coverlet,--linter,jetbrains/qodana-dotnet:latest"
pr-mode: false
44 changes: 44 additions & 0 deletions NET/coverlet/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace QodanaCoverageTest;

public class Class1 : Interface1
{
public Class1()
{
var y = 5;

Check warning on line 7 in NET/coverlet/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'y' is assigned but its value is never used

Check warning on line 7 in NET/coverlet/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'y' is assigned but its value is never used
}

public void UsedMethod()
{
LocalMethod();
void LocalMethod()
{
var x = 5;

Check warning on line 15 in NET/coverlet/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'x' is assigned but its value is never used

Check warning on line 15 in NET/coverlet/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'x' is assigned but its value is never used
}
void UnusedLocalMethod() { }

Check warning on line 17 in NET/coverlet/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The local function 'UnusedLocalMethod' is declared but never used

Check warning on line 17 in NET/coverlet/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The local function 'UnusedLocalMethod' is declared but never used
}

public void UsedMethodWithIf(int i)
{
if (i == 1)
{
Console.Write(i);
}
else
{
Console.Write(i);
Console.Write(i);
}
}

public int UsedLambdaMethod() => 4;

public int UnusedLambdaMethod() => 5;

private void UnusedMethod()
{
void UnusedLocalMethod()

Check warning on line 39 in NET/coverlet/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The local function 'UnusedLocalMethod' is declared but never used

Check warning on line 39 in NET/coverlet/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The local function 'UnusedLocalMethod' is declared but never used
{

}
}
}
28 changes: 28 additions & 0 deletions NET/coverlet/Class2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace QodanaCoverageTest;

public class Class2
{
public Class2()
{

}

public void UnusedMethod()
{

}

public int MyProperty => 42;

public int MyProperty1
{
get => MyProperty;
set => MyProperty2 = value;
}

public int MyProperty2
{
get;
set;
}
}
7 changes: 7 additions & 0 deletions NET/coverlet/Enum1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace QodanaCoverageTest;

public enum Enum1
{
VAL1,
VAL2,
}
14 changes: 14 additions & 0 deletions NET/coverlet/Interface1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace QodanaCoverageTest;

public interface Interface1
{
void Foo()
{

}

void Bar()
{

}
}
22 changes: 22 additions & 0 deletions NET/coverlet/NotInReportClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace QodanaCoverageTest;

public class NotInReportClass : Interface1
{
public NotInReportClass()
{
var y = 42;

Check warning on line 7 in NET/coverlet/NotInReportClass.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'y' is assigned but its value is never used

Check warning on line 7 in NET/coverlet/NotInReportClass.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'y' is assigned but its value is never used
}

public void UsedMethodWithIf(int i)
{
if (i == 1)
{
Console.Write(i);
}
else
{
Console.Write(i);
Console.Write(i);
}
}
}
21 changes: 21 additions & 0 deletions NET/coverlet/QodanaCoverageTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="UnitTestProject\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="UnitTestProject\**" />
</ItemGroup>

<ItemGroup>
<None Remove="UnitTestProject\**" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions NET/coverlet/QodanaCoverageTest.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QodanaCoverageTest", "QodanaCoverageTest.csproj", "{2849E952-A756-4191-AC98-AF80D2848500}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestProject", "UnitTestProject\UnitTestProject.csproj", "{4ED497EE-E195-4408-B5B9-861FED661884}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2849E952-A756-4191-AC98-AF80D2848500}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2849E952-A756-4191-AC98-AF80D2848500}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2849E952-A756-4191-AC98-AF80D2848500}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2849E952-A756-4191-AC98-AF80D2848500}.Release|Any CPU.Build.0 = Release|Any CPU
{4ED497EE-E195-4408-B5B9-861FED661884}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4ED497EE-E195-4408-B5B9-861FED661884}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4ED497EE-E195-4408-B5B9-861FED661884}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4ED497EE-E195-4408-B5B9-861FED661884}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions NET/coverlet/UnitTestProject/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using QodanaCoverageTest;

namespace UnitTestProject;

public class Tests
{
[SetUp]
public void Setup()
{
}

[Test]
public void Test1()
{
var cls = new Class1();
cls.UsedMethod();
cls.UsedMethodWithIf(1);
cls.UsedLambdaMethod();
Assert.Pass();
}
}
22 changes: 22 additions & 0 deletions NET/coverlet/UnitTestProject/UnitTestProject.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.3.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.5.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\QodanaCoverageTest.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions NET/coverlet/UnitTestProject/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
2 changes: 2 additions & 0 deletions NET/coverlet/qodana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
profile:
name: qodana.single:NetCoverageInspection

0 comments on commit 9175799

Please sign in to comment.