Skip to content

Commit

Permalink
Improve error logging for failed resizetizering (#22064)
Browse files Browse the repository at this point in the history
* Improve error logging for failed resizetizering

* Fix tests
  • Loading branch information
mattleibow committed Apr 28, 2024
1 parent 6654fac commit e33d176
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/SingleProject/Resizetizer/src/ResizetizeImages.cs
Expand Up @@ -88,9 +88,7 @@ public override System.Threading.Tasks.Task ExecuteAsync()
}
catch (Exception ex)
{
LogWarning("MAUI0000", ex.ToString());
throw;
LogCodedError("MAUI0000", $"There was an exception processing the image '{img.Filename}': {ex}");
}
});

Expand Down
5 changes: 4 additions & 1 deletion src/SingleProject/Resizetizer/src/SkiaSharpSvgTools.cs
Expand Up @@ -21,10 +21,13 @@ public SkiaSharpSvgTools(string filename, SKSize? baseSize, SKColor? backgroundC
sw.Start();

svg = new SKSvg();
svg.Load(filename);
var pic = svg.Load(filename);

sw.Stop();
Logger?.Log($"Open SVG took {sw.ElapsedMilliseconds}ms ({filename})");

if (pic.CullRect.Size.IsEmpty)
Logger?.Log($"SVG picture did not have a size and will fail to generate. ({Filename})");
}

public override SKSize GetOriginalSize() =>
Expand Down
42 changes: 36 additions & 6 deletions src/SingleProject/Resizetizer/test/UnitTests/MSBuildTaskFixture.cs
@@ -1,20 +1,34 @@
using System;
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Build.Framework;
using Xunit.Abstractions;

