Skip to content

Commit d8360e4

Browse files
authored
Enable nullable on remaining tests (#3507)
1 parent bb4c8bc commit d8360e4

19 files changed

+83
-147
lines changed

test/Microsoft.TestPlatform.PerformanceTests/PerformanceTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
using Microsoft.TestPlatform.PerformanceTests.TranslationLayer;
1010
using Microsoft.VisualStudio.TestTools.UnitTesting;
1111

12-
#nullable disable
13-
1412
namespace Microsoft.TestPlatform.PerformanceTests;
1513

1614
/// <summary>

test/Microsoft.TestPlatform.PerformanceTests/ProtocolV1Tests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
using TestResult = Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult;
1515

16-
#nullable disable
17-
1816
namespace Microsoft.TestPlatform.PerformanceTests;
1917

2018
[TestClass]

test/Microsoft.TestPlatform.PerformanceTests/ProtocolV2Tests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
using TestResult = Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult;
1616

17-
#nullable disable
18-
1917
namespace Microsoft.TestPlatform.PerformanceTests;
2018

2119
[TestClass]

test/Microsoft.TestPlatform.PerformanceTests/SocketTests.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
using Microsoft.VisualStudio.TestTools.UnitTesting;
1515

16-
#nullable disable
17-
1816
namespace Microsoft.TestPlatform.PerformanceTests;
1917

2018
[TestClass]
@@ -28,8 +26,8 @@ public void SocketThroughput2()
2826
// implementation.
2927
var server = new SocketServer();
3028
var client = new SocketClient();
31-
ICommunicationChannel serverChannel = null;
32-
ICommunicationChannel clientChannel = null;
29+
ICommunicationChannel? serverChannel = null;
30+
ICommunicationChannel? clientChannel = null;
3331
ManualResetEventSlim dataTransferred = new(false);
3432
ManualResetEventSlim clientConnected = new(false);
3533
ManualResetEventSlim serverConnected = new(false);
@@ -107,7 +105,7 @@ public void SocketThroughput1()
107105
watch.Elapsed.Should().BeLessOrEqualTo(20.Seconds());
108106
}
109107

110-
private static void SendData(ICommunicationChannel channel, Stopwatch watch)
108+
private static void SendData(ICommunicationChannel? channel, Stopwatch watch)
111109
{
112110
var dataBytes = new byte[65536];
113111
for (int i = 0; i < dataBytes.Length; i++)
@@ -120,7 +118,7 @@ private static void SendData(ICommunicationChannel channel, Stopwatch watch)
120118
watch.Start();
121119
for (int i = 0; i < 20000; i++)
122120
{
123-
channel.Send(dataBytesStr);
121+
channel!.Send(dataBytesStr);
124122
}
125123
}
126124

test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/DiscoveryPerfTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
77
using Microsoft.VisualStudio.TestTools.UnitTesting;
88

9-
#nullable disable
10-
119
namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;
1210

1311
[TestClass]

test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/DiscoveryEventHandler.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
88
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
99

10-
#nullable disable
11-
1210
namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;
1311

1412
/// <inheritdoc />
@@ -17,17 +15,12 @@ public class DiscoveryEventHandler2 : ITestDiscoveryEventsHandler2
1715
/// <summary>
1816
/// Gets the discovered test cases.
1917
/// </summary>
20-
public List<TestCase> DiscoveredTestCases { get; }
18+
public List<TestCase> DiscoveredTestCases { get; } = new List<TestCase>();
2119

2220
/// <summary>
2321
/// Gets the metrics.
2422
/// </summary>
25-
public IDictionary<string, object> Metrics { get; private set; }
26-
27-
public DiscoveryEventHandler2()
28-
{
29-
DiscoveredTestCases = new List<TestCase>();
30-
}
23+
public IDictionary<string, object> Metrics { get; private set; } = new Dictionary<string, object>();
3124

