Skip to content

Commit

Permalink
Enable nullable on extension and utilities tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Mar 22, 2022
1 parent 7aa51fc commit d263565
Show file tree
Hide file tree
Showing 37 changed files with 357 additions and 463 deletions.
Expand Up @@ -7,8 +7,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Helpers;

[TestClass]
Expand Down
Expand Up @@ -15,12 +15,10 @@

using Moq;

#nullable disable

namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Helpers;

[TestClass]
public class DotnetHostHelperTest : IDisposable
public sealed class DotnetHostHelperTest : IDisposable
{
private readonly Mock<IFileHelper> _fileHelper = new();
private readonly Mock<IProcessHelper> _processHelper = new();
Expand Down Expand Up @@ -71,13 +69,11 @@ public void GetDotnetPathByArchitecture_SameArchitecture()
// Arrange
string dotnetRootX64 = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.X64);
string dotnetRootArm64 = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.ARM64);
string dotnetRootX86 = null;
if (platformSystem == PlatformOperatingSystem.Windows)
{
dotnetRootX86 = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.X86);
}
string? dotnetRootX86 = platformSystem == PlatformOperatingSystem.Windows
? _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.X86)
: null;
string dotnetRoot = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, targetArchitecture);
Dictionary<string, string> envVars = new()
Dictionary<string, string?> envVars = new()
{
{ "DOTNET_ROOT_X64", dotnetRootX64 },
{ "DOTNET_ROOT_ARM64", dotnetRootArm64 },
Expand All @@ -87,13 +83,13 @@ public void GetDotnetPathByArchitecture_SameArchitecture()

_environmentHelper.SetupGet(x => x.Architecture).Returns(platformArchitecture);
_environmentHelper.SetupGet(x => x.OperatingSystem).Returns(platformSystem);
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(envVar)).Returns(Path.GetDirectoryName(envVars[envVar]));
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(envVar)).Returns(Path.GetDirectoryName(envVars[envVar])!);
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable("ProgramFiles")).Returns("notfound");
_fileHelper.Setup(x => x.DirectoryExists(Path.GetDirectoryName(envVars[envVar]))).Returns(true);
_fileHelper.Setup(x => x.Exists(envVars[envVar])).Returns(true);
if (found)
{
_fileHelper.Setup(x => x.GetStream(envVars[envVar], FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(envVars[envVar]));
_fileHelper.Setup(x => x.GetStream(envVars[envVar], FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(envVars[envVar]!));
}

// Act & Assert
Expand Down Expand Up @@ -122,8 +118,8 @@ public void GetDotnetPathByArchitecture_EnvVars_DirectoryNotExists_TryNext(strin

_environmentHelper.SetupGet(x => x.Architecture).Returns(platformArchitecture);
_environmentHelper.SetupGet(x => x.OperatingSystem).Returns(PlatformOperatingSystem.Windows);
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(notExists)).Returns(Path.GetDirectoryName(envVars[notExists]));
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(nextEnv)).Returns(Path.GetDirectoryName(envVars[nextEnv]));
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(notExists)).Returns(Path.GetDirectoryName(envVars[notExists])!);
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(nextEnv)).Returns(Path.GetDirectoryName(envVars[nextEnv])!);
_fileHelper.Setup(x => x.DirectoryExists(Path.GetDirectoryName(envVars[nextEnv]))).Returns(true);
_fileHelper.Setup(x => x.Exists(envVars[nextEnv])).Returns(true);
_fileHelper.Setup(x => x.GetStream(envVars[nextEnv], FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(envVars[nextEnv]));
Expand All @@ -146,7 +142,7 @@ public void GetDotnetPathByArchitecture_GlobalInstallation_Windows(PlatformArchi
Mock<IRegistryKey> nativeArchSubKey = new();
installedVersionKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(architectureSubKey.Object);
architectureSubKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(nativeArchSubKey.Object);
nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>())).Returns(Path.GetDirectoryName(dotnetMuxer));
nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>())).Returns(Path.GetDirectoryName(dotnetMuxer)!);
_windowsRegistrytHelper.Setup(x => x.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)).Returns(installedVersionKey.Object);
_fileHelper.Setup(x => x.Exists(dotnetMuxer)).Returns(true);
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));
Expand All @@ -168,13 +164,17 @@ public void GetDotnetPathByArchitecture_GlobalInstallation_NullSubkeys(bool null
Mock<IRegistryKey> installedVersionKey = new();
Mock<IRegistryKey> architectureSubKey = new();
Mock<IRegistryKey> nativeArchSubKey = new();
installedVersionKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(nullArchitecture ? null : architectureSubKey.Object);
architectureSubKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(nullNative ? null : nativeArchSubKey.Object);
nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>())).Returns(nullInstallLocation ? null : "");
_windowsRegistrytHelper.Setup(x => x.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)).Returns(nullInstalledVersion ? null : installedVersionKey.Object);
installedVersionKey.Setup(x => x.OpenSubKey(It.IsAny<string>()))
.Returns(nullArchitecture ? null! : architectureSubKey.Object);
architectureSubKey.Setup(x => x.OpenSubKey(It.IsAny<string>()))
.Returns(nullNative ? null! : nativeArchSubKey.Object);
nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>()))
.Returns(nullInstallLocation ? null! : "");
_windowsRegistrytHelper.Setup(x => x.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
.Returns(nullInstalledVersion ? null! : installedVersionKey.Object);
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable("ProgramFiles")).Returns("notfound");

