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

chore: align analyzers across repos #2776

Merged
merged 5 commits into from
Apr 30, 2024
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.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
340 changes: 263 additions & 77 deletions .editorconfig

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions samples/FxExtensibility/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ public static class AssertEx
/// <returns>True if object is of the given type.</returns>
/// <exception cref="AssertFailedException">If object is not of the given type.</exception>
#pragma warning disable IDE0060 // Remove unused parameter
public static bool IsOfType<T>(this Assert assert, object obj)
#pragma warning restore IDE0060 // Remove unused parameter
{
return obj is T
public static bool IsOfType<T>(this Assert assert, object obj) => obj is T
? true
: throw new AssertFailedException(string.Format(
CultureInfo.InvariantCulture,
"Expected object of type {0} but found object of type {1}",
typeof(T),
obj ?? obj.GetType()));
}

/// <summary>
/// A chain/grouping of assert statements.
Expand Down
12 changes: 2 additions & 10 deletions samples/FxExtensibility/AssertIs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ public class AssertIs
/// <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
{
return number % divisor == 0
public bool Divisor(int number, int divisor) => number % divisor == 0
? true
: throw new AssertFailedException(string.Format(CultureInfo.InvariantCulture, "{0} is not a divisor of {1}", divisor, number));
}

/// <summary>
/// Determines if a number is positive.
Expand All @@ -35,11 +31,7 @@ public bool Divisor(int number, int divisor)
/// <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
{
return number > 0
public bool Positive(int number) => number > 0
? true
: throw new AssertFailedException(string.Format(CultureInfo.InvariantCulture, "{0} is not positive", number));
}
}
4 changes: 2 additions & 2 deletions samples/Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public static async Task<int> Main(string[] args)
// Opt-out telemetry
Environment.SetEnvironmentVariable("DOTNET_CLI_TELEMETRY_OPTOUT", "1");

var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);
ITestApplicationBuilder testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);
testApplicationBuilder.AddMSTest(() => new[] { Assembly.GetEntryAssembly()! });

// Enable Trx
// testApplicationBuilder.AddTrxReportProvider();
using var testApplication = await testApplicationBuilder.BuildAsync();
using ITestApplication testApplication = await testApplicationBuilder.BuildAsync();
return await testApplication.RunAsync();
}
}
67 changes: 32 additions & 35 deletions src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// 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;
Expand Down Expand Up @@ -65,10 +65,7 @@ public AssemblyEnumerator(MSTestSettings settings)
#if NET5_0_OR_GREATER
[Obsolete]
#endif
public override object InitializeLifetimeService()
{
return null!;
}
public override object InitializeLifetimeService() => null!;

