Skip to content

Commit bb4c8bc

Browse files
authored
Enable nullable on extension and utilities tests (#3504)
1 parent 7648527 commit bb4c8bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+408
-495
lines changed

StringUtils.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Diagnostics.CodeAnalysis;
5+
6+
namespace Microsoft.TestPlatform;
7+
8+
internal static class StringUtils
9+
{
10+
[SuppressMessage("ApiDesign", "RS0030:Do not used banned APIs", Justification = "Replacement API to allow nullable hints for compiler")]
11+
public static bool IsNullOrEmpty([NotNullWhen(returnValue: false)] this string? value)
12+
=> string.IsNullOrEmpty(value);
13+
14+
[SuppressMessage("ApiDesign", "RS0030:Do not used banned APIs", Justification = "Replacement API to allow nullable hints for compiler")]
15+
public static bool IsNullOrWhiteSpace([NotNullWhen(returnValue: false)] this string? value)
16+
=> string.IsNullOrWhiteSpace(value);
17+
}

test/Microsoft.TestPlatform.AcceptanceTests/AcceptanceTestBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class AcceptanceTestBase : IntegrationTestBase
6161
public const string LATEST_TO_LEGACY = "Latest;LatestPreview;LatestStable;RecentStable;MostDownloaded;PreviousStable;LegacyStable";
6262
public const string LATESTPREVIEW_TO_LEGACY = "LatestPreview;LatestStable;RecentStable;MostDownloaded;PreviousStable;LegacyStable";
6363
public const string LATEST = "Latest";
64-
public const string LATESTSTABLE= "LatestStable";
64+
public const string LATESTSTABLE = "LatestStable";
6565
internal const string MSTEST = "MSTest";
6666

6767
public static string And(string left, string right)

test/Microsoft.TestPlatform.CoreUtilities.UnitTests/Helpers/CommandLineArgumentsHelperTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
using Microsoft.VisualStudio.TestTools.UnitTesting;
99

10-
#nullable disable
11-
1210
namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Helpers;
1311

1412
[TestClass]

test/Microsoft.TestPlatform.CoreUtilities.UnitTests/Helpers/DotnetHostHelperTest.cs

+27-28
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515

1616
using Moq;
1717

18-
#nullable disable
19-
2018
namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Helpers;
2119

2220
[TestClass]
23-
public class DotnetHostHelperTest : IDisposable
21+
public sealed class DotnetHostHelperTest : IDisposable
2422
{
2523
private readonly Mock<IFileHelper> _fileHelper = new();
2624
private readonly Mock<IProcessHelper> _processHelper = new();
@@ -71,13 +69,11 @@ public void GetDotnetPathByArchitecture_EnvVars(PlatformArchitecture targetArchi
7169
// Arrange
7270
string dotnetRootX64 = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.X64);
7371
string dotnetRootArm64 = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.ARM64);
74-
string dotnetRootX86 = null;
75-
if (platformSystem == PlatformOperatingSystem.Windows)
76-
{
77-
dotnetRootX86 = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.X86);
78-
}
72+
string? dotnetRootX86 = platformSystem == PlatformOperatingSystem.Windows
73+
? _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.X86)
74+
: null;
7975
string dotnetRoot = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, targetArchitecture);
80-
Dictionary<string, string> envVars = new()
76+
Dictionary<string, string?> envVars = new()
8177
{
8278
{ "DOTNET_ROOT_X64", dotnetRootX64 },
8379
{ "DOTNET_ROOT_ARM64", dotnetRootArm64 },
@@ -87,13 +83,13 @@ public void GetDotnetPathByArchitecture_EnvVars(PlatformArchitecture targetArchi
8783

8884
_environmentHelper.SetupGet(x => x.Architecture).Returns(platformArchitecture);
8985
_environmentHelper.SetupGet(x => x.OperatingSystem).Returns(platformSystem);
90-
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(envVar)).Returns(Path.GetDirectoryName(envVars[envVar]));
86+
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(envVar)).Returns(Path.GetDirectoryName(envVars[envVar])!);
9187
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable("ProgramFiles")).Returns("notfound");
9288
_fileHelper.Setup(x => x.DirectoryExists(Path.GetDirectoryName(envVars[envVar]))).Returns(true);
9389
_fileHelper.Setup(x => x.Exists(envVars[envVar])).Returns(true);
9490
if (found)
9591
{
96-
_fileHelper.Setup(x => x.GetStream(envVars[envVar], FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(envVars[envVar]));
92+
_fileHelper.Setup(x => x.GetStream(envVars[envVar], FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(envVars[envVar]!));
9793
}
9894

9995
// Act & Assert
@@ -122,8 +118,8 @@ public void GetDotnetPathByArchitecture_EnvVars_DirectoryNotExists_TryNext(strin
122118

123119
_environmentHelper.SetupGet(x => x.Architecture).Returns(platformArchitecture);
124120
_environmentHelper.SetupGet(x => x.OperatingSystem).Returns(PlatformOperatingSystem.Windows);
125-
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(notExists)).Returns(Path.GetDirectoryName(envVars[notExists]));
126-
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(nextEnv)).Returns(Path.GetDirectoryName(envVars[nextEnv]));
121+
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(notExists)).Returns(Path.GetDirectoryName(envVars[notExists])!);
122+
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(nextEnv)).Returns(Path.GetDirectoryName(envVars[nextEnv])!);
127123
_fileHelper.Setup(x => x.DirectoryExists(Path.GetDirectoryName(envVars[nextEnv]))).Returns(true);
128124
_fileHelper.Setup(x => x.Exists(envVars[nextEnv])).Returns(true);
129125
_fileHelper.Setup(x => x.GetStream(envVars[nextEnv], FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(envVars[nextEnv]));
@@ -146,7 +142,7 @@ public void GetDotnetPathByArchitecture_GlobalInstallation_Windows(PlatformArchi
146142
Mock<IRegistryKey> nativeArchSubKey = new();
147143
installedVersionKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(architectureSubKey.Object);
148144
architectureSubKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(nativeArchSubKey.Object);
149-
nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>())).Returns(Path.GetDirectoryName(dotnetMuxer));
145+
nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>())).Returns(Path.GetDirectoryName(dotnetMuxer)!);
150146
_windowsRegistrytHelper.Setup(x => x.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)).Returns(installedVersionKey.Object);
151147
_fileHelper.Setup(x => x.Exists(dotnetMuxer)).Returns(true);
152148
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));
@@ -168,13 +164,17 @@ public void GetDotnetPathByArchitecture_GlobalInstallation_NullSubkeys(bool null
168164
Mock<IRegistryKey> installedVersionKey = new();
169165
Mock<IRegistryKey> architectureSubKey = new();
170166
Mock<IRegistryKey> nativeArchSubKey = new();
171-
installedVersionKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(nullArchitecture ? null : architectureSubKey.Object);
172-
architectureSubKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(nullNative ? null : nativeArchSubKey.Object);
173-
nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>())).Returns(nullInstallLocation ? null : "");
174-
_windowsRegistrytHelper.Setup(x => x.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)).Returns(nullInstalledVersion ? null : installedVersionKey.Object);
167+
installedVersionKey.Setup(x => x.OpenSubKey(It.IsAny<string>()))
168+
.Returns(nullArchitecture ? null! : architectureSubKey.Object);
169+
architectureSubKey.Setup(x => x.OpenSubKey(It.IsAny<string>()))
170+
.Returns(nullNative ? null! : nativeArchSubKey.Object);
171+
nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>()))
172+
.Returns(nullInstallLocation ? null! : "");
173+
_windowsRegistrytHelper.Setup(x => x.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
174+
.Returns(nullInstalledVersion ? null! : installedVersionKey.Object);
175175
_environmentVariableHelper.Setup(x => x.GetEnvironmentVariable("ProgramFiles")).Returns("notfound");
176176

177-
//Act & Assert
177+
// Act & Assert
178178
var dotnetHostHelper = new DotnetHostHelper(_fileHelper.Object, _environmentHelper.Object, _windowsRegistrytHelper.Object, _environmentVariableHelper.Object, _processHelper.Object);
179179
Assert.IsFalse(dotnetHostHelper.TryGetDotnetPathByArchitecture(PlatformArchitecture.X64, out string muxerPath));
180180
}
@@ -198,7 +198,7 @@ public void GetDotnetPathByArchitecture_GlobalInstallation_Unix(PlatformArchitec
198198
_environmentHelper.SetupGet(x => x.OperatingSystem).Returns(os);
199199
_fileHelper.Setup(x => x.Exists(installLocation)).Returns(true);
200200
_fileHelper.Setup(x => x.Exists(dotnetMuxer)).Returns(true);
201-
_fileHelper.Setup(x => x.GetStream(installLocation, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer))));
201+
_fileHelper.Setup(x => x.GetStream(installLocation, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer)!)));
202202
if (found)
203203
{
204204
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));
@@ -226,7 +226,7 @@ public void GetDotnetPathByArchitecture_DefaultInstallation_Win(PlatformArchitec
226226
if (found)
227227
{
228228
_fileHelper.Setup(x => x.Exists(dotnetMuxer)).Returns(true);
229-
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer))));
229+
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer)!)));
230230
_fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));
231231
}
232232

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

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