3225
public void HandleRawMessage(string rawMessage)
3326
{

test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/EventHandler/RunEventHandler.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
88
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
99

10-
#nullable disable
11-
1210
namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;
1311

1412
/// <inheritdoc />
@@ -22,7 +20,7 @@ public class RunEventHandler : ITestRunEventsHandler2
2220
/// <summary>
2321
/// Gets the metrics.
2422
/// </summary>
25-
public IDictionary<string, object> Metrics { get; private set; }
23+
public IDictionary<string, object> Metrics { get; private set; } = new Dictionary<string, object>();
2624

2725
/// <summary>
2826
/// Gets the log message.

test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/ExecutionPerfTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
77
using Microsoft.VisualStudio.TestTools.UnitTesting;
88

9-
#nullable disable
10-
119
namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;
1210

1311
[TestClass]

test/Microsoft.TestPlatform.PerformanceTests/TranslationLayer/TelemetryPerfTestBase.cs

+13-12
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
1313
using Microsoft.VisualStudio.TestTools.UnitTesting;
1414

15-
#nullable disable
16-
1715
namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;
1816

1917
[TestClass]
@@ -36,9 +34,9 @@ public TelemetryPerfTestBase()
3634
/// </summary>
3735
/// <param name="handlerMetrics"></param>
3836
/// <param name="scenario"></param>
39-
public void PostTelemetry(IDictionary<string, object> handlerMetrics, PerfAnalyzer perfAnalyzer, string projectName, [CallerMemberName] string scenario = null)
37+
public void PostTelemetry(IDictionary<string, object> handlerMetrics, PerfAnalyzer perfAnalyzer, string projectName, [CallerMemberName] string? scenario = null)
4038
{
41-
var properties = new Dictionary<string, string>
39+
var properties = new Dictionary<string, string?>
4240
{
4341
["Version"] = "1.0.1",
4442
["Project"] = projectName,
@@ -48,16 +46,19 @@ public void PostTelemetry(IDictionary<string, object> handlerMetrics, PerfAnalyz
4846

4947
var metrics = new Dictionary<string, double>();
5048

51-
foreach (var entry in handlerMetrics)
49+
if (handlerMetrics is not null)
5250
{
53-
var stringValue = entry.Value.ToString();
54-
if (double.TryParse(stringValue, out var doubleValue))
55-
{
56-
metrics.Add(entry.Key, doubleValue);
57-
}
58-
else
51+
foreach (var entry in handlerMetrics)
5952
{
60-
properties.Add(entry.Key, stringValue);
53+
var stringValue = entry.Value.ToString();
54+
if (double.TryParse(stringValue, out var doubleValue))
55+
{
56+
metrics.Add(entry.Key, doubleValue);
57+
}
58+
else
59+
{
60+
properties.Add(entry.Key, stringValue);
61+
}
6162
}
6263
}
6364

test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DefaultTestHostManagerTests.cs

+13-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.IO;
89
using System.Linq;
910
using System.Reflection;
@@ -27,8 +28,6 @@
2728

2829
using Moq;
2930

30-
#nullable disable
31-
3231
namespace TestPlatform.TestHostProvider.Hosting.UnitTests;
3332

3433
[TestClass]
@@ -40,10 +39,10 @@ public class DefaultTestHostManagerTests
4039
private readonly Mock<IFileHelper> _mockFileHelper;
4140
private readonly Mock<IDotnetHostHelper> _mockDotnetHostHelper;
4241
private readonly Mock<IEnvironment> _mockEnvironment;
43-
4442
private readonly DefaultTestHostManager _testHostManager;
45-
private TestableTestHostManager _testableTestHostManager;
46-
private string _errorMessage;
43+
44+
private TestableTestHostManager? _testableTestHostManager;
45+
private string? _errorMessage;
4746
private int _exitCode;
4847
private int _testHostId;
4948

@@ -404,17 +403,17 @@ public void LaunchTestHostShouldSetExitCallbackInCaseCustomHost()
404403
[TestCategory("Windows")]
405404
public void GetTestSourcesShouldReturnAppropriateSourceIfAppxRecipeIsProvided()
406405
{
407-
var sourcePath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation()), @"..\..\..\..\TestAssets\UWPTestAssets\UnitTestApp8.build.appxrecipe");
406+
var sourcePath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation())!, @"..\..\..\..\TestAssets\UWPTestAssets\UnitTestApp8.build.appxrecipe");
408407
IEnumerable<string> sources = _testHostManager.GetTestSources(new List<string> { sourcePath });
409408
Assert.IsTrue(sources.Any());
410-
Assert.IsTrue(sources.FirstOrDefault().EndsWith(".exe", StringComparison.OrdinalIgnoreCase));
409+
Assert.IsTrue(sources.First().EndsWith(".exe", StringComparison.OrdinalIgnoreCase));
411410
}
412411

