Skip to content

Commit

Permalink
Try agin, with logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Dec 13, 2024
1 parent 0be653d commit bb0ce6c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
60 changes: 20 additions & 40 deletions src/SingleProject/Resizetizer/test/UnitTests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using SkiaSharp;
using SkiaSharp.Extended;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Maui.Resizetizer.Tests
{
Expand All @@ -18,28 +19,28 @@ public class BaseTest : IDisposable
private readonly string DeleteDirectory;
protected readonly string DestinationDirectory;

public BaseTest(string testContextDirectory = null)
public BaseTest(ITestOutputHelper output)
{
if (!string.IsNullOrEmpty(testContextDirectory))
{
DestinationDirectory = testContextDirectory;
DeleteDirectory = testContextDirectory;
}
else
{
var name = GetType().FullName;
if (name.StartsWith(TestFolderName + ".", StringComparison.OrdinalIgnoreCase))
name = name.Substring(TestFolderName.Length + 1);
Output = output;

var dir = Path.Combine(Path.GetTempPath(), TestFolderName, name, Path.GetRandomFileName());
var name = GetType().FullName;
if (name.StartsWith(TestFolderName + ".", StringComparison.OrdinalIgnoreCase))
name = name.Substring(TestFolderName.Length + 1);

DestinationDirectory = dir;
DeleteDirectory = dir;
}
var dir = Path.Combine(Path.GetTempPath(), TestFolderName, name, Path.GetRandomFileName());

DestinationDirectory = dir;
DeleteDirectory = dir;

Output.WriteLine($"Using DestinationDirectory={DestinationDirectory}");
}

public ITestOutputHelper Output { get; }

public virtual void Dispose()
{
Output.WriteLine($"Cleaning up directories={DeleteDirectory}");

if (Directory.Exists(DeleteDirectory))
Directory.Delete(DeleteDirectory, true);
}
Expand Down Expand Up @@ -137,33 +138,12 @@ void AssertFileMatchesReal(string actualFilename, object[] args = null, [CallerM
}
else
{
using var actual = SKImage.FromEncodedData(actualFilename);
using var expected = SKImage.FromEncodedData(expectedFilename);

var similarity = SKPixelComparer.Compare(actual, expected);

var isSimilar = similarity.ErrorPixelPercentage <= ImageErrorThreshold;

if (!isSimilar)
{
var root = GetTestProjectRoot();

var maskFilename = Path.Combine(root, "errors", expectedFilename);
maskFilename = Path.ChangeExtension(maskFilename, ".mask.png");

Directory.CreateDirectory(Path.GetDirectoryName(maskFilename));
var root = GetTestProjectRoot();
var diffDir = Path.Combine(root, "errors");

using (var mask = SKPixelComparer.GenerateDifferenceMask(actual, expected))
using (var data = mask.Encode(SKEncodedImageFormat.Png, 100))
using (var maskFile = File.Create(maskFilename))
{
data.SaveTo(maskFile);
}
Output.WriteLine($"Validating image similarity with diff dir:{diffDir}");

Assert.True(
isSimilar,
$"Image was not equal. Error was {similarity.ErrorPixelPercentage}% ({similarity.AbsoluteError} pixels). See {maskFilename}");
}
ImageAssert.Equivalent(actualFilename, expectedFilename, diffDir);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ protected MSBuildTaskTestFixture()
}

protected MSBuildTaskTestFixture(ITestOutputHelper? output)
: base(output)
{
Output = output;
}

public ITestOutputHelper? Output { get; }

// IBuildEngine

bool IBuildEngine.ContinueOnError => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.7.0" PrivateAssets="all" />
<PackageReference Include="SkiaSharp" />
<PackageReference Include="SkiaSharp.Extended" />
<PackageReference Include="xunit" Version="$(XunitPackageVersion)"/>
<PackageReference Include="xunit" Version="$(XunitPackageVersion)" />
<PackageReference Include="coverlet.collector" Version="$(CoverletCollectorPackageVersion)" />
<PackageReference Include="Svg.Skia" />
</ItemGroup>
Expand All @@ -28,4 +28,8 @@
<None Include="imageresults\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\..\..\Graphics\tests\Graphics.Tests\ImageAssert.cs" Link="ImageAssert.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using SkiaSharp;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Maui.Resizetizer.Tests
{
Expand All @@ -11,7 +12,8 @@ public class Resize : BaseTest
readonly string DestinationFilename;
readonly TestLogger Logger;

public Resize()
public Resize(ITestOutputHelper outputHelper)
: base(outputHelper)
{
DestinationFilename = Path.Combine(DestinationDirectory, Path.GetRandomFileName() + ".png");
Logger = new TestLogger();
Expand Down

0 comments on commit bb0ce6c

Please sign in to comment.