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

Set analysis level to latest-recommended #1816

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ dotnet_diagnostic.IDE0017.severity = warning
# CA1001: Types that own disposable fields should be disposable
dotnet_diagnostic.CA1001.severity = warning

# CA1510: Use ArgumentNullException throw helper
dotnet_diagnostic.CA1510.severity = none

# CA1806: Do not ignore method results
dotnet_diagnostic.CA1806.severity = warning

Expand Down
3 changes: 2 additions & 1 deletion eng/Analyzers.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>preview</AnalysisLevel>
<!-- Use recommended ruleset of the preview SDK installed -->
<AnalysisLevel>latest-recommended</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

Expand Down
11 changes: 8 additions & 3 deletions samples/FxExtensibility/AssertEx.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;
using System.Globalization;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MSTest.Extensibility.Samples;

[SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Justification = "This is only some sample type")]
public static class AssertEx
{
private static AssertIs s_assertIs;
Expand All @@ -26,9 +30,10 @@ public static bool IsOfType<T>(this Assert assert, object obj)

throw new AssertFailedException(
string.Format(
"Expected object of type {0} but found object of type {1}",
typeof(T),
obj ?? obj.GetType()));
CultureInfo.InvariantCulture,
"Expected object of type {0} but found object of type {1}",
typeof(T),
obj ?? obj.GetType()));
}

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions samples/FxExtensibility/AssertIs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Globalization;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MSTest.Extensibility.Samples;
Expand All @@ -17,14 +19,16 @@ public class AssertIs
/// <param name="divisor">Its proclaimed divisor.</param>
/// <returns>True if it is a divisor.</returns>
/// <exception cref="AssertFailedException">If it is not a divisor.</exception>
#pragma warning disable CA1822 // Mark members as static
public bool Divisor(int number, int divisor)
#pragma warning restore CA1822 // Mark members as static
{
if (number % divisor == 0)
{
return true;
}

throw new AssertFailedException(string.Format("{0} is not a divisor of {1}", divisor, number));
throw new AssertFailedException(string.Format(CultureInfo.InvariantCulture, "{0} is not a divisor of {1}", divisor, number));
}

/// <summary>
Expand All @@ -33,13 +37,15 @@ public bool Divisor(int number, int divisor)
/// <param name="number">The number.</param>
/// <returns>True if it is positive.</returns>
/// <exception cref="AssertFailedException">If the number is not positive.</exception>
#pragma warning disable CA1822 // Mark members as static
public bool Positive(int number)
#pragma warning restore CA1822 // Mark members as static
{
if (number > 0)
{
return true;
}

throw new AssertFailedException(string.Format("{0} is not positive", number));
throw new AssertFailedException(string.Format(CultureInfo.InvariantCulture, "{0} is not positive", number));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ internal virtual TypeEnumerator GetTypeEnumerator(Type type, string assemblyFile
return new TypeEnumerator(type, assemblyFileName, ReflectHelper, typeValidator, testMethodValidator, testIdGenerationStrategy);
}

private IEnumerable<UnitTestElement> DiscoverTestsInType(string assemblyFileName, string? runSettingsXml, Type type,
private List<UnitTestElement> DiscoverTestsInType(string assemblyFileName, string? runSettingsXml, Type type,
List<string> warningMessages, bool discoverInternals, TestDataSourceDiscoveryOption discoveryOption,
TestIdGenerationStrategy testIdGenerationStrategy)
{
Expand Down Expand Up @@ -392,7 +392,7 @@ private static bool ProcessTestDataSourceTests(UnitTestElement test, MethodInfo
&& testDisplayNameFirstSeen.TryGetValue(discoveredTest.DisplayName!, out var firstIndexSeen))
{
var warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute_DuplicateDisplayName, firstIndexSeen, index, discoveredTest.DisplayName);
warning = string.Format(CultureInfo.CurrentUICulture, Resource.CannotExpandIDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, warning);
warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, warning);
PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning($"DynamicDataEnumerator: {warning}");

// Duplicated display name so bail out. Caller will handle adding the original test.
Expand All @@ -410,7 +410,7 @@ private static bool ProcessTestDataSourceTests(UnitTestElement test, MethodInfo
var warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute_CannotSerialize, index, discoveredTest.DisplayName);
warning += Environment.NewLine;
warning += ex.ToString();
warning = string.Format(CultureInfo.CurrentUICulture, Resource.CannotExpandIDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, warning);
warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, warning);
PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning($"DynamicDataEnumerator: {warning}");

// Serialization failed for the type, bail out. Caller will handle adding the original test.
Expand Down
7 changes: 4 additions & 3 deletions src/Adapter/MSTest.TestAdapter/Discovery/TypeEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
Expand Down Expand Up @@ -138,7 +139,7 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT

var testMethod = new TestMethod(method, method.Name, _type.FullName!, _assemblyFilePath, isAsync, _testIdGenerationStrategy);

if (!string.Equals(method.DeclaringType!.FullName, _type.FullName))
if (!string.Equals(method.DeclaringType!.FullName, _type.FullName, StringComparison.Ordinal))
{
testMethod.DeclaringClassFullName = method.DeclaringType.FullName;
}
Expand Down Expand Up @@ -193,9 +194,9 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT
}

