Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper.GetMuxerFromGlobalRegistrationOnUnix
DotnetHostHelper.cs
Source:DotnetHostHelper.cs
...168 }169 }170 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Muxer was not found using DOTNET_ROOT* env variables.");171 // Try to search for global registration172 muxerPath = isWinOs ? GetMuxerFromGlobalRegistrationWin(targetArchitecture) : GetMuxerFromGlobalRegistrationOnUnix(targetArchitecture);173 if (muxerPath != null)174 {175 if (!_fileHelper.Exists(muxerPath))176 {177 // If muxer doesn't exists or it's wrong we stop the search178 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Muxer file not found for global registration '{muxerPath}'");179 muxerPath = null;180 return false;181 }182 if (!IsValidArchitectureMuxer(targetArchitecture, muxerPath))183 {184 // If muxer is wrong we stop the search185 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Muxer resolved using global registration is not compatible with the target architecture: '{muxerPath}'");186 muxerPath = null;187 return false;188 }189 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Muxer compatible with '{targetArchitecture}' resolved from global registration: '{muxerPath}'");190 return true;191 }192 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Muxer not found using global registrations");193 // Try searching in default installation location if it exists194 if (isWinOs)195 {196 // If we're on x64/arm64 SDK and target is x86 we need to search on non virtualized windows folder197 if ((_environment.Architecture == PlatformArchitecture.X64 || _environment.Architecture == PlatformArchitecture.ARM64) &&198 targetArchitecture == PlatformArchitecture.X86)199 {200 muxerPath = Path.Combine(_environmentVariableHelper.GetEnvironmentVariable("ProgramFiles(x86)")!, "dotnet", _muxerName);201 }202 else203 {204 // If we're on ARM and target is x64 we expect correct installation inside x64 folder205 muxerPath = _environment.Architecture == PlatformArchitecture.ARM64 && targetArchitecture == PlatformArchitecture.X64206 ? Path.Combine(_environmentVariableHelper.GetEnvironmentVariable("ProgramFiles")!, "dotnet", "x64", _muxerName)207 : Path.Combine(_environmentVariableHelper.GetEnvironmentVariable("ProgramFiles")!, "dotnet", _muxerName);208 }209 }210 else211 {212 if (_environment.OperatingSystem == PlatformOperatingSystem.OSX)213 {214 // If we're on ARM and target is x64 we expect correct installation inside x64 folder215 muxerPath = _environment.Architecture == PlatformArchitecture.ARM64 && targetArchitecture == PlatformArchitecture.X64216 ? Path.Combine("/usr/local/share/dotnet/x64", _muxerName)217 : Path.Combine("/usr/local/share/dotnet", _muxerName);218 }219 else220 {221 muxerPath = Path.Combine("/usr/share/dotnet", _muxerName);222 }223 }224 if (!_fileHelper.Exists(muxerPath))225 {226 // If muxer doesn't exists we stop the search227 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Muxer was not found in default installation location: '{muxerPath}'");228 muxerPath = null;229 return false;230 }231 if (!IsValidArchitectureMuxer(targetArchitecture, muxerPath))232 {233 // If muxer is wrong we stop the search234 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Muxer resolved in default installation path is not compatible with the target architecture: '{muxerPath}'");235 muxerPath = null;236 return false;237 }238 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Muxer compatible with '{targetArchitecture}' resolved from default installation path: '{muxerPath}'");239 return true;240 }241 private string? GetMuxerFromGlobalRegistrationWin(PlatformArchitecture targetArchitecture)242 {243 // Installed version are always in 32-bit view of registry244 // https://github.com/dotnet/designs/blob/main/accepted/2020/install-locations.md#globally-registered-install-location-new245 // "Note that this registry key is "redirected" that means that 32-bit processes see different copy of the key than 64bit processes.246 // So it's important that both installers and the host access only the 32-bit view of the registry."247 using IRegistryKey? hklm = _windowsRegistryHelper.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);248 if (hklm == null)249 {250 EqtTrace.Verbose($@"DotnetHostHelper.GetMuxerFromGlobalRegistrationWin: Missing SOFTWARE\dotnet\Setup\InstalledVersions subkey");251 return null;252 }253 using IRegistryKey? dotnetInstalledVersion = hklm.OpenSubKey(@"SOFTWARE\dotnet\Setup\InstalledVersions");254 if (dotnetInstalledVersion == null)255 {256 EqtTrace.Verbose($@"DotnetHostHelper.GetMuxerFromGlobalRegistrationWin: Missing RegistryHive.LocalMachine for RegistryView.Registry32");257 return null;258 }259 using IRegistryKey? nativeArch = dotnetInstalledVersion.OpenSubKey(targetArchitecture.ToString().ToLowerInvariant());260 string? installLocation = nativeArch?.GetValue("InstallLocation")?.ToString();261 if (installLocation == null)262 {263 EqtTrace.Verbose($@"DotnetHostHelper.GetMuxerFromGlobalRegistrationWin: Missing registry InstallLocation");264 return null;265 }266 string path = Path.Combine(installLocation.Trim(), _muxerName);267 EqtTrace.Verbose($@"DotnetHostHelper.GetMuxerFromGlobalRegistrationWin: Muxer resolved using win registry key 'SOFTWARE\dotnet\Setup\InstalledVersions\{targetArchitecture.ToString().ToLowerInvariant()}\InstallLocation' in '{path}'");268 return path;269 }270 private string? GetMuxerFromGlobalRegistrationOnUnix(PlatformArchitecture targetArchitecture)271 {272 string baseInstallLocation = "/etc/dotnet/";273 // We search for architecture specific installation274 string installLocation = $"{baseInstallLocation}install_location_{targetArchitecture.ToString().ToLowerInvariant()}";275 // We try to load archless install location file276 if (!_fileHelper.Exists(installLocation))277 {278 installLocation = $"{baseInstallLocation}install_location";279 }280 if (!_fileHelper.Exists(installLocation))281 {282 return null;283 }284 try285 {286 using Stream stream = _fileHelper.GetStream(installLocation, FileMode.Open, FileAccess.Read);287 using StreamReader streamReader = new(stream);288 string content = streamReader.ReadToEnd().Trim();289 EqtTrace.Verbose($"DotnetHostHelper: '{installLocation}' content '{content}'");290 string path = Path.Combine(content, _muxerName);291 EqtTrace.Verbose($"DotnetHostHelper: Muxer resolved using '{installLocation}' in '{path}'");292 return path;293 }294 catch (Exception ex)295 {296 EqtTrace.Error($"DotnetHostHelper.GetMuxerFromGlobalRegistrationOnUnix: Exception during '{installLocation}' muxer resolution.\n{ex}");297 }298 return null;299 }300 private PlatformArchitecture? GetMuxerArchitectureByPEHeaderOnWin(string path)301 {302 try303 {304 using Stream stream = _fileHelper.GetStream(path, FileMode.Open, FileAccess.Read);305 using PEReader peReader = new(stream);306 switch (peReader.PEHeaders.CoffHeader.Machine)307 {308 case Machine.Amd64:309 return PlatformArchitecture.X64;310 case Machine.IA64:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!