From 0507806aae93a3f9b7194dfc270fb6115cb2551f Mon Sep 17 00:00:00 2001 From: Philip Wood Date: Sat, 6 May 2017 17:53:44 +0100 Subject: [PATCH 1/4] 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 faa41f7dc..3773fd69e 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)); + } + + 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"); + } - EnsureDirectoryExists(artifactsForUnitTestsDir); - DotNetCoreTest(unitTestAssemblies, settings); + 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 8fdb917df..d993290ef 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 06a30386f..0cc0f5280 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 f2bb53540..0c706952f 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 d20d9e384..941c015cd 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 @@ - + From 28de6f2fbf1b3299f9e01c925a913720798a3a8b Mon Sep 17 00:00:00 2001 From: Philip Wood Date: Tue, 9 May 2017 13:06:54 +0100 Subject: [PATCH 2/4] Oops. Forgot to do this switcheroo after testing locally. --- build.cake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.cake b/build.cake index 3773fd69e..57460772e 100644 --- a/build.cake +++ b/build.cake @@ -147,7 +147,7 @@ Task("RunUnitTests") ReportGenerator(coverageSummaryFile, artifactsForUnitTestsDir); - if (!AppVeyor.IsRunningOnAppVeyor) + if (AppVeyor.IsRunningOnAppVeyor) { var repoToken = EnvironmentVariable(coverallsRepoToken); if (string.IsNullOrEmpty(repoToken)) From 944e96c4f80b9176137a9dc76a6dce9f5b1f83cc Mon Sep 17 00:00:00 2001 From: Philip Wood Date: Tue, 9 May 2017 19:44:47 +0100 Subject: [PATCH 3/4] update to latest xunit prerelease --- test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj | 4 ++-- test/Ocelot.UnitTests/Ocelot.UnitTests.csproj | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj b/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj index 0c706952f..18ae6b229 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 941c015cd..403db47ff 100644 --- a/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj +++ b/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj @@ -29,7 +29,7 @@ - + @@ -48,7 +48,7 @@ - + From e9ab5d64eaa2b2607753d5d4af4d8aecb604fdc6 Mon Sep 17 00:00:00 2001 From: Philip Wood Date: Tue, 9 May 2017 20:33:21 +0100 Subject: [PATCH 4/4] Not sure how I missed the acceptance test xunit update on the last commit. And, improve logging around test coverage. --- build.cake | 6 ++++-- test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/build.cake b/build.cake index 57460772e..eddaffc0b 100644 --- a/build.cake +++ b/build.cake @@ -18,6 +18,7 @@ var artifactsForUnitTestsDir = artifactsDir + Directory("UnitTests"); var unitTestAssemblies = @"./test/Ocelot.UnitTests/Ocelot.UnitTests.csproj"; var minCodeCoverage = 75d; var coverallsRepoToken = "coveralls-repo-token-ocelot"; +var coverallsRepo = "https://coveralls.io/github/TomPallister/Ocelot"; // acceptance testing var artifactsForAcceptanceTestsDir = artifactsDir + Directory("AcceptanceTests"); @@ -155,7 +156,7 @@ Task("RunUnitTests") throw new Exception(string.Format("Coveralls repo token not found. Set environment variable '{0}'", coverallsRepoToken)); } - Information("Uploading test coverage to coveralls.io"); + Information(string.Format("Uploading test coverage to {0}", coverallsRepo)); CoverallsNet(coverageSummaryFile, CoverallsNetReportType.OpenCover, new CoverallsNetSettings() { RepoToken = repoToken @@ -173,7 +174,8 @@ Task("RunUnitTests") if(double.Parse(sequenceCoverage) < minCodeCoverage) { - throw new Exception(string.Format("Code coverage fell below the threshold of {0}%", minCodeCoverage)); + var whereToCheck = !AppVeyor.IsRunningOnAppVeyor ? coverallsRepo : artifactsForUnitTestsDir; + throw new Exception(string.Format("Code coverage fell below the threshold of {0}%. You can find the code coverage report at {1}", minCodeCoverage, whereToCheck)); }; } diff --git a/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj b/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj index 0cc0f5280..4adb231f8 100644 --- a/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj +++ b/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj @@ -31,7 +31,7 @@ - + @@ -51,7 +51,7 @@ - +