forked from microsoft/vstest
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestCaseFilterArgumentProcessor.cs
149 lines (122 loc) · 4.81 KB
/
TestCaseFilterArgumentProcessor.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
{
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using Microsoft.VisualStudio.TestPlatform.CommandLine;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
/// <summary>
/// Argument Executor for the "/TestCaseFilter" command line argument.
/// </summary>
internal class TestCaseFilterArgumentProcessor : IArgumentProcessor
{
#region Constants
/// <summary>
/// The name of the command line argument that the TestCaseFilterArgumentExecutor handles.
/// </summary>
public const string CommandName = "/TestCaseFilter";
#endregion
private Lazy<IArgumentProcessorCapabilities> metadata;
private Lazy<IArgumentExecutor> executor;
/// <summary>
/// Gets the metadata.
/// </summary>
public Lazy<IArgumentProcessorCapabilities> Metadata
{
get
{
if (this.metadata == null)
{
this.metadata = new Lazy<IArgumentProcessorCapabilities>(() => new TestCaseFilterArgumentProcessorCapabilities());
}
return this.metadata;
}
}
/// <summary>
/// Gets or sets the executor.
/// </summary>
public Lazy<IArgumentExecutor> Executor
{
get
{
if (this.executor == null)
{
this.executor = new Lazy<IArgumentExecutor>(() => new TestCaseFilterArgumentExecutor(CommandLineOptions.Instance));
}
return this.executor;
}
set
{
this.executor = value;
}
}
}
internal class TestCaseFilterArgumentProcessorCapabilities : BaseArgumentProcessorCapabilities
{
public override string CommandName => TestCaseFilterArgumentProcessor.CommandName;
public override bool AllowMultiple => false;
public override bool IsAction => false;
public override ArgumentProcessorPriority Priority => ArgumentProcessorPriority.Normal;
public override string HelpContentResourceName => CommandLineResources.TestCaseFilterArgumentHelp;
public override HelpContentPriority HelpPriority => HelpContentPriority.TestCaseFilterArgumentProcessorHelpPriority;
}
/// <summary>
/// Argument Executor for the "/TestCaseFilter" command line argument.
/// </summary>
internal class TestCaseFilterArgumentExecutor : IArgumentExecutor
{
#region Fields
/// <summary>
/// Used for getting sources.
/// </summary>
private CommandLineOptions commandLineOptions;
#endregion
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="options">
/// The options.
/// </param>
public TestCaseFilterArgumentExecutor(CommandLineOptions options)
{
Contract.Requires(options != null);
this.commandLineOptions = options;
}
#endregion
#region IArgumentExecutor
/// <summary>
/// Initializes with the argument that was provided with the command.
/// </summary>
/// <param name="argument">Argument that was provided with the command.</param>
public void Initialize(string argument)
{
var defaultFilter = this.commandLineOptions.TestCaseFilterValue;
var hasDefaultFilter = !string.IsNullOrWhiteSpace(defaultFilter);
if (!hasDefaultFilter && string.IsNullOrWhiteSpace(argument))
{
throw new CommandLineException(string.Format(CultureInfo.CurrentUICulture, CommandLineResources.TestCaseFilterValueRequired));
}
if (!hasDefaultFilter)
{
this.commandLineOptions.TestCaseFilterValue = argument;
}
else
{
// Merge default filter an provided filter by AND operator to have both the default filter and custom filter applied.
this.commandLineOptions.TestCaseFilterValue = $"({defaultFilter})&({argument})";
}
}
/// <summary>
/// The TestCaseFilter is already set, return success.
/// </summary>
/// <returns> The <see cref="ArgumentProcessorResult"/> Success </returns>
public ArgumentProcessorResult Execute()
{
return ArgumentProcessorResult.Success;
}
#endregion
}
}