diff --git a/tests/ImageSharp.Sandbox46/Tests/TestUtilities/Integration/ReferencePngEncoder.cs b/tests/ImageSharp.Sandbox46/Tests/TestUtilities/Integration/ReferencePngEncoder.cs deleted file mode 100644 index 806cb05be..000000000 --- a/tests/ImageSharp.Sandbox46/Tests/TestUtilities/Integration/ReferencePngEncoder.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace ImageSharp.Tests.TestUtilities.Integration -{ - using System.IO; - - using ImageSharp.Formats; - using ImageSharp.PixelFormats; - - public class ReferencePngEncoder : IImageEncoder - { - public static ReferencePngEncoder Instance { get; } = new ReferencePngEncoder(); - - public void Encode(Image image, Stream stream, IEncoderOptions options) - where TPixel : struct, IPixel - { - using (System.Drawing.Bitmap sdBitmap = IntegrationTestUtils.ToSystemDrawingBitmap(image)) - { - sdBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); - } - } - } -} diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index 59c5813ad..a4df150e5 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -6,6 +6,7 @@ namespace ImageSharp.Tests { using System; + using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; @@ -40,12 +41,8 @@ namespace ImageSharp.Tests /// public string TestName { get; set; } = string.Empty; - /// - /// Gets the recommended file name for the output of the test - /// - /// - /// The required extension - public string GetTestOutputFileName(string extension = null, string tag = null) + + private string GetTestOutputFileNameImpl(string extension, string tag) { string fn = string.Empty; @@ -89,6 +86,40 @@ namespace ImageSharp.Tests return $"{this.GetTestOutputDir()}/{this.TestName}{pixName}{fn}{tag}{extension}"; } + /// + /// Gets the recommended file name for the output of the test + /// + /// The required extension + /// The settings modifying the output path + /// The file test name + public string GetTestOutputFileName(string extension = null, object settings = null) + { + string tag = null; + string s = settings as string; + + if (s != null) + { + tag = s; + } + else if (settings != null) + { + Type type = settings.GetType(); + TypeInfo info = type.GetTypeInfo(); + if (info.IsPrimitive || info.IsEnum || type == typeof(decimal)) + { + tag = settings.ToString(); + } + else + { + IEnumerable properties = settings.GetType().GetRuntimeProperties(); + + tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}")); + } + } + return this.GetTestOutputFileNameImpl(extension, tag); + } + + /// /// Encodes image by the format matching the required extension, than saves it to the recommended output file. /// @@ -97,12 +128,17 @@ namespace ImageSharp.Tests /// The requested extension /// Optional encoder /// Optional encoder options - public void SaveTestOutputFile(Image image, string extension = null, IImageEncoder encoder = null, IEncoderOptions options = null, string tag = null) + public void SaveTestOutputFile( + Image image, + string extension = null, + IImageEncoder encoder = null, + IEncoderOptions options = null, + object settings = null) where TPixel : struct, IPixel { - string path = this.GetTestOutputFileName(extension: extension, tag:tag); - extension = Path.GetExtension(path); - IImageFormat format = GetImageFormatByExtension(extension); + string path = this.GetTestOutputFileName(extension: extension, settings: settings); + string extension1 = Path.GetExtension(path); + IImageFormat format = GetImageFormatByExtension(extension1); encoder = encoder ?? format.Encoder; @@ -112,8 +148,8 @@ namespace ImageSharp.Tests } } - internal string GetReferenceOutputFileName(string extension = null, string tag = null) - => this.GetTestOutputFileName(extension, tag).Replace("TestOutput", "ReferenceOutput"); + internal string GetReferenceOutputFileName(string extension = null, object settings = null) + => this.GetTestOutputFileName(extension, settings).Replace("TestOutput", "ReferenceOutput"); internal void Init(string typeName, string methodName) { diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs new file mode 100644 index 000000000..b3e1eb47e --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -0,0 +1,20 @@ +namespace ImageSharp.Tests +{ + using System; + + public static class TestEnvironment + { + private static Lazy runsOnCi = new Lazy( + () => + { + bool isCi; + return bool.TryParse(Environment.GetEnvironmentVariable("CI"), out isCi) && isCi; + }); + + // ReSharper disable once InconsistentNaming + /// + /// Gets a value indicating whether test execution runs on CI. + /// + internal static bool RunsOnCI => runsOnCi.Value; + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index e1edfbe8a..11314d0cb 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -27,36 +27,13 @@ namespace ImageSharp.Tests public static Image DebugSave(this Image image, ITestImageProvider provider, object settings = null, string extension = "png") where TPixel : struct, IPixel { - if (bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) && isCi) + if (TestEnvironment.RunsOnCI) { return image; } // We are running locally then we want to save it out - string tag = null; - string s = settings as string; - - if (s != null) - { - tag = s; - } - else if (settings != null) - { - Type type = settings.GetType(); - TypeInfo info = type.GetTypeInfo(); - if (info.IsPrimitive || info.IsEnum || type == typeof(decimal)) - { - tag = settings.ToString(); - } - else - { - IEnumerable properties = settings.GetType().GetRuntimeProperties(); - - tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}")); - } - } - - provider.Utility.SaveTestOutputFile(image, extension, tag: tag); + provider.Utility.SaveTestOutputFile(image, extension, settings: settings); return image; } @@ -70,35 +47,11 @@ namespace ImageSharp.Tests int scalingFactor = ImageComparer.DefaultScalingFactor) where TPixel : struct, IPixel { - // We are running locally then we want to save it out - string tag = null; - string s = settings as string; - - if (s != null) - { - tag = s; - } - else if (settings != null) - { - Type type = settings.GetType(); - TypeInfo info = type.GetTypeInfo(); - if (info.IsPrimitive || info.IsEnum || type == typeof(decimal)) - { - tag = settings.ToString(); - } - else - { - IEnumerable properties = settings.GetType().GetRuntimeProperties(); - - tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}")); - } - } - - string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, tag); + string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, settings); - if (!(bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) && isCi)) + if (!TestEnvironment.RunsOnCI) { - provider.Utility.SaveTestOutputFile(image, extension, tag: tag); + provider.Utility.SaveTestOutputFile(image, extension, settings: settings); } if (!File.Exists(referenceOutputFile))