mirror of
https://github.com/zoriya/octokit.net.git
synced 2025-12-05 23:06:10 +00:00
Generate code coverage output with coverlet (#1866)
* add support for Codecov reporting on Windows
This commit is contained in:
committed by
Brendan Forster
parent
60e60ca384
commit
f70c402d78
@@ -6,7 +6,10 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Cake.Coverlet" Version="1.3.1" />
|
||||
<PackageReference Include="Cake.Frosting" Version="0.34.1" />
|
||||
<PackageReference Include="Cake.Codecov" Version="0.4.0" />
|
||||
<PackageReference Include="Codecov" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -13,6 +13,7 @@ public class Context : FrostingContext
|
||||
public BuildVersion Version { get; set; }
|
||||
|
||||
public DirectoryPath Artifacts { get; set; }
|
||||
public DirectoryPath CodeCoverage { get; set; }
|
||||
|
||||
public bool IsLocalBuild { get; set; }
|
||||
public bool IsPullRequest { get; set; }
|
||||
|
||||
@@ -15,6 +15,7 @@ public class Lifetime : FrostingLifetime<Context>
|
||||
context.CoreOnly = context.Argument("CoreOnly", !context.IsRunningOnWindows());
|
||||
|
||||
context.Artifacts = "./packaging/";
|
||||
context.CodeCoverage = "./coverage-results/";
|
||||
|
||||
// Build system information.
|
||||
var buildSystem = context.BuildSystem();
|
||||
|
||||
@@ -13,7 +13,8 @@ public sealed class Clean : FrostingTask<Context>
|
||||
|
||||
var directories = context.GetDirectories("./**/bin", globberSettings)
|
||||
+ context.GetDirectories("./**/obj", globberSettings)
|
||||
+ context.Artifacts;
|
||||
+ context.Artifacts
|
||||
+ context.CodeCoverage;
|
||||
|
||||
foreach (var directory in directories)
|
||||
{
|
||||
|
||||
75
build/Tasks/CodeCoverage.cs
Normal file
75
build/Tasks/CodeCoverage.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cake.Codecov;
|
||||
using Cake.Common;
|
||||
using Cake.Common.Build;
|
||||
using Cake.Common.Diagnostics;
|
||||
using Cake.Core.IO;
|
||||
using Cake.Frosting;
|
||||
|
||||
[Dependency(typeof(Build))]
|
||||
public sealed class CodeCoverage : FrostingTask<Context>
|
||||
{
|
||||
public override void Run(Context context)
|
||||
{
|
||||
var coverageFiles = new List<FilePath>();
|
||||
|
||||
if (context.AppVeyor)
|
||||
{
|
||||
foreach (var project in context.Projects.Where(x => x.UnitTests))
|
||||
{
|
||||
context.Information("Executing Code Coverage for Project {0}...", project.Name);
|
||||
|
||||
var dotNetCoreCoverage = context.CodeCoverage
|
||||
.CombineWithFilePath(project.Name + "-netcoreapp2.0.xml");
|
||||
coverageFiles.Add(dotNetCoreCoverage);
|
||||
|
||||
context.Coverlet(project, new CoverletToolSettings()
|
||||
{
|
||||
Configuration = context.Configuration,
|
||||
Framework = "netcoreapp2.0",
|
||||
Output = dotNetCoreCoverage.FullPath
|
||||
});
|
||||
|
||||
if (context.IsRunningOnWindows())
|
||||
{
|
||||
var dotNetFrameworkCoverage = context.CodeCoverage
|
||||
.CombineWithFilePath(project.Name + "-net452.xml");
|
||||
coverageFiles.Add(dotNetFrameworkCoverage);
|
||||
|
||||
context.Coverlet(project, new CoverletToolSettings
|
||||
{
|
||||
Configuration = context.Configuration,
|
||||
Framework = "net452",
|
||||
Output = dotNetFrameworkCoverage.FullPath
|
||||
});
|
||||
}
|
||||
|
||||
context.Information("Uploading Coverage Files: {0}", string.Join(",", coverageFiles.Select(path => path.GetFilename().ToString())));
|
||||
|
||||
var buildVersion = $"{context.Version.FullSemVer}.build.{context.EnvironmentVariable("APPVEYOR_BUILD_NUMBER")}";
|
||||
|
||||
var userProfilePath = context.EnvironmentVariable("USERPROFILE");
|
||||
var codecovPath = new DirectoryPath(userProfilePath)
|
||||
.CombineWithFilePath(".nuget\\packages\\codecov\\1.1.0\\tools\\codecov.exe");
|
||||
|
||||
context.Tools.RegisterFile(codecovPath);
|
||||
|
||||
foreach (var coverageFile in coverageFiles)
|
||||
{
|
||||
var settings = new CodecovSettings
|
||||
{
|
||||
Files = new[] { coverageFile.MakeAbsolute(context.Environment).FullPath },
|
||||
Verbose = true,
|
||||
EnvironmentVariables = new Dictionary<string, string>()
|
||||
{
|
||||
{ "APPVEYOR_BUILD_VERSION", buildVersion}
|
||||
}
|
||||
};
|
||||
|
||||
context.Codecov(settings);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Cake.Frosting;
|
||||
|
||||
[Dependency(typeof(UnitTests))]
|
||||
[Dependency(typeof(ConventionTests))]
|
||||
[Dependency(typeof(CodeCoverage))]
|
||||
[Dependency(typeof(ValidateLINQPadSamples))]
|
||||
public sealed class Package : FrostingTask<Context>
|
||||
{
|
||||
|
||||
62
build/Tools/Coverlet.cs
Normal file
62
build/Tools/Coverlet.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cake.Common.Diagnostics;
|
||||
using Cake.Core;
|
||||
using Cake.Core.Annotations;
|
||||
using Cake.Core.IO;
|
||||
using Cake.Core.Tooling;
|
||||
|
||||
public class CoverletTool : Tool<CoverletToolSettings>
|
||||
{
|
||||
private readonly ICakeEnvironment _environment;
|
||||
|
||||
public CoverletTool(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner runner, IToolLocator tools)
|
||||
: base(fileSystem, environment, runner, tools)
|
||||
{
|
||||
_environment = environment;
|
||||
}
|
||||
|
||||
public void Coverlet(Project project, CoverletToolSettings settings)
|
||||
{
|
||||
var arguments = new ProcessArgumentBuilder();
|
||||
|
||||
var filePath = FilePath.FromString($"bin\\{settings.Configuration}\\{settings.Framework}\\{project.Name}.dll");
|
||||
var fullPath = project.Path.GetDirectory().CombineWithFilePath(filePath).MakeAbsolute(_environment);
|
||||
|
||||
arguments.Append($"\"{fullPath}\" --target \"dotnet\" --targetargs \"test -c {settings.Configuration} {project.Path.FullPath} --no-build\" --format opencover --output \"{settings.Output}\"");
|
||||
|
||||
Run(settings, arguments);
|
||||
}
|
||||
|
||||
protected override string GetToolName()
|
||||
{
|
||||
return "Coverlet";
|
||||
}
|
||||
|
||||
protected override IEnumerable<string> GetToolExecutableNames()
|
||||
{
|
||||
return new[] { "coverlet", "coverlet.exe" };
|
||||
}
|
||||
}
|
||||
|
||||
public class CoverletToolSettings : ToolSettings
|
||||
{
|
||||
public string Configuration { get; set; }
|
||||
public string Framework { get; set; }
|
||||
public string Output { get; set; }
|
||||
}
|
||||
|
||||
public static class CoverletAliases
|
||||
{
|
||||
[CakeMethodAlias]
|
||||
public static void Coverlet(this ICakeContext context, Project project, CoverletToolSettings settings = null)
|
||||
{
|
||||
if (context == null)
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
|
||||
if (settings == null)
|
||||
throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
new CoverletTool(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools).Coverlet(project, settings);
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,13 @@ public class BuildVersion
|
||||
{
|
||||
public string Prefix { get; set; }
|
||||
public string Suffix { get; set; }
|
||||
public string FullSemVer { get; set; }
|
||||
|
||||
public BuildVersion(string version, string suffix)
|
||||
public BuildVersion(string version, string suffix, string fullSemVer)
|
||||
{
|
||||
Prefix = version;
|
||||
Suffix = suffix;
|
||||
FullSemVer = fullSemVer;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Suffix))
|
||||
{
|
||||
@@ -29,6 +31,10 @@ public class BuildVersion
|
||||
|
||||
public static BuildVersion Calculate(Context context)
|
||||
{
|
||||
string version = null;
|
||||
string semVersion = null;
|
||||
string fullSemVer = null;
|
||||
|
||||
context.Information("Calculating semantic version...");
|
||||
|
||||
if (!context.IsLocalBuild)
|
||||
@@ -40,14 +46,15 @@ public class BuildVersion
|
||||
// Run in interactive mode to get the properties for the rest of the script
|
||||
var assertedversions = GitVersionRunner.Run(context, GitVersionOutput.Json);
|
||||
|
||||
var version = assertedversions.MajorMinorPatch;
|
||||
var semVersion = assertedversions.LegacySemVerPadded;
|
||||
version = assertedversions.MajorMinorPatch;
|
||||
semVersion = assertedversions.LegacySemVerPadded;
|
||||
fullSemVer = assertedversions.FullSemVer;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(version))
|
||||
{
|
||||
throw new CakeException("Could not calculate version of build.");
|
||||
}
|
||||
|
||||
return new BuildVersion(version, semVersion.Substring(version.Length).TrimStart('-'));
|
||||
return new BuildVersion(version, semVersion.Substring(version.Length).TrimStart('-'), fullSemVer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user