Skip to content

Commit 67494ec

Browse files
committed
(build) include Cake.Coverlet code for now
1 parent bb243bc commit 67494ec

File tree

5 files changed

+390
-4
lines changed

5 files changed

+390
-4
lines changed

build/build/Tasks/Test/UnitTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Cake.Common.Build.AzurePipelines.Data;
2-
using Cake.Common.Tools.DotNetCore.Test;
2+
using Cake.Common.Tools.DotNet.Test;
33
using Cake.Coverlet;
44
using Cake.Incubator.LoggingExtensions;
5+
using Common.Addins.Cake.Coverlet;
56
using Common.Utilities;
67

78
namespace Build.Tasks;
@@ -63,7 +64,7 @@ private static void TestProjectForTarget(BuildContext context, FilePath project,
6364
{
6465
var testResultsPath = Paths.TestOutput;
6566
var projectName = $"{project.GetFilenameWithoutExtension()}.{framework}";
66-
var settings = new DotNetCoreTestSettings
67+
var settings = new DotNetTestSettings
6768
{
6869
Framework = framework,
6970
NoBuild = true,
@@ -97,7 +98,6 @@ private static void TestProjectForTarget(BuildContext context, FilePath project,
9798
settings.Filter = context.IsRunningOnUnix() ? $"TestCategory!={Constants.NoMono}" : $"TestCategory!={Constants.NoNet48}";
9899
}
99100

100-
// TODO update to DotNetTest when available
101-
context.DotNetCoreTest(project.FullPath, settings, coverletSettings);
101+
context.DotNetTest(project.FullPath, settings, coverletSettings);
102102
}
103103
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
using Cake.Coverlet;
2+
3+
namespace Common.Addins.Cake.Coverlet;
4+
5+
internal static class ArgumentsProcessor
6+
{
7+
public static ProcessArgumentBuilder ProcessMSBuildArguments(
8+
CoverletSettings settings,
9+
ICakeEnvironment cakeEnvironment,
10+
ProcessArgumentBuilder builder,
11+
FilePath project)
12+
{
13+
builder.AppendMSBuildProperty(nameof(CoverletSettings.CollectCoverage), settings.CollectCoverage.ToString());
14+
builder.AppendPropertyList(nameof(CoverletSettings.CoverletOutputFormat), SplitFlagEnum(settings.CoverletOutputFormat));
15+
16+
if (settings.Threshold.HasValue)
17+
{
18+
if (settings.Threshold > 100)
19+
{
20+
throw new Exception("Threshold Percentage cannot be set as greater than 100%");
21+
}
22+
23+
builder.AppendMSBuildProperty(nameof(CoverletSettings.Threshold), settings.Threshold.ToString());
24+
25+
if (settings.ThresholdType != ThresholdType.NotSet)
26+
{
27+
builder.AppendPropertyList(nameof(CoverletSettings.ThresholdType), SplitFlagEnum(settings.ThresholdType));
28+
}
29+
}
30+
31+
if (settings.CoverletOutputDirectory != null && string.IsNullOrEmpty(settings.CoverletOutputName))
32+
{
33+
var directoryPath = settings.CoverletOutputDirectory
34+
.MakeAbsolute(cakeEnvironment).FullPath;
35+
36+
builder.AppendMSBuildPropertyQuoted("CoverletOutput", directoryPath);
37+
}
38+
else if (!string.IsNullOrEmpty(settings.CoverletOutputName))
39+
{
40+
var dir = settings.CoverletOutputDirectory ?? project.GetDirectory();
41+
var directoryPath = dir.MakeAbsolute(cakeEnvironment).FullPath;
42+
43+
var filepath = FilePath.FromString(settings.OutputTransformer(settings.CoverletOutputName, directoryPath));
44+
45+
builder.AppendMSBuildPropertyQuoted("CoverletOutput", filepath.MakeAbsolute(cakeEnvironment).FullPath);
46+
}
47+
48+
if (settings.ExcludeByFile.Count > 0)
49+
{
50+
builder.AppendPropertyList(nameof(CoverletSettings.ExcludeByFile), settings.ExcludeByFile);
51+
}
52+
53+
54+
if (settings.Include.Count > 0)
55+
{
56+
builder.AppendPropertyList(nameof(CoverletSettings.Include), settings.Include);
57+
}
58+
59+
60+
if (settings.Exclude.Count > 0)
61+
{
62+
builder.AppendPropertyList(nameof(CoverletSettings.Exclude), settings.Exclude);
63+
}
64+
65+
if (settings.ExcludeByAttribute.Count > 0)
66+
{
67+
builder.AppendPropertyList(nameof(CoverletSettings.ExcludeByAttribute), settings.ExcludeByAttribute);
68+
}
69+
70+
if (settings.MergeWithFile != null && settings.MergeWithFile.GetExtension() == ".json")
71+
{
72+
builder.AppendMSBuildPropertyQuoted("MergeWith", settings.MergeWithFile.MakeAbsolute(cakeEnvironment).FullPath);
73+
}
74+
75+
if (settings.IncludeTestAssembly.HasValue)
76+
{
77+
builder.AppendMSBuildProperty(nameof(CoverletSettings.IncludeTestAssembly), settings.IncludeTestAssembly.Value ? bool.TrueString : bool.FalseString);
78+
}
79+
80+
return builder;
81+
}
82+
83+
public static ProcessArgumentBuilder ProcessToolArguments(
84+
CoverletSettings settings,
85+
ICakeEnvironment cakeEnvironment,
86+
ProcessArgumentBuilder builder,
87+
FilePath project)
88+
{
89+
builder.AppendSwitch("--format", SplitFlagEnum(settings.CoverletOutputFormat));
90+
91+
if (settings.Threshold.HasValue)
92+
{
93+
if (settings.Threshold > 100)
94+
{
95+
throw new Exception("Threshold Percentage cannot be set as greater than 100%");
96+
}
97+
98+
builder.AppendSwitch(nameof(CoverletSettings.Threshold), settings.Threshold.ToString());
99+
100+
if (settings.ThresholdType != ThresholdType.NotSet)
101+
{
102+
builder.AppendSwitchQuoted("--threshold-type", SplitFlagEnum(settings.ThresholdType));
103+
}
104+
}
105+
106+
if (settings.CoverletOutputDirectory != null && string.IsNullOrEmpty(settings.CoverletOutputName))
107+
{
108+
var directoryPath = settings.CoverletOutputDirectory
109+
.MakeAbsolute(cakeEnvironment).FullPath;
110+
111+
builder.AppendSwitchQuoted("--output", directoryPath);
112+
}
113+
else if (!string.IsNullOrEmpty(settings.CoverletOutputName))
114+
{
115+
var dir = settings.CoverletOutputDirectory ?? project.GetDirectory();
116+
var directoryPath = dir.MakeAbsolute(cakeEnvironment).FullPath;
117+
118+
var filepath = FilePath.FromString(settings.OutputTransformer(settings.CoverletOutputName, directoryPath));
119+
120+
builder.AppendSwitchQuoted("--output", filepath.MakeAbsolute(cakeEnvironment).FullPath);
121+
}
122+
123+
if (settings.ExcludeByFile.Count > 0)
124+
{
125+
builder.AppendSwitchQuoted("--exclude-by-file", settings.ExcludeByFile);
126+
}
127+
128+
if (settings.ExcludeByAttribute.Count > 0)
129+
{
130+
builder.AppendSwitchQuoted("--exclude-by-attribute", settings.ExcludeByAttribute);
131+
}
132+
133+
if (settings.Exclude.Count > 0)
134+
{
135+
builder.AppendSwitchQuoted("--exclude", settings.Exclude);
136+
}
137+
138+
if (settings.Include.Count > 0)
139+
{
140+
builder.AppendSwitchQuoted("--include", settings.Include);
141+
}
142+
143+
if (settings.MergeWithFile != null && settings.MergeWithFile.GetExtension() == ".json")
144+
{
145+
builder.AppendSwitchQuoted("--merge-with", settings.MergeWithFile.MakeAbsolute(cakeEnvironment).FullPath);
146+
}
147+
148+
if (settings.IncludeTestAssembly.HasValue)
149+
{
150+
builder.AppendSwitch("--include-test-assembly", settings.IncludeTestAssembly.Value ? bool.TrueString : bool.FalseString);
151+
}
152+
153+
return builder;
154+
}
155+
156+
private static IEnumerable<string> SplitFlagEnum(Enum @enum) => @enum.ToString("g").Split(',').Select(s => s.ToLowerInvariant());
157+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace Common.Addins.Cake.Coverlet;
2+
3+
internal static class BuilderExtension
4+
{
5+
internal static ProcessArgumentBuilder AppendMSBuildProperty(this ProcessArgumentBuilder builder, string propertyName, string value)
6+
{
7+
builder.AppendSwitch($"/property:{propertyName}", "=", value);
8+
return builder;
9+
}
10+
11+
internal static ProcessArgumentBuilder AppendMSBuildPropertyQuoted(this ProcessArgumentBuilder builder, string propertyName, string value)
12+
{
13+
builder.AppendSwitchQuoted($"/property:{propertyName}", "=", value);
14+
return builder;
15+
}
16+
17+
internal static ProcessArgumentBuilder AppendPropertyList(this ProcessArgumentBuilder builder, string propertyName, IEnumerable<string> values)
18+
{
19+
builder.Append($"/property:{propertyName}=\\\"{string.Join(",", values.Select(s => s.Trim()))}\\\"");
20+
return builder;
21+
}
22+
23+
internal static ProcessArgumentBuilder AppendSwitchQuoted(this ProcessArgumentBuilder builder, string @switch, IEnumerable<string> values)
24+
{
25+
foreach (var type in values.Select(s => s.Trim()))
26+
{
27+
builder.AppendSwitchQuoted(@switch, type);
28+
}
29+
return builder;
30+
}
31+
32+
internal static ProcessArgumentBuilder AppendSwitch(this ProcessArgumentBuilder builder, string @switch, IEnumerable<string> values)
33+
{
34+
foreach (var type in values.Select(s => s.Trim()))
35+
{
36+
builder.AppendSwitch(@switch, type);
37+
}
38+
return builder;
39+
}
40+
}

0 commit comments

Comments
 (0)