Browse Source

Make sure that netcoreapp2.1 test execution is testing the netcoreapp2.1 build of ImageSharp

pull/604/head
Anton Firszov 8 years ago
parent
commit
7172e432da
  1. 24
      src/ImageSharp/Common/Helpers/TestHelpers.cs
  2. 21
      tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs
  3. 26
      tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs

24
src/ImageSharp/Common/Helpers/TestHelpers.cs

@ -0,0 +1,24 @@
// Copyright(c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Common.Helpers
{
/// <summary>
/// Internal utilities intended to be only used in tests.
/// </summary>
internal static class TestHelpers
{
/// <summary>
/// This constant is useful to verify the target framework ImageSharp has been built against.
/// Only intended to be used in tests!
/// </summary>
internal const string ImageSharpBuiltAgainst =
#if NETSTANDARD1_1
"netstandard1.1";
#elif NETCOREAPP2_1
"netcoreapp2.1";
#else
"netstandard2.0";
#endif
}
}

21
tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs

@ -30,6 +30,13 @@ namespace SixLabors.ImageSharp.Tests
return Boolean.TryParse(Environment.GetEnvironmentVariable("CI"), out isCi) && isCi;
});
private static readonly Lazy<string> NetCoreVersionLazy = new Lazy<string>(GetNetCoreVersion);
/// <summary>
/// Gets the .NET Core version, if running on .NET Core, otherwise returns null.
/// </summary>
internal static string NetCoreVersion => NetCoreVersionLazy.Value;
// ReSharper disable once InconsistentNaming
/// <summary>
/// Gets a value indicating whether test execution runs on CI.
@ -123,5 +130,19 @@ namespace SixLabors.ImageSharp.Tests
return path;
}
/// <summary>
/// Solution borrowed from:
/// https://github.com/dotnet/BenchmarkDotNet/issues/448#issuecomment-308424100
/// </summary>
private static string GetNetCoreVersion()
{
Assembly assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
string[] assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
return "";
}
}
}

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

@ -4,6 +4,7 @@
using System;
using System.IO;
using SixLabors.ImageSharp.Common.Helpers;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Gif;
@ -13,9 +14,11 @@ using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs;
using Xunit;
using Xunit.Abstractions;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests
{
public class TestEnvironmentTests
{
public TestEnvironmentTests(ITestOutputHelper output)
@ -31,6 +34,29 @@ namespace SixLabors.ImageSharp.Tests
Assert.True(Directory.Exists(path));
}
/// <summary>
/// We need this test to make sure that the netcoreapp2.1 test execution actually covers the netcoreapp2.1 build configuration of ImageSharp.
/// </summary>
[Fact]
public void ImageSharpAssemblyUnderTest_MatchesExpectedTargetFramework()
{
this.Output.WriteLine("NetCoreVersion: " + TestEnvironment.NetCoreVersion);
this.Output.WriteLine("ImageSharpBuiltAgainst: " + TestHelpers.ImageSharpBuiltAgainst);
if (string.IsNullOrEmpty(TestEnvironment.NetCoreVersion))
{
this.Output.WriteLine("Not running under .NET Core!");
}
else if (TestEnvironment.NetCoreVersion.StartsWith("2.1"))
{
Assert.Equal("netcoreapp2.1", TestHelpers.ImageSharpBuiltAgainst);
}
else
{
Assert.Equal("netstandard2.0", TestHelpers.ImageSharpBuiltAgainst);
}
}
[Fact]
public void SolutionDirectoryFullPath()
{

Loading…
Cancel
Save