//Act & Assert
// Act & Assert
var dotnetHostHelper = new DotnetHostHelper(_fileHelper.Object, _environmentHelper.Object, _windowsRegistrytHelper.Object, _environmentVariableHelper.Object, _processHelper.Object);
Assert.IsFalse(dotnetHostHelper.TryGetDotnetPathByArchitecture(PlatformArchitecture.X64, out string muxerPath));
}
Expand All @@ -198,7 +198,7 @@ public void GetDotnetPathByArchitecture_GlobalInstallation_Unix(PlatformArchitec
_environmentHelper.SetupGet(x => x.OperatingSystem).Returns(os);
_fileHelper.Setup(x => x.Exists(installLocation)).Returns(true);
_fileHelper.Setup(x => x.Exists(dotnetMuxer)).Returns(true);
_fileHelper.Setup(x => x.GetStream(installLocation, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer))));
_fileHelper.Setup(x => x.GetStream(installLocation, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer)!)));
if (found)
{
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));
Expand Down Expand Up @@ -226,7 +226,7 @@ public void GetDotnetPathByArchitecture_DefaultInstallation_Win(PlatformArchitec
if (found)
{
_fileHelper.Setup(x => x.Exists(dotnetMuxer)).Returns(true);
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer))));
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer)!)));
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));
}

Expand Down Expand Up @@ -254,7 +254,7 @@ public void GetDotnetPathByArchitecture_DefaultInstallation_Unix(PlatformArchite
_environmentHelper.Setup(x => x.Architecture).Returns(platformArchitecture);
string expectedMuxerPath = Path.Combine(expectedFolder, "dotnet");
_fileHelper.Setup(x => x.Exists(expectedMuxerPath)).Returns(true);
_fileHelper.Setup(x => x.GetStream(expectedMuxerPath, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer))));
_fileHelper.Setup(x => x.GetStream(expectedMuxerPath, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer)!)));
if (found)
{
_fileHelper.Setup(x => x.GetStream(expectedMuxerPath, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));
Expand All @@ -268,8 +268,7 @@ public void GetDotnetPathByArchitecture_DefaultInstallation_Unix(PlatformArchite

public void Dispose() => _muxerHelper.Dispose();


class MockMuxerHelper : IDisposable
private class MockMuxerHelper : IDisposable
{
private static readonly string DotnetMuxerWinX86 = "TestAssets/dotnetWinX86.exe";
private static readonly string DotnetMuxerWinX64 = "TestAssets/dotnetWinX64.exe";
Expand All @@ -296,7 +295,7 @@ public string RenameMuxerAndReturnPath(PlatformOperatingSystem platform, Platfor
case PlatformOperatingSystem.Windows:
{
muxerPath = Path.Combine(tmpDirectory, Guid.NewGuid().ToString("N"), subfolder, "dotnet.exe");
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath));
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath)!);
if (architecture == PlatformArchitecture.ARM64)
{
File.Copy(DotnetMuxerWinArm64, muxerPath);
Expand All @@ -318,7 +317,7 @@ public string RenameMuxerAndReturnPath(PlatformOperatingSystem platform, Platfor
case PlatformOperatingSystem.OSX:
{
muxerPath = Path.Combine(tmpDirectory, Guid.NewGuid().ToString("N"), subfolder, "dotnet");
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath));
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath)!);
if (architecture == PlatformArchitecture.ARM64)
{
File.Copy(DotnetMuxerMacArm64, muxerPath);
Expand All @@ -335,7 +334,7 @@ public string RenameMuxerAndReturnPath(PlatformOperatingSystem platform, Platfor
case PlatformOperatingSystem.Unix:
{
muxerPath = Path.Combine(tmpDirectory, Guid.NewGuid().ToString("N"), subfolder, "dotnet");
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath));
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath)!);
File.WriteAllText(muxerPath, "not supported");
break;
}
Expand All @@ -351,7 +350,7 @@ public void Dispose()
{
foreach (var muxer in _muxers)
{
Directory.Delete(Path.GetDirectoryName(muxer), true);
Directory.Delete(Path.GetDirectoryName(muxer)!, true);
}
}
}
Expand Down
Expand Up @@ -7,8 +7,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Helpers;