271-
272-
class MockMuxerHelper : IDisposable
271+
private class MockMuxerHelper : IDisposable
273272
{
274273
private static readonly string DotnetMuxerWinX86 = "TestAssets/dotnetWinX86.exe";
275274
private static readonly string DotnetMuxerWinX64 = "TestAssets/dotnetWinX64.exe";
@@ -296,7 +295,7 @@ public string RenameMuxerAndReturnPath(PlatformOperatingSystem platform, Platfor
296295
case PlatformOperatingSystem.Windows:
297296
{
298297
muxerPath = Path.Combine(tmpDirectory, Guid.NewGuid().ToString("N"), subfolder, "dotnet.exe");
299-
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath));
298+
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath)!);
300299
if (architecture == PlatformArchitecture.ARM64)
301300
{
302301
File.Copy(DotnetMuxerWinArm64, muxerPath);
@@ -318,7 +317,7 @@ public string RenameMuxerAndReturnPath(PlatformOperatingSystem platform, Platfor
318317
case PlatformOperatingSystem.OSX:
319318
{
320319
muxerPath = Path.Combine(tmpDirectory, Guid.NewGuid().ToString("N"), subfolder, "dotnet");
321-
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath));
320+
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath)!);
322321
if (architecture == PlatformArchitecture.ARM64)
323322
{
324323
File.Copy(DotnetMuxerMacArm64, muxerPath);
@@ -335,7 +334,7 @@ public string RenameMuxerAndReturnPath(PlatformOperatingSystem platform, Platfor
335334
case PlatformOperatingSystem.Unix:
336335
{
337336
muxerPath = Path.Combine(tmpDirectory, Guid.NewGuid().ToString("N"), subfolder, "dotnet");
338-
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath));
337+
Directory.CreateDirectory(Path.GetDirectoryName(muxerPath)!);
339338
File.WriteAllText(muxerPath, "not supported");
340339
break;
341340
}
@@ -351,7 +350,7 @@ public void Dispose()
351350
{
352351
foreach (var muxer in _muxers)
353352
{
354-
Directory.Delete(Path.GetDirectoryName(muxer), true);
353+
Directory.Delete(Path.GetDirectoryName(muxer)!, true);
355354
}
356355
}
357356
}

