diff --git a/test/Microsoft.TestPlatform.PerformanceTests/PerformanceTests.cs b/test/Microsoft.TestPlatform.PerformanceTests/PerformanceTests.cs index 7db4ae1e30..9da6b0a366 100644 --- a/test/Microsoft.TestPlatform.PerformanceTests/PerformanceTests.cs +++ b/test/Microsoft.TestPlatform.PerformanceTests/PerformanceTests.cs @@ -5,8 +5,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -#nullable disable - namespace Microsoft.TestPlatform.PerformanceTests; /// diff --git a/test/Microsoft.TestPlatform.PerformanceTests/ProtocolV1Tests.cs b/test/Microsoft.TestPlatform.PerformanceTests/ProtocolV1Tests.cs index 7444a2d715..678501a6fa 100644 --- a/test/Microsoft.TestPlatform.PerformanceTests/ProtocolV1Tests.cs +++ b/test/Microsoft.TestPlatform.PerformanceTests/ProtocolV1Tests.cs @@ -11,8 +11,6 @@ using TestResult = Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult; -#nullable disable - namespace Microsoft.TestPlatform.PerformanceTests; [TestClass] @@ -30,7 +28,7 @@ public class ProtocolV1Tests Traits = { new Trait("Priority", "0"), new Trait("Category", "unit") } }; - private static DateTimeOffset s_startTime = new(new DateTime(2007, 3, 10, 0, 0, 0, DateTimeKind.Utc)); + private static readonly DateTimeOffset StartTime = new(new DateTime(2007, 3, 10, 0, 0, 0, DateTimeKind.Utc)); private static readonly TestResult TestResult = new(TestCase) { @@ -42,7 +40,7 @@ public class ProtocolV1Tests DisplayName = "sampleTestResult", ComputerName = "sampleComputerName", Duration = TimeSpan.MaxValue, - StartTime = s_startTime, + StartTime = StartTime, EndTime = DateTimeOffset.MaxValue }; diff --git a/test/Microsoft.TestPlatform.PerformanceTests/ProtocolV2Tests.cs b/test/Microsoft.TestPlatform.PerformanceTests/ProtocolV2Tests.cs index f1c4bfe33a..73b06d5de4 100644 --- a/test/Microsoft.TestPlatform.PerformanceTests/ProtocolV2Tests.cs +++ b/test/Microsoft.TestPlatform.PerformanceTests/ProtocolV2Tests.cs @@ -11,8 +11,6 @@ using TestResult = Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult; -#nullable disable - namespace Microsoft.TestPlatform.PerformanceTests; [TestClass] diff --git a/test/Microsoft.TestPlatform.PerformanceTests/SocketTests.cs b/test/Microsoft.TestPlatform.PerformanceTests/SocketTests.cs index aabc1b20e8..69efea980c 100644 --- a/test/Microsoft.TestPlatform.PerformanceTests/SocketTests.cs +++ b/test/Microsoft.TestPlatform.PerformanceTests/SocketTests.cs @@ -11,8 +11,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -#nullable disable - namespace Microsoft.TestPlatform.PerformanceTests; [TestClass] @@ -25,8 +23,8 @@ public void SocketThroughput2() // implementation. var server = new SocketServer(); var client = new SocketClient(); - ICommunicationChannel serverChannel = null; - ICommunicationChannel clientChannel = null; + ICommunicationChannel? serverChannel = null; + ICommunicationChannel? clientChannel = null; ManualResetEventSlim dataTransferred = new(false); ManualResetEventSlim clientConnected = new(false); ManualResetEventSlim serverConnected = new(false); @@ -104,7 +102,7 @@ public void SocketThroughput1() Assert.IsTrue(watch.Elapsed < TimeSpan.FromSeconds(4), "Elapsed: " + watch.Elapsed); } - private static void SendData(ICommunicationChannel channel, Stopwatch watch) + private static void SendData(ICommunicationChannel? channel, Stopwatch watch) { var dataBytes = new byte[65536]; for (int i = 0; i < dataBytes.Length; i++) @@ -117,7 +115,7 @@ private static void SendData(ICommunicationChannel channel, Stopwatch watch) watch.Start(); for (int i = 0; i < 20000; i++) { - channel.Send(dataBytesStr); + channel!.Send(dataBytesStr); } } diff --git a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/DiscoveryPerfTests.cs b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/DiscoveryPerfTests.cs index c5f359db39..6bc2427250 100644 --- a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/DiscoveryPerfTests.cs +++ b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/DiscoveryPerfTests.cs @@ -7,8 +7,6 @@ using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; using Microsoft.VisualStudio.TestTools.UnitTesting; -#nullable disable - namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer; [TestClass] diff --git a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/DiscoveryEventHandler.cs b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/DiscoveryEventHandler.cs index 13343dc5d6..4505ffb9e0 100644 --- a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/DiscoveryEventHandler.cs +++ b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/DiscoveryEventHandler.cs @@ -7,8 +7,6 @@ using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; -#nullable disable - namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer; /// @@ -22,7 +20,7 @@ public class DiscoveryEventHandler2 : ITestDiscoveryEventsHandler2 /// /// Gets the metrics. /// - public IDictionary Metrics { get; private set; } + public IDictionary? Metrics { get; private set; } public DiscoveryEventHandler2() { diff --git a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/RunEventHandler.cs b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/RunEventHandler.cs index 26a93bb5bc..2d07782eab 100644 --- a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/RunEventHandler.cs +++ b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/RunEventHandler.cs @@ -7,8 +7,6 @@ using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; -#nullable disable - namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer; /// @@ -22,12 +20,12 @@ public class RunEventHandler : ITestRunEventsHandler2 /// /// Gets the metrics. /// - public IDictionary Metrics { get; private set; } + public IDictionary? Metrics { get; private set; } /// /// Gets the log message. /// - public string LogMessage { get; private set; } + public string? LogMessage { get; private set; } /// /// Gets the test message level. diff --git a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/ExecutionPerfTests.cs b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/ExecutionPerfTests.cs index ee5c63f7bf..14aa5c1150 100644 --- a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/ExecutionPerfTests.cs +++ b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/ExecutionPerfTests.cs @@ -7,15 +7,13 @@ using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; using Microsoft.VisualStudio.TestTools.UnitTesting; -#nullable disable - namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer; [TestClass] public class ExecutionPerfTests : TelemetryPerfTestbase { - private IVsTestConsoleWrapper _vstestConsoleWrapper; - private RunEventHandler _runEventHandler; + private readonly IVsTestConsoleWrapper _vstestConsoleWrapper; + private readonly RunEventHandler _runEventHandler; public ExecutionPerfTests() { diff --git a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/TelemetryPerfTestBase.cs b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/TelemetryPerfTestBase.cs index 53655ae2b5..1133ffca31 100644 --- a/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/TelemetryPerfTestBase.cs +++ b/test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/TelemetryPerfTestBase.cs @@ -12,8 +12,6 @@ using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions; using Microsoft.VisualStudio.TestTools.UnitTesting; -#nullable disable - namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer; [TestClass] @@ -21,7 +19,7 @@ public class TelemetryPerfTestbase { private const string TelemetryInstrumentationKey = "76b373ba-8a55-45dd-b6db-7f1a83288691"; private readonly TelemetryClient _client; - private readonly DirectoryInfo _currentDirectory = new DirectoryInfo(typeof(DiscoveryPerfTests).GetTypeInfo().Assembly.GetAssemblyLocation()).Parent; + private readonly DirectoryInfo _currentDirectory = new DirectoryInfo(typeof(DiscoveryPerfTests).GetTypeInfo().Assembly.GetAssemblyLocation()).Parent!; public TelemetryPerfTestbase() { @@ -34,21 +32,24 @@ public TelemetryPerfTestbase() /// /// /// - public void PostTelemetry(string perfScenario, IDictionary handlerMetrics) + public void PostTelemetry(string perfScenario, IDictionary? handlerMetrics) { - var properties = new Dictionary(); + var properties = new Dictionary(); var metrics = new Dictionary(); - foreach (var entry in handlerMetrics) + if (handlerMetrics is not null) { - var stringValue = entry.Value.ToString(); - if (double.TryParse(stringValue, out var doubleValue)) - { - metrics.Add(entry.Key, doubleValue); - } - else + foreach (var entry in handlerMetrics) { - properties.Add(entry.Key, stringValue); + var stringValue = entry.Value.ToString(); + if (double.TryParse(stringValue, out var doubleValue)) + { + metrics.Add(entry.Key, doubleValue); + } + else + { + properties.Add(entry.Key, stringValue); + } } } _client.TrackEvent(perfScenario, properties, metrics); @@ -93,7 +94,7 @@ private string BuildConfiguration private string GetConsoleRunnerPath() { // Find the root - var root = _currentDirectory.Parent.Parent.Parent; + var root = _currentDirectory.Parent!.Parent!.Parent!; // Path to artifacts vstest.console return Path.Combine(root.FullName, BuildConfiguration, "net451", "win7-x64", "vstest.console.exe"); } diff --git a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DefaultTestHostManagerTests.cs b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DefaultTestHostManagerTests.cs index 7fd3915a62..13b04ab8aa 100644 --- a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DefaultTestHostManagerTests.cs +++ b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DefaultTestHostManagerTests.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Reflection; @@ -27,8 +28,6 @@ using Moq; -#nullable disable - namespace TestPlatform.TestHostProvider.Hosting.UnitTests; [TestClass] @@ -40,10 +39,10 @@ public class DefaultTestHostManagerTests private readonly Mock _mockFileHelper; private readonly Mock _mockDotnetHostHelper; private readonly Mock _mockEnvironment; - private readonly DefaultTestHostManager _testHostManager; - private TestableTestHostManager _testableTestHostManager; - private string _errorMessage; + + private TestableTestHostManager? _testableTestHostManager; + private string? _errorMessage; private int _exitCode; private int _testHostId; @@ -404,17 +403,17 @@ public void LaunchTestHostShouldSetExitCallbackInCaseCustomHost() [TestCategory("Windows")] public void GetTestSourcesShouldReturnAppropriateSourceIfAppxRecipeIsProvided() { - var sourcePath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation()), @"..\..\..\..\TestAssets\UWPTestAssets\UnitTestApp8.build.appxrecipe"); + var sourcePath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation())!, @"..\..\..\..\TestAssets\UWPTestAssets\UnitTestApp8.build.appxrecipe"); IEnumerable sources = _testHostManager.GetTestSources(new List { sourcePath }); Assert.IsTrue(sources.Any()); - Assert.IsTrue(sources.FirstOrDefault().EndsWith(".exe", StringComparison.OrdinalIgnoreCase)); + Assert.IsTrue(sources.First().EndsWith(".exe", StringComparison.OrdinalIgnoreCase)); } [TestMethod] [TestCategory("Windows")] public void AppxManifestFileShouldReturnAppropriateSourceIfAppxManifestIsProvided() { - var appxManifestPath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation()), @"..\..\..\..\TestAssets\UWPTestAssets\AppxManifest.xml"); + var appxManifestPath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation())!, @"..\..\..\..\TestAssets\UWPTestAssets\AppxManifest.xml"); string source = AppxManifestFile.GetApplicationExecutableName(appxManifestPath); Assert.AreEqual("UnitTestApp8.exe", source); } @@ -495,13 +494,13 @@ public async Task CleanTestHostAsyncShouldNotThrowIfTestHostIsNotStarted() Assert.IsTrue(isVerified); } - private void TestableTestHostManagerHostExited(object sender, HostProviderEventArgs e) + private void TestableTestHostManagerHostExited(object? sender, HostProviderEventArgs e) { _errorMessage = e.Data.TrimEnd(Environment.NewLine.ToCharArray()); _exitCode = e.ErrroCode; } - private void TestHostManagerHostExited(object sender, HostProviderEventArgs e) + private void TestHostManagerHostExited(object? sender, HostProviderEventArgs e) { if (e.ErrroCode != 0) { @@ -509,11 +508,12 @@ private void TestHostManagerHostExited(object sender, HostProviderEventArgs e) } } - private void TestHostManagerHostLaunched(object sender, HostProviderEventArgs e) + private void TestHostManagerHostLaunched(object? sender, HostProviderEventArgs e) { _testHostId = e.ProcessId; } + [MemberNotNull(nameof(_testableTestHostManager))] private void ErrorCallBackTestHelper(string errorMessage, int exitCode) { _testableTestHostManager = new TestableTestHostManager( @@ -547,6 +547,7 @@ private void ErrorCallBackTestHelper(string errorMessage, int exitCode) _mockProcessHelper.Setup(ph => ph.TryGetExitCode(It.IsAny(), out exitCode)).Returns(true); } + [MemberNotNull(nameof(_testableTestHostManager))] private void ExitCallBackTestHelper(int exitCode) { _testableTestHostManager = new TestableTestHostManager( @@ -578,7 +579,7 @@ private void ExitCallBackTestHelper(int exitCode) _mockProcessHelper.Setup(ph => ph.TryGetExitCode(It.IsAny(), out exitCode)).Returns(true); } - private TestProcessStartInfo GetDefaultStartInfo() + private static TestProcessStartInfo GetDefaultStartInfo() { return new TestProcessStartInfo(); } diff --git a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs index 48609b6b8d..289e0799e4 100644 --- a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs +++ b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs @@ -27,8 +27,6 @@ using Moq; -#nullable disable - namespace TestPlatform.TestHostProvider.UnitTests.Hosting; [TestClass] @@ -37,35 +35,21 @@ public class DotnetTestHostManagerTests private const string DefaultDotnetPath = "c:\\tmp\\dotnet.exe"; private readonly Mock _mockTestHostLauncher; - private readonly Mock _mockProcessHelper; - private readonly Mock _mockFileHelper; - private readonly Mock _mockWindowsRegistry; - private readonly Mock _mockMessageLogger; - private readonly Mock _mockEnvironment; - private readonly Mock _mockRunsettingHelper; - private readonly TestRunnerConnectionInfo _defaultConnectionInfo; - private readonly string[] _testSource = { "test.dll" }; - private readonly string _defaultTestHostPath; - private readonly TestProcessStartInfo _defaultTestProcessStartInfo; - private readonly TestableDotnetTestHostManager _dotnetHostManager; - private readonly Mock _mockEnvironmentVariable; - private string _errorMessage; - + private string? _errorMessage; private int _exitCode; - private int _testHostId; private readonly string _temp = Path.GetTempPath(); @@ -102,7 +86,7 @@ public DotnetTestHostManagerTests() _mockProcessHelper.Setup(ph => ph.GetCurrentProcessFileName()).Returns(DefaultDotnetPath); _mockProcessHelper.Setup(ph => ph.GetTestEngineDirectory()).Returns(DefaultDotnetPath); _mockProcessHelper.Setup(ph => ph.GetCurrentProcessArchitecture()).Returns(PlatformArchitecture.X64); - _mockEnvironmentVariable.Setup(ev => ev.GetEnvironmentVariable(It.IsAny())).Returns(Path.GetDirectoryName(DefaultDotnetPath)); + _mockEnvironmentVariable.Setup(ev => ev.GetEnvironmentVariable(It.IsAny())).Returns(Path.GetDirectoryName(DefaultDotnetPath)!); _mockFileHelper.Setup(ph => ph.Exists(_defaultTestHostPath)).Returns(true); _mockFileHelper.Setup(ph => ph.Exists(DefaultDotnetPath)).Returns(true); @@ -514,7 +498,7 @@ public void GetTestHostProcessStartInfoShouldThrowExceptionWhenDotnetIsNotInstal dotnetExeName = "dotnet"; } - var paths = Environment.GetEnvironmentVariable("PATH").Split(separator); + var paths = Environment.GetEnvironmentVariable("PATH")!.Split(separator); foreach (string path in paths) { @@ -629,7 +613,7 @@ public void GetTestHostProcessStartInfoShouldIncludeTestHostPathNextToTestRunner string testhostNextToTestDll = Path.Combine(_temp, "testhost.dll"); _mockFileHelper.Setup(ph => ph.Exists(testhostNextToTestDll)).Returns(false); - var here = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); + var here = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!; var expectedTestHostPath = Path.Combine(here, "testhost.dll"); _mockFileHelper.Setup(ph => ph.Exists(expectedTestHostPath)).Returns(true); @@ -671,7 +655,7 @@ public void GetTestHostProcessStartInfoShouldIncludeTestHostPathNextToTestRunner string testhostNextToTestDll = Path.Combine(_temp, "testhost.dll"); _mockFileHelper.Setup(ph => ph.Exists(testhostNextToTestDll)).Returns(false); - var here = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); + var here = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!; var testhostNextToRunner = Path.Combine(here, "testhost.dll"); _mockFileHelper.Setup(ph => ph.Exists(testhostNextToRunner)).Returns(true); @@ -1003,13 +987,13 @@ public async Task CleanTestHostAsyncShouldNotThrowIfTestHostIsNotStarted() Assert.IsTrue(isVerified); } - private void DotnetHostManagerExitCodeTesterHostExited(object sender, HostProviderEventArgs e) + private void DotnetHostManagerExitCodeTesterHostExited(object? sender, HostProviderEventArgs e) { _errorMessage = e.Data.TrimEnd(Environment.NewLine.ToCharArray()); _exitCode = e.ErrroCode; } - private void DotnetHostManagerHostExited(object sender, HostProviderEventArgs e) + private void DotnetHostManagerHostExited(object? sender, HostProviderEventArgs e) { if (e.ErrroCode != 0) { @@ -1017,7 +1001,7 @@ private void DotnetHostManagerHostExited(object sender, HostProviderEventArgs e) } } - private void DotnetHostManagerHostLaunched(object sender, HostProviderEventArgs e) + private void DotnetHostManagerHostLaunched(object? sender, HostProviderEventArgs e) { _testHostId = e.ProcessId; } diff --git a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/TestHostManagerCallbacksTests.cs b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/TestHostManagerCallbacksTests.cs index fd393257fe..27cced2518 100644 --- a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/TestHostManagerCallbacksTests.cs +++ b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/TestHostManagerCallbacksTests.cs @@ -7,8 +7,6 @@ using Microsoft.TestPlatform.TestHostProvider.Hosting; using Microsoft.VisualStudio.TestTools.UnitTesting; -#nullable disable - namespace TestPlatform.TestHostProvider.Hosting.UnitTests; [TestClass] diff --git a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Microsoft.TestPlatform.TestHostProvider.UnitTests.csproj b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Microsoft.TestPlatform.TestHostProvider.UnitTests.csproj index ac6ed32f77..0f52d72466 100644 --- a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Microsoft.TestPlatform.TestHostProvider.UnitTests.csproj +++ b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Microsoft.TestPlatform.TestHostProvider.UnitTests.csproj @@ -12,6 +12,9 @@ netcoreapp3.1 Exe + + + diff --git a/test/SettingsMigrator.UnitTests/MigratorTests.cs b/test/SettingsMigrator.UnitTests/MigratorTests.cs index 85f35a8922..2608bc891f 100644 --- a/test/SettingsMigrator.UnitTests/MigratorTests.cs +++ b/test/SettingsMigrator.UnitTests/MigratorTests.cs @@ -6,8 +6,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -#nullable disable - namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator.UnitTests; [TestClass] @@ -22,13 +20,13 @@ public class MigratorTests "" + ""; - private Migrator _migrator; - private string _newRunsettingsPath; + private readonly Migrator _migrator; + private readonly string _newRunsettingsPath; + private string _oldTestsettingsPath; private string _oldRunsettingsPath; - [TestInitialize] - public void TestInit() + public MigratorTests() { _migrator = new Migrator(); _newRunsettingsPath = Path.Combine(Path.GetTempPath(), "generatedRunsettings.runsettings"); diff --git a/test/SettingsMigrator.UnitTests/PathResolverTests.cs b/test/SettingsMigrator.UnitTests/PathResolverTests.cs index 1f11cf29c6..ab80e10019 100644 --- a/test/SettingsMigrator.UnitTests/PathResolverTests.cs +++ b/test/SettingsMigrator.UnitTests/PathResolverTests.cs @@ -7,17 +7,14 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -#nullable disable - namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator.UnitTests; [TestClass] public class PathResolverTests { - private PathResolver _pathResolver; + private readonly PathResolver _pathResolver; - [TestInitialize] - public void TestInit() + public PathResolverTests() { _pathResolver = new PathResolver(); } diff --git a/test/SettingsMigrator.UnitTests/ProgramTests.cs b/test/SettingsMigrator.UnitTests/ProgramTests.cs index ab299ed744..8afeb3462f 100644 --- a/test/SettingsMigrator.UnitTests/ProgramTests.cs +++ b/test/SettingsMigrator.UnitTests/ProgramTests.cs @@ -3,8 +3,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -#nullable disable - namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator.UnitTests; [TestClass] diff --git a/test/TranslationLayer.UnitTests/VsTestConsoleRequestSenderTests.cs b/test/TranslationLayer.UnitTests/VsTestConsoleRequestSenderTests.cs index ef03391d16..f1185b8945 100644 --- a/test/TranslationLayer.UnitTests/VsTestConsoleRequestSenderTests.cs +++ b/test/TranslationLayer.UnitTests/VsTestConsoleRequestSenderTests.cs @@ -28,19 +28,14 @@ using Payloads = Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Payloads; using TestResult = Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult; -#nullable disable - namespace Microsoft.TestPlatform.VsTestConsole.TranslationLayer.UnitTests; [TestClass] public class VsTestConsoleRequestSenderTests { private readonly ITranslationLayerRequestSender _requestSender; - private readonly Mock _mockCommunicationManager; - private readonly int _waitTimeout = 2000; - private readonly int _protocolVersion = 6; private readonly IDataSerializer _serializer = JsonDataSerializer.Instance; @@ -455,7 +450,7 @@ public void DiscoverTestsShouldCompleteWithSingleFullyDiscoveredSource() Payload = JToken.FromObject(payload) }; - DiscoveryCompleteEventArgs receivedDiscoveryCompleteEventArgs = null; + DiscoveryCompleteEventArgs? receivedDiscoveryCompleteEventArgs = null; _mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(testsFound)); mockHandler.Setup(mh => mh.HandleDiscoveredTests(It.IsAny>())) @@ -467,7 +462,7 @@ public void DiscoverTestsShouldCompleteWithSingleFullyDiscoveredSource() _requestSender.DiscoverTests(sources, null, new TestPlatformOptions(), null, mockHandler.Object); mockHandler.Verify(mh => mh.HandleDiscoveryComplete(It.IsAny(), null), Times.Once, "Discovery Complete must be called"); - Assert.IsNotNull(receivedDiscoveryCompleteEventArgs.FullyDiscoveredSources); + Assert.IsNotNull(receivedDiscoveryCompleteEventArgs!.FullyDiscoveredSources); Assert.AreEqual(1, receivedDiscoveryCompleteEventArgs.FullyDiscoveredSources.Count); } @@ -495,7 +490,7 @@ public void DiscoverTestsShouldCompleteWithCorrectAbortedValuesIfAbortingWasRequ Payload = JToken.FromObject(payload) }; - DiscoveryCompleteEventArgs receivedDiscoveryCompleteEventArgs = null; + DiscoveryCompleteEventArgs? receivedDiscoveryCompleteEventArgs = null; _mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(testsFound)); mockHandler.Setup(mh => mh.HandleDiscoveredTests(It.IsAny>())) @@ -510,7 +505,7 @@ public void DiscoverTestsShouldCompleteWithCorrectAbortedValuesIfAbortingWasRequ // Assert mockHandler.Verify(mh => mh.HandleDiscoveryComplete(It.IsAny(), null), Times.Once, "Discovery Complete must be called"); - Assert.IsNotNull(receivedDiscoveryCompleteEventArgs.FullyDiscoveredSources); + Assert.IsNotNull(receivedDiscoveryCompleteEventArgs!.FullyDiscoveredSources); Assert.AreEqual(1, receivedDiscoveryCompleteEventArgs.FullyDiscoveredSources.Count); Assert.AreEqual(-1, receivedDiscoveryCompleteEventArgs.TotalCount); Assert.AreEqual(true, receivedDiscoveryCompleteEventArgs.IsAborted); @@ -526,7 +521,7 @@ public void DiscoverTestsShouldReportBackTestsWithTraitsInTestsFoundMessage() var testCase = new TestCase("hello", new Uri("world://how"), "1.dll"); testCase.Traits.Add(new Trait("a", "b")); - List receivedTestCases = null; + List? receivedTestCases = null; var testCaseList = new List() { testCase }; var testsFound = CreateMessage(MessageType.TestCasesFound, testCaseList); @@ -564,7 +559,7 @@ public async Task DiscoverTestsAsyncShouldReportBackTestsWithTraitsInTestsFoundM var testCase = new TestCase("hello", new Uri("world://how"), "1.dll"); testCase.Traits.Add(new Trait("a", "b")); - List receivedTestCases = null; + List? receivedTestCases = null; var testCaseList = new List() { testCase }; var testsFound = CreateMessage(MessageType.TestCasesFound, testCaseList); @@ -574,7 +569,7 @@ public async Task DiscoverTestsAsyncShouldReportBackTestsWithTraitsInTestsFoundM _mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(testsFound)); mockHandler.Setup(mh => mh.HandleDiscoveredTests(It.IsAny>())) .Callback( - (IEnumerable tests) => + (IEnumerable? tests) => { receivedTestCases = tests?.ToList(); _mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult((discoveryComplete))); @@ -602,7 +597,7 @@ public void DiscoverTestsShouldReportBackTestsWithTraitsInDiscoveryCompleteMessa var testCase = new TestCase("hello", new Uri("world://how"), "1.dll"); testCase.Traits.Add(new Trait("a", "b")); - List receivedTestCases = null; + List? receivedTestCases = null; var testCaseList = new List() { testCase }; var payload = new DiscoveryCompletePayload() { TotalTests = 1, LastDiscoveredTests = testCaseList, IsAborted = false }; @@ -635,7 +630,7 @@ public async Task DiscoverTestsAsyncShouldReportBackTestsWithTraitsInDiscoveryCo var testCase = new TestCase("hello", new Uri("world://how"), "1.dll"); testCase.Traits.Add(new Trait("a", "b")); - List receivedTestCases = null; + List? receivedTestCases = null; var testCaseList = new List() { testCase }; var payload = new DiscoveryCompletePayload() { TotalTests = 1, LastDiscoveredTests = testCaseList, IsAborted = false }; @@ -766,7 +761,7 @@ public void DiscoverTestsShouldLogErrorWhenProcessExited() Task.Run(() => _requestSender.OnProcessExited(), c).Wait(c); Assert.IsTrue(c.IsCancellationRequested); - }).Returns(Task.FromResult((Message)null)); + }).Returns(Task.FromResult((Message?)null)); mockHandler.Setup(mh => mh.HandleDiscoveryComplete(It.IsAny(), null)).Callback(() => manualEvent.Set()); @@ -793,7 +788,7 @@ public async Task DiscoverTestsAsyncShouldLogErrorWhenProcessExited() Task.Run(() => _requestSender.OnProcessExited(), c).Wait(c); Assert.IsTrue(c.IsCancellationRequested); - }).Returns(Task.FromResult((Message)null)); + }).Returns(Task.FromResult((Message?)null)); await _requestSender.DiscoverTestsAsync(sources, null, new TestPlatformOptions(), null, mockHandler.Object); @@ -971,7 +966,7 @@ public void StartTestRunShouldNotThrowIfTestPlatformOptionsIsNull() { // Arrange. var sources = new List() { "1.dll" }; - TestRunRequestPayload receivedRequest = null; + TestRunRequestPayload? receivedRequest = null; var mockHandler = new Mock(); @@ -994,7 +989,7 @@ public void StartTestRunShouldIncludeFilterInRequestPayload() // Arrange. var sources = new List() { "1.dll" }; var filter = "GivingCampaign"; - TestRunRequestPayload receivedRequest = null; + TestRunRequestPayload? receivedRequest = null; var mockHandler = new Mock(); @@ -1236,7 +1231,7 @@ public void StartTestRunWithCustomHostShouldNotThrowIfTestPlatformOptionsIsNull( { // Arrange. var sources = new List() { "1.dll" }; - TestRunRequestPayload receivedRequest = null; + TestRunRequestPayload? receivedRequest = null; var mockHandler = new Mock(); @@ -1259,7 +1254,7 @@ public void StartTestRunWithCustomHostShouldIncludeFilterInRequestPayload() // Arrange. var sources = new List() { "1.dll" }; var filter = "GivingCampaign"; - TestRunRequestPayload receivedRequest = null; + TestRunRequestPayload? receivedRequest = null; var mockHandler = new Mock(); @@ -1447,7 +1442,7 @@ public void StartTestRunWithSelectedTestsHavingTraitsShouldReturnTestRunComplete var testCaseList = new List() { testCase }; - TestRunChangedEventArgs receivedChangeEventArgs = null; + TestRunChangedEventArgs? receivedChangeEventArgs = null; var dummyCompleteArgs = new TestRunCompleteEventArgs(null, false, false, null, null, null, TimeSpan.FromMilliseconds(1)); var dummyLastRunArgs = new TestRunChangedEventArgs(null, new List { testResult }, null); @@ -1500,7 +1495,7 @@ public async Task StartTestRunAsyncWithSelectedTestsHavingTraitsShouldReturnTest var testCaseList = new List() { testCase }; - TestRunChangedEventArgs receivedChangeEventArgs = null; + TestRunChangedEventArgs? receivedChangeEventArgs = null; var dummyCompleteArgs = new TestRunCompleteEventArgs(null, false, false, null, null, null, TimeSpan.FromMilliseconds(1)); var dummyLastRunArgs = new TestRunChangedEventArgs(null, new List { testResult }, null); @@ -1553,7 +1548,7 @@ public void StartTestRunWithSelectedTestsHavingTraitsShouldReturnTestRunStatsWit var testCaseList = new List() { testCase }; - TestRunChangedEventArgs receivedChangeEventArgs = null; + TestRunChangedEventArgs? receivedChangeEventArgs = null; var dummyCompleteArgs = new TestRunCompleteEventArgs(null, false, false, null, null, null, TimeSpan.FromMilliseconds(1)); var dummyLastRunArgs = new TestRunChangedEventArgs(null, null, null); @@ -1610,7 +1605,7 @@ public async Task StartTestRunAsyncWithSelectedTestsHavingTraitsShouldReturnTest var testCaseList = new List() { testCase }; - TestRunChangedEventArgs receivedChangeEventArgs = null; + TestRunChangedEventArgs? receivedChangeEventArgs = null; var dummyCompleteArgs = new TestRunCompleteEventArgs(null, false, false, null, null, null, TimeSpan.FromMilliseconds(1)); var dummyLastRunArgs = new TestRunChangedEventArgs(null, null, null); @@ -1905,7 +1900,7 @@ public void StartTestRunShouldLogErrorOnProcessExited() Task.Run(() => _requestSender.OnProcessExited(), c).Wait(c); Assert.IsTrue(c.IsCancellationRequested); - }).Returns(Task.FromResult((Message)null)); + }).Returns(Task.FromResult((Message?)null)); mockHandler.Setup(mh => mh.HandleTestRunComplete(It.IsAny(), null, null, null)).Callback(() => manualEvent.Set()); @@ -1936,7 +1931,7 @@ public async Task StartTestRunAsyncShouldLogErrorOnProcessExited() Task.Run(() => _requestSender.OnProcessExited(), c).Wait(c); Assert.IsTrue(c.IsCancellationRequested); - }).Returns(Task.FromResult((Message)null)); + }).Returns(Task.FromResult((Message?)null)); await _requestSender.StartTestRunAsync(sources, null, null, null, mockHandler.Object); diff --git a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperAsyncTests.cs b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperAsyncTests.cs index 0c5751c073..925b9de123 100644 --- a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperAsyncTests.cs +++ b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperAsyncTests.cs @@ -17,33 +17,24 @@ using Moq; -#nullable disable - namespace Microsoft.TestPlatform.VsTestConsole.TranslationLayer.UnitTests; [TestClass] public class VsTestConsoleWrapperAsyncTests { - private IVsTestConsoleWrapper _consoleWrapper; - - private Mock _mockProcessManager; - - private Mock _mockRequestSender; - - private Mock _mockProcessHelper; - + private readonly IVsTestConsoleWrapper _consoleWrapper; + private readonly Mock _mockProcessManager; + private readonly Mock _mockRequestSender; + private readonly Mock _mockProcessHelper; private readonly List _testSources = new() { "Hello", "World" }; - private readonly List _testCases = new() { new TestCase("a.b.c", new Uri("d://uri"), "a.dll"), new TestCase("d.e.f", new Uri("g://uri"), "d.dll") }; + private readonly ConsoleParameters _consoleParameters; - private ConsoleParameters _consoleParameters; - - [TestInitialize] - public void TestInitialize() + public VsTestConsoleWrapperAsyncTests() { _consoleParameters = new ConsoleParameters(); diff --git a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs index 8599ec8cab..fe3c21ac6c 100644 --- a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs +++ b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs @@ -18,33 +18,24 @@ using Moq; -#nullable disable - namespace Microsoft.TestPlatform.VsTestConsole.TranslationLayer.UnitTests; [TestClass] public class VsTestConsoleWrapperTests { - private IVsTestConsoleWrapper _consoleWrapper; - - private Mock _mockProcessManager; - - private Mock _mockProcessHelper; - - private Mock _mockRequestSender; - + private readonly IVsTestConsoleWrapper _consoleWrapper; + private readonly Mock _mockProcessManager; + private readonly Mock _mockProcessHelper; + private readonly Mock _mockRequestSender; private readonly List _testSources = new() { "Hello", "World" }; - private readonly List _testCases = new() { new TestCase("a.b.c", new Uri("d://uri"), "a.dll"), new TestCase("d.e.f", new Uri("g://uri"), "d.dll") }; + private readonly ConsoleParameters _consoleParameters; - private ConsoleParameters _consoleParameters; - - [TestInitialize] - public void TestInitialize() + public VsTestConsoleWrapperTests() { _consoleParameters = new ConsoleParameters();