Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take TestCaseFilter from runsettings #2356

Merged
merged 15 commits into from Mar 25, 2020
7 changes: 5 additions & 2 deletions scripts/test.ps1
Expand Up @@ -82,8 +82,11 @@ Get-ChildItem "Env:\dotnet_*"
& "$env:DOTNET_ROOT\dotnet.exe" --info

"`n`n---- x86 dotnet"
& "${env:DOTNET_ROOT(x86)}\dotnet.exe" --info

# avoid erroring out because we don't have the sdk for x86 that global.json requires
try {
& "${env:DOTNET_ROOT(x86)}\dotnet.exe" --info 2> $null
} catch {}


# Dotnet build doesn't support --packages yet. See https://github.com/dotnet/cli/issues/2712
$env:NUGET_PACKAGES = $env:TP_PACKAGES_DIR
Expand Down
Expand Up @@ -488,6 +488,10 @@ public bool ResultsDirectorySet
public bool CollectSourceInformationSet { get; private set; } = false;

/// <summary>
/// Default filter to use to filter tests
/// </summary>
public string TestCaseFilter { get; private set; }

/// Path to dotnet executable to be used to invoke testhost.dll. Specifying this will skip looking up testhost.exe and will force usage of the testhost.dll.
/// </summary>
public string DotnetHostPath { get; private set; }
Expand Down Expand Up @@ -576,6 +580,13 @@ public override XmlElement ToXml()
root.AppendChild(targetDevice);
}

if (!string.IsNullOrEmpty(this.TestCaseFilter))
{
XmlElement testCaseFilter = doc.CreateElement(nameof(TestCaseFilter));
testCaseFilter.InnerXml = this.TestCaseFilter;
root.AppendChild(testCaseFilter);
}

if (!string.IsNullOrEmpty(this.DotnetHostPath))
{
XmlElement dotnetHostPath = doc.CreateElement(nameof(DotnetHostPath));
Expand Down Expand Up @@ -879,6 +890,11 @@ public static RunConfiguration FromXml(XmlReader reader)
runConfiguration.TargetDevice = reader.ReadElementContentAsString();
break;

case "TestCaseFilter":
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
runConfiguration.TestCaseFilter = reader.ReadElementContentAsString();
break;

case "DotNetHostPath":
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
runConfiguration.DotnetHostPath = reader.ReadElementContentAsString();
Expand Down
6 changes: 6 additions & 0 deletions src/vstest.console/Processors/RunSettingsArgumentProcessor.cs
Expand Up @@ -142,6 +142,12 @@ public void Initialize(string argument)
this.commandLineOptions.InIsolation = true;
this.runSettingsManager.UpdateRunSettingsNode(InIsolationArgumentExecutor.RunSettingsPath, "true");
}

var testCaseFilter = this.runSettingsManager.QueryRunSettingsNode("RunConfiguration.TestCaseFilter");
if (testCaseFilter != null)
{
this.commandLineOptions.TestCaseFilterValue = testCaseFilter;
}
}
catch (XmlException exception)
{
Expand Down
15 changes: 13 additions & 2 deletions src/vstest.console/Processors/TestCaseFilterArgumentProcessor.cs
Expand Up @@ -117,12 +117,23 @@ public TestCaseFilterArgumentExecutor(CommandLineOptions options)
/// <param name="argument">Argument that was provided with the command.</param>
public void Initialize(string argument)
{
if (string.IsNullOrWhiteSpace(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));
}

this.commandLineOptions.TestCaseFilterValue = argument;
if (!hasDefaultFilter)
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
{
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>
Expand Down
5 changes: 4 additions & 1 deletion src/vstest.console/TestPlatformHelpers/TestRequestManager.cs
Expand Up @@ -150,6 +150,7 @@ public void DiscoverTests(DiscoveryRequestPayload discoveryPayload, ITestDiscove

var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runsettings);
var batchSize = runConfiguration.BatchSize;
var testCaseFilterFromRunsettings = runConfiguration.TestCaseFilter;

if (requestData.IsTelemetryOptedIn)
{
Expand All @@ -160,10 +161,12 @@ public void DiscoverTests(DiscoveryRequestPayload discoveryPayload, ITestDiscove
this.LogCommandsTelemetryPoints(requestData);
}



// create discovery request
var criteria = new DiscoveryCriteria(discoveryPayload.Sources, batchSize, this.commandLineOptions.TestStatsEventTimeout, runsettings)
{
TestCaseFilter = this.commandLineOptions.TestCaseFilterValue
TestCaseFilter = this.commandLineOptions.TestCaseFilterValue ?? testCaseFilterFromRunsettings
};

// Make sure to run the run request inside a lock as the below section is not thread-safe
Expand Down
Expand Up @@ -363,6 +363,30 @@ public void InitializeShouldNotSetInIsolataionToTrueIfEnvironmentVariablesNotSpe
Assert.IsNull(this.settingsProvider.QueryRunSettingsNode(InIsolationArgumentExecutor.RunSettingsPath));
}

[TestMethod]
public void InitializeShouldUpdateTestCaseFilterIfProvided()
{
// Arrange.
var fileName = "C:\\temp\\r.runsettings";
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
var filter = "TestCategory=Included";
var settingsXml = $"<RunSettings><RunConfiguration><TestCaseFilter>{filter}</TestCaseFilter></RunConfiguration></RunSettings>";

var executor = new TestableRunSettingsArgumentExecutor(
CommandLineOptions.Instance,
this.settingsProvider,
settingsXml);

// Setup mocks.
var mockFileHelper = new Mock<IFileHelper>();
mockFileHelper.Setup(fh => fh.Exists(It.IsAny<string>())).Returns(true);
executor.FileHelper = mockFileHelper.Object;

// Act.
executor.Initialize(fileName);

// Assert.
Assert.AreEqual(filter, CommandLineOptions.Instance.TestCaseFilterValue);
}
#endregion

#region Testable Implementations
Expand Down
Expand Up @@ -64,13 +64,29 @@ public void ExecutorInitializeWithNullOrEmptyTestCaseFilterShouldThrowCommandLin
}

[TestMethod]
public void ExecutorInitializeWithValidTestCaseFilterShouldAddTestCaseFilterToCommandLineOptions()
public void ExecutorInitializeWithNullOrEmptyTestCaseFilterShouldNotThrowWhenTestFilterWasSpecifiedByPreviousStep()
{
var options = CommandLineOptions.Instance;
options.TestCaseFilterValue = "Test=FilterFromPreviousStep";
TestCaseFilterArgumentExecutor executor = new TestCaseFilterArgumentExecutor(options);

executor.Initialize(null);
}

[TestMethod]
public void ExecutorInitializeWithTestCaseFilterShouldMergeWithTheValueProvidedByPreviousStep()
{
var options = CommandLineOptions.Instance;
var defaultValue = "Test=FilterFromPreviousStep";
options.TestCaseFilterValue = defaultValue;
Assert.AreEqual(defaultValue, options.TestCaseFilterValue);
TestCaseFilterArgumentExecutor executor = new TestCaseFilterArgumentExecutor(options);

executor.Initialize("Debug");
Assert.AreEqual("Debug", options.TestCaseFilterValue);
var value = "Test=NewFilter";
executor.Initialize(value);

var expectedValue = $"({defaultValue})&({value})";
Assert.AreEqual(expectedValue, options.TestCaseFilterValue);
}

[TestMethod]
Expand Down