15
15
16
16
using Moq ;
17
17
18
- #nullable disable
19
-
20
18
namespace Microsoft . TestPlatform . CoreUtilities . UnitTests . Helpers ;
21
19
22
20
[ TestClass ]
23
- public class DotnetHostHelperTest : IDisposable
21
+ public sealed class DotnetHostHelperTest : IDisposable
24
22
{
25
23
private readonly Mock < IFileHelper > _fileHelper = new ( ) ;
26
24
private readonly Mock < IProcessHelper > _processHelper = new ( ) ;
@@ -71,13 +69,11 @@ public void GetDotnetPathByArchitecture_EnvVars(PlatformArchitecture targetArchi
71
69
// Arrange
72
70
string dotnetRootX64 = _muxerHelper . RenameMuxerAndReturnPath ( platformSystem , PlatformArchitecture . X64 ) ;
73
71
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 ;
79
75
string dotnetRoot = _muxerHelper . RenameMuxerAndReturnPath ( platformSystem , targetArchitecture ) ;
80
- Dictionary < string , string > envVars = new ( )
76
+ Dictionary < string , string ? > envVars = new ( )
81
77
{
82
78
{ "DOTNET_ROOT_X64" , dotnetRootX64 } ,
83
79
{ "DOTNET_ROOT_ARM64" , dotnetRootArm64 } ,
@@ -87,13 +83,13 @@ public void GetDotnetPathByArchitecture_EnvVars(PlatformArchitecture targetArchi
87
83
88
84
_environmentHelper . SetupGet ( x => x . Architecture ) . Returns ( platformArchitecture ) ;
89
85
_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 ] ) ! ) ;
91
87
_environmentVariableHelper . Setup ( x => x . GetEnvironmentVariable ( "ProgramFiles" ) ) . Returns ( "notfound" ) ;
92
88
_fileHelper . Setup ( x => x . DirectoryExists ( Path . GetDirectoryName ( envVars [ envVar ] ) ) ) . Returns ( true ) ;
93
89
_fileHelper . Setup ( x => x . Exists ( envVars [ envVar ] ) ) . Returns ( true ) ;
94
90
if ( found )
95
91
{
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 ] ! ) ) ;
97
93
}
98
94
99
95
// Act & Assert
@@ -122,8 +118,8 @@ public void GetDotnetPathByArchitecture_EnvVars_DirectoryNotExists_TryNext(strin
122
118
123
119
_environmentHelper . SetupGet ( x => x . Architecture ) . Returns ( platformArchitecture ) ;
124
120
_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 ] ) ! ) ;
127
123
_fileHelper . Setup ( x => x . DirectoryExists ( Path . GetDirectoryName ( envVars [ nextEnv ] ) ) ) . Returns ( true ) ;
128
124
_fileHelper . Setup ( x => x . Exists ( envVars [ nextEnv ] ) ) . Returns ( true ) ;
129
125
_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
146
142
Mock < IRegistryKey > nativeArchSubKey = new ( ) ;
147
143
installedVersionKey . Setup ( x => x . OpenSubKey ( It . IsAny < string > ( ) ) ) . Returns ( architectureSubKey . Object ) ;
148
144
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 ) ! ) ;
150
146
_windowsRegistrytHelper . Setup ( x => x . OpenBaseKey ( RegistryHive . LocalMachine , RegistryView . Registry32 ) ) . Returns ( installedVersionKey . Object ) ;
151
147
_fileHelper . Setup ( x => x . Exists ( dotnetMuxer ) ) . Returns ( true ) ;
152
148
_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
168
164
Mock < IRegistryKey > installedVersionKey = new ( ) ;
169
165
Mock < IRegistryKey > architectureSubKey = new ( ) ;
170
166
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 ) ;
175
175
_environmentVariableHelper . Setup ( x => x . GetEnvironmentVariable ( "ProgramFiles" ) ) . Returns ( "notfound" ) ;
176
176
177
- //Act & Assert
177
+ // Act & Assert
178
178
var dotnetHostHelper = new DotnetHostHelper ( _fileHelper . Object , _environmentHelper . Object , _windowsRegistrytHelper . Object , _environmentVariableHelper . Object , _processHelper . Object ) ;
179
179
Assert . IsFalse ( dotnetHostHelper . TryGetDotnetPathByArchitecture ( PlatformArchitecture . X64 , out string muxerPath ) ) ;
180
180
}
@@ -198,7 +198,7 @@ public void GetDotnetPathByArchitecture_GlobalInstallation_Unix(PlatformArchitec
198
198
_environmentHelper . SetupGet ( x => x . OperatingSystem ) . Returns ( os ) ;
199
199
_fileHelper . Setup ( x => x . Exists ( installLocation ) ) . Returns ( true ) ;
200
200
_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 ) ! ) ) ) ;
202
202
if ( found )
203
203
{
204
204
_fileHelper . Setup ( x => x . GetStream ( dotnetMuxer , FileMode . Open , FileAccess . Read ) ) . Returns ( File . OpenRead ( dotnetMuxer ) ) ;
@@ -226,7 +226,7 @@ public void GetDotnetPathByArchitecture_DefaultInstallation_Win(PlatformArchitec
226
226
if ( found )
227
227
{
228
228
_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 ) ! ) ) ) ;
230
230
_fileHelper . Setup ( x => x . GetStream ( dotnetMuxer , FileMode . Open , FileAccess . Read ) ) . Returns ( File . OpenRead ( dotnetMuxer ) ) ;
231
231
}
232
232
@@ -254,7 +254,7 @@ public void GetDotnetPathByArchitecture_DefaultInstallation_Unix(PlatformArchite
254
254
_environmentHelper . Setup ( x => x . Architecture ) . Returns ( platformArchitecture ) ;
255
255
string expectedMuxerPath = Path . Combine ( expectedFolder , "dotnet" ) ;
256
256
_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 ) ! ) ) ) ;
258
258
if ( found )
259
259
{
260
260
_fileHelper . Setup ( x => x . GetStream ( expectedMuxerPath , FileMode . Open , FileAccess . Read ) ) . Returns ( File . OpenRead ( dotnetMuxer ) ) ;
@@ -268,8 +268,7 @@ public void GetDotnetPathByArchitecture_DefaultInstallation_Unix(PlatformArchite
268
268
269
269
public void Dispose ( ) => _muxerHelper . Dispose ( ) ;
270
270
271
-
272
- class MockMuxerHelper : IDisposable
271
+ private class MockMuxerHelper : IDisposable
273
272
{
274
273
private static readonly string DotnetMuxerWinX86 = "TestAssets/dotnetWinX86.exe" ;
275
274
private static readonly string DotnetMuxerWinX64 = "TestAssets/dotnetWinX64.exe" ;
@@ -296,7 +295,7 @@ public string RenameMuxerAndReturnPath(PlatformOperatingSystem platform, Platfor
296
295
case PlatformOperatingSystem . Windows :
297
296
{
298
297
muxerPath = Path . Combine ( tmpDirectory , Guid . NewGuid ( ) . ToString ( "N" ) , subfolder , "dotnet.exe" ) ;
299
- Directory . CreateDirectory ( Path . GetDirectoryName ( muxerPath ) ) ;
298
+ Directory . CreateDirectory ( Path . GetDirectoryName ( muxerPath ) ! ) ;
300
299
if ( architecture == PlatformArchitecture . ARM64 )
301
300
{
302
301
File . Copy ( DotnetMuxerWinArm64 , muxerPath ) ;
@@ -318,7 +317,7 @@ public string RenameMuxerAndReturnPath(PlatformOperatingSystem platform, Platfor
318
317
case PlatformOperatingSystem . OSX :
319
318
{
320
319
muxerPath = Path . Combine ( tmpDirectory , Guid . NewGuid ( ) . ToString ( "N" ) , subfolder , "dotnet" ) ;
321
- Directory . CreateDirectory ( Path . GetDirectoryName ( muxerPath ) ) ;
320
+ Directory . CreateDirectory ( Path . GetDirectoryName ( muxerPath ) ! ) ;
322
321
if ( architecture == PlatformArchitecture . ARM64 )
323
322
{
324
323
File . Copy ( DotnetMuxerMacArm64 , muxerPath ) ;
@@ -335,7 +334,7 @@ public string RenameMuxerAndReturnPath(PlatformOperatingSystem platform, Platfor
335
334
case PlatformOperatingSystem . Unix :
336
335
{
337
336
muxerPath = Path . Combine ( tmpDirectory , Guid . NewGuid ( ) . ToString ( "N" ) , subfolder , "dotnet" ) ;
338
- Directory . CreateDirectory ( Path . GetDirectoryName ( muxerPath ) ) ;
337
+ Directory . CreateDirectory ( Path . GetDirectoryName ( muxerPath ) ! ) ;
339
338
File . WriteAllText ( muxerPath , "not supported" ) ;
340
339
break ;
341
340
}
@@ -351,7 +350,7 @@ public void Dispose()
351
350
{
352
351
foreach ( var muxer in _muxers )
353
352
{
354
- Directory . Delete ( Path . GetDirectoryName ( muxer ) , true ) ;
353
+ Directory . Delete ( Path . GetDirectoryName ( muxer ) ! , true ) ;
355
354
}
356
355
}
357
356
}
0 commit comments