Browse Source

refactored GetFormatsDirectory() to GetInputImagesDirectoryFullPath()

pull/298/head
Anton Firszov 9 years ago
parent
commit
2af76f62b3
  1. 54
      tests/ImageSharp.Tests/TestFile.cs
  2. 52
      tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs
  3. 42
      tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs
  4. 14
      tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

54
tests/ImageSharp.Tests/TestFile.cs

@ -27,7 +27,7 @@ namespace ImageSharp.Tests
/// <summary>
/// The formats directory.
/// </summary>
private static readonly string FormatsDirectory = GetFormatsDirectory();
private static readonly string FormatsDirectory = TestEnvironment.GetInputImagesDirectoryFullPath();
/// <summary>
/// The image.
@ -143,57 +143,5 @@ namespace ImageSharp.Tests
{
return Image.Load<Rgba32>(this.Bytes, options);
}
/// <summary>
/// Gets the correct path to the formats directory.
/// </summary>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
private static string GetFormatsDirectory()
{
List<string> 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)}]");
}
/// <summary>
/// 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.
/// </summary>
/// <param name="directories">The directories list</param>
private static void AddFormatsDirectoryFromTestAssebmlyPath(List<string> 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);
}
}
}
}

52
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<bool> runsOnCi = new Lazy<bool>(
() =>
{
@ -16,5 +25,48 @@ namespace ImageSharp.Tests
/// Gets a value indicating whether test execution runs on CI.
/// </summary>
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;
}
/// <summary>
/// Gets the correct path to the InputImages directory.
/// </summary>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
internal static string GetInputImagesDirectoryFullPath()
{
string soulitionDir = GetSolutionDirectoryFullPath();
return Path.Combine(soulitionDir, InputImagesRelativePath);
}
}
}

42
tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs

@ -0,0 +1,42 @@
// <copyright file="FlagsHelper.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// 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));
}
}
}

14
tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

@ -3,10 +3,12 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
// 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)]

Loading…
Cancel
Save