From 0507806aae93a3f9b7194dfc270fb6115cb2551f Mon Sep 17 00:00:00 2001 From: Philip Wood Date: Sat, 6 May 2017 17:53:44 +0100 Subject: [PATCH] Rough first go at test coverage. Might need tidying up. --- build.cake | 72 +++++++++++++++++-- src/Ocelot/Ocelot.csproj | 5 ++ .../Ocelot.AcceptanceTests.csproj | 4 +- .../Ocelot.IntegrationTests.csproj | 4 +- test/Ocelot.UnitTests/Ocelot.UnitTests.csproj | 9 ++- 5 files changed, 83 insertions(+), 11 deletions(-) diff --git a/build.cake b/build.cake index faa41f7d..3773fd69 100644 --- a/build.cake +++ b/build.cake @@ -1,6 +1,10 @@ #tool "nuget:?package=GitVersion.CommandLine" #tool "nuget:?package=GitReleaseNotes" #addin "nuget:?package=Cake.Json" +#tool "nuget:?package=OpenCover" +#tool "nuget:?package=ReportGenerator" +#tool coveralls.net +#addin Cake.Coveralls // compile var compileConfig = Argument("configuration", "Release"); @@ -12,6 +16,8 @@ var artifactsDir = Directory("artifacts"); // unit testing var artifactsForUnitTestsDir = artifactsDir + Directory("UnitTests"); var unitTestAssemblies = @"./test/Ocelot.UnitTests/Ocelot.UnitTests.csproj"; +var minCodeCoverage = 75d; +var coverallsRepoToken = "coveralls-repo-token-ocelot"; // acceptance testing var artifactsForAcceptanceTestsDir = artifactsDir + Directory("AcceptanceTests"); @@ -118,13 +124,69 @@ Task("RunUnitTests") .IsDependentOn("Compile") .Does(() => { - var settings = new DotNetCoreTestSettings + if (IsRunningOnWindows()) { - Configuration = compileConfig, - }; + var coverageSummaryFile = artifactsForUnitTestsDir + File("coverage.xml"); + + EnsureDirectoryExists(artifactsForUnitTestsDir); + + OpenCover(tool => + { + tool.DotNetCoreTest(unitTestAssemblies); + }, + new FilePath(coverageSummaryFile), + new OpenCoverSettings() + { + Register="user", + ArgumentCustomization=args=>args.Append(@"-oldstyle -returntargetcode") + } + .WithFilter("+[Ocelot*]*") + .WithFilter("-[xunit*]*") + .WithFilter("-[Ocelot*Tests]*") + ); + + ReportGenerator(coverageSummaryFile, artifactsForUnitTestsDir); + + if (!AppVeyor.IsRunningOnAppVeyor) + { + var repoToken = EnvironmentVariable(coverallsRepoToken); + if (string.IsNullOrEmpty(repoToken)) + { + throw new Exception(string.Format("Coveralls repo token not found. Set environment variable '{0}'", coverallsRepoToken)); + } - EnsureDirectoryExists(artifactsForUnitTestsDir); - DotNetCoreTest(unitTestAssemblies, settings); + Information("Uploading test coverage to coveralls.io"); + CoverallsNet(coverageSummaryFile, CoverallsNetReportType.OpenCover, new CoverallsNetSettings() + { + RepoToken = repoToken + }); + } + else + { + Information("We are not running on the build server so we won't publish the coverage report to coveralls.io"); + } + + var sequenceCoverage = XmlPeek(coverageSummaryFile, "//CoverageSession/Summary/@sequenceCoverage"); + var branchCoverage = XmlPeek(coverageSummaryFile, "//CoverageSession/Summary/@branchCoverage"); + + Information("Sequence Coverage: " + sequenceCoverage); + + if(double.Parse(sequenceCoverage) < minCodeCoverage) + { + throw new Exception(string.Format("Code coverage fell below the threshold of {0}%", minCodeCoverage)); + }; + + } + else + { + var settings = new DotNetCoreTestSettings + { + Configuration = compileConfig, + }; + + EnsureDirectoryExists(artifactsForUnitTestsDir); + DotNetCoreTest(unitTestAssemblies, settings); + } }); Task("RunAcceptanceTests") diff --git a/src/Ocelot/Ocelot.csproj b/src/Ocelot/Ocelot.csproj index 8fdb917d..d993290e 100644 --- a/src/Ocelot/Ocelot.csproj +++ b/src/Ocelot/Ocelot.csproj @@ -20,6 +20,11 @@ Tom Pallister + + full + True + + diff --git a/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj b/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj index 06a30386..0cc0f528 100644 --- a/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj +++ b/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj @@ -31,7 +31,7 @@ - + @@ -51,7 +51,7 @@ - + diff --git a/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj b/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj index f2bb5354..0c706952 100644 --- a/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj +++ b/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj @@ -31,7 +31,7 @@ - + @@ -42,7 +42,7 @@ - + diff --git a/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj b/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj index d20d9e38..941c015c 100644 --- a/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj +++ b/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj @@ -14,6 +14,11 @@ false + + full + True + + @@ -24,7 +29,7 @@ - + @@ -43,7 +48,7 @@ - +