test/Microsoft.TestPlatform.CoreUtilities.UnitTests/Helpers/EnvironmentHelperTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
using Microsoft.VisualStudio.TestTools.UnitTesting;
99

10-
#nullable disable
11-
1210
namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Helpers;
1311

1412
[TestClass]

test/Microsoft.TestPlatform.CoreUtilities.UnitTests/Helpers/FileHelperTests.cs

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

9-
#nullable disable
10-
119
namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Helpers;
1210

1311
[TestClass]

test/Microsoft.TestPlatform.CoreUtilities.UnitTests/Output/OutputExtensionsTests.cs

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

99
using Moq;
1010

11-
#nullable disable
12-
1311
namespace Microsoft.TestPlatform.CoreUtilities.UnitTests.Output;
1412

1513
[TestClass]

test/Microsoft.TestPlatform.CoreUtilities.UnitTests/Tracing/EqtTraceTests.cs

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
4+
using System;
55
#if NETFRAMEWORK
66
using System.Diagnostics;
77
#endif
8-
using System;
98
using System.IO;
109

1110
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
1211

13-
/* Unmerged change from project 'Microsoft.TestPlatform.CoreUtilities.UnitTests (net451)'
14-
Before:
15-
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
16-
using System;
17-
After:
18-
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
19-
20-
using System;
21-
*/
2212
using Microsoft.VisualStudio.TestTools.UnitTesting;
2313

