Files
Amaury Levé 03ff455578 Add editorconfig with tuned configuration (#1300)
Add editorconfig with tuned configuration
2022-02-14 09:14:38 +01:00

182 lines
12 KiB
C#

// Copyright (c) Toni Solarin-Sodara
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Coverlet.Core;
using Newtonsoft.Json;
using Xunit;
namespace Coverlet.Integration.Tests
{
public class DeterministicBuild : BaseTest, IDisposable
{
private readonly string _testProjectPath = Path.GetFullPath("../../../../coverlet.integration.determisticbuild");
private string? _testProjectTfm;
private const string PropsFileName = "DeterministicTest.props";
private readonly string _buildConfiguration;
public DeterministicBuild()
{
_buildConfiguration = GetAssemblyBuildConfiguration().ToString();
}
private void CreateDeterministicTestPropsFile()
{
var deterministicTestProps = new XDocument();
deterministicTestProps.Add(
new XElement("Project",
new XElement("PropertyGroup",
new XElement("coverletMsbuilVersion", GetPackageVersion("*msbuild*.nupkg")),
new XElement("coverletCollectorsVersion", GetPackageVersion("*collector*.nupkg")))));
_testProjectTfm = XElement.Load(Path.Combine(_testProjectPath, "coverlet.integration.determisticbuild.csproj"))!.
Descendants("PropertyGroup")!.Single().Element("TargetFramework")!.Value;
deterministicTestProps.Save(Path.Combine(_testProjectPath, PropsFileName));
}
private protected void AssertCoverage(string standardOutput = "", bool checkDeterministicReport = true)
{
if (_buildConfiguration == "Debug")
{
bool coverageChecked = false;
string reportFilePath = "";
foreach (string coverageFile in Directory.GetFiles(_testProjectPath, "coverage.json", SearchOption.AllDirectories))
{
JsonConvert.DeserializeObject<Modules>(File.ReadAllText(coverageFile))
.Document("DeepThought.cs")
.Class("Coverlet.Integration.DeterministicBuild.DeepThought")
.Method("System.Int32 Coverlet.Integration.DeterministicBuild.DeepThought::AnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything()")
.AssertLinesCovered((6, 1), (7, 1), (8, 1));
coverageChecked = true;
reportFilePath = coverageFile;
}
Assert.True(coverageChecked, $"Coverage check fail\n{standardOutput}");
File.Delete(reportFilePath);
Assert.False(File.Exists(reportFilePath));
if (checkDeterministicReport)
{
// Verify deterministic report
foreach (string coverageFile in Directory.GetFiles(_testProjectPath, "coverage.cobertura.xml", SearchOption.AllDirectories))
{
Assert.Contains("/_/test/coverlet.integration.determisticbuild/DeepThought.cs", File.ReadAllText(coverageFile));
File.Delete(coverageFile);
}
}
}
}
[Fact]
public void Msbuild()
{
CreateDeterministicTestPropsFile();
DotnetCli($"build -c {_buildConfiguration} /p:DeterministicSourcePaths=true", out string standardOutput, out string _, _testProjectPath);
Assert.Contains("Build succeeded.", standardOutput);
string sourceRootMappingFilePath = Path.Combine(_testProjectPath, "bin", _buildConfiguration, _testProjectTfm!, "CoverletSourceRootsMapping");
Assert.True(File.Exists(sourceRootMappingFilePath), sourceRootMappingFilePath);
Assert.True(!string.IsNullOrEmpty(File.ReadAllText(sourceRootMappingFilePath)), "Empty CoverletSourceRootsMapping file");
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
DotnetCli($"test -c {_buildConfiguration} --no-build /p:CollectCoverage=true /p:DeterministicReport=true /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true", out standardOutput, out _, _testProjectPath);
Assert.Contains("Passed!", standardOutput);
Assert.Contains("| coverletsample.integration.determisticbuild | 100% | 100% | 100% |", standardOutput);
Assert.True(File.Exists(Path.Combine(_testProjectPath, "coverage.json")));
AssertCoverage(standardOutput);
// Process exits hang on clean seem that process doesn't close, maybe some mbuild node reuse? btw manually tested
// DotnetCli("clean", out standardOutput, out standardError, _fixture.TestProjectPath);
// Assert.False(File.Exists(sourceRootMappingFilePath));
RunCommand("git", "clean -fdx", out _, out _, _testProjectPath);
}
[Fact]
public void Msbuild_SourceLink()
{
CreateDeterministicTestPropsFile();
DotnetCli($"build -c {_buildConfiguration} /p:DeterministicSourcePaths=true", out string standardOutput, out string _, _testProjectPath);
Assert.Contains("Build succeeded.", standardOutput);
string sourceRootMappingFilePath = Path.Combine(_testProjectPath, "bin", _buildConfiguration, _testProjectTfm!, "CoverletSourceRootsMapping");
Assert.True(File.Exists(sourceRootMappingFilePath), sourceRootMappingFilePath);
Assert.True(!string.IsNullOrEmpty(File.ReadAllText(sourceRootMappingFilePath)), "Empty CoverletSourceRootsMapping file");
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
DotnetCli($"test -c {_buildConfiguration} --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:UseSourceLink=true /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true", out standardOutput, out _, _testProjectPath);
Assert.Contains("Passed!", standardOutput);
Assert.Contains("| coverletsample.integration.determisticbuild | 100% | 100% | 100% |", standardOutput);
Assert.True(File.Exists(Path.Combine(_testProjectPath, "coverage.json")));
Assert.Contains("raw.githubusercontent.com", File.ReadAllText(Path.Combine(_testProjectPath, "coverage.json")));
AssertCoverage(standardOutput, checkDeterministicReport: false);
// Process exits hang on clean seem that process doesn't close, maybe some mbuild node reuse? btw manually tested
// DotnetCli("clean", out standardOutput, out standardError, _fixture.TestProjectPath);
// Assert.False(File.Exists(sourceRootMappingFilePath));
RunCommand("git", "clean -fdx", out _, out _, _testProjectPath);
}
[Fact]
public void Collectors()
{
CreateDeterministicTestPropsFile();
DotnetCli($"build -c {_buildConfiguration} /p:DeterministicSourcePaths=true", out string standardOutput, out string _, _testProjectPath);
Assert.Contains("Build succeeded.", standardOutput);
string sourceRootMappingFilePath = Path.Combine(_testProjectPath, "bin", GetAssemblyBuildConfiguration().ToString(), _testProjectTfm!, "CoverletSourceRootsMapping");
Assert.True(File.Exists(sourceRootMappingFilePath), sourceRootMappingFilePath);
Assert.NotEmpty(File.ReadAllText(sourceRootMappingFilePath));
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, "[coverletsample.integration.determisticbuild]*DeepThought", deterministicReport: true);
Assert.True(DotnetCli($"test -c {_buildConfiguration} --no-build \"{_testProjectPath}\" --collect:\"XPlat Code Coverage\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(_testProjectPath, "log.txt")}", out standardOutput, out _), standardOutput);
Assert.Contains("Passed!", standardOutput);
AssertCoverage(standardOutput);
// Check out/in process collectors injection
string dataCollectorLogContent = File.ReadAllText(Directory.GetFiles(_testProjectPath, "log.datacollector.*.txt").Single());
Assert.Contains("[coverlet]Initializing CoverletCoverageDataCollector with configuration:", dataCollectorLogContent);
Assert.Contains("[coverlet]Initialize CoverletInProcDataCollector", File.ReadAllText(Directory.GetFiles(_testProjectPath, "log.host.*.txt").Single()));
Assert.Contains("[coverlet]Mapping resolved", dataCollectorLogContent);
// Process exits hang on clean seem that process doesn't close, maybe some mbuild node reuse? btw manually tested
// DotnetCli("clean", out standardOutput, out standardError, _fixture.TestProjectPath);
// Assert.False(File.Exists(sourceRootMappingFilePath));
RunCommand("git", "clean -fdx", out _, out _, _testProjectPath);
}
[Fact]
public void Collectors_SourceLink()
{
CreateDeterministicTestPropsFile();
DotnetCli($"build -c {_buildConfiguration} /p:DeterministicSourcePaths=true", out string standardOutput, out string _, _testProjectPath);
Assert.Contains("Build succeeded.", standardOutput);
string sourceRootMappingFilePath = Path.Combine(_testProjectPath, "bin", GetAssemblyBuildConfiguration().ToString(), _testProjectTfm!, "CoverletSourceRootsMapping");
Assert.True(File.Exists(sourceRootMappingFilePath), sourceRootMappingFilePath);
Assert.NotEmpty(File.ReadAllText(sourceRootMappingFilePath));
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, "[coverletsample.integration.determisticbuild]*DeepThought", sourceLink: true);
Assert.True(DotnetCli($"test -c {_buildConfiguration} --no-build \"{_testProjectPath}\" --collect:\"XPlat Code Coverage\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(_testProjectPath, "log.txt")}", out standardOutput, out _), standardOutput);
Assert.Contains("Passed!", standardOutput);
AssertCoverage(standardOutput, checkDeterministicReport: false);
Assert.Contains("raw.githubusercontent.com", File.ReadAllText(Directory.GetFiles(_testProjectPath, "coverage.cobertura.xml", SearchOption.AllDirectories).Single()));
// Check out/in process collectors injection
string dataCollectorLogContent = File.ReadAllText(Directory.GetFiles(_testProjectPath, "log.datacollector.*.txt").Single());
Assert.Contains("[coverlet]Initializing CoverletCoverageDataCollector with configuration:", dataCollectorLogContent);
Assert.Contains("[coverlet]Initialize CoverletInProcDataCollector", File.ReadAllText(Directory.GetFiles(_testProjectPath, "log.host.*.txt").Single()));
Assert.Contains("[coverlet]Mapping resolved", dataCollectorLogContent);
// Process exits hang on clean seem that process doesn't close, maybe some mbuild node reuse? btw manually tested
// DotnetCli("clean", out standardOutput, out standardError, _fixture.TestProjectPath);
// Assert.False(File.Exists(sourceRootMappingFilePath));
RunCommand("git", "clean -fdx", out _, out _, _testProjectPath);
}
public void Dispose()
{
File.Delete(Path.Combine(_testProjectPath, PropsFileName));
}
}
}