From b8d3e3a1364e516dcf638b65ca67287033c86431 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 22 Jul 2017 01:27:09 +0200 Subject: [PATCH] refactored GetFormatsDirectory() to GetInputImagesDirectoryFullPath() --- tests/ImageSharp.Tests/TestFile.cs | 54 +------------------ .../TestUtilities/TestEnvironment.cs | 52 ++++++++++++++++++ .../Tests/TestEnvironmentTests.cs | 42 +++++++++++++++ .../Tests/TestUtilityExtensionsTests.cs | 14 ++--- 4 files changed, 99 insertions(+), 63 deletions(-) create mode 100644 tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index f1b78383cb..9255059ae4 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -27,7 +27,7 @@ namespace ImageSharp.Tests /// /// The formats directory. /// - private static readonly string FormatsDirectory = GetFormatsDirectory(); + private static readonly string FormatsDirectory = TestEnvironment.GetInputImagesDirectoryFullPath(); /// /// The image. @@ -143,57 +143,5 @@ namespace ImageSharp.Tests { return Image.Load(this.Bytes, options); } - - /// - /// Gets the correct path to the formats directory. - /// - /// - /// The . - /// - private static string GetFormatsDirectory() - { - List directories = new List< string > { - "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(x => Directory.Exists(x)); - - 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); - } - } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index b3e1eb47e2..11440433ec 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -1,9 +1,18 @@ namespace ImageSharp.Tests { using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Reflection; + using System.Security; public static class TestEnvironment { + public const string ImageSharpSolution = "ImageSharp.sln"; + + public const string InputImagesRelativePath = @"tests\ImageSharp.Tests\TestImages\Formats"; + private static Lazy runsOnCi = new Lazy( () => { @@ -16,5 +25,48 @@ namespace ImageSharp.Tests /// Gets a value indicating whether test execution runs on CI. /// internal static bool RunsOnCI => runsOnCi.Value; + + internal static string GetSolutionDirectoryFullPath() + { + string assemblyLocation = typeof(TestFile).GetTypeInfo().Assembly.Location; + + var assemblyFile = new FileInfo(assemblyLocation); + + DirectoryInfo directory = assemblyFile.Directory; + + while (!directory.EnumerateFiles(ImageSharpSolution).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 path to the InputImages directory. + /// + /// + /// The . + /// + internal static string GetInputImagesDirectoryFullPath() + { + string soulitionDir = GetSolutionDirectoryFullPath(); + + return Path.Combine(soulitionDir, InputImagesRelativePath); + } + } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs new file mode 100644 index 0000000000..9a28e3fb41 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs @@ -0,0 +1,42 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +// ReSharper disable InconsistentNaming +namespace ImageSharp.Tests +{ + using System.IO; + + using Xunit; + using Xunit.Abstractions; + + public class TestEnvironmentTests + { + public TestEnvironmentTests(ITestOutputHelper output) + { + this.Output = output; + } + + private ITestOutputHelper Output { get; } + + + [Fact] + public void GetSolutionDirectoryFullPath() + { + string path = TestEnvironment.GetSolutionDirectoryFullPath(); + this.Output.WriteLine(path); + + Assert.True(Directory.Exists(path)); + } + + [Fact] + public void GetInputImagesDirectoryFullPath() + { + string path = TestEnvironment.GetInputImagesDirectoryFullPath(); + this.Output.WriteLine(path); + + Assert.True(Directory.Exists(path)); + } + } +} diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index 437c295b9c..e0a057e954 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -3,10 +3,12 @@ // Licensed under the Apache License, Version 2.0. // +// ReSharper disable InconsistentNaming namespace ImageSharp.Tests { using System; using System.Collections.Generic; + using System.IO; using System.Linq; using System.Numerics; using System.Reflection; @@ -49,16 +51,8 @@ namespace ImageSharp.Tests return image; } - - [Fact] - public void Baz() - { - Type type = typeof(Rgba32).GetTypeInfo().Assembly.GetType("ImageSharp.Rgba32"); - this.Output.WriteLine(type.ToString()); - - Type fake = typeof(Rgba32).GetTypeInfo().Assembly.GetType("ImageSharp.dsaada_DASqewrr"); - Assert.Null(fake); - } + + [Theory] [WithFile(TestImages.Bmp.Car, PixelTypes.Rgba32, true)]