413412
[TestMethod]
414413
[TestCategory("Windows")]
415414
public void AppxManifestFileShouldReturnAppropriateSourceIfAppxManifestIsProvided()
416415
{
417-
var appxManifestPath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation()), @"..\..\..\..\TestAssets\UWPTestAssets\AppxManifest.xml");
416+
var appxManifestPath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation())!, @"..\..\..\..\TestAssets\UWPTestAssets\AppxManifest.xml");
418417
string source = AppxManifestFile.GetApplicationExecutableName(appxManifestPath);
419418
Assert.AreEqual("UnitTestApp8.exe", source);
420419
}
@@ -495,25 +494,26 @@ public async Task CleanTestHostAsyncShouldNotThrowIfTestHostIsNotStarted()
495494
Assert.IsTrue(isVerified);
496495
}
497496

498-
private void TestableTestHostManagerHostExited(object sender, HostProviderEventArgs e)
497+
private void TestableTestHostManagerHostExited(object? sender, HostProviderEventArgs e)
499498
{
500499
_errorMessage = e.Data.TrimEnd(Environment.NewLine.ToCharArray());
501500
_exitCode = e.ErrroCode;
502501
}
503502

504-
private void TestHostManagerHostExited(object sender, HostProviderEventArgs e)
503+
private void TestHostManagerHostExited(object? sender, HostProviderEventArgs e)
505504
{
506505
if (e.ErrroCode != 0)
507506
{
508507
_errorMessage = e.Data.TrimEnd(Environment.NewLine.ToCharArray());
509508
}
510509
}
511510

512-
private void TestHostManagerHostLaunched(object sender, HostProviderEventArgs e)
511+
private void TestHostManagerHostLaunched(object? sender, HostProviderEventArgs e)
513512
{
514513
_testHostId = e.ProcessId;
515514
}
516515

