From 39c993d1c05ff286ff6b5daf18c4e895227a313c Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 29 Feb 2020 01:07:03 +0100 Subject: [PATCH] implicit & safer execution of PrepareRemoteExecutor --- .../Formats/Jpg/JpegDecoderTests.cs | 5 --- .../ArrayPoolMemoryAllocatorTests.cs | 5 --- .../Processors/Convolution/BokehBlurTest.cs | 5 --- .../Attributes/ImageDataAttributeBase.cs | 7 ++++ .../TestUtilities/TestEnvironment.cs | 32 ++++++++++++++++--- 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 103c9d077..25cf5dd37 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -27,11 +27,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg private const float ProgressiveTolerance = 0.2F / 100; - static JpegDecoderTests() - { - TestEnvironment.PrepareRemoteExecutor(); - } - private static ImageComparer GetImageComparer(TestImageProvider provider) where TPixel : unmanaged, IPixel { diff --git a/tests/ImageSharp.Tests/Memory/Allocators/ArrayPoolMemoryAllocatorTests.cs b/tests/ImageSharp.Tests/Memory/Allocators/ArrayPoolMemoryAllocatorTests.cs index 1e079fcf5..8db79fca0 100644 --- a/tests/ImageSharp.Tests/Memory/Allocators/ArrayPoolMemoryAllocatorTests.cs +++ b/tests/ImageSharp.Tests/Memory/Allocators/ArrayPoolMemoryAllocatorTests.cs @@ -28,11 +28,6 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators /// private static MemoryAllocatorFixture StaticFixture { get; } = new MemoryAllocatorFixture(); - static ArrayPoolMemoryAllocatorTests() - { - TestEnvironment.PrepareRemoteExecutor(); - } - public class BufferTests : BufferTestSuite { public BufferTests() diff --git a/tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs index ebbecab93..9dc135016 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs @@ -19,11 +19,6 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution { public class BokehBlurTest { - static BokehBlurTest() - { - TestEnvironment.PrepareRemoteExecutor(); - } - private static readonly string Components10x2 = @" [[ 0.00451261+0.0165137j 0.02161237-0.00299122j 0.00387479-0.02682816j -0.02752798-0.01788438j -0.03553877+0.0154543j -0.01428268+0.04224722j diff --git a/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs index 976bb9a96..85b178c73 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs @@ -18,6 +18,13 @@ namespace SixLabors.ImageSharp.Tests protected readonly PixelTypes PixelTypes; + static ImageDataAttributeBase() + { + // ImageDataAttributes are used in almost all tests, thus a good place to enforce the execution of + // TestEnvironment static constructor before anything else is done. + TestEnvironment.EnsureSharedInitializersDone(); + } + /// /// Initializes a new instance of the class. /// diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index ea880cb38..4152d3bc6 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -34,6 +34,11 @@ namespace SixLabors.ImageSharp.Tests private static readonly Lazy NetCoreVersionLazy = new Lazy(GetNetCoreVersion); + static TestEnvironment() + { + PrepareRemoteExecutor(); + } + /// /// Gets the .NET Core version, if running on .NET Core, otherwise returns an empty string. /// @@ -111,6 +116,13 @@ namespace SixLabors.ImageSharp.Tests internal static bool IsFramework => string.IsNullOrEmpty(NetCoreVersion); + /// + /// A dummy operation to enforce the execution of the static constructor. + /// + internal static void EnsureSharedInitializersDone() + { + } + /// /// Creates the image output directory. /// @@ -141,7 +153,7 @@ namespace SixLabors.ImageSharp.Tests /// When running in 32 bits, enforces 32 bit execution of Microsoft.DotNet.RemoteExecutor.exe /// with the help of CorFlags.exe found in Windows SDK. /// - internal static void PrepareRemoteExecutor() + private static void PrepareRemoteExecutor() { if (!IsFramework) { @@ -153,12 +165,11 @@ namespace SixLabors.ImageSharp.Tests if (File.Exists(remoteExecutorConfigPath)) { - // already prepared + // Already initialized return; } string testProjectConfigPath = TestAssemblyFile.FullName + ".config"; - File.Copy(testProjectConfigPath, remoteExecutorConfigPath); if (Is64BitProcess) @@ -184,7 +195,17 @@ namespace SixLabors.ImageSharp.Tests string remoteExecutorPath = Path.Combine(TestAssemblyFile.DirectoryName, "Microsoft.DotNet.RemoteExecutor.exe"); - string args = $"{remoteExecutorPath} /32Bit+ /Force"; + string remoteExecutorTmpPath = $"{remoteExecutorPath}._tmp"; + + if (File.Exists(remoteExecutorTmpPath)) + { + // Already initialized + return; + } + + File.Copy(remoteExecutorPath, remoteExecutorTmpPath); + + string args = $"{remoteExecutorTmpPath} /32Bit+ /Force"; var si = new ProcessStartInfo() { @@ -206,6 +227,9 @@ namespace SixLabors.ImageSharp.Tests $@"Failed to run {si.FileName} {si.Arguments}:\n STDOUT: {standardOutput}\n STDERR: {standardError}"); } + File.Delete(remoteExecutorPath); + File.Copy(remoteExecutorTmpPath, remoteExecutorPath); + static FileInfo Find(DirectoryInfo root, string name) { FileInfo fi = root.EnumerateFiles(name).FirstOrDefault();