From b17ba162c4aa1254ff1790cc157fd42f9973e443 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 8 Nov 2024 21:21:46 +1100 Subject: [PATCH] Move off validate arg from VSTest ObjectModel (#4019) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amaury Levé --- Directory.Packages.props | 2 +- src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj | 1 - src/Adapter/MSTest.TestAdapter/MSTestSettings.cs | 3 +-- .../MSTest.TestAdapter/VSTestAdapter/MSTestDiscoverer.cs | 6 +++--- .../MSTest.TestAdapter/VSTestAdapter/MSTestExecutor.cs | 8 ++++---- .../MSTestAdapter.PlatformServices.csproj | 1 - .../Services/MSTestAdapterSettings.cs | 4 ++-- .../Services/SettingsProvider.cs | 5 +---- .../Utilities/DeploymentUtilityBase.cs | 4 ++-- .../ObjectModel/Condition.cs | 2 +- .../ObjectModel/FastFilter.cs | 5 ++--- .../ObjectModel/FilterExpression.cs | 6 +++--- .../ObjectModel/FilterExpressionWrapper.cs | 4 ++-- .../ObjectModel/TestCaseFilterExpression.cs | 4 ++-- .../MSTest.IntegrationTests.csproj | 1 + .../Utilities/TestCaseFilterFactory.cs | 8 ++++---- 16 files changed, 29 insertions(+), 35 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 17313de04f..47f6a9a321 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -43,7 +43,7 @@ - + diff --git a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj index b7e241b8fd..7eda3660e6 100644 --- a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj +++ b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj @@ -53,7 +53,6 @@ - diff --git a/src/Adapter/MSTest.TestAdapter/MSTestSettings.cs b/src/Adapter/MSTest.TestAdapter/MSTestSettings.cs index 7e0a3586f3..6aa0c4c10f 100644 --- a/src/Adapter/MSTest.TestAdapter/MSTestSettings.cs +++ b/src/Adapter/MSTest.TestAdapter/MSTestSettings.cs @@ -9,7 +9,6 @@ using Microsoft.Testing.Platform.Configurations; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices; -using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; @@ -401,7 +400,7 @@ internal static void Reset() /// An instance of the class. private static MSTestSettings ToSettings(XmlReader reader, IMessageLogger? logger) { - ValidateArg.NotNull(reader, "reader"); + Guard.NotNull(reader); // Expected format of the xml is: - // diff --git a/src/Adapter/MSTest.TestAdapter/VSTestAdapter/MSTestDiscoverer.cs b/src/Adapter/MSTest.TestAdapter/VSTestAdapter/MSTestDiscoverer.cs index 061beb1f1e..47a01bf4f6 100644 --- a/src/Adapter/MSTest.TestAdapter/VSTestAdapter/MSTestDiscoverer.cs +++ b/src/Adapter/MSTest.TestAdapter/VSTestAdapter/MSTestDiscoverer.cs @@ -31,9 +31,9 @@ public class MSTestDiscoverer : ITestDiscoverer internal static void DiscoverTests(IEnumerable sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink, IConfiguration? configuration) { - ValidateArg.NotNull(sources, "sources"); - ValidateArg.NotNull(logger, "logger"); - ValidateArg.NotNull(discoverySink, "discoverySink"); + Guard.NotNull(sources); + Guard.NotNull(logger); + Guard.NotNull(discoverySink); if (MSTestDiscovererHelpers.InitializeDiscovery(sources, discoveryContext, logger, configuration)) { diff --git a/src/Adapter/MSTest.TestAdapter/VSTestAdapter/MSTestExecutor.cs b/src/Adapter/MSTest.TestAdapter/VSTestAdapter/MSTestExecutor.cs index abce9402ed..fcad29af3a 100644 --- a/src/Adapter/MSTest.TestAdapter/VSTestAdapter/MSTestExecutor.cs +++ b/src/Adapter/MSTest.TestAdapter/VSTestAdapter/MSTestExecutor.cs @@ -51,8 +51,8 @@ internal MSTestExecutor(CancellationToken cancellationToken) internal void RunTests(IEnumerable? tests, IRunContext? runContext, IFrameworkHandle? frameworkHandle, IConfiguration? configuration) { PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("MSTestExecutor.RunTests: Running tests from testcases."); - ValidateArg.NotNull(frameworkHandle, "frameworkHandle"); - ValidateArg.NotNullOrEmpty(tests, "tests"); + Guard.NotNull(frameworkHandle); + Guard.NotNullOrEmpty(tests); if (!MSTestDiscovererHelpers.InitializeDiscovery(from test in tests select test.Source, runContext, frameworkHandle, configuration)) { @@ -65,8 +65,8 @@ internal void RunTests(IEnumerable? tests, IRunContext? runContext, IF internal void RunTests(IEnumerable? sources, IRunContext? runContext, IFrameworkHandle? frameworkHandle, IConfiguration? configuration) { PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("MSTestExecutor.RunTests: Running tests from sources."); - ValidateArg.NotNull(frameworkHandle, "frameworkHandle"); - ValidateArg.NotNullOrEmpty(sources, "sources"); + Guard.NotNull(frameworkHandle); + Guard.NotNullOrEmpty(sources); if (!MSTestDiscovererHelpers.InitializeDiscovery(sources, runContext, frameworkHandle, configuration)) { return; diff --git a/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj b/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj index bdeecf4929..7752d8d63a 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj +++ b/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj @@ -35,7 +35,6 @@ - diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Services/MSTestAdapterSettings.cs b/src/Adapter/MSTestAdapter.PlatformServices/Services/MSTestAdapterSettings.cs index b873e98279..9ceb4b58d9 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Services/MSTestAdapterSettings.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Services/MSTestAdapterSettings.cs @@ -55,7 +55,7 @@ public MSTestAdapterSettings() /// An instance of the class. public static MSTestAdapterSettings ToSettings(XmlReader reader) { - ValidateArg.NotNull(reader, "reader"); + Guard.NotNull(reader); // Expected format of the xml is: - // @@ -317,7 +317,7 @@ public List GetDirectoryListWithRecursiveProperty(string private void ReadAssemblyResolutionPath(XmlReader reader) { - ValidateArg.NotNull(reader, "reader"); + Guard.NotNull(reader); // Expected format of the xml is: - // diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Services/SettingsProvider.cs b/src/Adapter/MSTestAdapter.PlatformServices/Services/SettingsProvider.cs index 4f380bab85..1282b1b418 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Services/SettingsProvider.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Services/SettingsProvider.cs @@ -5,9 +5,6 @@ using Microsoft.Testing.Platform.Configurations; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; -#if !WINDOWS_UWP -using Microsoft.VisualStudio.TestPlatform.ObjectModel; -#endif namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices; @@ -57,7 +54,7 @@ internal static void Load(IConfiguration configuration) public void Load(XmlReader reader) { #if !WINDOWS_UWP - ValidateArg.NotNull(reader, "reader"); + Guard.NotNull(reader); s_settings = MSTestAdapterSettings.ToSettings(reader); #endif } diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/DeploymentUtilityBase.cs b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/DeploymentUtilityBase.cs index 2c4d4951d1..0fbcabc78e 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/DeploymentUtilityBase.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/DeploymentUtilityBase.cs @@ -145,7 +145,7 @@ protected IEnumerable Deploy(IList deploymentItems, stri // Copy the deployment items. (As deployment item can correspond to directories as well, so each deployment item may map to n files) foreach (DeploymentItem deploymentItem in deploymentItems) { - ValidateArg.NotNull(deploymentItem, "deploymentItem should not be null."); + Guard.NotNull(deploymentItem); // Validate the output directory. if (!IsOutputDirectoryValid(deploymentItem, deploymentDirectory, warnings)) @@ -399,7 +399,7 @@ private static void LogWarnings(ITestExecutionRecorder testExecutionRecorder, IE private bool Deploy(string source, IRunContext? runContext, ITestExecutionRecorder testExecutionRecorder, IList deploymentItems, TestRunDirectories runDirectories) { - ValidateArg.NotNull(runDirectories, "runDirectories"); + Guard.NotNull(runDirectories); if (EqtTrace.IsInfoEnabled) { EqtTrace.Info("MSTestExecutor: Found that deployment items for source {0} are: ", source); diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/Condition.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/Condition.cs index 467e5b550f..3cdfd97863 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/Condition.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/Condition.cs @@ -78,7 +78,7 @@ internal Condition(string name, Operation operation, string value) /// internal bool Evaluate(Func propertyValueProvider) { - ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider)); + Guard.NotNull(propertyValueProvider); bool result = false; string[]? multiValue = GetPropertyValue(propertyValueProvider); switch (Operation) diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FastFilter.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FastFilter.cs index d87691cda2..6e596db037 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FastFilter.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FastFilter.cs @@ -8,7 +8,6 @@ using System.Text.RegularExpressions; using Microsoft.Testing.Platform; -using Microsoft.VisualStudio.TestPlatform.ObjectModel; namespace Microsoft.Testing.Extensions.VSTestBridge.ObjectModel; @@ -17,7 +16,7 @@ internal sealed class FastFilter { internal FastFilter(ImmutableDictionary> filterProperties, Operation filterOperation, Operator filterOperator) { - ValidateArg.NotNullOrEmpty(filterProperties, nameof(filterProperties)); + Guard.NotNullOrEmpty(filterProperties); FilterProperties = filterProperties; @@ -45,7 +44,7 @@ internal FastFilter(ImmutableDictionary> filterProperties, internal bool Evaluate(Func propertyValueProvider) { - ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider)); + Guard.NotNull(propertyValueProvider); bool matched = false; foreach (string name in FilterProperties.Keys) diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FilterExpression.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FilterExpression.cs index c8a7a12eba..53531b3d83 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FilterExpression.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FilterExpression.cs @@ -154,7 +154,7 @@ private static void ProcessOperator(Stack filterStack, Operato /// internal static FilterExpression Parse(string filterString, out FastFilter? fastFilter) { - ValidateArg.NotNull(filterString, nameof(filterString)); + Guard.NotNull(filterString); // Below parsing doesn't error out on pattern (), so explicitly search for that (empty parenthesis). Match invalidInput = GetEmptyParenthesisPattern().Match(filterString); @@ -318,7 +318,7 @@ private T IterateFilterExpression(Func, T> getNode /// True if evaluation is successful. internal bool Evaluate(Func propertyValueProvider) { - ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider)); + Guard.NotNull(propertyValueProvider); return IterateFilterExpression((current, result) => { @@ -341,7 +341,7 @@ internal bool Evaluate(Func propertyValueProvider) internal static IEnumerable TokenizeFilterExpressionString(string str) { - ValidateArg.NotNull(str, nameof(str)); + Guard.NotNull(str); return TokenizeFilterExpressionStringHelper(str); static IEnumerable TokenizeFilterExpressionStringHelper(string s) diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FilterExpressionWrapper.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FilterExpressionWrapper.cs index 5196f44f71..8e2b757025 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FilterExpressionWrapper.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FilterExpressionWrapper.cs @@ -34,7 +34,7 @@ internal class FilterExpressionWrapper /// public FilterExpressionWrapper(string filterString, FilterOptions? options) { - ValidateArg.NotNullOrEmpty(filterString, nameof(filterString)); + Guard.NotNullOrEmpty(filterString); FilterString = filterString; FilterOptions = options; @@ -112,7 +112,7 @@ public FilterExpressionWrapper(string filterString) /// public bool Evaluate(Func propertyValueProvider) { - ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider)); + Guard.NotNull(propertyValueProvider); return UseFastFilter ? _fastFilter.Evaluate(propertyValueProvider) diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/TestCaseFilterExpression.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/TestCaseFilterExpression.cs index 0933cf3eb0..191a56d01f 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/TestCaseFilterExpression.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/TestCaseFilterExpression.cs @@ -56,8 +56,8 @@ public TestCaseFilterExpression(FilterExpressionWrapper filterWrapper) /// public bool MatchTestCase(TestCase testCase, Func propertyValueProvider) { - ValidateArg.NotNull(testCase, nameof(testCase)); - ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider)); + Guard.NotNull(testCase); + Guard.NotNull(propertyValueProvider); return _validForMatch && _filterWrapper.Evaluate(propertyValueProvider); } diff --git a/test/IntegrationTests/MSTest.IntegrationTests/MSTest.IntegrationTests.csproj b/test/IntegrationTests/MSTest.IntegrationTests/MSTest.IntegrationTests.csproj index f3d07dbd58..43e0e78f28 100644 --- a/test/IntegrationTests/MSTest.IntegrationTests/MSTest.IntegrationTests.csproj +++ b/test/IntegrationTests/MSTest.IntegrationTests/MSTest.IntegrationTests.csproj @@ -14,6 +14,7 @@ + diff --git a/test/IntegrationTests/MSTest.IntegrationTests/Utilities/TestCaseFilterFactory.cs b/test/IntegrationTests/MSTest.IntegrationTests/Utilities/TestCaseFilterFactory.cs index 8cc6a6ebd7..a662bb0f00 100644 --- a/test/IntegrationTests/MSTest.IntegrationTests/Utilities/TestCaseFilterFactory.cs +++ b/test/IntegrationTests/MSTest.IntegrationTests/Utilities/TestCaseFilterFactory.cs @@ -20,7 +20,7 @@ internal static class TestCaseFilterFactory public static ITestCaseFilterExpression ParseTestFilter(string filterString) { - ValidateArg.NotNullOrEmpty(filterString, nameof(filterString)); + Guard.NotNullOrEmpty(filterString); if (Regex.IsMatch(filterString, @"\(\s*\)")) { throw new FormatException($"Invalid filter, empty parenthesis: {filterString}"); @@ -119,7 +119,7 @@ public TestFilterExpression(string filter, Func, bool> expr private static void MergeExpression(Stack, bool>>> exp, Operator op) { - ValidateArg.NotNull(exp, nameof(exp)); + Guard.NotNull(exp); if (op is not Operator.And and not Operator.Or) { throw new ArgumentException($"Unexpected operator: {op}", nameof(op)); @@ -190,7 +190,7 @@ private static IEnumerable TokenizeFilter(string filterString) private static IEnumerable TokenizeCondition(string conditionString) { - ValidateArg.NotNullOrEmpty(conditionString, nameof(conditionString)); + Guard.NotNullOrEmpty(conditionString); var token = new StringBuilder(conditionString.Length); for (int i = 0; i < conditionString.Length; i++) @@ -286,7 +286,7 @@ private static bool ContainsComparer(string[] values, string value) private static Expression, bool>> ConditionExpression(string conditionString) { - ValidateArg.NotNull(conditionString, nameof(conditionString)); + Guard.NotNull(conditionString); string[] condition = TokenizeCondition(conditionString).ToArray();