Repository navigation
Expand file tree
/
Copy pathCollectors.cs
More file actions
196 lines (172 loc) · 7.95 KB
/
Copy pathCollectors.cs
File metadata and controls
196 lines (172 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Toni Solarin-Sodara
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using Coverlet.Tests.Utils;
using Xunit;
namespace Coverlet.Integration.Tests
{
public class TestSDK_17_13_0 : Collectors
{
public TestSDK_17_13_0(ITestOutputHelper output) : base(output)
{
TestSDKVersion = "17.13.0";
}
private protected override void AssertCollectorsInjection(ClonedTemplateProject clonedTemplateProject)
{
// Check out/in process collectors injection
Assert.Contains("[coverlet]", File.ReadAllText(clonedTemplateProject.GetFiles("log.datacollector.*.txt").Single()));
// There is a bug in this SDK version https://github.com/microsoft/vstest/pull/2221
// in-proc coverlet.collector.dll collector with version != 1.0.0.0 won't be loaded
// Assert.Contains("[coverlet]", File.ReadAllText(clonedTemplateProject.GetFiles("log.host.*.txt").Single()));
}
}
public class TestSDK_17_12_0 : Collectors
{
public TestSDK_17_12_0(ITestOutputHelper output) : base(output)
{
TestSDKVersion = "17.12.0";
}
}
public class TestSDK_Preview : Collectors
{
public TestSDK_Preview(ITestOutputHelper output) : base(output)
{
TestSDKVersion = "17.14.0-preview-25107-01";
}
}
public abstract class Collectors : BaseTest
{
private readonly string _buildConfiguration;
private readonly string _buildTargetFramework;
public Collectors(ITestOutputHelper output) : base(output)
{
_buildConfiguration = TestUtils.GetBuildConfigurationString();
_buildTargetFramework = TestUtils.GetAssemblyTargetFramework();
}
protected string? TestSDKVersion { get; set; }
private ClonedTemplateProject PrepareTemplateProject()
{
if (TestSDKVersion is null)
{
throw new ArgumentNullException("Invalid TestSDKVersion");
}
ClonedTemplateProject clonedTemplateProject = CloneTemplateProject(testSDKVersion: TestSDKVersion);
UpdateNugetConfigWithLocalPackageFolder(clonedTemplateProject.ProjectRootPath!);
AddCoverletCollectorsRef(clonedTemplateProject.ProjectRootPath!);
return clonedTemplateProject;
}
private protected virtual void AssertCollectorsInjection(ClonedTemplateProject clonedTemplateProject)
{
// Check out/in process collectors injection
Assert.Contains("[coverlet]Initializing CoverletCoverageDataCollector with configuration:", File.ReadAllText(clonedTemplateProject.GetFiles("log.datacollector.*.txt").Single()));
Assert.Contains("[coverlet]Initialize CoverletInProcDataCollector", File.ReadAllText(clonedTemplateProject.GetFiles("log.host.*.txt").Single()));
}
[Fact]
public void TestVsTest_Test()
{
// This test requires VSTest mode using 'dotnet test' which is only available on .NET 8/9
Assert.SkipWhen(TestUtils.IsNet10OrLater(), "VSTest mode is not available on .NET 10.0 or later");
using ClonedTemplateProject clonedTemplateProject = PrepareTemplateProject();
int result = DotnetCli($"test -c {_buildConfiguration} -f {_buildTargetFramework} \"{clonedTemplateProject.ProjectRootPath}\" --collect:\"XPlat Code Coverage\" --diag:{Path.Combine(clonedTemplateProject.ProjectRootPath, "log.txt")}", out string standardOutput, out string standardError, clonedTemplateProject.ProjectRootPath!);
// We don't have any result to check because tests and code to instrument are in same assembly so we need to pass
// IncludeTestAssembly=true we do it in other test
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
Assert.Contains("Passed!", standardOutput);
AssertCollectorsInjection(clonedTemplateProject);
}
[Fact]
public void TestVsTest_Test_Settings()
{
// This test requires VSTest mode using 'dotnet test' which is only available on .NET 8/9
Assert.SkipWhen(TestUtils.IsNet10OrLater(), "VSTest mode is not available on .NET 10.0 or later");
using ClonedTemplateProject clonedTemplateProject = PrepareTemplateProject();
string runSettingsPath = AddCollectorRunsettingsFile(clonedTemplateProject.ProjectRootPath!);
int result = DotnetCli($"test -c {_buildConfiguration} -f {_buildTargetFramework} \"{clonedTemplateProject.ProjectRootPath}\" --collect:\"XPlat Code Coverage\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(clonedTemplateProject.ProjectRootPath, "log.txt")}", out string standardOutput, out string standardError);
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
Assert.Contains("Passed!", standardOutput);
AssertCoverage(clonedTemplateProject);
AssertCollectorsInjection(clonedTemplateProject);
}
[Fact]
public void TestVsTest_VsTest()
{
using ClonedTemplateProject clonedTemplateProject = PrepareTemplateProject();
string runSettingsPath = AddCollectorRunsettingsFile(clonedTemplateProject.ProjectRootPath!);
int result = DotnetCli($"publish -c {_buildConfiguration} -f {_buildTargetFramework} {clonedTemplateProject.ProjectRootPath}", out string standardOutput, out string standardError);
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
string publishedTestFile = clonedTemplateProject.GetFiles("*" + ClonedTemplateProject.AssemblyName + ".dll").Single(f => f.Contains("publish"));
Assert.NotNull(publishedTestFile);
result = DotnetCli($"vstest \"{publishedTestFile}\" --collect:\"XPlat Code Coverage\" --diag:{Path.Combine(clonedTemplateProject.ProjectRootPath, "log.txt")}", out standardOutput, out standardError);
// We don't have any result to check because tests and code to instrument are in same assembly so we need to pass
// IncludeTestAssembly=true we do it in other test
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
Assert.Contains("Passed!", standardOutput);
AssertCollectorsInjection(clonedTemplateProject);
}
[Fact]
public void TestVsTest_VsTest_Settings()
{
using ClonedTemplateProject clonedTemplateProject = PrepareTemplateProject();
string runSettingsPath = AddCollectorRunsettingsFile(clonedTemplateProject.ProjectRootPath!);
int result = DotnetCli($"publish -c {_buildConfiguration} -f {_buildTargetFramework} \"{clonedTemplateProject.ProjectRootPath}\"", out string standardOutput, out string standardError);
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
string publishedTestFile = clonedTemplateProject.GetFiles("*" + ClonedTemplateProject.AssemblyName + ".dll").Single(f => f.Contains("publish"));
Assert.NotNull(publishedTestFile);
result = DotnetCli($"vstest \"{publishedTestFile}\" --collect:\"XPlat Code Coverage\" --ResultsDirectory:\"{clonedTemplateProject.ProjectRootPath}\" /settings:\"{runSettingsPath}\" --diag:{Path.Combine(clonedTemplateProject.ProjectRootPath, "log.txt")}", out standardOutput, out standardError);
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
Assert.Contains("Passed!", standardOutput);
AssertCoverage(clonedTemplateProject);
AssertCollectorsInjection(clonedTemplateProject);
}
}
}