How to use TryGetDotnetPathByArchitecture method of Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper class

Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper.TryGetDotnetPathByArchitecture

DotnetTestHostManager.cs

Source:DotnetTestHostManager.cs Github

copy

Full Screen

...387 {388 EqtTrace.Verbose($"DotnetTestHostmanager: Forcing the search to x64 architecure, IsDefaultTargetArchitecture '{_runsettingHelper.IsDefaultTargetArchitecture}' OS '{_platformEnvironment.OperatingSystem}' framework '{_targetFramework}'");389 }390 PlatformArchitecture finalTargetArchitecture = forceToX64 ? PlatformArchitecture.X64 : targetArchitecture;391 if (!_dotnetHostHelper.TryGetDotnetPathByArchitecture(finalTargetArchitecture, out string? muxerPath))392 {393 string message = string.Format(CultureInfo.CurrentCulture, Resources.NoDotnetMuxerFoundForArchitecture, $"dotnet{(_platformEnvironment.OperatingSystem == PlatformOperatingSystem.Windows ? ".exe" : string.Empty)}", finalTargetArchitecture.ToString());394 EqtTrace.Error(message);395 throw new TestPlatformException(message);396 }397 startInfo.FileName = muxerPath;398 }399 EqtTrace.Verbose("DotnetTestHostmanager: Full path of testhost.dll is {0}", testHostPath);400 args = "exec" + args;401 args += " " + testHostPath.AddDoubleQuote();402 }403 EqtTrace.Verbose("DotnetTestHostmanager: Full path of host exe is {0}", startInfo.FileName);404 // Attempt to upgrade netcoreapp2.1 and earlier versions of testhost to netcoreapp3.1 or a newer runtime,405 // assuming that the user does not have that old runtime installed....

Full Screen

Full Screen

DotnetHostHelperTest.cs

Source:DotnetHostHelperTest.cs Github

copy

Full Screen