namespace Microsoft.Maui.Resizetizer.Tests
{
public abstract class MSBuildTaskTestFixture<TTask> : BaseTest, IBuildEngine
where TTask : Microsoft.Build.Framework.ITask
{
protected readonly TestLogger Logger;
protected readonly TestLogger? Logger;

protected List<BuildErrorEventArgs> LogErrorEvents = new List<BuildErrorEventArgs>();
protected List<BuildMessageEventArgs> LogMessageEvents = new List<BuildMessageEventArgs>();
protected List<CustomBuildEventArgs> LogCustomEvents = new List<CustomBuildEventArgs>();
protected List<BuildWarningEventArgs> LogWarningEvents = new List<BuildWarningEventArgs>();

protected MSBuildTaskTestFixture()
: this(null)
{
}

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

public ITestOutputHelper? Output { get; }

// IBuildEngine

bool IBuildEngine.ContinueOnError => false;
Expand All @@ -27,12 +41,28 @@ public abstract class MSBuildTaskTestFixture<TTask> : BaseTest, IBuildEngine

bool IBuildEngine.BuildProjectFile(string projectFileName, string[] targetNames, IDictionary globalProperties, IDictionary targetOutputs) => throw new NotImplementedException();

void IBuildEngine.LogCustomEvent(CustomBuildEventArgs e) => LogCustomEvents.Add(e);
void IBuildEngine.LogCustomEvent(CustomBuildEventArgs e)
{
LogCustomEvents.Add(e);
Output?.WriteLine($"CUSTOM : {e.Message}");
}

void IBuildEngine.LogErrorEvent(BuildErrorEventArgs e) => LogErrorEvents.Add(e);
void IBuildEngine.LogErrorEvent(BuildErrorEventArgs e)
{
LogErrorEvents.Add(e);
Output?.WriteLine($"ERROR : {e.Message}");
}

void IBuildEngine.LogMessageEvent(BuildMessageEventArgs e) => LogMessageEvents.Add(e);
void IBuildEngine.LogMessageEvent(BuildMessageEventArgs e)
{
LogMessageEvents.Add(e);
Output?.WriteLine($"MESSAGE: {e.Message}");
}

void IBuildEngine.LogWarningEvent(BuildWarningEventArgs e) => LogWarningEvents.Add(e);
void IBuildEngine.LogWarningEvent(BuildWarningEventArgs e)
{
LogWarningEvents.Add(e);
Output?.WriteLine($"WARNING: {e.Message}");
}
}
}
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Build.Utilities;
using SkiaSharp;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Maui.Resizetizer.Tests
{
Expand All @@ -16,6 +17,11 @@ public abstract class ExecuteForApp : MSBuildTaskTestFixture<ResizetizeImages>
{
protected static readonly Dictionary<string, string> ResizeMetadata = new() { ["Resize"] = "true" };

protected ExecuteForApp(ITestOutputHelper output)
: base(output)
{
}

protected ResizetizeImages GetNewTask(string type, params ITaskItem[] items) =>
new ResizetizeImages
{
Expand All @@ -33,13 +39,66 @@ public abstract class ExecuteForApp : MSBuildTaskTestFixture<ResizetizeImages>

public abstract class ExecuteForPlatformApp : ExecuteForApp
{
protected ExecuteForPlatformApp(ITestOutputHelper output)
: base(output)
{
}

protected abstract string Platform { get; }

protected abstract string GetPlatformOutputFileName(string file);

protected virtual string GetPlatformCopyOutputFileName(string file) =>
GetPlatformOutputFileName(file);

protected ResizetizeImages GetNewTask(params ITaskItem[] items) =>
GetNewTask(Platform, items);

[Theory]
[InlineData("appicon.svg")]
[InlineData("bicycle.svg")]
[InlineData("camera.svg")]
[InlineData("camera.png")]
[InlineData("dotnet_bot.svg")]
[InlineData("dotnet_logo.svg")]
[InlineData("find_icon.svg")]
[InlineData("not_working.svg")]
[InlineData("prismicon.svg")]
[InlineData("warning.svg")]
[InlineData("yes_working.svg")]
public void BasicImageProcessingWorks(string image)
{
var items = new[]
{
new TaskItem($"images/{image}"),
};

var task = GetNewTask(items);
var success = task.Execute();
Assert.True(success, LogErrorEvents.FirstOrDefault()?.Message);

if (Path.GetExtension(image) == ".png" || Path.GetExtension(image) == ".jpeg" || Path.GetExtension(image) == ".gif")
AssertFileExists(GetPlatformCopyOutputFileName(Path.ChangeExtension(image, ".png")));
else
AssertFileExists(GetPlatformOutputFileName(Path.ChangeExtension(image, ".png")));
}

[Theory]
[InlineData("link_out.svg")]
public void BadImagesReportImageWithError(string image)
{
var items = new[]
{
new TaskItem($"images/{image}"),
};

var task = GetNewTask(items);
var success = task.Execute();
Assert.False(success);

Assert.Contains(image, LogErrorEvents.FirstOrDefault()?.Message, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public void GenerationSkippedOnIncrementalBuild()
{
Expand Down Expand Up @@ -179,11 +238,19 @@ public void FailsOnAlmostExactMatchingMultipleFiles()

public class ExecuteForAndroid : ExecuteForPlatformApp
{
public ExecuteForAndroid(ITestOutputHelper output)
: base(output)
{
}

protected override string Platform => "android";

protected override string GetPlatformOutputFileName(string file) =>
$"drawable-mdpi/{file}";

protected override string GetPlatformCopyOutputFileName(string file) =>
$"drawable/{file}";

[Fact]
public void NoItemsSucceed()
{
Expand Down Expand Up @@ -930,9 +997,18 @@ public void NonExistantFilesAreDeleted()

public class ExecuteForiOS : ExecuteForPlatformApp
{
public ExecuteForiOS(ITestOutputHelper output)
: base(output)
{
}

protected override string Platform => "ios";

protected override string GetPlatformOutputFileName(string file) => $"{file}";
protected override string GetPlatformOutputFileName(string file) =>
$"{file}";

protected override string GetPlatformCopyOutputFileName(string file) =>
$"Resources/{file}";

[Fact]
public void NoItemsSucceed()
Expand Down Expand Up @@ -1243,6 +1319,11 @@ public void NonExistantFilesAreDeleted()

public class ExecuteForWindows : ExecuteForPlatformApp
{
public ExecuteForWindows(ITestOutputHelper output)
: base(output)
{
}

protected override string Platform => "uwp";

protected override string GetPlatformOutputFileName(string file) =>
Expand Down Expand Up @@ -1602,6 +1683,11 @@ public void NonExistantFilesAreDeleted()

public class ExecuteForAny : ExecuteForApp
{
public ExecuteForAny(ITestOutputHelper output)
: base(output)
{
}

[Theory]
[InlineData("image.svg", "100,100", true)]
[InlineData("image.png", "100,100", true)]
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e33d176

Please sign in to comment.