From e34c44564c8c17310bde103e330eeb4de554d853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller?= Date: Fri, 3 Dec 2021 13:21:50 +0100 Subject: [PATCH 1/3] made cli output invariant --- Documentation/Changelog.md | 1 + src/coverlet.console/Program.cs | 9 +++++--- .../Abstractions/IFormatHelper.cs | 9 ++++++++ src/coverlet.core/Helpers/FormatHelper.cs | 13 +++++++++++ .../CoverageResultTask.cs | 8 ++++--- .../InstrumentationTask.cs | 1 + .../Helpers/FormatHelperTests.cs | 22 +++++++++++++++++++ 7 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 src/coverlet.core/Abstractions/IFormatHelper.cs create mode 100644 src/coverlet.core/Helpers/FormatHelper.cs create mode 100644 test/coverlet.core.tests/Helpers/FormatHelperTests.cs diff --git a/Documentation/Changelog.md b/Documentation/Changelog.md index 51a458036..03194109c 100644 --- a/Documentation/Changelog.md +++ b/Documentation/Changelog.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Fixed +-Fix summary output format for culture de-DE [#1263](https://github.com/coverlet-coverage/coverlet/issues/1263) -Fix branch coverage issue for finally block with await [#1233](https://github.com/coverlet-coverage/coverlet/issues/1233) -Fix threshold doesn't work when coverage empty [#1205](https://github.com/coverlet-coverage/coverlet/issues/1205) -Fix branch coverage issue for il switch [#1177](https://github.com/coverlet-coverage/coverlet/issues/1177) diff --git a/src/coverlet.console/Program.cs b/src/coverlet.console/Program.cs index 6f206674f..806d2c64e 100644 --- a/src/coverlet.console/Program.cs +++ b/src/coverlet.console/Program.cs @@ -26,6 +26,7 @@ static int Main(string[] args) IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); + serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); // We need to keep singleton/static semantics @@ -242,13 +243,15 @@ static int Main(string[] args) var averageBranchPercent = branchPercentCalculation.AverageModulePercent; var averageMethodPercent = methodPercentCalculation.AverageModulePercent; + IFormatHelper formatHelper = serviceProvider.GetRequiredService(); + foreach (var _module in result.Modules) { var linePercent = summary.CalculateLineCoverage(_module.Value).Percent; var branchPercent = summary.CalculateBranchCoverage(_module.Value).Percent; var methodPercent = summary.CalculateMethodCoverage(_module.Value).Percent; - coverageTable.AddRow(Path.GetFileNameWithoutExtension(_module.Key), $"{linePercent}%", $"{branchPercent}%", $"{methodPercent}%"); + coverageTable.AddRow(Path.GetFileNameWithoutExtension(_module.Key), formatHelper.Invariant($"{linePercent}%"), formatHelper.Invariant($"{branchPercent}%"), formatHelper.Invariant($"{methodPercent}%")); } logger.LogInformation(coverageTable.ToStringAlternative()); @@ -257,8 +260,8 @@ static int Main(string[] args) coverageTable.Rows.Clear(); coverageTable.AddColumn(new[] { "", "Line", "Branch", "Method" }); - coverageTable.AddRow("Total", $"{totalLinePercent}%", $"{totalBranchPercent}%", $"{totalMethodPercent}%"); - coverageTable.AddRow("Average", $"{averageLinePercent}%", $"{averageBranchPercent}%", $"{averageMethodPercent}%"); + coverageTable.AddRow("Total", formatHelper.Invariant($"{totalLinePercent}%"), formatHelper.Invariant($"{totalBranchPercent}%"), formatHelper.Invariant($"{totalMethodPercent}%")); + coverageTable.AddRow("Average", formatHelper.Invariant($"{averageLinePercent}%"), formatHelper.Invariant($"{averageBranchPercent}%"), formatHelper.Invariant($"{averageMethodPercent}%")); logger.LogInformation(coverageTable.ToStringAlternative()); if (process.ExitCode > 0) diff --git a/src/coverlet.core/Abstractions/IFormatHelper.cs b/src/coverlet.core/Abstractions/IFormatHelper.cs new file mode 100644 index 000000000..e3ebeef89 --- /dev/null +++ b/src/coverlet.core/Abstractions/IFormatHelper.cs @@ -0,0 +1,9 @@ +using System; + +namespace Coverlet.Core.Abstractions +{ + internal interface IFormatHelper + { + string Invariant(FormattableString value); + } +} diff --git a/src/coverlet.core/Helpers/FormatHelper.cs b/src/coverlet.core/Helpers/FormatHelper.cs new file mode 100644 index 000000000..4f1109f6e --- /dev/null +++ b/src/coverlet.core/Helpers/FormatHelper.cs @@ -0,0 +1,13 @@ +using System; +using Coverlet.Core.Abstractions; + +namespace Coverlet.Core.Helpers +{ + public class FormatHelper : IFormatHelper + { + public string Invariant(FormattableString value) + { + return FormattableString.Invariant(value); + } + } +} diff --git a/src/coverlet.msbuild.tasks/CoverageResultTask.cs b/src/coverlet.msbuild.tasks/CoverageResultTask.cs index 3c1d72a74..35ec16954 100644 --- a/src/coverlet.msbuild.tasks/CoverageResultTask.cs +++ b/src/coverlet.msbuild.tasks/CoverageResultTask.cs @@ -200,13 +200,15 @@ public override bool Execute() var averageBranchPercent = branchPercentCalculation.AverageModulePercent; var averageMethodPercent = methodPercentCalculation.AverageModulePercent; + IFormatHelper formatHelper = ServiceProvider.GetService(); + foreach (var module in result.Modules) { var linePercent = summary.CalculateLineCoverage(module.Value).Percent; var branchPercent = summary.CalculateBranchCoverage(module.Value).Percent; var methodPercent = summary.CalculateMethodCoverage(module.Value).Percent; - coverageTable.AddRow(Path.GetFileNameWithoutExtension(module.Key), $"{linePercent}%", $"{branchPercent}%", $"{methodPercent}%"); + coverageTable.AddRow(Path.GetFileNameWithoutExtension(module.Key), formatHelper.Invariant($"{linePercent}%"), formatHelper.Invariant($"{branchPercent}%"), formatHelper.Invariant($"{methodPercent}%")); } Console.WriteLine(); @@ -216,8 +218,8 @@ public override bool Execute() coverageTable.Rows.Clear(); coverageTable.AddColumn(new[] { "", "Line", "Branch", "Method" }); - coverageTable.AddRow("Total", $"{totalLinePercent}%", $"{totalBranchPercent}%", $"{totalMethodPercent}%"); - coverageTable.AddRow("Average", $"{averageLinePercent}%", $"{averageBranchPercent}%", $"{averageMethodPercent}%"); + coverageTable.AddRow("Total", formatHelper.Invariant($"{totalLinePercent}%"), formatHelper.Invariant($"{totalBranchPercent}%"), formatHelper.Invariant($"{totalMethodPercent}%")); + coverageTable.AddRow("Average", formatHelper.Invariant($"{averageLinePercent}%"), formatHelper.Invariant($"{averageBranchPercent}%"), formatHelper.Invariant($"{averageMethodPercent}%")); Console.WriteLine(coverageTable.ToStringAlternative()); diff --git a/src/coverlet.msbuild.tasks/InstrumentationTask.cs b/src/coverlet.msbuild.tasks/InstrumentationTask.cs index 9be72b5c6..85f8c804a 100644 --- a/src/coverlet.msbuild.tasks/InstrumentationTask.cs +++ b/src/coverlet.msbuild.tasks/InstrumentationTask.cs @@ -69,6 +69,7 @@ public override bool Execute() serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); + serviceCollection.AddTransient(); serviceCollection.AddTransient(_ => _logger); serviceCollection.AddTransient(); // We cache resolutions diff --git a/test/coverlet.core.tests/Helpers/FormatHelperTests.cs b/test/coverlet.core.tests/Helpers/FormatHelperTests.cs new file mode 100644 index 000000000..746947b03 --- /dev/null +++ b/test/coverlet.core.tests/Helpers/FormatHelperTests.cs @@ -0,0 +1,22 @@ +using System.Globalization; +using System.Threading; +using Xunit; + +namespace Coverlet.Core.Helpers.Tests +{ + public class FormatHelperTests + { + [Theory] + [InlineData(2.2d, "2.2")] + public void TestInvariantFormattableString(double number, string expected) + { + var currentCulture = Thread.CurrentThread.CurrentCulture; + Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); + + var actual = new FormatHelper().Invariant($"{number}"); + + Assert.Equal(expected, actual); + Thread.CurrentThread.CurrentCulture = currentCulture; + } + } +} From c5d82fe438ec8388c21ff46d0a0be69d21b97d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller?= Date: Sun, 5 Dec 2021 22:53:28 +0100 Subject: [PATCH 2/3] code review --- src/coverlet.console/Program.cs | 12 +++++------ .../Abstractions/IFormatHelper.cs | 9 --------- src/coverlet.core/Helpers/FormatHelper.cs | 13 ------------ .../CoverageResultTask.cs | 11 +++++----- .../InstrumentationTask.cs | 1 - .../Helpers/FileSystemTests.cs | 20 ------------------- 6 files changed, 12 insertions(+), 54 deletions(-) delete mode 100644 src/coverlet.core/Abstractions/IFormatHelper.cs delete mode 100644 src/coverlet.core/Helpers/FormatHelper.cs delete mode 100644 test/coverlet.core.tests/Helpers/FileSystemTests.cs diff --git a/src/coverlet.console/Program.cs b/src/coverlet.console/Program.cs index 806d2c64e..ae3b02d03 100644 --- a/src/coverlet.console/Program.cs +++ b/src/coverlet.console/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; +using System.Globalization; using System.IO; using System.Linq; using System.Text; @@ -26,7 +27,6 @@ static int Main(string[] args) IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); - serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); // We need to keep singleton/static semantics @@ -243,15 +243,13 @@ static int Main(string[] args) var averageBranchPercent = branchPercentCalculation.AverageModulePercent; var averageMethodPercent = methodPercentCalculation.AverageModulePercent; - IFormatHelper formatHelper = serviceProvider.GetRequiredService(); - foreach (var _module in result.Modules) { var linePercent = summary.CalculateLineCoverage(_module.Value).Percent; var branchPercent = summary.CalculateBranchCoverage(_module.Value).Percent; var methodPercent = summary.CalculateMethodCoverage(_module.Value).Percent; - coverageTable.AddRow(Path.GetFileNameWithoutExtension(_module.Key), formatHelper.Invariant($"{linePercent}%"), formatHelper.Invariant($"{branchPercent}%"), formatHelper.Invariant($"{methodPercent}%")); + coverageTable.AddRow(Path.GetFileNameWithoutExtension(_module.Key), $"{InvariantFormat(linePercent)}%", $"{InvariantFormat(branchPercent)}%", $"{InvariantFormat(methodPercent)}%"); } logger.LogInformation(coverageTable.ToStringAlternative()); @@ -260,8 +258,8 @@ static int Main(string[] args) coverageTable.Rows.Clear(); coverageTable.AddColumn(new[] { "", "Line", "Branch", "Method" }); - coverageTable.AddRow("Total", formatHelper.Invariant($"{totalLinePercent}%"), formatHelper.Invariant($"{totalBranchPercent}%"), formatHelper.Invariant($"{totalMethodPercent}%")); - coverageTable.AddRow("Average", formatHelper.Invariant($"{averageLinePercent}%"), formatHelper.Invariant($"{averageBranchPercent}%"), formatHelper.Invariant($"{averageMethodPercent}%")); + coverageTable.AddRow("Total", $"{InvariantFormat(totalLinePercent)}%", $"{InvariantFormat(totalBranchPercent)}%", $"{InvariantFormat(totalMethodPercent)}%"); + coverageTable.AddRow("Average", $"{InvariantFormat(averageLinePercent)}%", $"{InvariantFormat(averageBranchPercent)}%", $"{InvariantFormat(averageMethodPercent)}%"); logger.LogInformation(coverageTable.ToStringAlternative()); if (process.ExitCode > 0) @@ -317,5 +315,7 @@ static int Main(string[] args) } static string GetAssemblyVersion() => typeof(Program).Assembly.GetName().Version.ToString(); + + static string InvariantFormat(double value) => value.ToString(CultureInfo.InvariantCulture); } } diff --git a/src/coverlet.core/Abstractions/IFormatHelper.cs b/src/coverlet.core/Abstractions/IFormatHelper.cs deleted file mode 100644 index e3ebeef89..000000000 --- a/src/coverlet.core/Abstractions/IFormatHelper.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Coverlet.Core.Abstractions -{ - internal interface IFormatHelper - { - string Invariant(FormattableString value); - } -} diff --git a/src/coverlet.core/Helpers/FormatHelper.cs b/src/coverlet.core/Helpers/FormatHelper.cs deleted file mode 100644 index 4f1109f6e..000000000 --- a/src/coverlet.core/Helpers/FormatHelper.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using Coverlet.Core.Abstractions; - -namespace Coverlet.Core.Helpers -{ - public class FormatHelper : IFormatHelper - { - public string Invariant(FormattableString value) - { - return FormattableString.Invariant(value); - } - } -} diff --git a/src/coverlet.msbuild.tasks/CoverageResultTask.cs b/src/coverlet.msbuild.tasks/CoverageResultTask.cs index 35ec16954..ebc17a0e4 100644 --- a/src/coverlet.msbuild.tasks/CoverageResultTask.cs +++ b/src/coverlet.msbuild.tasks/CoverageResultTask.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Text; @@ -200,15 +201,13 @@ public override bool Execute() var averageBranchPercent = branchPercentCalculation.AverageModulePercent; var averageMethodPercent = methodPercentCalculation.AverageModulePercent; - IFormatHelper formatHelper = ServiceProvider.GetService(); - foreach (var module in result.Modules) { var linePercent = summary.CalculateLineCoverage(module.Value).Percent; var branchPercent = summary.CalculateBranchCoverage(module.Value).Percent; var methodPercent = summary.CalculateMethodCoverage(module.Value).Percent; - coverageTable.AddRow(Path.GetFileNameWithoutExtension(module.Key), formatHelper.Invariant($"{linePercent}%"), formatHelper.Invariant($"{branchPercent}%"), formatHelper.Invariant($"{methodPercent}%")); + coverageTable.AddRow(Path.GetFileNameWithoutExtension(module.Key), $"{InvariantFormat(linePercent)}%", $"{InvariantFormat(branchPercent)}%", $"{InvariantFormat(methodPercent)}%"); } Console.WriteLine(); @@ -218,8 +217,8 @@ public override bool Execute() coverageTable.Rows.Clear(); coverageTable.AddColumn(new[] { "", "Line", "Branch", "Method" }); - coverageTable.AddRow("Total", formatHelper.Invariant($"{totalLinePercent}%"), formatHelper.Invariant($"{totalBranchPercent}%"), formatHelper.Invariant($"{totalMethodPercent}%")); - coverageTable.AddRow("Average", formatHelper.Invariant($"{averageLinePercent}%"), formatHelper.Invariant($"{averageBranchPercent}%"), formatHelper.Invariant($"{averageMethodPercent}%")); + coverageTable.AddRow("Total", $"{InvariantFormat(totalLinePercent)}%", $"{InvariantFormat(totalBranchPercent)}%", $"{InvariantFormat(totalMethodPercent)}%"); + coverageTable.AddRow("Average", $"{InvariantFormat(averageLinePercent)}%", $"{InvariantFormat(averageBranchPercent)}%", $"{InvariantFormat(averageMethodPercent)}%"); Console.WriteLine(coverageTable.ToStringAlternative()); @@ -256,5 +255,7 @@ public override bool Execute() return true; } + + private static string InvariantFormat(double value) => value.ToString(CultureInfo.InvariantCulture); } } diff --git a/src/coverlet.msbuild.tasks/InstrumentationTask.cs b/src/coverlet.msbuild.tasks/InstrumentationTask.cs index 85f8c804a..9be72b5c6 100644 --- a/src/coverlet.msbuild.tasks/InstrumentationTask.cs +++ b/src/coverlet.msbuild.tasks/InstrumentationTask.cs @@ -69,7 +69,6 @@ public override bool Execute() serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); - serviceCollection.AddTransient(); serviceCollection.AddTransient(_ => _logger); serviceCollection.AddTransient(); // We cache resolutions diff --git a/test/coverlet.core.tests/Helpers/FileSystemTests.cs b/test/coverlet.core.tests/Helpers/FileSystemTests.cs deleted file mode 100644 index 618a94f89..000000000 --- a/test/coverlet.core.tests/Helpers/FileSystemTests.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Coverlet.Core.Helpers; -using Xunit; - -namespace Coverlet.Core.Helpers.Tests -{ - public class FileSystemTests - { - [Theory] - [InlineData(null, null)] - [InlineData("", "")] - [InlineData("filename.cs", "filename.cs")] - [InlineData("filename{T}.cs", "filename{{T}}.cs")] - public void TestEscapeFileName(string fileName, string expected) - { - var actual = FileSystem.EscapeFileName(fileName); - - Assert.Equal(expected, actual); - } - } -} \ No newline at end of file From fa510d5c09b2a301cef1587c95d244997170819d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller?= Date: Sun, 5 Dec 2021 22:59:52 +0100 Subject: [PATCH 3/3] code review --- .../Helpers/FileSystemTests.cs | 20 +++++++++++++++++ .../Helpers/FormatHelperTests.cs | 22 ------------------- 2 files changed, 20 insertions(+), 22 deletions(-) create mode 100644 test/coverlet.core.tests/Helpers/FileSystemTests.cs delete mode 100644 test/coverlet.core.tests/Helpers/FormatHelperTests.cs diff --git a/test/coverlet.core.tests/Helpers/FileSystemTests.cs b/test/coverlet.core.tests/Helpers/FileSystemTests.cs new file mode 100644 index 000000000..618a94f89 --- /dev/null +++ b/test/coverlet.core.tests/Helpers/FileSystemTests.cs @@ -0,0 +1,20 @@ +using Coverlet.Core.Helpers; +using Xunit; + +namespace Coverlet.Core.Helpers.Tests +{ + public class FileSystemTests + { + [Theory] + [InlineData(null, null)] + [InlineData("", "")] + [InlineData("filename.cs", "filename.cs")] + [InlineData("filename{T}.cs", "filename{{T}}.cs")] + public void TestEscapeFileName(string fileName, string expected) + { + var actual = FileSystem.EscapeFileName(fileName); + + Assert.Equal(expected, actual); + } + } +} \ No newline at end of file diff --git a/test/coverlet.core.tests/Helpers/FormatHelperTests.cs b/test/coverlet.core.tests/Helpers/FormatHelperTests.cs deleted file mode 100644 index 746947b03..000000000 --- a/test/coverlet.core.tests/Helpers/FormatHelperTests.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Globalization; -using System.Threading; -using Xunit; - -namespace Coverlet.Core.Helpers.Tests -{ - public class FormatHelperTests - { - [Theory] - [InlineData(2.2d, "2.2")] - public void TestInvariantFormattableString(double number, string expected) - { - var currentCulture = Thread.CurrentThread.CurrentCulture; - Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); - - var actual = new FormatHelper().Invariant($"{number}"); - - Assert.Equal(expected, actual); - Thread.CurrentThread.CurrentCulture = currentCulture; - } - } -}