...31 _environmentHelper.SetupGet(x => x.Architecture).Returns(PlatformArchitecture.X64);32 _processHelper.Setup(x => x.GetCurrentProcessFileName()).Returns(finalMuxerPath);33 _processHelper.Setup(x => x.GetCurrentProcessArchitecture()).Returns(PlatformArchitecture.X64);34 // Act & Assert35 Assert.IsTrue(dotnetHostHelper.TryGetDotnetPathByArchitecture(PlatformArchitecture.X64, out string? muxerPath));36 Assert.AreEqual(finalMuxerPath, muxerPath);37 }38 [DataTestMethod]39 [DataRow(PlatformArchitecture.X86, PlatformArchitecture.X64, PlatformOperatingSystem.Windows, "DOTNET_ROOT(x86)")]40 [DataRow(PlatformArchitecture.X86, PlatformArchitecture.X64, PlatformOperatingSystem.Windows, "DOTNET_ROOT")]41 [DataRow(PlatformArchitecture.ARM64, PlatformArchitecture.X64, PlatformOperatingSystem.Windows, "DOTNET_ROOT(x86)", false)]42 [DataRow(PlatformArchitecture.ARM64, PlatformArchitecture.X64, PlatformOperatingSystem.Windows, "DOTNET_ROOT_ARM64")]43 [DataRow(PlatformArchitecture.ARM64, PlatformArchitecture.X64, PlatformOperatingSystem.Windows, "DOTNET_ROOT")]44 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.X64, PlatformOperatingSystem.Windows, "DOTNET_ROOT(x86)", false)]45 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.ARM64, PlatformOperatingSystem.Windows, "DOTNET_ROOT_X64")]46 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.ARM64, PlatformOperatingSystem.Windows, "DOTNET_ROOT")]47 [DataRow(PlatformArchitecture.ARM64, PlatformArchitecture.X64, PlatformOperatingSystem.OSX, "DOTNET_ROOT_ARM64")]48 [DataRow(PlatformArchitecture.ARM64, PlatformArchitecture.X64, PlatformOperatingSystem.OSX, "DOTNET_ROOT")]49 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.ARM64, PlatformOperatingSystem.OSX, "DOTNET_ROOT_X64")]50 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.ARM64, PlatformOperatingSystem.OSX, "DOTNET_ROOT")]51 public void GetDotnetPathByArchitecture_EnvVars(PlatformArchitecture targetArchitecture,52 PlatformArchitecture platformArchitecture,53 PlatformOperatingSystem platformSystem,54 string envVar,55 bool found = true)56 {57 // Arrange58 string dotnetRootX64 = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.X64);59 string dotnetRootArm64 = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.ARM64);60 string? dotnetRootX86 = platformSystem == PlatformOperatingSystem.Windows61 ? _muxerHelper.RenameMuxerAndReturnPath(platformSystem, PlatformArchitecture.X86)62 : null;63 string dotnetRoot = _muxerHelper.RenameMuxerAndReturnPath(platformSystem, targetArchitecture);64 Dictionary<string, string?> envVars = new()65 {66 { "DOTNET_ROOT_X64", dotnetRootX64 },67 { "DOTNET_ROOT_ARM64", dotnetRootArm64 },68 { "DOTNET_ROOT(x86)", dotnetRootX86 },69 { "DOTNET_ROOT", dotnetRoot },70 };71 _environmentHelper.SetupGet(x => x.Architecture).Returns(platformArchitecture);72 _environmentHelper.SetupGet(x => x.OperatingSystem).Returns(platformSystem);73 _environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(envVar)).Returns(Path.GetDirectoryName(envVars[envVar])!);74 _environmentVariableHelper.Setup(x => x.GetEnvironmentVariable("ProgramFiles")).Returns("notfound");75 _fileHelper.Setup(x => x.DirectoryExists(Path.GetDirectoryName(envVars[envVar])!)).Returns(true);76 _fileHelper.Setup(x => x.Exists(envVars[envVar])).Returns(true);77 if (found)78 {79 _fileHelper.Setup(x => x.GetStream(envVars[envVar]!, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(envVars[envVar]!));80 }81 // Act & Assert82 var dotnetHostHelper = new DotnetHostHelper(_fileHelper.Object, _environmentHelper.Object, _windowsRegistrytHelper.Object, _environmentVariableHelper.Object, _processHelper.Object);83 Assert.AreEqual(found, dotnetHostHelper.TryGetDotnetPathByArchitecture(targetArchitecture, out string? muxerPath));84 Assert.AreEqual(found ? envVars[envVar] : null, muxerPath);85 }86 [DataTestMethod]87 [DataRow("DOTNET_ROOT_ARM64", "DOTNET_ROOT", PlatformArchitecture.ARM64, PlatformArchitecture.X64)]88 [DataRow("DOTNET_ROOT(x86)", "DOTNET_ROOT", PlatformArchitecture.X86, PlatformArchitecture.X64)]89 public void GetDotnetPathByArchitecture_EnvVars_DirectoryNotExists_TryNext(string notExists, string nextEnv, PlatformArchitecture targetAchitecture, PlatformArchitecture platformArchitecture)90 {91 // Arrange92 string dotnetRootX64 = _muxerHelper.RenameMuxerAndReturnPath(PlatformOperatingSystem.Windows, PlatformArchitecture.X64);93 string dotnetRootArm64 = _muxerHelper.RenameMuxerAndReturnPath(PlatformOperatingSystem.Windows, PlatformArchitecture.ARM64);94 string dotnetRootX86 = _muxerHelper.RenameMuxerAndReturnPath(PlatformOperatingSystem.Windows, PlatformArchitecture.X86);95 string dotnetRoot = _muxerHelper.RenameMuxerAndReturnPath(PlatformOperatingSystem.Windows, targetAchitecture);96 Dictionary<string, string> envVars = new()97 {98 { "DOTNET_ROOT_X64", dotnetRootX64 },99 { "DOTNET_ROOT_ARM64", dotnetRootArm64 },100 { "DOTNET_ROOT(x86)", dotnetRootX86 },101 { "DOTNET_ROOT", dotnetRoot },102 };103 _environmentHelper.SetupGet(x => x.Architecture).Returns(platformArchitecture);104 _environmentHelper.SetupGet(x => x.OperatingSystem).Returns(PlatformOperatingSystem.Windows);105 _environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(notExists)).Returns(Path.GetDirectoryName(envVars[notExists])!);106 _environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(nextEnv)).Returns(Path.GetDirectoryName(envVars[nextEnv])!);107 _fileHelper.Setup(x => x.DirectoryExists(Path.GetDirectoryName(envVars[nextEnv])!)).Returns(true);108 _fileHelper.Setup(x => x.Exists(envVars[nextEnv])).Returns(true);109 _fileHelper.Setup(x => x.GetStream(envVars[nextEnv], FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(envVars[nextEnv]));110 //Act & Assert111 var dotnetHostHelper = new DotnetHostHelper(_fileHelper.Object, _environmentHelper.Object, _windowsRegistrytHelper.Object, _environmentVariableHelper.Object, _processHelper.Object);112 Assert.IsTrue(dotnetHostHelper.TryGetDotnetPathByArchitecture(targetAchitecture, out string? muxerPath));113 Assert.AreEqual(envVars[nextEnv], muxerPath);114 }115 [DataTestMethod]116 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.X64, true)]117 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.X86, false)]118 public void GetDotnetPathByArchitecture_GlobalInstallation_Windows(PlatformArchitecture muxerArchitecture, PlatformArchitecture targetArchitecture, bool found)119 {120 // Arrange121 string dotnetMuxer = _muxerHelper.RenameMuxerAndReturnPath(PlatformOperatingSystem.Windows, muxerArchitecture);122 Mock<IRegistryKey> installedVersionKey = new();123 Mock<IRegistryKey> architectureSubKey = new();124 Mock<IRegistryKey> nativeArchSubKey = new();125 installedVersionKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(architectureSubKey.Object);126 architectureSubKey.Setup(x => x.OpenSubKey(It.IsAny<string>())).Returns(nativeArchSubKey.Object);127 nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>())).Returns(Path.GetDirectoryName(dotnetMuxer)!);128 _windowsRegistrytHelper.Setup(x => x.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)).Returns(installedVersionKey.Object);129 _fileHelper.Setup(x => x.Exists(dotnetMuxer)).Returns(true);130 _fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));131 //Act & Assert132 var dotnetHostHelper = new DotnetHostHelper(_fileHelper.Object, _environmentHelper.Object, _windowsRegistrytHelper.Object, _environmentVariableHelper.Object, _processHelper.Object);133 Assert.AreEqual(found, dotnetHostHelper.TryGetDotnetPathByArchitecture(targetArchitecture, out string? muxerPath));134 Assert.AreEqual(found ? dotnetMuxer : null, muxerPath);135 }136 [DataTestMethod]137 [DataRow(true, false, false, false)]138 [DataRow(false, true, false, false)]139 [DataRow(false, false, true, false)]140 [DataRow(false, false, false, true)]141 public void GetDotnetPathByArchitecture_GlobalInstallation_NullSubkeys(bool nullInstalledVersion, bool nullArchitecture, bool nullNative, bool nullInstallLocation)142 {143 // Arrange144 Mock<IRegistryKey> installedVersionKey = new();145 Mock<IRegistryKey> architectureSubKey = new();146 Mock<IRegistryKey> nativeArchSubKey = new();147 installedVersionKey.Setup(x => x.OpenSubKey(It.IsAny<string>()))148 .Returns(nullArchitecture ? null! : architectureSubKey.Object);149 architectureSubKey.Setup(x => x.OpenSubKey(It.IsAny<string>()))150 .Returns(nullNative ? null! : nativeArchSubKey.Object);151 nativeArchSubKey.Setup(x => x.GetValue(It.IsAny<string>()))152 .Returns(nullInstallLocation ? null! : "");153 _windowsRegistrytHelper.Setup(x => x.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))154 .Returns(nullInstalledVersion ? null! : installedVersionKey.Object);155 _environmentVariableHelper.Setup(x => x.GetEnvironmentVariable("ProgramFiles")).Returns("notfound");156 // Act & Assert157 var dotnetHostHelper = new DotnetHostHelper(_fileHelper.Object, _environmentHelper.Object, _windowsRegistrytHelper.Object, _environmentVariableHelper.Object, _processHelper.Object);158 Assert.IsFalse(dotnetHostHelper.TryGetDotnetPathByArchitecture(PlatformArchitecture.X64, out string? muxerPath));159 }160 [DataTestMethod]161 [DataRow(PlatformArchitecture.ARM64, "/etc/dotnet/install_location_arm64", true, PlatformOperatingSystem.OSX)]162 [DataRow(PlatformArchitecture.X64, "/etc/dotnet/install_location_x64", true, PlatformOperatingSystem.OSX)]163 [DataRow(PlatformArchitecture.ARM64, "/etc/dotnet/install_location", true, PlatformOperatingSystem.OSX)]164 [DataRow(PlatformArchitecture.X64, "/etc/dotnet/install_location", true, PlatformOperatingSystem.OSX)]165 [DataRow(PlatformArchitecture.ARM64, "/etc/dotnet/install_location_x64", false, PlatformOperatingSystem.OSX)]166 [DataRow(PlatformArchitecture.ARM64, "/etc/dotnet/install_location_arm64", false, PlatformOperatingSystem.Unix)]167 [DataRow(PlatformArchitecture.X64, "/etc/dotnet/install_location_x64", false, PlatformOperatingSystem.Unix)]168 [DataRow(PlatformArchitecture.ARM64, "/etc/dotnet/install_location", false, PlatformOperatingSystem.Unix)]169 [DataRow(PlatformArchitecture.X64, "/etc/dotnet/install_location", false, PlatformOperatingSystem.Unix)]170 [DataRow(PlatformArchitecture.ARM64, "/etc/dotnet/install_location_x64", false, PlatformOperatingSystem.Unix)]171 public void GetDotnetPathByArchitecture_GlobalInstallation_Unix(PlatformArchitecture targetArchitecture, string installLocation, bool found, PlatformOperatingSystem os)172 {173 // Arrange174 string dotnetMuxer = _muxerHelper.RenameMuxerAndReturnPath(os, targetArchitecture);175 _environmentHelper.SetupGet(x => x.OperatingSystem).Returns(os);176 _fileHelper.Setup(x => x.Exists(installLocation)).Returns(true);177 _fileHelper.Setup(x => x.Exists(dotnetMuxer)).Returns(true);178 _fileHelper.Setup(x => x.GetStream(installLocation, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer)!)));179 if (found)180 {181 _fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));182 }183 //Act & Assert184 var dotnetHostHelper = new DotnetHostHelper(_fileHelper.Object, _environmentHelper.Object, _windowsRegistrytHelper.Object, _environmentVariableHelper.Object, _processHelper.Object);185 Assert.AreEqual(found, dotnetHostHelper.TryGetDotnetPathByArchitecture(targetArchitecture, out string? muxerPath));186 Assert.AreEqual(found ? dotnetMuxer : null, muxerPath);187 }188 [DataTestMethod]189 [DataRow(PlatformArchitecture.X86, PlatformArchitecture.X64, "ProgramFiles(x86)", "dotnet", true)]190 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.ARM64, "ProgramFiles", @"dotnet\x64", true)]191 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.X64, "ProgramFiles", "dotnet", true)]192 [DataRow(PlatformArchitecture.X86, PlatformArchitecture.X86, "ProgramFiles", "dotnet", true)]193 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.X64, "ProgramFiles", "dotnet", false)]194 [TestCategory("Windows")]195 public void GetDotnetPathByArchitecture_DefaultInstallation_Win(PlatformArchitecture targetArchitecture, PlatformArchitecture platformArchitecture, string envVar, string subfolder, bool found)196 {197 // Arrange198 string dotnetMuxer = _muxerHelper.RenameMuxerAndReturnPath(PlatformOperatingSystem.Windows, targetArchitecture, subfolder);199 _environmentVariableHelper.Setup(x => x.GetEnvironmentVariable(envVar)).Returns(dotnetMuxer.Replace(Path.Combine(subfolder, "dotnet.exe"), string.Empty));200 _environmentHelper.Setup(x => x.Architecture).Returns(platformArchitecture);201 if (found)202 {203 _fileHelper.Setup(x => x.Exists(dotnetMuxer)).Returns(true);204 _fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer)!)));205 _fileHelper.Setup(x => x.GetStream(dotnetMuxer, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));206 }207 //Act & Assert208 var dotnetHostHelper = new DotnetHostHelper(_fileHelper.Object, _environmentHelper.Object, _windowsRegistrytHelper.Object, _environmentVariableHelper.Object, _processHelper.Object);209 Assert.AreEqual(found, dotnetHostHelper.TryGetDotnetPathByArchitecture(targetArchitecture, out string? muxerPath));210 Assert.AreEqual(found ? dotnetMuxer : null, muxerPath);211 }212 [DataTestMethod]213 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.X64, "/usr/local/share/dotnet", "", true, PlatformOperatingSystem.OSX)]214 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.ARM64, "/usr/local/share/dotnet/x64", "", true, PlatformOperatingSystem.OSX)]215 [DataRow(PlatformArchitecture.ARM64, PlatformArchitecture.X64, "/usr/local/share/dotnet", "", true, PlatformOperatingSystem.OSX)]216 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.X64, "/usr/local/share/dotnet", "", false, PlatformOperatingSystem.OSX)]217 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.X64, "/usr/share/dotnet", "", false, PlatformOperatingSystem.Unix)]218 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.ARM64, "/usr/share/dotnet/x64", "", false, PlatformOperatingSystem.Unix)]219 [DataRow(PlatformArchitecture.ARM64, PlatformArchitecture.X64, "/usr/share/dotnet", "", false, PlatformOperatingSystem.Unix)]220 [DataRow(PlatformArchitecture.X64, PlatformArchitecture.X64, "/usr/share/dotnet", "", false, PlatformOperatingSystem.Unix)]221 public void GetDotnetPathByArchitecture_DefaultInstallation_Unix(PlatformArchitecture targetArchitecture, PlatformArchitecture platformArchitecture, string expectedFolder, string subfolder, bool found, PlatformOperatingSystem os)222 {223 // Arrange224 string dotnetMuxer = _muxerHelper.RenameMuxerAndReturnPath(os, targetArchitecture, subfolder);225 _environmentHelper.SetupGet(x => x.OperatingSystem).Returns(os);226 _environmentHelper.Setup(x => x.Architecture).Returns(platformArchitecture);227 string expectedMuxerPath = Path.Combine(expectedFolder, "dotnet");228 _fileHelper.Setup(x => x.Exists(expectedMuxerPath)).Returns(true);229 _fileHelper.Setup(x => x.GetStream(expectedMuxerPath, FileMode.Open, FileAccess.Read)).Returns(new MemoryStream(Encoding.UTF8.GetBytes(Path.GetDirectoryName(dotnetMuxer)!)));230 if (found)231 {232 _fileHelper.Setup(x => x.GetStream(expectedMuxerPath, FileMode.Open, FileAccess.Read)).Returns(File.OpenRead(dotnetMuxer));233 }234 //Act & Assert235 var dotnetHostHelper = new DotnetHostHelper(_fileHelper.Object, _environmentHelper.Object, _windowsRegistrytHelper.Object, _environmentVariableHelper.Object, _processHelper.Object);236 Assert.AreEqual(found, dotnetHostHelper.TryGetDotnetPathByArchitecture(targetArchitecture, out string? muxerPath));237 Assert.AreEqual(found ? expectedMuxerPath : null, muxerPath);238 }239 public void Dispose() => _muxerHelper.Dispose();240 private class MockMuxerHelper : IDisposable241 {242 private static readonly string DotnetMuxerWinX86 = "TestAssets/dotnetWinX86.exe";243 private static readonly string DotnetMuxerWinX64 = "TestAssets/dotnetWinX64.exe";244 private static readonly string DotnetMuxerWinArm64 = "TestAssets/dotnetWinArm64.exe";245 private static readonly string DotnetMuxerMacArm64 = "TestAssets/dotnetMacArm64";246 private static readonly string DotnetMuxerMacX64 = "TestAssets/dotnetMacX64";247 private readonly List<string> _muxers = new();248 public MockMuxerHelper()249 {250 Assert.IsTrue(File.Exists(DotnetMuxerWinX86));...