var workItemAttributes = _reflectHelper.GetCustomAttributes<WorkItemAttribute>(method);
if (workItemAttributes.Any())
if (workItemAttributes.Length != 0)
{
testElement.WorkItemIds = workItemAttributes.Select(x => x.Id.ToString()).ToArray();
testElement.WorkItemIds = workItemAttributes.Select(x => x.Id.ToString(CultureInfo.InvariantCulture)).ToArray();
}

// get DisplayName from TestMethodAttribute
Expand Down
3 changes: 3 additions & 0 deletions src/Adapter/MSTest.TestAdapter/Execution/TestAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ public void RunAssemblyInitialize(TestContext testContext)

if (testContext == null)
{
// TODO: This exception should be of type ArgumentNullException
#pragma warning disable CA2201 // Do not raise reserved exception types
throw new NullReferenceException(Resource.TestContextIsNull);
#pragma warning restore CA2201 // Do not raise reserved exception types
}

// If assembly initialization is not done, then do it.
Expand Down
5 changes: 4 additions & 1 deletion src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public bool HasExecutableCleanupMethod
}

// Otherwise, if any base cleanups were pushed to the stack we need to run them
if (BaseClassCleanupMethodsStack.Any())
if (BaseClassCleanupMethodsStack.Count != 0)
{
return true;
}
Expand Down Expand Up @@ -239,7 +239,10 @@ public void RunClassInitialize(TestContext testContext)

if (testContext == null)
{
// TODO: Change this exception type to ArgumentNullException
#pragma warning disable CA2201 // Do not raise reserved exception types
throw new NullReferenceException(Resource.TestContextIsNull);
#pragma warning restore CA2201 // Do not raise reserved exception types
}

