Skip to content

Commit 2e2b578

Browse files
Add deterministic build tests sample (#820)
Add deterministic build tests sample
1 parent a0e22ec commit 2e2b578

File tree

18 files changed

+489
-0
lines changed

18 files changed

+489
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace ClassLibrary1
4+
{
5+
public class Class1
6+
{
7+
public int Method()
8+
{
9+
return 42;
10+
}
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30014.187
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnitTestProject1", "XUnitTestProject1\XUnitTestProject1.csproj", "{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClassLibrary1", "ClassLibrary1\ClassLibrary1.csproj", "{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8AE3B75E-33BA-4E07-AD78-2DBCC3392262}"
11+
ProjectSection(SolutionItems) = preProject
12+
DeterministicBuild.targets = DeterministicBuild.targets
13+
Directory.Build.props = Directory.Build.props
14+
Directory.Build.targets = Directory.Build.targets
15+
HowTo.md = HowTo.md
16+
EndProjectSection
17+
EndProject
18+
Global
19+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
20+
Debug|Any CPU = Debug|Any CPU
21+
Debug|x64 = Debug|x64
22+
Debug|x86 = Debug|x86
23+
Release|Any CPU = Release|Any CPU
24+
Release|x64 = Release|x64
25+
Release|x86 = Release|x86
26+
EndGlobalSection
27+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
28+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|x64.ActiveCfg = Debug|Any CPU
31+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|x64.Build.0 = Debug|Any CPU
32+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|x86.ActiveCfg = Debug|Any CPU
33+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Debug|x86.Build.0 = Debug|Any CPU
34+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|x64.ActiveCfg = Release|Any CPU
37+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|x64.Build.0 = Release|Any CPU
38+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|x86.ActiveCfg = Release|Any CPU
39+
{BFF46C49-E8A2-4BA9-9A8D-C56C933E3973}.Release|x86.Build.0 = Release|Any CPU
40+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|x64.ActiveCfg = Debug|Any CPU
43+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|x64.Build.0 = Debug|Any CPU
44+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|x86.ActiveCfg = Debug|Any CPU
45+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Debug|x86.Build.0 = Debug|Any CPU
46+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|Any CPU.Build.0 = Release|Any CPU
48+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|x64.ActiveCfg = Release|Any CPU
49+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|x64.Build.0 = Release|Any CPU
50+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|x86.ActiveCfg = Release|Any CPU
51+
{47C1D631-2ACB-47CF-BE3A-F8FA42715D7B}.Release|x86.Build.0 = Release|Any CPU
52+
EndGlobalSection
53+
GlobalSection(SolutionProperties) = preSolution
54+
HideSolutionNode = FALSE
55+
EndGlobalSection
56+
GlobalSection(ExtensibilityGlobals) = postSolution
57+
SolutionGuid = {89E650B4-E15F-46F0-B8F4-CE372B2092AB}
58+
EndGlobalSection
59+
EndGlobal
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!-- This target must be imported into Directory.Build.targets -->
2+
<!-- Workaround. Remove once we're on 3.1.300+
3+
https://github.com/dotnet/sourcelink/issues/572 -->
4+
<Project>
5+
<PropertyGroup>
6+
<TargetFrameworkMonikerAssemblyAttributesPath>$([System.IO.Path]::Combine('$(IntermediateOutputPath)','$(TargetFrameworkMoniker).AssemblyAttributes$(DefaultLanguageSourceExtension)'))</TargetFrameworkMonikerAssemblyAttributesPath>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<EmbeddedFiles Include="$(GeneratedAssemblyInfoFile)"/>
10+
</ItemGroup>
11+
<ItemGroup>
12+
<SourceRoot Include="$(NuGetPackageRoot)" />
13+
</ItemGroup>
14+
15+
<Target Name="CoverletGetPathMap"
16+
DependsOnTargets="InitializeSourceRootMappedPaths"
17+
Returns="@(_LocalTopLevelSourceRoot)"
18+
Condition="'$(DeterministicSourcePaths)' == 'true'">
19+
<ItemGroup>
20+
<_LocalTopLevelSourceRoot Include="@(SourceRoot)" Condition="'%(SourceRoot.NestedRoot)' == ''"/>
21+
</ItemGroup>
22+
</Target>
23+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
<PropertyGroup>
4+
<RestoreSources>
5+
https://api.nuget.org/v3/index.json;
6+
..\..\..\..\..\bin\$(Configuration)\Packages
7+
</RestoreSources>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Project>
2+
<!-- Deterministic build workaround -->
3+
<Import Project="$(MSBuildThisFileDirectory)DeterministicBuild.targets" />
4+
</Project>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
To run test we need to generates packages to reference in on test project.
2+
Run from repo root
3+
```
4+
C:\git\coverlet
5+
λ dotnet pack
6+
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
7+
Copyright (C) Microsoft Corporation. All rights reserved.
8+
9+
Restore completed in 73,36 ms for C:\git\coverlet\src\coverlet.core\coverlet.core.csproj.
10+
Restore completed in 73,41 ms for C:\git\coverlet\test\coverlet.testsubject\coverlet.testsubject.csproj.
11+
Restore completed in 73,33 ms for C:\git\coverlet\test\coverlet.tests.projectsample.excludedbyattribute\coverlet.tests.projectsample.excludedbyattribute.csproj.
12+
Restore completed in 73,34 ms for C:\git\coverlet\src\coverlet.collector\coverlet.collector.csproj.
13+
Restore completed in 73,35 ms for C:\git\coverlet\test\coverlet.tests.xunit.extensions\coverlet.tests.xunit.extensions.csproj.
14+
Restore completed in 75,92 ms for C:\git\coverlet\test\coverlet.integration.tests\coverlet.integration.tests.csproj.
15+
Restore completed in 73,41 ms for C:\git\coverlet\src\coverlet.console\coverlet.console.csproj.
16+
Restore completed in 73,36 ms for C:\git\coverlet\test\coverlet.tests.projectsample.empty\coverlet.tests.projectsample.empty.csproj.
17+
Restore completed in 73,47 ms for C:\git\coverlet\src\coverlet.msbuild.tasks\coverlet.msbuild.tasks.csproj.
18+
Restore completed in 73,37 ms for C:\git\coverlet\test\coverlet.core.tests.samples.netstandard\coverlet.core.tests.samples.netstandard.csproj.
19+
Restore completed in 76,37 ms for C:\git\coverlet\test\coverlet.collector.tests\coverlet.collector.tests.csproj.
20+
Restore completed in 77,05 ms for C:\git\coverlet\test\coverlet.integration.template\coverlet.integration.template.csproj.
21+
Restore completed in 77,2 ms for C:\git\coverlet\test\coverlet.core.performancetest\coverlet.core.performancetest.csproj.
22+
Restore completed in 87,7 ms for C:\git\coverlet\test\coverlet.core.tests\coverlet.core.tests.csproj.
23+
coverlet.core -> C:\git\coverlet\src\coverlet.core\bin\Debug\netstandard2.0\coverlet.core.dll
24+
coverlet.collector -> C:\git\coverlet\src\coverlet.collector\bin\Debug\netcoreapp2.0\coverlet.collector.dll
25+
coverlet.msbuild.tasks -> C:\git\coverlet\src\coverlet.msbuild.tasks\bin\Debug\netstandard2.0\coverlet.msbuild.tasks.dll
26+
coverlet.console -> C:\git\coverlet\src\coverlet.console\bin\Debug\netcoreapp2.2\coverlet.console.dll
27+
coverlet.console -> C:\git\coverlet\src\coverlet.console\bin\Debug\netcoreapp2.2\coverlet.console.dll
28+
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.collector.1.3.0-preview.6.ga0e22ec622.nupkg'.
29+
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.console.1.7.2-preview.6.ga0e22ec622.nupkg'.
30+
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.console.1.7.2-preview.6.ga0e22ec622.snupkg'.
31+
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.collector.1.3.0-preview.6.ga0e22ec622.snupkg'.
32+
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.msbuild.2.9.0-preview.6.ga0e22ec622.nupkg'.
33+
Successfully created package 'C:\git\coverlet\bin\Debug\Packages\coverlet.msbuild.2.9.0-preview.6.ga0e22ec622.snupkg'.
34+
```
35+
Add msbuild package version generated to `"..\Documentation\Examples\MSBuild\DeterministicBuild\XUnitTestProject1\XUnitTestProject1.csproj"`
36+
```xml
37+
<Project Sdk="Microsoft.NET.Sdk">
38+
39+
<PropertyGroup>
40+
<TargetFramework>netcoreapp3.1</TargetFramework>
41+
<IsPackable>false</IsPackable>
42+
</PropertyGroup>
43+
44+
<ItemGroup>
45+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
46+
<PackageReference Include="xunit" Version="2.4.0" />
47+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
48+
<PackageReference Include="coverlet.collector" Version="1.2.0" />
49+
<!-- version comes from coverlet.msbuild.2.9.0-preview.6.ga0e22ec622.nupkg -->
50+
<PackageReference Include="coverlet.msbuild" Version="2.9.0-preview.6.ga0e22ec622" >
51+
<PrivateAssets>all</PrivateAssets>
52+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
53+
</PackageReference>
54+
</ItemGroup>
55+
56+
<ItemGroup>
57+
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
58+
</ItemGroup>
59+
60+
</Project>
61+
62+
```
63+
Go to test project folder and run
64+
```
65+
C:\git\coverlet\Documentation\Examples\MSBuild\DeterministicBuild (detbuilddocs -> origin)
66+
λ dotnet test /p:CollectCoverage=true /p:DeterministicSourcePaths=true
67+
Test run for C:\git\coverlet\Documentation\Examples\MSBuild\DeterministicBuild\XUnitTestProject1\bin\Debug\netcoreapp3.1\XUnitTestProject1.dll(.NETCoreApp,Version=v3.1)
68+
Microsoft (R) Test Execution Command Line Tool Version 16.5.0
69+
Copyright (c) Microsoft Corporation. All rights reserved.
70+
71+
Starting test execution, please wait...
72+
73+
A total of 1 test files matched the specified pattern.
74+
75+
Test Run Successful.
76+
Total tests: 1
77+
Passed: 1
78+
Total time: 1,1471 Seconds
79+
80+
Calculating coverage result...
81+
Generating report 'C:\git\coverlet\Documentation\Examples\MSBuild\DeterministicBuild\XUnitTestProject1\coverage.json'
82+
83+
+---------------+------+--------+--------+
84+
| Module | Line | Branch | Method |
85+
+---------------+------+--------+--------+
86+
| ClassLibrary1 | 100% | 100% | 100% |
87+
+---------------+------+--------+--------+
88+
89+
+---------+------+--------+--------+
90+
| | Line | Branch | Method |
91+
+---------+------+--------+--------+
92+
| Total | 100% | 100% | 100% |
93+
+---------+------+--------+--------+
94+
| Average | 100% | 100% | 100% |
95+
+---------+------+--------+--------+
96+
```
97+
You should see on output folder the coverlet source root mapping file generated.
98+
This is the confirmation that you're running coverage on deterministic build.
99+
```
100+
Documentation\Examples\MSBuild\DeterministicBuild\XUnitTestProject1\bin\Debug\netcoreapp3.1\CoverletSourceRootsMapping
101+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace XUnitTestProject1
5+
{
6+
public class UnitTest1
7+
{
8+
[Fact]
9+
public void Test1()
10+
{
11+
new ClassLibrary1.Class1().Method();
12+
}
13+
}
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
10+
<PackageReference Include="xunit" Version="2.4.0" />
11+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
12+
<PackageReference Include="coverlet.collector" Version="1.2.0" />
13+
<PackageReference Include="coverlet.msbuild" Version="ADD VERSION FROM bin\Debug\Packages generated running 'dotnet pack' from repo root" >
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace ClassLibrary1
4+
{
5+
public class Class1
6+
{
7+
public int Method()
8+
{
9+
return 42;
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)