// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; using System.IO; using System.Linq; using System.Reflection; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; namespace SixLabors.ImageSharp.Tests { using System.Runtime.InteropServices; public static class TestEnvironment { private const string ImageSharpSolutionFileName = "ImageSharp.sln"; private const string InputImagesRelativePath = @"tests\Images\Input"; private const string ActualOutputDirectoryRelativePath = @"tests\Images\ActualOutput"; private const string ReferenceOutputDirectoryRelativePath = @"tests\Images\External\ReferenceOutput"; private static Lazy solutionDirectoryFullPath = new Lazy(GetSolutionDirectoryFullPathImpl); private static Lazy runsOnCi = new Lazy( () => { bool isCi; return bool.TryParse(Environment.GetEnvironmentVariable("CI"), out isCi) && isCi; }); private static Lazy configuration = new Lazy(CreateDefaultConfiguration); // ReSharper disable once InconsistentNaming /// /// Gets a value indicating whether test execution runs on CI. /// internal static bool RunsOnCI => runsOnCi.Value; internal static string SolutionDirectoryFullPath => solutionDirectoryFullPath.Value; internal static Configuration Configuration => configuration.Value; private static Configuration CreateDefaultConfiguration() { var configuration = new Configuration( new PngConfigurationModule(), new JpegConfigurationModule(), new GifConfigurationModule(), new BmpConfigurationModule() ); configuration.SetDecoder(ImageFormats.Png, SystemDrawingReferenceDecoder.Instance); configuration.SetEncoder(ImageFormats.Png, SystemDrawingReferenceEncoder.Png); configuration.AddImageFormatDetector(new PngImageFormatDetector()); return configuration; } private static string GetSolutionDirectoryFullPathImpl() { string assemblyLocation = typeof(TestFile).GetTypeInfo().Assembly.Location; var assemblyFile = new FileInfo(assemblyLocation); DirectoryInfo directory = assemblyFile.Directory; while (!directory.EnumerateFiles(ImageSharpSolutionFileName).Any()) { try { directory = directory.Parent; } catch (Exception ex) { throw new Exception( $"Unable to find ImageSharp solution directory from {assemblyLocation} because of {ex.GetType().Name}!", ex); } if (directory == null) { throw new Exception($"Unable to find ImageSharp solution directory from {assemblyLocation}!"); } } return directory.FullName; } /// /// Gets the correct full path to the Input Images directory. /// internal static string InputImagesDirectoryFullPath => Path.Combine(SolutionDirectoryFullPath, InputImagesRelativePath).Replace('\\', Path.DirectorySeparatorChar); /// /// Gets the correct full path to the Actual Output directory. (To be written to by the test cases.) /// internal static string ActualOutputDirectoryFullPath => Path.Combine( SolutionDirectoryFullPath, ActualOutputDirectoryRelativePath).Replace('\\', Path.DirectorySeparatorChar); /// /// Gets the correct full path to the Expected Output directory. (To compare the test results to.) /// internal static string ReferenceOutputDirectoryFullPath => Path.Combine( SolutionDirectoryFullPath, ReferenceOutputDirectoryRelativePath).Replace('\\', Path.DirectorySeparatorChar); internal static string GetReferenceOutputFileName(string actualOutputFileName) => actualOutputFileName.Replace("ActualOutput", @"External\ReferenceOutput").Replace('\\', Path.DirectorySeparatorChar); internal static IImageDecoder GetReferenceDecoder(string filePath) { IImageFormat format = GetImageFormat(filePath); return Configuration.FindDecoder(format); } internal static IImageEncoder GetReferenceEncoder(string filePath) { IImageFormat format = GetImageFormat(filePath); return Configuration.FindEncoder(format); } internal static IImageFormat GetImageFormat(string filePath) { string extension = Path.GetExtension(filePath).ToLower(); if (extension[0] == '.') extension = extension.Substring(1); IImageFormat format = Configuration.FindFormatByFileExtension(extension); return format; } internal static bool IsLinux => System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Linux); } }