Skip to content

Commit

Permalink
Leveraging Code Coverage (#79)
Browse files Browse the repository at this point in the history
So much yml pain
  • Loading branch information
jaredpar authored Nov 28, 2023
1 parent 549a6de commit 145db97
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 20 deletions.
47 changes: 40 additions & 7 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,32 @@ on:

jobs:
build:
env:
TEST_ARTIFACTS_DIR: ${{ github.workspace }}/artifacts/test
strategy:
matrix:
os: [windows-latest, ubuntu-latest]
include:
- os: windows-latest
artifact: windows.complog
slash: \
- os: ubuntu-latest
artifact: ubuntu.complog
slash: /

env:
TEST_ARTIFACTS_PATH: ${{ github.workspace }}${{ matrix.slash }}artifacts${{ matrix.slash }}test
TEST_RESULTS_PATH: ${{ github.workspace }}${{ matrix.slash }}artifacts${{ matrix.slash }}test-results
TEST_COVERAGE_PATH: ${{ github.workspace }}${{ matrix.slash }}artifacts${{ matrix.slash }}coverage

name: Build and Test ${{ matrix.os }}
runs-on: ${{ matrix.os }}

steps:

# Setup the output directories for usage
- name: Create directories
shell: pwsh
run: New-Item -Type Directory -Path @("${{ env.TEST_ARTIFACTS_PATH }}", "${{ env.TEST_RESULTS_PATH }}", "${{ env.TEST_COVERAGE_PATH }}")

- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
Expand All @@ -41,35 +52,57 @@ jobs:
run: dotnet restore
- name: Build
run: dotnet build --no-restore -bl

- name: Test Linux
run: dotnet test --no-build --framework net7.0 --logger "console;verbosity=detailed" --logger "trx;LogFileName=${{ github.workspace }}/TestResults-Linux.trx"
run: >
dotnet test --no-build --framework net7.0
--logger "console;verbosity=detailed"
--logger "trx;LogFileName=${{ env.TEST_RESULTS_PATH }}/TestResults-Linux.trx"
-p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=${{ env.TEST_COVERAGE_PATH }}/coverage.linux.xml
if: matrix.os == 'ubuntu-latest'

- name: Test Windows .NET Core
run: dotnet test --no-build --framework net7.0 --logger "console;verbosity=detailed" --logger "trx;LogFileName=${{ github.workspace}}/TestResults-Windows-Core.trx"
run: >
dotnet test --no-build --framework net7.0
--logger "console;verbosity=detailed"
--logger "trx;LogFileName=${{ env.TEST_RESULTS_PATH}}/TestResults-Windows-Core.trx"
-p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=${{ env.TEST_COVERAGE_PATH }}/coverage.windows.core.xml
if: matrix.os == 'windows-latest'

- name: Test Windows .NET Framework
run: dotnet test --no-build --framework net472 --logger "console;verbosity=detailed" --logger "trx;LogFileName=${{ github.workspace}}/TestResults-Windows-Framework.trx"
run: >
dotnet test --no-build --framework net7.0
--logger "console;verbosity=detailed"
--logger "trx;LogFileName=${{ env.TEST_RESULTS_PATH}}/TestResults-Windows-Framework.trx"
-p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=${{ env.TEST_COVERAGE_PATH }}/coverage.windows.framework.xml
if: matrix.os == 'windows-latest'

- name: Create Compiler Log
run: dotnet run --project src/Basic.CompilerLog/Basic.CompilerLog.csproj create msbuild.binlog

- name: Publish Compiler Log
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.artifact }}
path: msbuild.complog

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ${{ env.TEST_COVERAGE_PATH }}

