/
XUnit2Runner.cs
155 lines (134 loc) · 5.43 KB
/
XUnit2Runner.cs
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
using System;
using System.Linq;
using Cake.Core;
using Cake.Core.IO;
using Cake.Core.Tooling;
namespace Cake.Common.Tools.XUnit
{
/// <summary>
/// The xUnit.net v2 test runner.
/// </summary>
public sealed class XUnit2Runner : Tool<XUnit2Settings>
{
private readonly ICakeEnvironment _environment;
/// <summary>
/// Initializes a new instance of the <see cref="XUnit2Runner" /> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="globber">The globber.</param>
/// <param name="runner">The runner.</param>
public XUnit2Runner(IFileSystem fileSystem, ICakeEnvironment environment, IGlobber globber, IProcessRunner runner)
: base(fileSystem, environment, runner, globber)
{
_environment = environment;
}
/// <summary>
/// Runs the tests in the specified assembly.
/// </summary>
/// <param name="assemblyPath">The assembly path.</param>
/// <param name="settings">The settings.</param>
public void Run(FilePath assemblyPath, XUnit2Settings settings)
{
if (assemblyPath == null)
{
throw new ArgumentNullException("assemblyPath");
}
if (settings == null)
{
throw new ArgumentNullException("settings");
}
// Make sure we got output directory set when generating reports.
if (settings.OutputDirectory == null || string.IsNullOrWhiteSpace(settings.OutputDirectory.FullPath))
{
if (settings.HtmlReport)
{
throw new CakeException("Cannot generate HTML report when no output directory has been set.");
}
if (settings.XmlReport || settings.XmlReportV1)
{
throw new CakeException("Cannot generate XML report when no output directory has been set.");
}
}
Run(settings, GetArguments(assemblyPath, settings));
}
private ProcessArgumentBuilder GetArguments(FilePath assemblyPath, XUnit2Settings settings)
{
var builder = new ProcessArgumentBuilder();
// Get the absolute path to the assembly.
assemblyPath = assemblyPath.MakeAbsolute(_environment);
// Add the assembly to build.
builder.AppendQuoted(assemblyPath.FullPath);
// No shadow copy?
if (!settings.ShadowCopy)
{
builder.Append("-noshadow");
}
// No app domain?
if (settings.NoAppDomain)
{
builder.Append("-noappdomain");
}
// Generate HTML report?
if (settings.HtmlReport)
{
var assemblyFilename = assemblyPath.GetFilename().AppendExtension(".html");
var outputPath = settings.OutputDirectory.MakeAbsolute(_environment).GetFilePath(assemblyFilename);
builder.Append("-html");
builder.AppendQuoted(outputPath.FullPath);
}
// Generate XML report?
if (settings.XmlReport || settings.XmlReportV1)
{
var assemblyFilename = assemblyPath.GetFilename().AppendExtension(".xml");
var outputPath = settings.OutputDirectory.MakeAbsolute(_environment).GetFilePath(assemblyFilename);
builder.Append(settings.XmlReportV1 ? "-xmlv1" : "-xml");
builder.AppendQuoted(outputPath.FullPath);
}
// parallelize test execution?
if (settings.Parallelism != ParallelismOption.None)
{
builder.Append("-parallel " + settings.Parallelism.ToString().ToLowerInvariant());
}
// max thread count for collection parallelization
if (settings.MaxThreads.HasValue)
{
if (settings.MaxThreads.Value == 0)
{
builder.Append("-maxthreads unlimited");
}
else
{
builder.Append("-maxthreads " + settings.MaxThreads.Value);
}
}
foreach (var trait in settings.TraitsToInclude
.SelectMany(pair => pair.Value.Select(v => new { Name = pair.Key, Value = v })))
{
builder.Append("-trait \"{0}={1}\"", trait.Name, trait.Value);
}
foreach (var trait in settings.TraitsToExclude
.SelectMany(pair => pair.Value.Select(v => new { Name = pair.Key, Value = v })))
{
builder.Append("-notrait \"{0}={1}\"", trait.Name, trait.Value);
}
return builder;
}
/// <summary>
/// Gets the name of the tool.
/// </summary>
/// <returns>The name of the tool.</returns>
protected override string GetToolName()
{
return "xUnit.net (v2)";
}
/// <summary>
/// Gets the possible names of the tool executable.
/// </summary>
/// <returns>The tool executable name.</returns>
protected override System.Collections.Generic.IEnumerable<string> GetToolExecutableNames()
{
return new[] { "xunit.console.exe" };
}
}
}