Full Screen

Full Screen

DotnetHostHelper.cs

Source:DotnetHostHelper.cs Github

copy

Full Screen

...99 }100 }101 return false;102 }103 public bool TryGetDotnetPathByArchitecture(PlatformArchitecture targetArchitecture, [NotNullWhen(true)] out string? muxerPath)104 {105 // If current process is the same as the target architecture we return the current process filename.106 if (_processHelper.GetCurrentProcessArchitecture() == targetArchitecture)107 {108 string currentProcessFileName = _processHelper.GetCurrentProcessFileName()!;109 if (Path.GetFileName(currentProcessFileName) == _muxerName)110 {111 muxerPath = currentProcessFileName;112 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Target architecture is the same as the current process architecture '{targetArchitecture}', and the current process is a muxer, using that: '{muxerPath}'");113 return true;114 }115 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Target architecture is the same as the current process architecture '{targetArchitecture}', but the current process is not a muxer: '{currentProcessFileName}'");116 }117 // We used similar approach as the runtime resolver.118 // https://github.com/dotnet/runtime/blob/main/src/native/corehost/fxr_resolver.cpp#L55119 bool isWinOs = _environment.OperatingSystem == PlatformOperatingSystem.Windows;120 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Searching for muxer named '{_muxerName}'");121 // Try to search using env vars in the order122 // DOTNET_ROOT_{arch}123 // DOTNET_ROOT(x86) if X86 on Win (here we cannot check if current process is WOW64 because this is SDK process arch and not real host arch so it's irrelevant)124 // "DOTNET_ROOT(x86) is used instead when running a 32-bit executable on a 64-bit OS."125 // DOTNET_ROOT126 string envKey = $"DOTNET_ROOT_{targetArchitecture.ToString().ToUpperInvariant()}";127 // Try on arch specific env var128 string? envVar = _environmentVariableHelper.GetEnvironmentVariable(envKey);129 // Try on non virtualized x86 var(should happen only on non-x86 architecture)130 if ((envVar == null || !_fileHelper.DirectoryExists(envVar)) &&131 targetArchitecture == PlatformArchitecture.X86 && _environment.OperatingSystem == PlatformOperatingSystem.Windows)132 {133 envKey = $"DOTNET_ROOT(x86)";134 envVar = _environmentVariableHelper.GetEnvironmentVariable(envKey);135 }136 // Try on default DOTNET_ROOT137 if (envVar == null || !_fileHelper.DirectoryExists(envVar))138 {139 envKey = "DOTNET_ROOT";140 envVar = _environmentVariableHelper.GetEnvironmentVariable(envKey);141 }142 if (envVar != null)143 {144 // If directory specified by env vars does not exists, it's like env var doesn't exists as well.145 if (!_fileHelper.DirectoryExists(envVar))146 {147 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Folder specified by env variable does not exist: '{envVar}={envKey}'");148 }149 else150 {151 muxerPath = Path.Combine(envVar, _muxerName);152 if (!_fileHelper.Exists(muxerPath))153 {154 // If environment variable was specified, and the directory it points at exists, but it does not contain a muxer, or the muxer is incompatible with the target architecture155 // we stop the search to be compliant with the approach that apphost (compiled .NET executables) use to find the muxer.156 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Folder specified by env variable does not contain any muxer: '{envVar}={envKey}'");157 muxerPath = null;158 return false;159 }160 if (!IsValidArchitectureMuxer(targetArchitecture, muxerPath))161 {162 EqtTrace.Verbose($"DotnetHostHelper: Invalid muxer resolved using env var key '{envKey}' in '{envVar}'");163 muxerPath = null;164 return false;165 }166 EqtTrace.Verbose($"DotnetHostHelper.TryGetDotnetPathByArchitecture: Muxer compatible with '{targetArchitecture}' resolved from env variable '{envKey}' in '{muxerPath}'");167 return true;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 }...

Full Screen

Full Screen

TryGetDotnetPathByArchitecture

Using AI Code Generation

copy

Full Screen

1{2 {3 static void Main(string[] args)4 {5 string dotnetPath = Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper.TryGetDotnetPathByArchitecture();6 Console.WriteLine(dotnetPath);7 Console.ReadLine();8 }9 }10}

Full Screen

Full Screen

TryGetDotnetPathByArchitecture

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;7using Microsoft.VisualStudio.TestPlatform.ObjectModel;8using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;9using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;10{11 {12 static void Main(string[] args)13 {14 var dotnetHostHelper = new DotnetHostHelper(new FileHelper());15 var dotnetPath = dotnetHostHelper.TryGetDotnetPathByArchitecture(Architecture.X64);16 Console.WriteLine(dotnetPath);17 Console.ReadLine();18 }19 }20}

Full Screen

Full Screen

TryGetDotnetPathByArchitecture

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;4using Microsoft.VisualStudio.TestPlatform.ObjectModel;5using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;6{7 {8 static void Main(string[] args)9 {10 var dotnetPath = DotnetHostHelper.TryGetDotnetPathByArchitecture(Architecture.X64);11 Console.WriteLine(dotnetPath);12 Console.ReadLine();13 }14 }15}16using System;17using System.IO;18using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;19using Microsoft.VisualStudio.TestPlatform.ObjectModel;20using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;21{22 {23 static void Main(string[] args)24 {25 var dotnetPath = DotnetHostHelper.GetDotnetPath();26 Console.WriteLine(dotnetPath);27 Console.ReadLine();28 }29 }30}31using System;32using System.IO;33using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;34using Microsoft.VisualStudio.TestPlatform.ObjectModel;35using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;36{37 {38 static void Main(string[] args)39 {40 var dotnetPath = DotnetHostHelper.GetDotnetExePath();41 Console.WriteLine(dotnetPath);42 Console.ReadLine();43 }44 }45}46using System;47using System.IO;48using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;49using Microsoft.VisualStudio.TestPlatform.ObjectModel;50using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;51{52 {53 static void Main(string[] args)54 {55 var dotnetPath = DotnetHostHelper.GetDotnetHostPath();56 Console.WriteLine(dotnetPath);57 Console.ReadLine();58 }59 }60}61using System;62using System.IO;

Full Screen

Full Screen

TryGetDotnetPathByArchitecture

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;2using System;3using System.IO;4{5 {6 static void Main(string[] args)7 {8 var path = DotnetHostHelper.TryGetDotnetPathByArchitecture(DotnetHostArchitecture.x64);9 Console.WriteLine("Dotnet path: " + path);10 Console.ReadKey();11 }12 }13}14using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;15using System;16using System.IO;17{18 {19 static void Main(string[] args)20 {21 var path = DotnetHostHelper.TryGetDotnetPathByArchitecture(DotnetHostArchitecture.x86);22 Console.WriteLine("Dotnet path: " + path);23 Console.ReadKey();24 }25 }26}27Dotnet path: C:\Program Files (x86)\dotnet\dotnet.exe

Full Screen

Full Screen

TryGetDotnetPathByArchitecture

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;2using System;3using System.IO;4using System.Reflection;5{6 {7 static void Main(string[] args)8 {9 var assemblyPath = Path.GetDirectoryName(typeof(Program).GetTypeInfo().Assembly.Location);10 var dotnetPath = DotnetHostHelper.TryGetDotnetPathByArchitecture(assemblyPath);11 Console.WriteLine(dotnetPath);12 }13 }14}15using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;16using System;17using System.IO;18using System.Reflection;19{20 {21 static void Main(string[] args)22 {23 var assemblyPath = Path.GetDirectoryName(typeof(Program).GetTypeInfo().Assembly.Location);24 var dotnetPath = DotnetHostHelper.GetDotnetPath(assemblyPath);25 Console.WriteLine(dotnetPath);26 }27 }28}29using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;30using System;31using System.IO;32using System.Reflection;33{34 {35 static void Main(string[] args)36 {37 var assemblyPath = Path.GetDirectoryName(typeof(Program).GetTypeInfo().Assembly.Location);38 var dotnetPath = DotnetHostHelper.GetDotnetHostPath(assemblyPath);39 Console.WriteLine(dotnetPath);40 }41 }42}43using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;44using System;45using System.IO;46using System.Reflection;47{48 {49 static void Main(string[] args)50 {51 var assemblyPath = Path.GetDirectoryName(typeof(Program).GetTypeInfo().Assembly.Location);52 var dotnetPath = DotnetHostHelper.GetDotnetHostPath(assemblyPath);53 Console.WriteLine(dotnetPath);54 }55 }56}

Full Screen

Full Screen

TryGetDotnetPathByArchitecture

Using AI Code Generation

copy

Full Screen

1var dotnetPath = DotnetHostHelper.TryGetDotnetPathByArchitecture(architecture);2Console.WriteLine(dotnetPath);3var dotnetPath = DotnetHostHelper.GetDotnetPath();4Console.WriteLine(dotnetPath);5var dotnetPath = DotnetHostHelper.GetDotnetHostPath();6Console.WriteLine(dotnetPath);7var dotnetPath = DotnetHostHelper.GetDotnetExePath();8Console.WriteLine(dotnetPath);9var dotnetPath = DotnetHostHelper.GetDotnetExeName();10Console.WriteLine(dotnetPath);11var dotnetPath = DotnetHostHelper.GetDotnetExeFileName();12Console.WriteLine(dotnetPath);13var dotnetPath = DotnetHostHelper.GetDotnetExeFileNameWithExtension();14Console.WriteLine(dotnetPath);15var dotnetPath = DotnetHostHelper.GetDotnetExePath();16Console.WriteLine(dotnetPath);17var dotnetPath = DotnetHostHelper.GetDotnetExePath();18Console.WriteLine(dotnetPath);

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful