From 6aaf7e43e5e2ecab27ea32e35b2b1a0fd0c69930 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 22 Jan 2020 00:37:10 +0100 Subject: [PATCH] cleanup PrepareRemoteExecutor() --- .../TestUtilities/TestEnvironment.cs | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index d7d16cfcd..fee05ad1c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; +using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Tests { @@ -46,13 +47,12 @@ namespace SixLabors.ImageSharp.Tests internal static string SolutionDirectoryFullPath => SolutionDirectoryFullPathLazy.Value; + private static readonly FileInfo TestAssemblyFile = + new FileInfo(typeof(TestEnvironment).GetTypeInfo().Assembly.Location); + private static string GetSolutionDirectoryFullPathImpl() { - string assemblyLocation = typeof(TestEnvironment).GetTypeInfo().Assembly.Location; - - var assemblyFile = new FileInfo(assemblyLocation); - - DirectoryInfo directory = assemblyFile.Directory; + DirectoryInfo directory = TestAssemblyFile.Directory; while (!directory.EnumerateFiles(ImageSharpSolutionFileName).Any()) { @@ -63,13 +63,13 @@ namespace SixLabors.ImageSharp.Tests catch (Exception ex) { throw new Exception( - $"Unable to find ImageSharp solution directory from {assemblyLocation} because of {ex.GetType().Name}!", + $"Unable to find ImageSharp solution directory from {TestAssemblyFile} because of {ex.GetType().Name}!", ex); } if (directory == null) { - throw new Exception($"Unable to find ImageSharp solution directory from {assemblyLocation}!"); + throw new Exception($"Unable to find ImageSharp solution directory from {TestAssemblyFile}!"); } } @@ -138,7 +138,7 @@ namespace SixLabors.ImageSharp.Tests /// /// Creates Microsoft.DotNet.RemoteExecutor.exe.config for .NET framework, /// When running in 32 bits, enforces 32 bit execution of Microsoft.DotNet.RemoteExecutor.exe - /// with the help of corflags.exe found in Windows SDK. + /// with the help of CorFlags.exe found in Windows SDK. /// internal static void PrepareRemoteExecutor() { @@ -147,16 +147,16 @@ namespace SixLabors.ImageSharp.Tests return; } - var assemblyFile = new FileInfo(typeof(TestEnvironment).GetTypeInfo().Assembly.Location); string remoteExecutorConfigPath = - Path.Combine(assemblyFile.DirectoryName, "Microsoft.DotNet.RemoteExecutor.exe.config"); + Path.Combine(TestAssemblyFile.DirectoryName, "Microsoft.DotNet.RemoteExecutor.exe.config"); if (File.Exists(remoteExecutorConfigPath)) { + // already prepared return; } - string testProjectConfigPath = assemblyFile.FullName + ".config"; + string testProjectConfigPath = TestAssemblyFile.FullName + ".config"; File.Copy(testProjectConfigPath, remoteExecutorConfigPath); @@ -165,43 +165,43 @@ namespace SixLabors.ImageSharp.Tests return; } - // Locate and run CorFlags.exe: - try - { - string windowsSdksDir = Path.Combine(Environment.GetEnvironmentVariable("PROGRAMFILES(x86)"), - "Microsoft SDKs", "Windows"); + EnsureRemoteExecutorIs32Bit(); + } + + /// + /// Locate and run CorFlags.exe /32Bit+ + /// https://docs.microsoft.com/en-us/dotnet/framework/tools/corflags-exe-corflags-conversion-tool + /// + private static void EnsureRemoteExecutorIs32Bit() + { + string windowsSdksDir = Path.Combine(Environment.GetEnvironmentVariable("PROGRAMFILES(x86)"), + "Microsoft SDKs", "Windows"); - FileInfo corFlagsFile = Find(new DirectoryInfo(windowsSdksDir), "corflags.exe"); + FileInfo corFlagsFile = Find(new DirectoryInfo(windowsSdksDir), "CorFlags.exe"); - string remoteExecutorPath = Path.Combine(assemblyFile.DirectoryName, "Microsoft.DotNet.RemoteExecutor.exe"); + string remoteExecutorPath = Path.Combine(TestAssemblyFile.DirectoryName, "Microsoft.DotNet.RemoteExecutor.exe"); - string args = $"{remoteExecutorPath} /32Bit+ /Force"; + string args = $"{remoteExecutorPath} /32Bit+ /Force"; - var si = new ProcessStartInfo() - { - FileName = corFlagsFile.FullName, - Arguments = args, - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true - }; - - using var proc = Process.Start(si); - proc.WaitForExit(); - string standardOutput = proc.StandardOutput.ReadToEnd(); - string standardError = proc.StandardError.ReadToEnd(); - Debug.Print(standardOutput); - Debug.Print(standardError); - } - catch (Exception ex) + var si = new ProcessStartInfo() { - // Avoid fatal exceptions here - Debug.Print(ex.Message); - } + FileName = corFlagsFile.FullName, + Arguments = args, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true + }; + + using var proc = Process.Start(si); + proc.WaitForExit(); + string standardOutput = proc.StandardOutput.ReadToEnd(); + string standardError = proc.StandardError.ReadToEnd(); + Debug.Print(standardOutput); + Debug.Print(standardError); static FileInfo Find(DirectoryInfo root, string name) { - FileInfo fi = root.EnumerateFiles().FirstOrDefault(f => f.Name.ToLower() == name); + FileInfo fi = root.EnumerateFiles(name).FirstOrDefault(); if (fi != null) { return fi;