Add deterministic build tests sample (#820)

Add deterministic build tests sample
This commit is contained in:
Marco Rossignoli
2020-04-18 19:31:03 +02:00
committed by GitHub
parent a0e22ec622
commit 2e2b578fcc
18 changed files with 489 additions and 0 deletions
@@ -0,0 +1,12 @@
using System;
namespace ClassLibrary1
{
public class Class1
{
public int Method()
{
return 42;
}
}
}
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
@@ -0,0 +1,59 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30014.187
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnitTestProject1", "XUnitTestProject1\XUnitTestProject1.csproj", "{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClassLibrary1", "ClassLibrary1\ClassLibrary1.csproj", "{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8AE3B75E-33BA-4E07-AD78-2DBCC3392262}"
ProjectSection(SolutionItems) = preProject
DeterministicBuild.targets = DeterministicBuild.targets
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
HowTo.md = HowTo.md
EndProjectSection
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
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|x64.ActiveCfg = Debug|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|x64.Build.0 = Debug|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|x86.ActiveCfg = Debug|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|x86.Build.0 = Debug|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|Any CPU.Build.0 = Release|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|x64.ActiveCfg = Release|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|x64.Build.0 = Release|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|x86.ActiveCfg = Release|Any CPU
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|x86.Build.0 = Release|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|x64.ActiveCfg = Debug|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|x64.Build.0 = Debug|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|x86.ActiveCfg = Debug|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|x86.Build.0 = Debug|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|Any CPU.Build.0 = Release|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|x64.ActiveCfg = Release|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|x64.Build.0 = Release|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|x86.ActiveCfg = Release|Any CPU
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {89E650B4-E15F-46F0-B8F4-CE372B2092AB}
EndGlobalSection
EndGlobal
@@ -0,0 +1,23 @@
<!-- This target must be imported into Directory.Build.targets -->
<!-- Workaround. Remove once we're on 3.1.300+
https://github.com/dotnet/sourcelink/issues/572 -->
<Project>
<PropertyGroup>
<TargetFrameworkMonikerAssemblyAttributesPath>$([System.IO.Path]::Combine('$(IntermediateOutputPath)','$(TargetFrameworkMoniker).AssemblyAttributes$(DefaultLanguageSourceExtension)'))</TargetFrameworkMonikerAssemblyAttributesPath>
</PropertyGroup>
<ItemGroup>
<EmbeddedFiles Include="$(GeneratedAssemblyInfoFile)"/>
</ItemGroup>
<ItemGroup>
<SourceRoot Include="$(NuGetPackageRoot)" />
</ItemGroup>
<Target Name="CoverletGetPathMap"
DependsOnTargets="InitializeSourceRootMappedPaths"
Returns="@(_LocalTopLevelSourceRoot)"
Condition="'$(DeterministicSourcePaths)' == 'true'">
<ItemGroup>
<_LocalTopLevelSourceRoot Include="@(SourceRoot)" Condition="'%(SourceRoot.NestedRoot)' == ''"/>
</ItemGroup>
</Target>
</Project>
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<RestoreSources>
https://api.nuget.org/v3/index.json;
..\..\..\..\..\bin\$(Configuration)\Packages
</RestoreSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
</ItemGroup>
</Project>
@@ -0,0 +1,4 @@
<Project>
<!-- Deterministic build workaround -->
<Import Project="$(MSBuildThisFileDirectory)DeterministicBuild.targets" />
</Project>
@@ -0,0 +1,101 @@
To run test we need to generates packages to reference in on test project.
Run from repo root
```
C:\git\coverlet
λ dotnet pack
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 73,36 ms for C:\git\coverlet\src\coverlet.core\coverlet.core.csproj.
Restore completed in 73,41 ms for C:\git\coverlet\test\coverlet.testsubject\coverlet.testsubject.csproj.
Restore completed in 73,33 ms for C:\git\coverlet\test\coverlet.tests.projectsample.excludedbyattribute\coverlet.tests.projectsample.excludedbyattribute.csproj.
Restore completed in 73,34 ms for C:\git\coverlet\src\coverlet.collector\coverlet.collector.csproj.
Restore completed in 73,35 ms for C:\git\coverlet\test\coverlet.tests.xunit.extensions\coverlet.tests.xunit.extensions.csproj.
Restore completed in 75,92 ms for C:\git\coverlet\test\coverlet.integration.tests\coverlet.integration.tests.csproj.
Restore completed in 73,41 ms for C:\git\coverlet\src\coverlet.console\coverlet.console.csproj.
Restore completed in 73,36 ms for C:\git\coverlet\test\coverlet.tests.projectsample.empty\coverlet.tests.projectsample.empty.csproj.
Restore completed in 73,47 ms for C:\git\coverlet\src\coverlet.msbuild.tasks\coverlet.msbuild.tasks.csproj.
Restore completed in 73,37 ms for C:\git\coverlet\test\coverlet.core.tests.samples.netstandard\coverlet.core.tests.samples.netstandard.csproj.
Restore completed in 76,37 ms for C:\git\coverlet\test\coverlet.collector.tests\coverlet.collector.tests.csproj.
Restore completed in 77,05 ms for C:\git\coverlet\test\coverlet.integration.template\coverlet.integration.template.csproj.
Restore completed in 77,2 ms for C:\git\coverlet\test\coverlet.core.performancetest\coverlet.core.performancetest.csproj.
Restore completed in 87,7 ms for C:\git\coverlet\test\coverlet.core.tests\coverlet.core.tests.csproj.
coverlet.core -> C:\git\coverlet\src\coverlet.core\bin\Debug\netstandard2.0\coverlet.core.dll
coverlet.collector -> C:\git\coverlet\src\coverlet.collector\bin\Debug\netcoreapp2.0\coverlet.collector.dll
coverlet.msbuild.tasks -> C:\git\coverlet\src\coverlet.msbuild.tasks\bin\Debug\netstandard2.0\coverlet.msbuild.tasks.dll
coverlet.console -> C:\git\coverlet\src\coverlet.console\bin\Debug\netcoreapp2.2\coverlet.console.dll
coverlet.console -> C:\git\coverlet\src\coverlet.console\bin\Debug\netcoreapp2.2\coverlet.console.dll
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.collector.1.3.0-preview.6.ga0e22ec622.nupkg'.
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.console.1.7.2-preview.6.ga0e22ec622.nupkg'.
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.console.1.7.2-preview.6.ga0e22ec622.snupkg'.
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.collector.1.3.0-preview.6.ga0e22ec622.snupkg'.
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.msbuild.2.9.0-preview.6.ga0e22ec622.nupkg'.
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.msbuild.2.9.0-preview.6.ga0e22ec622.snupkg'.
```
Add msbuild package version generated to `"..\Documentation\Examples\MSBuild\DeterministicBuild\XUnitTestProject1\XUnitTestProject1.csproj"`
```xml
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<!-- version comes from coverlet.msbuild.2.9.0-preview.6.ga0e22ec622.nupkg -->
<PackageReference Include="coverlet.msbuild" Version="2.9.0-preview.6.ga0e22ec622" >
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
</ItemGroup>
</Project>
```
Go to test project folder and run
```
C:\git\coverlet\Documentation\Examples\MSBuild\DeterministicBuild (detbuilddocs -> origin)
λ dotnet test /p:CollectCoverage=true /p:DeterministicSourcePaths=true
Test run for C:\git\coverlet\Documentation\Examples\MSBuild\DeterministicBuild\XUnitTestProject1\bin\Debug\netcoreapp3.1\XUnitTestProject1.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.5.0
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Test Run Successful.
Total tests: 1
Passed: 1
Total time: 1,1471 Seconds
Calculating coverage result...
Generating report 'C:\git\coverlet\Documentation\Examples\MSBuild\DeterministicBuild\XUnitTestProject1\coverage.json'
+---------------+------+--------+--------+
| Module | Line | Branch | Method |
+---------------+------+--------+--------+
| ClassLibrary1 | 100% | 100% | 100% |
+---------------+------+--------+--------+
+---------+------+--------+--------+
| | Line | Branch | Method |
+---------+------+--------+--------+
| Total | 100% | 100% | 100% |
+---------+------+--------+--------+
| Average | 100% | 100% | 100% |
+---------+------+--------+--------+
```
You should see on output folder the coverlet source root mapping file generated.
This is the confirmation that you're running coverage on deterministic build.
```
Documentation\Examples\MSBuild\DeterministicBuild\XUnitTestProject1\bin\Debug\netcoreapp3.1\CoverletSourceRootsMapping
```
@@ -0,0 +1,14 @@
using System;
using Xunit;
namespace XUnitTestProject1
{
public class UnitTest1
{
[Fact]
public void Test1()
{
new ClassLibrary1.Class1().Method();
}
}
}
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="coverlet.msbuild" Version="ADD VERSION FROM bin\Debug\Packages generated running 'dotnet pack' from repo root" >
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
</ItemGroup>
</Project>