516+
[MemberNotNull(nameof(_testableTestHostManager))]
517517
private void ErrorCallBackTestHelper(string errorMessage, int exitCode)
518518
{
519519
_testableTestHostManager = new TestableTestHostManager(
@@ -547,6 +547,7 @@ private void ErrorCallBackTestHelper(string errorMessage, int exitCode)
547547
_mockProcessHelper.Setup(ph => ph.TryGetExitCode(It.IsAny<object>(), out exitCode)).Returns(true);
548548
}
549549

550+
[MemberNotNull(nameof(_testableTestHostManager))]
550551
private void ExitCallBackTestHelper(int exitCode)
551552
{
552553
_testableTestHostManager = new TestableTestHostManager(
@@ -578,7 +579,7 @@ private void ExitCallBackTestHelper(int exitCode)
578579
_mockProcessHelper.Setup(ph => ph.TryGetExitCode(It.IsAny<object>(), out exitCode)).Returns(true);
579580
}
580581

581-
private TestProcessStartInfo GetDefaultStartInfo()
582+
private static TestProcessStartInfo GetDefaultStartInfo()
582583
{
583584
return new TestProcessStartInfo();
584585
}

test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs

+8-24
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
using Moq;
2929

30-
#nullable disable
31-
3230
namespace TestPlatform.TestHostProvider.UnitTests.Hosting;
3331

3432
[TestClass]
@@ -37,35 +35,21 @@ public class DotnetTestHostManagerTests
3735
private const string DefaultDotnetPath = "c:\\tmp\\dotnet.exe";
3836

3937
private readonly Mock<ITestHostLauncher> _mockTestHostLauncher;
40-
4138
private readonly Mock<IProcessHelper> _mockProcessHelper;
42-
4339
private readonly Mock<IFileHelper> _mockFileHelper;
44-
4540
private readonly Mock<IWindowsRegistryHelper> _mockWindowsRegistry;
46-
4741
private readonly Mock<IMessageLogger> _mockMessageLogger;
48-
4942
private readonly Mock<IEnvironment> _mockEnvironment;
50-
5143
private readonly Mock<IRunSettingsHelper> _mockRunsettingHelper;
52-
5344
private readonly TestRunnerConnectionInfo _defaultConnectionInfo;
54-
5545
private readonly string[] _testSource = { "test.dll" };
56-
5746
private readonly string _defaultTestHostPath;
58-
5947
private readonly TestProcessStartInfo _defaultTestProcessStartInfo;
60-
6148
private readonly TestableDotnetTestHostManager _dotnetHostManager;
62-
6349
private readonly Mock<IEnvironmentVariableHelper> _mockEnvironmentVariable;
6450

65-
private string _errorMessage;
66-
51+
private string? _errorMessage;
6752
private int _exitCode;
68-
6953
private int _testHostId;
7054

7155
private readonly string _temp = Path.GetTempPath();
@@ -102,7 +86,7 @@ public DotnetTestHostManagerTests()
10286
_mockProcessHelper.Setup(ph => ph.GetCurrentProcessFileName()).Returns(DefaultDotnetPath);
10387
_mockProcessHelper.Setup(ph => ph.GetTestEngineDirectory()).Returns(DefaultDotnetPath);
10488
_mockProcessHelper.Setup(ph => ph.GetCurrentProcessArchitecture()).Returns(PlatformArchitecture.X64);
105-
_mockEnvironmentVariable.Setup(ev => ev.GetEnvironmentVariable(It.IsAny<string>())).Returns(Path.GetDirectoryName(DefaultDotnetPath));
89+
_mockEnvironmentVariable.Setup(ev => ev.GetEnvironmentVariable(It.IsAny<string>())).Returns(Path.GetDirectoryName(DefaultDotnetPath)!);
10690
_mockFileHelper.Setup(ph => ph.Exists(_defaultTestHostPath)).Returns(true);
10791
_mockFileHelper.Setup(ph => ph.Exists(DefaultDotnetPath)).Returns(true);
10892

@@ -514,7 +498,7 @@ public void GetTestHostProcessStartInfoShouldThrowExceptionWhenDotnetIsNotInstal
514498
dotnetExeName = "dotnet";
515499
}
516500

517-
var paths = Environment.GetEnvironmentVariable("PATH").Split(separator);
501+
var paths = Environment.GetEnvironmentVariable("PATH")!.Split(separator);
518502

519503
foreach (string path in paths)
520504
{
@@ -629,7 +613,7 @@ public void GetTestHostProcessStartInfoShouldIncludeTestHostPathNextToTestRunner
629613
string testhostNextToTestDll = Path.Combine(_temp, "testhost.dll");
630614
_mockFileHelper.Setup(ph => ph.Exists(testhostNextToTestDll)).Returns(false);
631615

632-
var here = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
616+
var here = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!;
633617
var expectedTestHostPath = Path.Combine(here, "testhost.dll");
634618
_mockFileHelper.Setup(ph => ph.Exists(expectedTestHostPath)).Returns(true);
635619

@@ -671,7 +655,7 @@ public void GetTestHostProcessStartInfoShouldIncludeTestHostPathNextToTestRunner
671655
string testhostNextToTestDll = Path.Combine(_temp, "testhost.dll");
672656
_mockFileHelper.Setup(ph => ph.Exists(testhostNextToTestDll)).Returns(false);
673657

674-
var here = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
658+
var here = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!;
675659
var testhostNextToRunner = Path.Combine(here, "testhost.dll");
676660
_mockFileHelper.Setup(ph => ph.Exists(testhostNextToRunner)).Returns(true);
677661

@@ -1003,21 +987,21 @@ public async Task CleanTestHostAsyncShouldNotThrowIfTestHostIsNotStarted()
1003987
Assert.IsTrue(isVerified);
1004988
}
1005989

1006-
private void DotnetHostManagerExitCodeTesterHostExited(object sender, HostProviderEventArgs e)
990+
private void DotnetHostManagerExitCodeTesterHostExited(object? sender, HostProviderEventArgs e)
1007991
{
1008992
_errorMessage = e.Data.TrimEnd(Environment.NewLine.ToCharArray());
1009993
_exitCode = e.ErrroCode;
1010994
}
1011995

1012-
private void DotnetHostManagerHostExited(object sender, HostProviderEventArgs e)
996+
private void DotnetHostManagerHostExited(object? sender, HostProviderEventArgs e)
1013997
{
1014998
if (e.ErrroCode != 0)
1015999
{
10161000
_errorMessage = e.Data.TrimEnd(Environment.NewLine.ToCharArray());
10171001
}
10181002
}
10191003

1020-
private void DotnetHostManagerHostLaunched(object sender, HostProviderEventArgs e)
1004+
private void DotnetHostManagerHostLaunched(object? sender, HostProviderEventArgs e)
10211005
{
10221006
_testHostId = e.ProcessId;
10231007
}

test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/TestHostManagerCallbacksTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using Microsoft.TestPlatform.TestHostProvider.Hosting;
88
using Microsoft.VisualStudio.TestTools.UnitTesting;
99

10-
#nullable disable
11-
1210
namespace TestPlatform.TestHostProvider.Hosting.UnitTests;
1311

1412
[TestClass]

0 commit comments

Comments
 (0)