Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ names. That narrowing is deliberate and pinned by `ColorParserTests.RejectsCssNa
`IconHelper`. Two pure helpers decide them so the logic is testable without spawning a process:

- `ExitCodeFor(BatchResult)` maps a finished batch, non-zero `Failed` gives 2

`Execute` checks one case before `ExitCodeFor`. A batch that wrote nothing and failed nothing found
nothing to process, because only the top level of the input folder is read. It prints
`DescribeEmptyInput`'s warning, which names the skipped subfolders, and returns 1. Before this, an
`--input` one level too high printed "Done. 0 file(s) written." and exited 0. Pinned by
`ProcessDirectoryTests.AnInputWhoseImagesAllSitInSubfoldersExitsNonZeroAndNamesThem`.

- `ExitCodeForParseErrors(IEnumerable<ErrorType>)` maps CommandLineParser failures. `--help` and
`--version` arrive here as errors but must still exit 0, which is the case that helper exists for

Expand Down
37 changes: 36 additions & 1 deletion IconHelper.Test/ProcessDirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,40 @@ public void ExecuteReportsSuccessForAGoodRun()
Assert.AreEqual(IconHelper.ExitSuccess, IconHelper.Execute(ArgumentsFor(input, temp.Combine("out"))));
}

[TestMethod]
public void AnInputWhoseImagesAllSitInSubfoldersExitsNonZeroAndNamesThem()
{
using TempDirectory temp = new();
string input = temp.Combine("icons");
Directory.CreateDirectory(Path.Join(input, "ui"));
Directory.CreateDirectory(Path.Join(input, "actions"));
WritePng(Path.Join(input, "ui", "art64.png"), 64);
WritePng(Path.Join(input, "actions", "save.png"), 64);

string output = temp.Combine("out");

Assert.AreEqual(
IconHelper.ExitInvalidArguments,
IconHelper.Execute(ArgumentsFor(input, output)),
"A run that found nothing to process must not report success.");
Assert.IsEmpty(Directory.GetFiles(output));

// Console output is not captured here: tests run in parallel and share Console.Out.
StringAssert.Contains(IconHelper.DescribeEmptyInput(input), "Skipped subfolders: actions, ui.");
}

[TestMethod]
public void DescribeEmptyInputSaysOnlyTheTopLevelIsReadWhenThereAreNoSubfolders()
{
using TempDirectory temp = new();
Directory.CreateDirectory(Path.Join(temp.FullPath, ".git"));

string message = IconHelper.DescribeEmptyInput(temp.FullPath);

StringAssert.Contains(message, "Only the top level of the input folder is read.");
Assert.DoesNotContain("Skipped subfolders", message);
}

[TestMethod]
public void ExitCodeIsSuccessWhenNothingFailed()
{
Expand All @@ -258,7 +292,8 @@ public void ExitCodeIsSuccessWhenNothingFailed()
[TestMethod]
public void ExitCodeIsSuccessForAnEmptyRun()
{
// Nothing to do is not a failure.
// ExitCodeFor only maps a batch that ran. Execute reports an input with nothing to process
// before it gets here, see AnInputWhoseImagesAllSitInSubfoldersExitsNonZeroAndNamesThem.
Assert.AreEqual(IconHelper.ExitSuccess, IconHelper.ExitCodeFor(new BatchResult(0, 0)));
}

Expand Down
28 changes: 28 additions & 0 deletions IconHelper/IconHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,41 @@ internal static int Execute(Arguments args)
return ExitInvalidArguments;
}

if (result.Written == 0 && result.Failed == 0)
{
// Only the top level of the input folder is read. An --input one level too high, such as
// icons/ above icons/ui/*.png, used to print "Done. 0 file(s) written." and exit 0, which
// a build script reads as success.
_ = args.TryResolveInput(out AbsoluteDirectoryPath? inputDirectory, out _);
Console.WriteLine(DescribeEmptyInput(inputDirectory!));
return ExitInvalidArguments;
}

Console.WriteLine(result.Failed == 0
? $"Done. {result.Written} file(s) written."
: $"Done. {result.Written} file(s) written, {result.Failed} failed.");

return ExitCodeFor(result);
}

/// <summary>
/// Explains a run that found nothing to process, naming the subfolders it did not look in, since
/// only the top level of the input folder is read.
/// </summary>
internal static string DescribeEmptyInput(string inputDirectory)
{
string[] subfolders = [.. Directory.GetDirectories(inputDirectory)
.Select(Path.GetFileName)
.OfType<string>()
.Where(name => !name.StartsWith('.'))
.Order(StringComparer.Ordinal)];

string message = $"Warning: no files to process in {inputDirectory}. Only the top level of the input folder is read.";
return subfolders.Length == 0
? message
: $"{message} Skipped subfolders: {string.Join(", ", subfolders)}.";
}

/// <summary>
/// Processes every file in the input directory, writing recoloured PNGs to the output directory.
/// A failure on one file is reported and skipped so that a single bad input cannot abort the
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ It is built on [SixLabors.ImageSharp](https://github.com/SixLabors/ImageSharp),

## Features

- **Batch Processing**: Processes every file in an input directory in a single run
- **Batch Processing**: Processes every file at the top level of an input directory in a single run
- **Coverage Output**: Flattens each image to a mask painted in one flat colour, with the shape carried entirely by the alpha channel
- **Automatic Trimming**: Detects the bounding box of the pixels that end up visible and crops to it
- **Square Centring**: Pads the trimmed artwork to a square canvas so icons align consistently
Expand Down Expand Up @@ -284,7 +284,7 @@ already contains its own output will not reprocess those files.
| Code | Meaning |
|------|---------|
| `0` | Every file was processed successfully. Also returned for `--help` and `--version` |
| `1` | The arguments were unusable, for example an `--input` directory that does not exist, an unrecognised `--color`, or `padding * 2 >= size` |
| `1` | The arguments were unusable, for example an `--input` directory that does not exist, an unrecognised `--color`, or `padding * 2 >= size`. Also returned when `--input` holds nothing to process |
| `2` | The batch ran to completion but at least one file could not be processed |

Code `2` means the run finished and the remaining icons were still written. Check the summary line
Expand All @@ -295,6 +295,9 @@ for how many succeeded.
- Output is always PNG, and the extension is rewritten to match, so `logo.jpg` becomes `logo.png`. If
the input directory holds two files with the same base name but different extensions, the first one
processed is written and the other is reported as failed, so the run exits `2`. Rename one of them.
- Only the top level of `--input` is read, and subfolders are not searched. If that finds nothing
to process, the run prints a warning that names the subfolders it skipped and exits `1`, so an
`--input` pointed one level too high (`icons/` above `icons/ui/*.png`) is not mistaken for success.
- `--output` must not be the `--input` directory, because the outputs would replace the `.png`
sources. That is rejected with exit code `1`.
- Input formats are whatever ImageSharp can decode (PNG, JPEG, BMP, GIF, TGA, TIFF, WebP, PBM, QOI).
Expand Down
Loading