Skip to content

Commit

Permalink
Take TestCaseFilter from runsettings (#2356)
Browse files Browse the repository at this point in the history
Take TestCaseFilter from runsettings and merge it with the one provided on console
  • Loading branch information
nohwnd authored Mar 25, 2020
1 parent 2f026a9 commit d10bcbb
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 8 deletions.
7 changes: 5 additions & 2 deletions scripts/test.ps1
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
{
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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";
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
Original file line number Diff line number Diff line change
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

0 comments on commit d10bcbb

Please sign in to comment.