Skip to content

Commit

Permalink
Enable nullable on remaining tests (#3507)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink authored Mar 31, 2022
1 parent bb4c8bc commit d8360e4
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
using Microsoft.TestPlatform.PerformanceTests.TranslationLayer;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.PerformanceTests;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

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

#nullable disable

namespace Microsoft.TestPlatform.PerformanceTests;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

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

#nullable disable

namespace Microsoft.TestPlatform.PerformanceTests;

[TestClass]
Expand Down
10 changes: 4 additions & 6 deletions test/Microsoft.TestPlatform.PerformanceTests/SocketTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.PerformanceTests;

[TestClass]
Expand All @@ -28,8 +26,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);
Expand Down Expand Up @@ -107,7 +105,7 @@ public void SocketThroughput1()
watch.Elapsed.Should().BeLessOrEqualTo(20.Seconds());
}

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++)
Expand All @@ -120,7 +118,7 @@ private static void SendData(ICommunicationChannel channel, Stopwatch watch)
watch.Start();
for (int i = 0; i < 20000; i++)
{
channel.Send(dataBytesStr);
channel!.Send(dataBytesStr);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;

#nullable disable

namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;

/// <inheritdoc />
Expand All @@ -17,17 +15,12 @@ public class DiscoveryEventHandler2 : ITestDiscoveryEventsHandler2
/// <summary>
/// Gets the discovered test cases.
/// </summary>
public List<TestCase> DiscoveredTestCases { get; }
public List<TestCase> DiscoveredTestCases { get; } = new List<TestCase>();

/// <summary>
/// Gets the metrics.
/// </summary>
public IDictionary<string, object> Metrics { get; private set; }

public DiscoveryEventHandler2()
{
DiscoveredTestCases = new List<TestCase>();
}
public IDictionary<string, object> Metrics { get; private set; } = new Dictionary<string, object>();

public void HandleRawMessage(string rawMessage)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;

#nullable disable

namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;

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

/// <summary>
/// Gets the log message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.PerformanceTests.TranslationLayer;

[TestClass]
Expand All @@ -36,9 +34,9 @@ public TelemetryPerfTestBase()
/// </summary>
/// <param name="handlerMetrics"></param>
/// <param name="scenario"></param>
public void PostTelemetry(IDictionary<string, object> handlerMetrics, PerfAnalyzer perfAnalyzer, string projectName, [CallerMemberName] string scenario = null)
public void PostTelemetry(IDictionary<string, object> handlerMetrics, PerfAnalyzer perfAnalyzer, string projectName, [CallerMemberName] string? scenario = null)
{
var properties = new Dictionary<string, string>
var properties = new Dictionary<string, string?>
{
["Version"] = "1.0.1",
["Project"] = projectName,
Expand All @@ -48,16 +46,19 @@ public void PostTelemetry(IDictionary<string, object> handlerMetrics, PerfAnalyz

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

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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,8 +28,6 @@

using Moq;

#nullable disable

namespace TestPlatform.TestHostProvider.Hosting.UnitTests;

[TestClass]
Expand All @@ -40,10 +39,10 @@ public class DefaultTestHostManagerTests
private readonly Mock<IFileHelper> _mockFileHelper;
private readonly Mock<IDotnetHostHelper> _mockDotnetHostHelper;
private readonly Mock<IEnvironment> _mockEnvironment;

private readonly DefaultTestHostManager _testHostManager;
private TestableTestHostManager _testableTestHostManager;
private string _errorMessage;

private TestableTestHostManager? _testableTestHostManager;
private string? _errorMessage;
private int _exitCode;
private int _testHostId;

Expand Down Expand Up @@ -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<string> sources = _testHostManager.GetTestSources(new List<string> { 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);
}
Expand Down Expand Up @@ -495,25 +494,26 @@ 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)
{
_errorMessage = e.Data.TrimEnd(Environment.NewLine.ToCharArray());
}
}

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(
Expand Down Expand Up @@ -547,6 +547,7 @@ private void ErrorCallBackTestHelper(string errorMessage, int exitCode)
_mockProcessHelper.Setup(ph => ph.TryGetExitCode(It.IsAny<object>(), out exitCode)).Returns(true);
}

[MemberNotNull(nameof(_testableTestHostManager))]
private void ExitCallBackTestHelper(int exitCode)
{
_testableTestHostManager = new TestableTestHostManager(
Expand Down Expand Up @@ -578,7 +579,7 @@ private void ExitCallBackTestHelper(int exitCode)
_mockProcessHelper.Setup(ph => ph.TryGetExitCode(It.IsAny<object>(), out exitCode)).Returns(true);
}

private TestProcessStartInfo GetDefaultStartInfo()
private static TestProcessStartInfo GetDefaultStartInfo()
{
return new TestProcessStartInfo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

using Moq;

#nullable disable

namespace TestPlatform.TestHostProvider.UnitTests.Hosting;

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

private readonly Mock<ITestHostLauncher> _mockTestHostLauncher;

private readonly Mock<IProcessHelper> _mockProcessHelper;

private readonly Mock<IFileHelper> _mockFileHelper;

private readonly Mock<IWindowsRegistryHelper> _mockWindowsRegistry;

private readonly Mock<IMessageLogger> _mockMessageLogger;

private readonly Mock<IEnvironment> _mockEnvironment;

private readonly Mock<IRunSettingsHelper> _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<IEnvironmentVariableHelper> _mockEnvironmentVariable;

private string _errorMessage;

private string? _errorMessage;
private int _exitCode;

private int _testHostId;

private readonly string _temp = Path.GetTempPath();
Expand Down Expand Up @@ -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<string>())).Returns(Path.GetDirectoryName(DefaultDotnetPath));
_mockEnvironmentVariable.Setup(ev => ev.GetEnvironmentVariable(It.IsAny<string>())).Returns(Path.GetDirectoryName(DefaultDotnetPath)!);
_mockFileHelper.Setup(ph => ph.Exists(_defaultTestHostPath)).Returns(true);
_mockFileHelper.Setup(ph => ph.Exists(DefaultDotnetPath)).Returns(true);

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -1003,21 +987,21 @@ 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)
{
_errorMessage = e.Data.TrimEnd(Environment.NewLine.ToCharArray());
}
}

private void DotnetHostManagerHostLaunched(object sender, HostProviderEventArgs e)
private void DotnetHostManagerHostLaunched(object? sender, HostProviderEventArgs e)
{
_testHostId = e.ProcessId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Microsoft.TestPlatform.TestHostProvider.Hosting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace TestPlatform.TestHostProvider.Hosting.UnitTests;

[TestClass]
Expand Down
Loading

0 comments on commit d8360e4

Please sign in to comment.