[TestClass]
Expand Down
Expand Up @@ -6,8 +6,6 @@
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Helpers;

[TestClass]
Expand Down
Expand Up @@ -8,8 +8,6 @@

using Moq;

#nullable disable

namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Output;

[TestClass]
Expand Down
@@ -1,35 +1,23 @@
// 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;
#if NETFRAMEWORK
using System.Diagnostics;
#endif
using System;
using System.IO;

using Microsoft.VisualStudio.TestPlatform.ObjectModel;

/* Unmerged change from project 'Microsoft.TestPlatform.CoreUtilities.UnitTests (net451)'
Before:
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using System;
After:
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using System;
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace TestPlatform.CoreUtilities.UnitTests;

[TestClass]
public class EqtTraceTests
{
private static string s_dirPath;
private static string s_logFile;
private static string? s_dirPath;
private static string? s_logFile;

[ClassInitialize]
public static void Init(TestContext _)
Expand Down Expand Up @@ -177,12 +165,12 @@ public void TraceShouldNotWriteIfDoNotInitializationIsSetToTrue()
Assert.IsFalse(ReadLogFile().Contains("Dummy Info Message: TraceShouldNotWriteIfDoNotInitializationIsSetToTrue"), "Did not expect Dummy Info message");
}

private string ReadLogFile()
private static string ReadLogFile()
{
string log = null;
string? log = null;
try
{
using var fs = new FileStream(s_logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var fs = new FileStream(s_logFile!, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var sr = new StreamReader(fs);
log = sr.ReadToEnd();
}
Expand All @@ -191,6 +179,7 @@ private string ReadLogFile()
Console.WriteLine(ex.Message);
}

Assert.IsNotNull(log);
return log;
}
}
Expand Up @@ -8,8 +8,6 @@
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace TestPlatform.CoreUtilities.UnitTests;

[TestClass]
Expand All @@ -18,7 +16,7 @@ public class JobQueueTests
[TestMethod]
public void ConstructorThrowsWhenNullProcessHandlerIsProvided()
{
JobQueue<string> jobQueue = null;
JobQueue<string>? jobQueue = null;
Assert.ThrowsException<ArgumentNullException>(() => jobQueue = new JobQueue<string>(null, "dp", int.MaxValue, int.MaxValue, false, (message) => { }));

if (jobQueue != null)
Expand All @@ -30,7 +28,7 @@ public void ConstructorThrowsWhenNullProcessHandlerIsProvided()
[TestMethod]
public void ThrowsWhenNullEmptyOrWhiteSpaceDisplayNameIsProvided()
{
JobQueue<string> jobQueue = null;
JobQueue<string>? jobQueue = null;
Assert.ThrowsException<ArgumentException>(() => jobQueue = new JobQueue<string>(GetEmptyProcessHandler<string>(), null, int.MaxValue, int.MaxValue, false, (message) => { }));
Assert.ThrowsException<ArgumentException>(() => jobQueue = new JobQueue<string>(GetEmptyProcessHandler<string>(), "", int.MaxValue, int.MaxValue, false, (message) => { }));
Assert.ThrowsException<ArgumentException>(() => jobQueue = new JobQueue<string>(GetEmptyProcessHandler<string>(), " ", int.MaxValue, int.MaxValue, false, (message) => { }));
Expand Down
Expand Up @@ -6,8 +6,6 @@
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace TestPlatform.CoreUtilities.UnitTests;

[TestClass]
Expand Down

0 comments on commit d263565

Please sign in to comment.