MethodInfo? initializeMethod = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private void ExecuteTestsWithTestRunner(
/// <param name="tcmProperties">Tcm properties.</param>
/// <param name="sourceLevelParameters">Source level parameters.</param>
/// <returns>Test context properties.</returns>
private static IDictionary<string, object?> GetTestContextProperties(
private static Dictionary<string, object?> GetTestContextProperties(
IDictionary<TestProperty, object?> tcmProperties,
IDictionary<string, object> sourceLevelParameters)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/MSTest.TestAdapter/Execution/TestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ private static Exception GetRealException(Exception ex)
/// <param name="className">The class name.</param>
/// <param name="methodName">The method name.</param>
/// <returns>Test framework exception with details.</returns>
private static Exception HandleMethodException(Exception ex, string className, string methodName)
private static TestFailedException HandleMethodException(Exception ex, string className, string methodName)
{
DebugEx.Assert(ex != null, "exception should not be null.");

Expand Down
13 changes: 8 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Execution/TestMethodRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ internal UnitTestResult[] Execute()
}
catch (TestFailedException ex)
{
result = new[] { new UnitTestResult(ex) };
result = [new UnitTestResult(ex)];
}
catch (Exception ex)
{
if (result == null || result.Length == 0)
{
result = new[] { new UnitTestResult() };
result = [new UnitTestResult()];
}

var newResult = new UnitTestResult(new TestFailedException(UnitTestOutcome.Error, ex.TryGetMessage(), ex.TryGetStackTraceInformation()))
Expand Down Expand Up @@ -390,13 +390,16 @@ private TestResult[] ExecuteTest(TestMethodInfo testMethodInfo)
}
catch (Exception ex)
{
return new[]
{
return
[
new TestResult()
{
// TODO: We need to change the exception type to more specific one.
#pragma warning disable CA2201 // Do not raise reserved exception types
TestFailureException = new Exception(string.Format(CultureInfo.CurrentCulture, Resource.UTA_ExecuteThrewException, ex?.Message, ex?.StackTrace), ex),
#pragma warning restore CA2201 // Do not raise reserved exception types
},
};
];
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class TypeCache : MarshalByRefObject
/// <summary>
/// Predefined test Attribute names.
/// </summary>
private static readonly string[] PredefinedNames = new string[] { "Priority", "TestCategory", "Owner" };
private static readonly string[] PredefinedNames = ["Priority", "TestCategory", "Owner"];

/// <summary>
/// Helper for reflection API's.
Expand Down Expand Up @@ -697,8 +697,8 @@ private MethodInfo GetMethodInfoForTestMethod(TestMethod testMethod, TestClassIn
testMethodInfo =
Array.Find(
methodsInClass,
method => method.Name.Equals(testMethod.Name)
&& method.DeclaringType!.FullName!.Equals(testMethod.DeclaringClassFullName)
method => method.Name.Equals(testMethod.Name, StringComparison.Ordinal)
&& method.DeclaringType!.FullName!.Equals(testMethod.DeclaringClassFullName, StringComparison.Ordinal)
&& method.HasCorrectTestMethodSignature(true, discoverInternals));
}
else
Expand All @@ -708,8 +708,8 @@ private MethodInfo GetMethodInfoForTestMethod(TestMethod testMethod, TestClassIn
// Prioritize the former while maintaining previous behavior for the latter.
var className = testClassInfo.ClassType.FullName;
testMethodInfo =
methodsInClass.Where(method => method.Name.Equals(testMethod.Name) && method.HasCorrectTestMethodSignature(true, discoverInternals))
.OrderByDescending(method => method.DeclaringType!.FullName!.Equals(className)).FirstOrDefault();
methodsInClass.Where(method => method.Name.Equals(testMethod.Name, StringComparison.Ordinal) && method.HasCorrectTestMethodSignature(true, discoverInternals))
.OrderByDescending(method => method.DeclaringType!.FullName!.Equals(className, StringComparison.Ordinal)).FirstOrDefault();
}

return testMethodInfo;
Expand Down
16 changes: 8 additions & 8 deletions src/Adapter/MSTest.TestAdapter/Execution/UnitTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ internal UnitTestResult[] RunSingleTest(TestMethod testMethod, IDictionary<strin
catch (TypeInspectionException ex)
{
// Catch any exception thrown while inspecting the test method and return failure.
return new UnitTestResult[] { new(ObjectModel.UnitTestOutcome.Failed, ex.Message) };
return [new(ObjectModel.UnitTestOutcome.Failed, ex.Message)];
}
}

Expand Down Expand Up @@ -234,12 +234,12 @@ private bool IsTestMethodRunnable(
if (testMethodInfo == null)
{
{
notRunnableResult = new UnitTestResult[]
{
notRunnableResult =
[
new(
ObjectModel.UnitTestOutcome.NotFound,
string.Format(CultureInfo.CurrentCulture, Resource.TestNotFound, testMethod.Name)),
};
];
return false;
}
}
Expand All @@ -248,10 +248,10 @@ private bool IsTestMethodRunnable(
if (!testMethodInfo.IsRunnable)
{
{
notRunnableResult = new UnitTestResult[]
{
notRunnableResult =
[
new(ObjectModel.UnitTestOutcome.NotRunnable, testMethodInfo.NotRunnableReason),
};
];
return false;
}
}
Expand All @@ -275,7 +275,7 @@ private bool IsTestMethodRunnable(
if (isIgnoreAttributeOnClass || isIgnoreAttributeOnMethod)
{
{
notRunnableResult = new[] { new UnitTestResult(ObjectModel.UnitTestOutcome.Ignored, ignoreMessage) };
notRunnableResult = [new UnitTestResult(ObjectModel.UnitTestOutcome.Ignored, ignoreMessage)];
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -57,7 +58,7 @@ internal static string GetTestName(this TestCase testCase, string? testClassName
var fullyQualifiedName = testCase.FullyQualifiedName;

// Not using Replace because there can be multiple instances of that string.
var name = fullyQualifiedName.StartsWith($"{testClassName}.")
var name = fullyQualifiedName.StartsWith($"{testClassName}.", StringComparison.Ordinal)
? fullyQualifiedName.Remove(0, $"{testClassName}.".Length)
: fullyQualifiedName;

Expand Down
8 changes: 4 additions & 4 deletions src/Adapter/MSTest.TestAdapter/Helpers/ReflectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public virtual bool IsAttributeDefined<TAttribute>(MemberInfo memberInfo, bool i
{
// If we could not obtain all attributes from cache, just get the one we need.
var specificAttributes = GetCustomAttributes<TAttribute>(memberInfo, inherit);
return specificAttributes.Any(a => string.Equals(a.GetType().AssemblyQualifiedName, requiredAttributeQualifiedName));
return specificAttributes.Any(a => string.Equals(a.GetType().AssemblyQualifiedName, requiredAttributeQualifiedName, StringComparison.Ordinal));
}

return attributes.ContainsKey(requiredAttributeQualifiedName);
Expand Down Expand Up @@ -336,8 +336,8 @@ internal virtual string[] GetCategories(MemberInfo categoryAttributeProvider, Ty
/// <param name="owningType">The type that owns <paramref name="testMethod"/>.</param>
/// <returns>True if test method should not run in parallel.</returns>
internal bool IsDoNotParallelizeSet(MemberInfo testMethod, Type owningType)
=> GetCustomAttributes<DoNotParallelizeAttribute>(testMethod).Any()
|| GetCustomAttributes<DoNotParallelizeAttribute>(owningType.GetTypeInfo()).Any();
=> GetCustomAttributes<DoNotParallelizeAttribute>(testMethod).Length != 0
|| GetCustomAttributes<DoNotParallelizeAttribute>(owningType.GetTypeInfo()).Length != 0;

/// <summary>
/// Get the parallelization behavior for a test assembly.
Expand All @@ -346,7 +346,7 @@ internal bool IsDoNotParallelizeSet(MemberInfo testMethod, Type owningType)
/// <returns>True if test assembly should not run in parallel.</returns>
internal static bool IsDoNotParallelizeSet(Assembly assembly)
=> PlatformServiceProvider.Instance.ReflectionOperations.GetCustomAttributes(assembly, typeof(DoNotParallelizeAttribute))
!.Any(); // TODO: Investigate if we rely on NRE
!.Length != 0; // TODO: Investigate if we rely on NRE

/// <summary>
/// Gets the class cleanup lifecycle set on an assembly.
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/MSTest.TestAdapter/ObjectModel/TestMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public sealed class TestMethod : ITestMethod
public const int TotalHierarchyLevels = HierarchyConstants.Levels.TotalLevelCount;

private readonly ReadOnlyCollection<string?> _hierarchy;
private string? _declaringClassFullName = null;
private string? _declaringAssemblyName = null;
private string? _declaringClassFullName;
private string? _declaringAssemblyName;

public TestMethod(string name, string fullClassName, string assemblyName, bool isAsync)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/MSTest.TestAdapter/TestMethodFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ internal TestProperty PropertyProvider(string propertyName)
try
{
// GetTestCaseFilter is present in DiscoveryContext but not in IDiscoveryContext interface.
MethodInfo? methodGetTestCaseFilter = context.GetType().GetRuntimeMethod("GetTestCaseFilter", new[] { typeof(IEnumerable<string>), typeof(Func<string, TestProperty>) });
MethodInfo? methodGetTestCaseFilter = context.GetType().GetRuntimeMethod("GetTestCaseFilter", [typeof(IEnumerable<string>), typeof(Func<string, TestProperty>)]);
return (ITestCaseFilterExpression?)methodGetTestCaseFilter?.Invoke(context, new object[] { _supportedProperties.Keys, (Func<string, TestProperty>)PropertyProvider });
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MSTestExecutor : ITestExecutor
/// <summary>
/// Token for canceling the test run.
/// </summary>
private TestRunCancellationToken? _cancellationToken = null;
private TestRunCancellationToken? _cancellationToken;

/// <summary>
/// Initializes a new instance of the <see cref="MSTestExecutor"/> class.
Expand Down
Loading