- name: Publish Test Results
uses: actions/upload-artifact@v3
if: always()
with:
name: test-results
path: ${{ github.workspace }}/*.trx
path: ${{ env.TEST_RESULTS_PATH }}/*.trx

- name: Publish Test Artifacts
uses: actions/upload-artifact@v3
if: always()
with:
name: Test Artifacts ${{ matrix.os }}
path: ${{ github.workspace }}/artifacts/test
path: $ {{ env.TEST_ARTIFACTS_PATH }}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<Compile Include="..\Shared\DotnetUtil.cs" Link="DotnetUtil.cs" />
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
<PackageReference Include="Basic.Reference.Assemblies.Net60" Version="1.2.4" />
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<ProjectReference Include="..\Basic.CompilerLog.Util\Basic.CompilerLog.Util.csproj" />
<ProjectReference Include="..\Basic.CompilerLog\Basic.CompilerLog.csproj" Condition="'$(TargetFramework)' == 'net7.0'" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
Expand Down
21 changes: 21 additions & 0 deletions src/Basic.CompilerLog.UnitTests/BasicAnalyzerHostTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Basic.CompilerLog.Util;
using Basic.CompilerLog.Util.Impl;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
Expand All @@ -26,4 +29,22 @@ public void Supported()
// To make sure this test is updated every time a new value is added
Assert.Equal(4, Enum.GetValues(typeof(BasicAnalyzerKind)).Length);
}

[Fact]
public void Dispose()
{
var host = new BasicAnalyzerHostNone(readGeneratedFiles: true, ImmutableArray<(SourceText, string)>.Empty, BasicAnalyzerHostOptions.None);
host.Dispose();
Assert.Throws<ObjectDisposedException>(() => { _ = host.AnalyzerReferences; });
}

[Fact]
public void Props()
{
var host = new BasicAnalyzerHostNone(readGeneratedFiles: true, ImmutableArray<(SourceText, string)>.Empty, BasicAnalyzerHostOptions.None);
host.Dispose();
Assert.Equal(BasicAnalyzerKind.None, host.Kind);
Assert.Same(BasicAnalyzerHostOptions.None, host.Options);
}

}
27 changes: 26 additions & 1 deletion src/Basic.CompilerLog.UnitTests/CompilerLogReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
#if NETCOREAPP
using System.Runtime.Loader;
#endif
using System.Text;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -170,6 +173,29 @@ public void AnalyzerLoadDispose(BasicAnalyzerKind kind)
Assert.True(data.BasicAnalyzerHost.IsDisposed);
}

#if NETCOREAPP

/// <summary>
/// Ensure that diagnostics are raised when the analyzer can't properly load all of the types
/// </summary>
[Fact]
public void AnalyzerDiagnostics()
{
var options = new BasicAnalyzerHostOptions(BasicAnalyzerKind.InMemory, cacheable: true);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, options);
var data = reader.ReadRawCompilationData(0).Item2;
var analyzers = data.Analyzers
.Where(x => x.FileName != "Microsoft.CodeAnalysis.NetAnalyzers.dll")
.ToList();
var host = new BasicAnalyzerHostInMemory(reader, analyzers, options);
foreach (var analyzer in host.AnalyzerReferences)
{
analyzer.GetAnalyzersForAllLanguages();
}
Assert.NotEmpty(host.GetDiagnostics());
}
#endif

[Fact]
public void ProjectSingleTarget()
{
Expand Down Expand Up @@ -328,5 +354,4 @@ public void MetadataCompat(string resourceName)
}
}
}

}
33 changes: 21 additions & 12 deletions src/Basic.CompilerLog.UnitTests/UsingAllCompilerLogTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using System.Configuration;
using Basic.CompilerLog.Util;
using Microsoft.CodeAnalysis;
using Xunit;
Expand All @@ -20,22 +21,26 @@ public UsingAllCompilerLogTests(ITestOutputHelper testOutputHelper, CompilerLogF
[Fact]
public async Task EmitToDisk()
{
var any = false;
var list = new List<Task>();
await foreach (var complogPath in Fixture.GetAllCompilerLogs(TestOutputHelper))
{
any = true;
TestOutputHelper.WriteLine(complogPath);
using var reader = CompilerLogReader.Create(complogPath);
foreach (var data in reader.ReadAllCompilationData())
var task = Task.Run(() =>
{
using var testDir = new TempDir();
TestOutputHelper.WriteLine($"\t{data.CompilerCall.ProjectFileName} ({data.CompilerCall.TargetFramework})");
var emitResult = data.EmitToDisk(testDir.DirectoryPath);
Assert.True(emitResult.Success);
}
using var reader = CompilerLogReader.Create(complogPath);
foreach (var data in reader.ReadAllCompilationData())
{
using var testDir = new TempDir();
TestOutputHelper.WriteLine($"{Path.GetFileName(complogPath)}: {data.CompilerCall.ProjectFileName} ({data.CompilerCall.TargetFramework})");
var emitResult = data.EmitToDisk(testDir.DirectoryPath);
Assert.True(emitResult.Success);
}
});

list.Add(task);
}

Assert.True(any);
Assert.NotEmpty(list);
await Task.WhenAll(list);
}

[Fact]
Expand All @@ -59,10 +64,14 @@ public async Task EmitToMemory()
[InlineData(false)]
public async Task ExportAndBuild(bool includeAnalyzers)
{
var list = new List<Task>();
await foreach (var complogPath in Fixture.GetAllCompilerLogs(TestOutputHelper))
{
ExportUtilTests.TestExport(TestOutputHelper, complogPath, expectedCount: null, includeAnalyzers, runBuild: true);
var task = Task.Run(() => ExportUtilTests.TestExport(TestOutputHelper, complogPath, expectedCount: null, includeAnalyzers, runBuild: true));
list.Add(task);
}

await Task.WhenAll(list);
}

[Theory]
Expand Down

0 comments on commit 145db97

Please sign in to comment.