mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
Add standard CLI support for Image Resizer (#44287)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request Adds a dedicated command-line interface (CLI) executable for Image Resizer (PowerToys.ImageResizerCLI.exe) ## Command `PowerToys.ImageResizerCLI.exe [options] [files...]` ## Options (High Level) | Option (aliases) | Description | |-----------------|-------------| | `--help` | Show help | | `--show-config` | Print current effective configuration | | `--destination`, `-d` | Output directory (optional) | | `--width`, `-w` | Width | | `--height`, `-h` | Height | | `--unit`, `-u` | Unit (Pixel / Percent / Inch / Centimeter) | | `--fit`, `-f` | Fit mode (Fill / Fit / Stretch) | | `--size`, `-s` | Preset size index (supports `0` for Custom) | | `--shrink-only` | Only shrink (do not enlarge) | | `--replace` | Replace original | | `--ignore-orientation` | Ignore EXIF orientation | | `--remove-metadata` | Strip metadata | | `--quality`, `-q` | JPEG quality (1–100) | | `--keep-date-modified` | Preserve source last-write time | | `--file-name` | Output filename format | ## Example usage ``` # Show help PowerToys.ImageResizerCLI.exe --help # Show current config PowerToys.ImageResizerCLI.exe --show-config # Resize with explicit dimensions PowerToys.ImageResizerCLI.exe --width 800 --height 600 .\image.png # Use preset size 0 (Custom) and output to a folder PowerToys.ImageResizerCLI.exe --size 0 -d "C:\Output" .\photo.png # Preserve source LastWriteTime PowerToys.ImageResizerCLI.exe --width 800 --height 600 --keep-date-modified -d "C:\Output" .\image.png ```  <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [ ] Closes: #xxx <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [x] **Localization:** All end-user-facing strings can be localized - [x] **Dev docs:** Added/updated - [x] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
This commit is contained in:
320
src/modules/imageresizer/tests/Cli/CliSettingsApplierTests.cs
Normal file
320
src/modules/imageresizer/tests/Cli/CliSettingsApplierTests.cs
Normal file
@@ -0,0 +1,320 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using ImageResizer.Cli;
|
||||
using ImageResizer.Models;
|
||||
using ImageResizer.Properties;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ImageResizer.Tests.Cli
|
||||
{
|
||||
[TestClass]
|
||||
public class CliSettingsApplierTests
|
||||
{
|
||||
private Settings CreateDefaultSettings()
|
||||
{
|
||||
var settings = new Settings();
|
||||
settings.Sizes.Add(new ResizeSize(0, "Small", ResizeFit.Fit, 854, 480, ResizeUnit.Pixel));
|
||||
settings.Sizes.Add(new ResizeSize(1, "Medium", ResizeFit.Fit, 1366, 768, ResizeUnit.Pixel));
|
||||
settings.Sizes.Add(new ResizeSize(2, "Large", ResizeFit.Fit, 1920, 1080, ResizeUnit.Pixel));
|
||||
return settings;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithCustomWidth_SetsCustomSizeWidth()
|
||||
{
|
||||
var options = new CliOptions { Width = 800 };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(800.0, settings.CustomSize.Width);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithCustomHeight_SetsCustomSizeHeight()
|
||||
{
|
||||
var options = new CliOptions { Height = 600 };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(600.0, settings.CustomSize.Height);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithCustomSize_SelectsCustomSizeIndex()
|
||||
{
|
||||
var options = new CliOptions { Width = 800, Height = 600 };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
// Custom size index should be settings.Sizes.Count
|
||||
Assert.AreEqual(settings.Sizes.Count, settings.SelectedSizeIndex);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithZeroWidth_SetsZeroForAutoCalculation()
|
||||
{
|
||||
var options = new CliOptions { Width = 0, Height = 600 };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(0.0, settings.CustomSize.Width);
|
||||
Assert.AreEqual(600.0, settings.CustomSize.Height);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithZeroHeight_SetsZeroForAutoCalculation()
|
||||
{
|
||||
var options = new CliOptions { Width = 800, Height = 0 };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(800.0, settings.CustomSize.Width);
|
||||
Assert.AreEqual(0.0, settings.CustomSize.Height);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithNullWidthAndHeight_DoesNotModifyCustomSize()
|
||||
{
|
||||
var options = new CliOptions { Width = null, Height = null };
|
||||
var settings = CreateDefaultSettings();
|
||||
var originalWidth = settings.CustomSize.Width;
|
||||
var originalHeight = settings.CustomSize.Height;
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
// When both null, should not modify CustomSize (keeps default 1024x640)
|
||||
Assert.AreEqual(originalWidth, settings.CustomSize.Width);
|
||||
Assert.AreEqual(originalHeight, settings.CustomSize.Height);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithUnit_SetsCustomSizeUnit()
|
||||
{
|
||||
var options = new CliOptions { Width = 100, Unit = ResizeUnit.Percent };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(ResizeUnit.Percent, settings.CustomSize.Unit);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithFit_SetsCustomSizeFit()
|
||||
{
|
||||
var options = new CliOptions { Width = 800, Fit = ResizeFit.Fill };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(ResizeFit.Fill, settings.CustomSize.Fit);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithValidSizeIndex_SetsSelectedSizeIndex()
|
||||
{
|
||||
var options = new CliOptions { SizeIndex = 1 };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(1, settings.SelectedSizeIndex);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithInvalidSizeIndex_DoesNotChangeSelection()
|
||||
{
|
||||
var options = new CliOptions { SizeIndex = 99 };
|
||||
var settings = CreateDefaultSettings();
|
||||
var originalIndex = settings.SelectedSizeIndex;
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
// Should remain unchanged when invalid
|
||||
Assert.AreEqual(originalIndex, settings.SelectedSizeIndex);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithNegativeSizeIndex_DoesNotChangeSelection()
|
||||
{
|
||||
var options = new CliOptions { SizeIndex = -1 };
|
||||
var settings = CreateDefaultSettings();
|
||||
var originalIndex = settings.SelectedSizeIndex;
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(originalIndex, settings.SelectedSizeIndex);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithShrinkOnly_SetsShrinkOnly()
|
||||
{
|
||||
var options = new CliOptions { ShrinkOnly = true };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.IsTrue(settings.ShrinkOnly);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithReplace_SetsReplace()
|
||||
{
|
||||
var options = new CliOptions { Replace = true };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.IsTrue(settings.Replace);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithIgnoreOrientation_SetsIgnoreOrientation()
|
||||
{
|
||||
var options = new CliOptions { IgnoreOrientation = true };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.IsTrue(settings.IgnoreOrientation);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithRemoveMetadata_SetsRemoveMetadata()
|
||||
{
|
||||
var options = new CliOptions { RemoveMetadata = true };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.IsTrue(settings.RemoveMetadata);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithJpegQualityLevel_SetsJpegQualityLevel()
|
||||
{
|
||||
var options = new CliOptions { JpegQualityLevel = 85 };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(85, settings.JpegQualityLevel);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithKeepDateModified_SetsKeepDateModified()
|
||||
{
|
||||
var options = new CliOptions { KeepDateModified = true };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.IsTrue(settings.KeepDateModified);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithFileName_SetsFileName()
|
||||
{
|
||||
var options = new CliOptions { FileName = "%1 (%2)" };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual("%1 (%2)", settings.FileName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithEmptyFileName_DoesNotChangeFileName()
|
||||
{
|
||||
var options = new CliOptions { FileName = string.Empty };
|
||||
var settings = CreateDefaultSettings();
|
||||
var originalFileName = settings.FileName;
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(originalFileName, settings.FileName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithMultipleOptions_AppliesAllOptions()
|
||||
{
|
||||
var options = new CliOptions
|
||||
{
|
||||
Width = 800,
|
||||
Height = 600,
|
||||
Unit = ResizeUnit.Percent,
|
||||
Fit = ResizeFit.Fill,
|
||||
ShrinkOnly = true,
|
||||
Replace = true,
|
||||
IgnoreOrientation = true,
|
||||
RemoveMetadata = true,
|
||||
JpegQualityLevel = 90,
|
||||
KeepDateModified = true,
|
||||
FileName = "test_%2",
|
||||
};
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(800.0, settings.CustomSize.Width);
|
||||
Assert.AreEqual(600.0, settings.CustomSize.Height);
|
||||
Assert.AreEqual(ResizeUnit.Percent, settings.CustomSize.Unit);
|
||||
Assert.AreEqual(ResizeFit.Fill, settings.CustomSize.Fit);
|
||||
Assert.IsTrue(settings.ShrinkOnly);
|
||||
Assert.IsTrue(settings.Replace);
|
||||
Assert.IsTrue(settings.IgnoreOrientation);
|
||||
Assert.IsTrue(settings.RemoveMetadata);
|
||||
Assert.AreEqual(90, settings.JpegQualityLevel);
|
||||
Assert.IsTrue(settings.KeepDateModified);
|
||||
Assert.AreEqual("test_%2", settings.FileName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_CustomSizeTakesPrecedence_OverSizeIndex()
|
||||
{
|
||||
var options = new CliOptions
|
||||
{
|
||||
Width = 800,
|
||||
Height = 600,
|
||||
SizeIndex = 1, // Should be ignored when Width/Height specified
|
||||
};
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
// Custom size should be selected, not preset
|
||||
Assert.AreEqual(settings.Sizes.Count, settings.SelectedSizeIndex);
|
||||
Assert.AreEqual(800.0, settings.CustomSize.Width);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithOnlyWidth_StillSelectsCustomSize()
|
||||
{
|
||||
var options = new CliOptions { Width = 800 };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(settings.Sizes.Count, settings.SelectedSizeIndex);
|
||||
Assert.AreEqual(800.0, settings.CustomSize.Width);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Apply_WithOnlyHeight_StillSelectsCustomSize()
|
||||
{
|
||||
var options = new CliOptions { Height = 600 };
|
||||
var settings = CreateDefaultSettings();
|
||||
|
||||
CliSettingsApplier.Apply(options, settings);
|
||||
|
||||
Assert.AreEqual(settings.Sizes.Count, settings.SelectedSizeIndex);
|
||||
Assert.AreEqual(600.0, settings.CustomSize.Height);
|
||||
}
|
||||
}
|
||||
}
|
||||
268
src/modules/imageresizer/tests/Models/CliOptionsTests.cs
Normal file
268
src/modules/imageresizer/tests/Models/CliOptionsTests.cs
Normal file
@@ -0,0 +1,268 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ImageResizer.Cli.Commands;
|
||||
using ImageResizer.Models;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ImageResizer.Tests.Models
|
||||
{
|
||||
[TestClass]
|
||||
public class CliOptionsTests
|
||||
{
|
||||
private static readonly string[] _multiFileArgs = new[] { "test1.jpg", "test2.jpg", "test3.jpg" };
|
||||
private static readonly string[] _mixedOptionsArgs = new[] { "--width", "800", "test1.jpg", "--height", "600", "test2.jpg" };
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithValidWidth_SetsWidth()
|
||||
{
|
||||
var args = new[] { "--width", "800", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(800.0, options.Width);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithValidHeight_SetsHeight()
|
||||
{
|
||||
var args = new[] { "--height", "600", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(600.0, options.Height);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithShortWidthAlias_WorksIdentically()
|
||||
{
|
||||
var longFormArgs = new[] { "--width", "800", "test.jpg" };
|
||||
var shortFormArgs = new[] { "-w", "800", "test.jpg" };
|
||||
var longForm = CliOptions.Parse(longFormArgs);
|
||||
var shortForm = CliOptions.Parse(shortFormArgs);
|
||||
|
||||
Assert.AreEqual(longForm.Width, shortForm.Width);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithShortHeightAlias_WorksIdentically()
|
||||
{
|
||||
var longFormArgs = new[] { "--height", "600", "test.jpg" };
|
||||
var shortFormArgs = new[] { "-h", "600", "test.jpg" };
|
||||
var longForm = CliOptions.Parse(longFormArgs);
|
||||
var shortForm = CliOptions.Parse(shortFormArgs);
|
||||
|
||||
Assert.AreEqual(longForm.Height, shortForm.Height);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithValidUnit_SetsUnit()
|
||||
{
|
||||
var args = new[] { "--unit", "Percent", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(ResizeUnit.Percent, options.Unit);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithValidFit_SetsFit()
|
||||
{
|
||||
var args = new[] { "--fit", "Fill", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(ResizeFit.Fill, options.Fit);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithSizeIndex_SetsSizeIndex()
|
||||
{
|
||||
var args = new[] { "--size", "2", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(2, options.SizeIndex);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithShrinkOnly_SetsShrinkOnly()
|
||||
{
|
||||
var args = new[] { "--shrink-only", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(true, options.ShrinkOnly);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithReplace_SetsReplace()
|
||||
{
|
||||
var args = new[] { "--replace", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(true, options.Replace);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithIgnoreOrientation_SetsIgnoreOrientation()
|
||||
{
|
||||
var args = new[] { "--ignore-orientation", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(true, options.IgnoreOrientation);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithRemoveMetadata_SetsRemoveMetadata()
|
||||
{
|
||||
var args = new[] { "--remove-metadata", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(true, options.RemoveMetadata);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithValidQuality_SetsQuality()
|
||||
{
|
||||
var args = new[] { "--quality", "85", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(85, options.JpegQualityLevel);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithKeepDateModified_SetsKeepDateModified()
|
||||
{
|
||||
var args = new[] { "--keep-date-modified", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(true, options.KeepDateModified);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithFileName_SetsFileName()
|
||||
{
|
||||
var args = new[] { "--filename", "%1 (%2)", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual("%1 (%2)", options.FileName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithDestination_SetsDestinationDirectory()
|
||||
{
|
||||
var args = new[] { "--destination", "C:\\Output", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual("C:\\Output", options.DestinationDirectory);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithShortDestinationAlias_WorksIdentically()
|
||||
{
|
||||
var longFormArgs = new[] { "--destination", "C:\\Output", "test.jpg" };
|
||||
var shortFormArgs = new[] { "-d", "C:\\Output", "test.jpg" };
|
||||
var longForm = CliOptions.Parse(longFormArgs);
|
||||
var shortForm = CliOptions.Parse(shortFormArgs);
|
||||
|
||||
Assert.AreEqual(longForm.DestinationDirectory, shortForm.DestinationDirectory);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithProgressLines_SetsProgressLines()
|
||||
{
|
||||
var args = new[] { "--progress-lines", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(true, options.ProgressLines);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithAccessibleAlias_SetsProgressLines()
|
||||
{
|
||||
var args = new[] { "--accessible", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(true, options.ProgressLines);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithMultipleFiles_AddsAllFiles()
|
||||
{
|
||||
var args = _multiFileArgs;
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(3, options.Files.Count);
|
||||
CollectionAssert.Contains(options.Files.ToList(), "test1.jpg");
|
||||
CollectionAssert.Contains(options.Files.ToList(), "test2.jpg");
|
||||
CollectionAssert.Contains(options.Files.ToList(), "test3.jpg");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithMixedOptionsAndFiles_ParsesCorrectly()
|
||||
{
|
||||
var args = _mixedOptionsArgs;
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(800.0, options.Width);
|
||||
Assert.AreEqual(600.0, options.Height);
|
||||
Assert.AreEqual(2, options.Files.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithHelp_SetsShowHelp()
|
||||
{
|
||||
var args = new[] { "--help" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.IsTrue(options.ShowHelp);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithShowConfig_SetsShowConfig()
|
||||
{
|
||||
var args = new[] { "--show-config" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.IsTrue(options.ShowConfig);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithNoArguments_ReturnsEmptyOptions()
|
||||
{
|
||||
var args = Array.Empty<string>();
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.IsNotNull(options);
|
||||
Assert.AreEqual(0, options.Files.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithZeroWidth_AllowsZeroValue()
|
||||
{
|
||||
var args = new[] { "--width", "0", "--height", "600", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(0.0, options.Width);
|
||||
Assert.AreEqual(600.0, options.Height);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_WithZeroHeight_AllowsZeroValue()
|
||||
{
|
||||
var args = new[] { "--width", "800", "--height", "0", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(800.0, options.Width);
|
||||
Assert.AreEqual(0.0, options.Height);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Parse_CaseInsensitiveEnums_ParsesCorrectly()
|
||||
{
|
||||
var args = new[] { "--unit", "pixel", "--fit", "fit", "test.jpg" };
|
||||
var options = CliOptions.Parse(args);
|
||||
|
||||
Assert.AreEqual(ResizeUnit.Pixel, options.Unit);
|
||||
Assert.AreEqual(ResizeFit.Fit, options.Fit);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,20 +25,27 @@ namespace ImageResizer.Models
|
||||
[TestMethod]
|
||||
public void FromCommandLineWorks()
|
||||
{
|
||||
// Use actual test files that exist in the test directory
|
||||
var testDir = Path.GetDirectoryName(typeof(ResizeBatchTests).Assembly.Location);
|
||||
var file1 = Path.Combine(testDir, "Test.jpg");
|
||||
var file2 = Path.Combine(testDir, "Test.png");
|
||||
var file3 = Path.Combine(testDir, "Test.gif");
|
||||
|
||||
var standardInput =
|
||||
"Image1.jpg" + EOL +
|
||||
"Image2.jpg";
|
||||
file1 + EOL +
|
||||
file2;
|
||||
var args = new[]
|
||||
{
|
||||
"/d", "OutputDir",
|
||||
"Image3.jpg",
|
||||
file3,
|
||||
};
|
||||
|
||||
var result = ResizeBatch.FromCommandLine(
|
||||
new StringReader(standardInput),
|
||||
args);
|
||||
|
||||
CollectionAssert.AreEquivalent(new List<string> { "Image1.jpg", "Image2.jpg", "Image3.jpg" }, result.Files.ToArray());
|
||||
var files = result.Files.Select(Path.GetFileName).ToArray();
|
||||
CollectionAssert.AreEquivalent(new List<string> { "Test.jpg", "Test.png", "Test.gif" }, files);
|
||||
|
||||
Assert.AreEqual("OutputDir", result.DestinationDirectory);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user