24-
#nullable disable
25-
2614
namespace TestPlatform.CoreUtilities.UnitTests;
2715

2816
[TestClass]
2917
public class EqtTraceTests
3018
{
31-
private static string s_dirPath;
32-
private static string s_logFile;
19+
private static string? s_dirPath;
20+
private static string? s_logFile;
3321

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

180-
private string ReadLogFile()
168+
private static string ReadLogFile()
181169
{
182-
string log = null;
170+
string? log = null;
183171
try
184172
{
185-
using var fs = new FileStream(s_logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
173+
using var fs = new FileStream(s_logFile!, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
186174
using var sr = new StreamReader(fs);
187175
log = sr.ReadToEnd();
188176
}
@@ -191,6 +179,7 @@ private string ReadLogFile()
191179
Console.WriteLine(ex.Message);
192180
}
193181

182+
Assert.IsNotNull(log);
194183
return log;
195184
}
196185
}

test/Microsoft.TestPlatform.CoreUtilities.UnitTests/Utilities/JobQueueTests.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
using Microsoft.VisualStudio.TestPlatform.Utilities;
99
using Microsoft.VisualStudio.TestTools.UnitTesting;
1010

11-
#nullable disable
12-
1311
namespace TestPlatform.CoreUtilities.UnitTests;
1412

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

2422
if (jobQueue != null)
@@ -30,7 +28,7 @@ public void ConstructorThrowsWhenNullProcessHandlerIsProvided()
3028
[TestMethod]
3129
public void ThrowsWhenNullEmptyOrWhiteSpaceDisplayNameIsProvided()
3230
{
33-
JobQueue<string> jobQueue = null;
31+
JobQueue<string>? jobQueue = null;
3432
Assert.ThrowsException<ArgumentException>(() => jobQueue = new JobQueue<string>(GetEmptyProcessHandler<string>(), null, int.MaxValue, int.MaxValue, false, (message) => { }));
3533
Assert.ThrowsException<ArgumentException>(() => jobQueue = new JobQueue<string>(GetEmptyProcessHandler<string>(), "", int.MaxValue, int.MaxValue, false, (message) => { }));
3634
Assert.ThrowsException<ArgumentException>(() => jobQueue = new JobQueue<string>(GetEmptyProcessHandler<string>(), " ", int.MaxValue, int.MaxValue, false, (message) => { }));

test/Microsoft.TestPlatform.CoreUtilities.UnitTests/Utilities/TimeSpanParserTests.cs

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

9-
#nullable disable
10-
119
namespace TestPlatform.CoreUtilities.UnitTests;
1210

1311
[TestClass]

0 commit comments

Comments
 (0)