// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Tests { using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; /// /// A test image file. /// public class TestFile { /// /// The test file cache. /// private static readonly ConcurrentDictionary Cache = new ConcurrentDictionary(); /// /// The formats directory. /// private static readonly string FormatsDirectory = GetFormatsDirectory(); private static readonly object Locker = new object(); /// /// The image. /// private Image image; /// /// The file. /// private readonly string file; /// /// Initializes a new instance of the class. /// /// The file. private TestFile(string file) { this.file = file; this.Bytes = File.ReadAllBytes(file); } /// /// Gets the bytes. /// public byte[] Bytes { get; } /// /// The file name. /// public string FilePath => this.file; /// /// The file name. /// public string FileName => Path.GetFileName(this.file); /// /// The file name without extension. /// public string FileNameWithoutExtension => Path.GetFileNameWithoutExtension(this.file); /// /// Gets the full qualified path to the file. /// /// /// The file path. /// /// /// The . /// public static string GetPath(string file) { return Path.Combine(FormatsDirectory, file); } /// /// Creates a new test file or returns one from the cache. /// /// The file path. /// /// The . /// public static TestFile Create(string file) { return Cache.GetOrAdd(file, (string fileName) => new TestFile(GetPath(file))); } /// /// Gets the file name. /// /// The value. /// /// The . /// public string GetFileName(object value) { return $"{this.FileNameWithoutExtension}-{value}{Path.GetExtension(this.file)}"; } /// /// Gets the file name without extension. /// /// The value. /// /// The . /// public string GetFileNameWithoutExtension(object value) { return this.FileNameWithoutExtension + "-" + value; } /// /// Creates a new image. /// /// /// The . /// public Image CreateImage() { return new Image(this.GetImage()); } /// /// Creates a new image. /// /// The options for the decoder. /// /// The . /// public Image CreateImage(IDecoderOptions options) { return Image.Load(this.Bytes, options); } private Image GetImage() { lock (Locker) { return this.image ?? (this.image = Image.Load(this.Bytes)); } } /// /// Gets the correct path to the formats directory. /// /// /// The . /// private static string GetFormatsDirectory() { var directories = new List { "TestImages/Formats/", // Here for code coverage tests. "tests/ImageSharp.Tests/TestImages/Formats/", // From travis/build script "../../../../../ImageSharp.Tests/TestImages/Formats/", // From Sandbox46 "../../../../TestImages/Formats/", "../../../TestImages/Formats/" }; directories = directories .SelectMany(x => new[] { Path.GetFullPath(x) }).ToList(); AddFormatsDirectoryFromTestAssebmlyPath(directories); string directory = directories.FirstOrDefault(Directory.Exists); if (directory != null) { return directory; } throw new System.Exception($"Unable to find Formats directory at any of these locations [{string.Join(", ", directories)}]"); } /// /// The path returned by Path.GetFullPath(x) can be relative to dotnet framework directory /// in certain scenarios like dotTrace test profiling. /// This method calculates and adds the format directory based on the ImageSharp.Tests assembly location. /// /// The directories list private static void AddFormatsDirectoryFromTestAssebmlyPath(List directories) { string assemblyLocation = typeof(TestFile).GetTypeInfo().Assembly.Location; assemblyLocation = Path.GetDirectoryName(assemblyLocation); if (assemblyLocation != null) { string dirFromAssemblyLocation = Path.Combine(assemblyLocation, "../../../TestImages/Formats/"); dirFromAssemblyLocation = Path.GetFullPath(dirFromAssemblyLocation); directories.Add(dirFromAssemblyLocation); } } } }