/// <summary>
/// Enumerates through all types in the assembly in search of valid test methods.
Expand All @@ -83,14 +80,14 @@ internal ICollection<UnitTestElement> EnumerateAssembly(string assemblyFileName,
var warningMessages = new List<string>();
var tests = new List<UnitTestElement>();

var assembly = PlatformServiceProvider.Instance.FileOperations.LoadAssembly(assemblyFileName, isReflectionOnly: false);
Assembly assembly = PlatformServiceProvider.Instance.FileOperations.LoadAssembly(assemblyFileName, isReflectionOnly: false);

var types = GetTypes(assembly, assemblyFileName, warningMessages);
var discoverInternals = assembly.GetCustomAttribute<DiscoverInternalsAttribute>() != null;
var testIdGenerationStrategy = assembly.GetCustomAttribute<TestIdGenerationStrategyAttribute>()?.Strategy
IReadOnlyList<Type> types = GetTypes(assembly, assemblyFileName, warningMessages);
bool discoverInternals = assembly.GetCustomAttribute<DiscoverInternalsAttribute>() != null;
TestIdGenerationStrategy testIdGenerationStrategy = assembly.GetCustomAttribute<TestIdGenerationStrategyAttribute>()?.Strategy
?? TestIdGenerationStrategy.FullyQualified;

var testDataSourceDiscovery = assembly.GetCustomAttribute<TestDataSourceDiscoveryAttribute>()?.DiscoveryOption
TestDataSourceDiscoveryOption testDataSourceDiscovery = assembly.GetCustomAttribute<TestDataSourceDiscoveryAttribute>()?.DiscoveryOption
#pragma warning disable CS0618 // Type or member is obsolete

// When using legacy strategy, there is no point in trying to "read" data during discovery
Expand All @@ -99,14 +96,14 @@ internal ICollection<UnitTestElement> EnumerateAssembly(string assemblyFileName,
? TestDataSourceDiscoveryOption.DuringExecution
: TestDataSourceDiscoveryOption.DuringDiscovery);
#pragma warning restore CS0618 // Type or member is obsolete
foreach (var type in types)
foreach (Type type in types)
{
if (type == null)
{
continue;
}

var testsInType = DiscoverTestsInType(assemblyFileName, RunSettingsXml, type, warningMessages, discoverInternals,
List<UnitTestElement> testsInType = DiscoverTestsInType(assemblyFileName, RunSettingsXml, type, warningMessages, discoverInternals,
testDataSourceDiscovery, testIdGenerationStrategy);
tests.AddRange(testsInType);
}
Expand Down Expand Up @@ -137,11 +134,11 @@ internal static IReadOnlyList<Type> GetTypes(Assembly assembly, string assemblyF
if (ex.LoaderExceptions != null)
{
// If not able to load all type, log a warning and continue with loaded types.
var message = string.Format(CultureInfo.CurrentCulture, Resource.TypeLoadFailed, assemblyFileName, GetLoadExceptionDetails(ex));
string message = string.Format(CultureInfo.CurrentCulture, Resource.TypeLoadFailed, assemblyFileName, GetLoadExceptionDetails(ex));

warningMessages?.Add(message);

foreach (var loaderEx in ex.LoaderExceptions)
foreach (Exception? loaderEx in ex.LoaderExceptions)
{
PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning("{0}", loaderEx);
}
Expand All @@ -168,10 +165,10 @@ internal static string GetLoadExceptionDetails(ReflectionTypeLoadException ex)
if (ex.LoaderExceptions?.Length > 0)
{
// Loader exceptions can contain duplicates, leave only unique exceptions.
foreach (var loaderException in ex.LoaderExceptions)
foreach (Exception? loaderException in ex.LoaderExceptions)
{
DebugEx.Assert(loaderException != null, "loader exception should not be null.");
var line = string.Format(CultureInfo.CurrentCulture, Resource.EnumeratorLoadTypeErrorFormat, loaderException.GetType(), loaderException.Message);
string line = string.Format(CultureInfo.CurrentCulture, Resource.EnumeratorLoadTypeErrorFormat, loaderException.GetType(), loaderException.Message);
if (!map.ContainsKey(line))
{
map.Add(line, null);
Expand Down Expand Up @@ -208,7 +205,7 @@ internal virtual TypeEnumerator GetTypeEnumerator(Type type, string assemblyFile
List<string> warningMessages, bool discoverInternals, TestDataSourceDiscoveryOption discoveryOption,
TestIdGenerationStrategy testIdGenerationStrategy)
{
var tempSourceLevelParameters = PlatformServiceProvider.Instance.SettingsProvider.GetProperties(assemblyFileName);
IDictionary<string, object> tempSourceLevelParameters = PlatformServiceProvider.Instance.SettingsProvider.GetProperties(assemblyFileName);
tempSourceLevelParameters = RunSettingsUtilities.GetTestRunParameters(runSettingsXml)?.ConcatWithOverwrites(tempSourceLevelParameters)
?? tempSourceLevelParameters
?? new Dictionary<string, object>();
Expand All @@ -220,9 +217,9 @@ internal virtual TypeEnumerator GetTypeEnumerator(Type type, string assemblyFile
try
{
typeFullName = type.FullName;
var testTypeEnumerator = GetTypeEnumerator(type, assemblyFileName, discoverInternals, testIdGenerationStrategy);
var unitTestCases = testTypeEnumerator.Enumerate(out var warningsFromTypeEnumerator);
var typeIgnored = ReflectHelper.IsAttributeDefined<IgnoreAttribute>(type, false);
TypeEnumerator testTypeEnumerator = GetTypeEnumerator(type, assemblyFileName, discoverInternals, testIdGenerationStrategy);
ICollection<UnitTestElement>? unitTestCases = testTypeEnumerator.Enumerate(out ICollection<string>? warningsFromTypeEnumerator);
bool typeIgnored = ReflectHelper.IsAttributeDefined<IgnoreAttribute>(type, false);

if (warningsFromTypeEnumerator != null)
{
Expand All @@ -231,7 +228,7 @@ internal virtual TypeEnumerator GetTypeEnumerator(Type type, string assemblyFile

if (unitTestCases != null)
{
foreach (var test in unitTestCases)
foreach (UnitTestElement test in unitTestCases)
{
if (discoveryOption == TestDataSourceDiscoveryOption.DuringDiscovery)
{
Expand Down Expand Up @@ -269,16 +266,16 @@ private bool DynamicDataAttached(IDictionary<string, object?> sourceLevelParamet
// NOTE: From this place we don't have any path that would let the user write a message on the TestContext and we don't do
// anything with what would be printed anyway so we can simply use a simple StringWriter.
using var writer = new StringWriter();
var testMethod = test.TestMethod;
var testContext = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, sourceLevelParameters);
var testMethodInfo = _typeCache.GetTestMethodInfo(testMethod, testContext, MSTestSettings.CurrentSettings.CaptureDebugTraces);
TestMethod testMethod = test.TestMethod;
MSTestAdapter.PlatformServices.Interface.ITestContext testContext = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, sourceLevelParameters);
TestMethodInfo? testMethodInfo = _typeCache.GetTestMethodInfo(testMethod, testContext, MSTestSettings.CurrentSettings.CaptureDebugTraces);
return testMethodInfo != null && TryProcessTestDataSourceTests(test, testMethodInfo, tests);
}

private static bool TryProcessTestDataSourceTests(UnitTestElement test, TestMethodInfo testMethodInfo, List<UnitTestElement> tests)
{
var methodInfo = testMethodInfo.MethodInfo;
var testDataSources = ReflectHelper.GetAttributes<Attribute>(methodInfo, false)?.OfType<FrameworkITestDataSource>();
MethodInfo methodInfo = testMethodInfo.MethodInfo;
IEnumerable<FrameworkITestDataSource>? testDataSources = ReflectHelper.GetAttributes<Attribute>(methodInfo, false)?.OfType<FrameworkITestDataSource>();
if (testDataSources == null || !testDataSources.Any())
{
return false;
Expand All @@ -290,7 +287,7 @@ private static bool TryProcessTestDataSourceTests(UnitTestElement test, TestMeth
}
catch (Exception ex)
{
var message = string.Format(CultureInfo.CurrentCulture, Resource.CannotEnumerateIDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, ex);
string message = string.Format(CultureInfo.CurrentCulture, Resource.CannotEnumerateIDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, ex);
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo($"DynamicDataEnumerator: {message}");
return false;
}
Expand All @@ -299,24 +296,24 @@ private static bool TryProcessTestDataSourceTests(UnitTestElement test, TestMeth
private static bool ProcessTestDataSourceTests(UnitTestElement test, MethodInfo methodInfo, IEnumerable<FrameworkITestDataSource> testDataSources,
List<UnitTestElement> tests)
{
foreach (var dataSource in testDataSources)
foreach (FrameworkITestDataSource dataSource in testDataSources)
{
var data = dataSource.GetData(methodInfo);
IEnumerable<object?[]> data = dataSource.GetData(methodInfo);
var testDisplayNameFirstSeen = new Dictionary<string, int>();
var discoveredTests = new List<UnitTestElement>();
var index = 0;
int index = 0;

foreach (var d in data)
foreach (object?[] d in data)
{
var discoveredTest = test.Clone();
UnitTestElement discoveredTest = test.Clone();
discoveredTest.DisplayName = dataSource.GetDisplayName(methodInfo, d) ?? discoveredTest.DisplayName;

// If strategy is DisplayName and we have a duplicate test name don't expand the test, bail out.
#pragma warning disable CS0618 // Type or member is obsolete
if (test.TestMethod.TestIdGenerationStrategy == TestIdGenerationStrategy.DisplayName
&& testDisplayNameFirstSeen.TryGetValue(discoveredTest.DisplayName!, out var firstIndexSeen))
&& testDisplayNameFirstSeen.TryGetValue(discoveredTest.DisplayName!, out int firstIndexSeen))
{
var warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute_DuplicateDisplayName, firstIndexSeen, index, discoveredTest.DisplayName);
string warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute_DuplicateDisplayName, firstIndexSeen, index, discoveredTest.DisplayName);
warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, warning);
PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning($"DynamicDataEnumerator: {warning}");

Expand All @@ -332,7 +329,7 @@ private static bool TryProcessTestDataSourceTests(UnitTestElement test, TestMeth
}
catch (SerializationException ex)
{
var warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute_CannotSerialize, index, discoveredTest.DisplayName);
string warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute_CannotSerialize, index, discoveredTest.DisplayName);
warning += Environment.NewLine;
warning += ex.ToString();
warning = string.Format(CultureInfo.CurrentCulture, Resource.CannotExpandIDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, warning);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ internal class AssemblyEnumeratorWrapper
return null;
}

var fullFilePath = PlatformServiceProvider.Instance.FileOperations.GetFullFilePath(assemblyFileName);
string fullFilePath = PlatformServiceProvider.Instance.FileOperations.GetFullFilePath(assemblyFileName);

try
{
if (!PlatformServiceProvider.Instance.FileOperations.DoesFileExist(fullFilePath))
{
var message = string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_FileDoesNotExist, fullFilePath);
string message = string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_FileDoesNotExist, fullFilePath);
throw new FileNotFoundException(message);
}

Expand All @@ -56,22 +56,22 @@ internal class AssemblyEnumeratorWrapper
}
catch (FileNotFoundException ex)
{
var message = string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_AssemblyDiscoveryFailure, fullFilePath, ex.Message);
string message = string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_AssemblyDiscoveryFailure, fullFilePath, ex.Message);
PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning($"{nameof(AssemblyEnumeratorWrapper)}.{nameof(this.GetTests)}: {Resource.TestAssembly_AssemblyDiscoveryFailure}", fullFilePath, ex);
warnings.Add(message);

return null;
}
catch (ReflectionTypeLoadException ex)
{
var message = string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_AssemblyDiscoveryFailure, fullFilePath, ex.Message);
string message = string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_AssemblyDiscoveryFailure, fullFilePath, ex.Message);
PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning($"{nameof(AssemblyEnumeratorWrapper)}.{nameof(this.GetTests)}: {Resource.TestAssembly_AssemblyDiscoveryFailure}", fullFilePath, ex);
PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning(Resource.ExceptionsThrown);
warnings.Add(message);

if (ex.LoaderExceptions != null)
{
foreach (var loaderEx in ex.LoaderExceptions)
foreach (Exception? loaderEx in ex.LoaderExceptions)
{
PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning("{0}", loaderEx);
}
Expand All @@ -91,7 +91,7 @@ internal class AssemblyEnumeratorWrapper
// Assembly.Load() fails to load the managed cpp executable, with FileLoadException. It can load the dll
// successfully though. This is known CLR issue.
PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning($"{nameof(AssemblyEnumeratorWrapper)}.{nameof(this.GetTests)}: {Resource.TestAssembly_AssemblyDiscoveryFailure}", fullFilePath, ex);
var message = ex is FileNotFoundException fileNotFoundEx ? fileNotFoundEx.Message : string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_AssemblyDiscoveryFailure, fullFilePath, ex.Message);
string message = ex is FileNotFoundException fileNotFoundEx ? fileNotFoundEx.Message : string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_AssemblyDiscoveryFailure, fullFilePath, ex.Message);

warnings.Add(message);
return null;
Expand All @@ -100,7 +100,7 @@ internal class AssemblyEnumeratorWrapper

private static ICollection<UnitTestElement> GetTestsInIsolation(string fullFilePath, IRunSettings? runSettings, out ICollection<string> warnings)
{
using var isolationHost = PlatformServiceProvider.Instance.CreateTestSourceHost(fullFilePath, runSettings, frameworkHandle: null);
using MSTestAdapter.PlatformServices.Interface.ITestSourceHost isolationHost = PlatformServiceProvider.Instance.CreateTestSourceHost(fullFilePath, runSettings, frameworkHandle: null);

// Create an instance of a type defined in adapter so that adapter gets loaded in the child app domain
var assemblyEnumerator = (AssemblyEnumerator)isolationHost.CreateInstanceForType(typeof(AssemblyEnumerator), new object[] { MSTestSettings.CurrentSettings })!;
Expand Down