Skip to content

Commit

Permalink
Fix #68 issue with error handling that was not falling back when usin…
Browse files Browse the repository at this point in the history
…g Sync mode.
  • Loading branch information
markgibbons25 committed Jun 15, 2020
1 parent 5a6a6bc commit 87f4e58
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 88 deletions.
3 changes: 3 additions & 0 deletions src/Dianoga.Tests/Dianoga.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
</ItemGroup>

<ItemGroup>
<None Update="TestImages\corrupted.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestImages\large.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dianoga.Optimizers;
using Dianoga.Optimizers.Pipelines.DianogaJpeg;
using FluentAssertions;
using FluentAssertions.Common;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -21,18 +22,32 @@ public void ShouldSquishSmallJpeg()
{
Test(@"TestImages\small.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\jpegoptim-windows\jpegoptim.exe",
"--strip-all --all-progressive -m90");
"--strip-all --all-progressive -m90", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldSquishLargeJpeg()
{
Test(@"TestImages\large.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\jpegoptim-windows\jpegoptim.exe",
"--strip-all --all-progressive -m90");
"--strip-all --all-progressive -m90", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

private void Test(string imagePath, string exePath, string exeArgs)
[Fact]
public void ShouldNotSquishCorruptedJpegLossy()
{
Test(@"TestImages\corrupted.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\jpegoptim-windows\jpegoptim.exe",
"--strip-all --all-progressive -m90", out var args, out var startingSize);
args.Stream.Length.Should().IsSameOrEqualTo(startingSize);
args.IsOptimized.Should().BeFalse();
}

private void Test(string imagePath, string exePath, string exeArgs, out OptimizerArgs argsOut, out long startingSize)
{
var inputStream = new MemoryStream();

Expand All @@ -45,18 +60,17 @@ private void Test(string imagePath, string exePath, string exeArgs)
sut.ExePath = exePath;
sut.AdditionalToolArguments = exeArgs;

var args = new OptimizerArgs(inputStream);

var startingSize = args.Stream.Length;
var args = new OptimizerArgs(inputStream);

startingSize = args.Stream.Length;

var stopwatch = new Stopwatch();
stopwatch.Start();
sut.Process(args);
stopwatch.Stop();
output.WriteLine($"Time: {stopwatch.ElapsedMilliseconds}ms");

args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
output.WriteLine($"Time: {stopwatch.ElapsedMilliseconds}ms");

argsOut = args;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dianoga.Optimizers;
using Dianoga.Optimizers.Pipelines.DianogaJpeg;
using FluentAssertions;
using FluentAssertions.Common;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -21,34 +22,62 @@ public void ShouldSquishSmallJpegLossless()
{
Test(@"TestImages\small.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\mozjpeg_3.3.1_x86\jpegtran.exe",
"-progressive");
"-progressive", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldSquishLargeJpegLossless()
{
Test(@"TestImages\large.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\mozjpeg_3.3.1_x86\jpegtran.exe",
"-progressive");
"-progressive", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldSquishSmallJpegLossy()
{
Test(@"TestImages\small.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\mozjpeg_3.3.1_x86\cjpeg.exe",
"-quality 80");
"-quality 80", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldSquishLargeJpegLossy()
{
Test(@"TestImages\large.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\mozjpeg_3.3.1_x86\cjpeg.exe",
"-quality 80");
"-quality 80", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldNotSquishCorruptedJpegLossless()
{
Test(@"TestImages\corrupted.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\mozjpeg_3.3.1_x86\jpegtran.exe",
"-quality 80", out var args, out var startingSize);
args.Stream.Length.Should().IsSameOrEqualTo(startingSize);
args.IsOptimized.Should().BeFalse();
}

[Fact]
public void ShouldNotSquishCorruptedJpegLossy()
{
Test(@"TestImages\corrupted.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\mozjpeg_3.3.1_x86\cjpeg.exe",
"-quality 80", out var args, out var startingSize);
args.Stream.Length.Should().IsSameOrEqualTo(startingSize);
args.IsOptimized.Should().BeFalse();
}

private void Test(string imagePath, string exePath, string exeArgs)
private void Test(string imagePath, string exePath, string exeArgs, out OptimizerArgs argsOut, out long startingSize)
{
var inputStream = new MemoryStream();

Expand All @@ -63,16 +92,15 @@ private void Test(string imagePath, string exePath, string exeArgs)

var args = new OptimizerArgs(inputStream);

var startingSize = args.Stream.Length;
startingSize = args.Stream.Length;

var stopwatch = new Stopwatch();
stopwatch.Start();
sut.Process(args);
stopwatch.Stop();
output.WriteLine($"Time: {stopwatch.ElapsedMilliseconds}ms");

args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
argsOut = args;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dianoga.Optimizers;
using Dianoga.Optimizers.Pipelines.DianogaPng;
using FluentAssertions;
using FluentAssertions.Common;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -21,18 +22,32 @@ public void ShouldSquishSmallPng()
{
Test(@"TestImages\small.png",
@"..\..\..\..\Dianoga\Dianoga Tools\PNGOptimizer\PNGOptimizerCL.exe",
"");
"", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldSquishLargePng()
{
Test(@"TestImages\large.png",
@"..\..\..\..\Dianoga\Dianoga Tools\PNGOptimizer\PNGOptimizerCL.exe",
"");
"", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldNotSquishCorruptedJpeg()
{
Test(@"TestImages\corrupted.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\PNGOptimizer\PNGOptimizerCL.exe",
"", out var args, out var startingSize);
args.Stream.Length.Should().IsSameOrEqualTo(startingSize);
args.IsOptimized.Should().BeFalse();
}

private void Test(string imagePath, string exePath, string exeArgs)
private void Test(string imagePath, string exePath, string exeArgs, out OptimizerArgs argsOut, out long startingSize)
{
var inputStream = new MemoryStream();

Expand All @@ -47,16 +62,15 @@ private void Test(string imagePath, string exePath, string exeArgs)

var args = new OptimizerArgs(inputStream);

var startingSize = args.Stream.Length;
startingSize = args.Stream.Length;

var stopwatch = new Stopwatch();
stopwatch.Start();
sut.Process(args);
stopwatch.Stop();
output.WriteLine($"Time: {stopwatch.ElapsedMilliseconds}ms");

args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
argsOut = args;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dianoga.Optimizers;
using Dianoga.Optimizers.Pipelines.DianogaPng;
using FluentAssertions;
using FluentAssertions.Common;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -22,18 +23,32 @@ public void ShouldSquishSmallPng()
{
Test(@"TestImages\small.png",
@"..\..\..\..\Dianoga\Dianoga Tools\pngquant\pngquant.exe",
"");
"", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldSquishLargePng()
{
Test(@"TestImages\large.png",
@"..\..\..\..\Dianoga\Dianoga Tools\pngquant\pngquant.exe",
"");
"", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldNotSquishCorruptedJpeg()
{
Test(@"TestImages\corrupted.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\pngquant\pngquant.exe",
"", out var args, out var startingSize);
args.Stream.Length.Should().IsSameOrEqualTo(startingSize);
args.IsOptimized.Should().BeFalse();
}

private void Test(string imagePath, string exePath, string exeArgs)
private void Test(string imagePath, string exePath, string exeArgs, out OptimizerArgs argsOut, out long startingSize)
{
var inputStream = new MemoryStream();

Expand All @@ -48,16 +63,15 @@ private void Test(string imagePath, string exePath, string exeArgs)

var args = new OptimizerArgs(inputStream);

var startingSize = args.Stream.Length;
startingSize = args.Stream.Length;

var stopwatch = new Stopwatch();
stopwatch.Start();
sut.Process(args);
stopwatch.Stop();
output.WriteLine($"Time: {stopwatch.ElapsedMilliseconds}ms");

args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
output.WriteLine($"Time: {stopwatch.ElapsedMilliseconds}ms");

argsOut = args;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dianoga.Optimizers;
using Dianoga.Optimizers.Pipelines.DianogaSvg;
using FluentAssertions;
using FluentAssertions.Common;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -21,18 +22,32 @@ public void ShouldSquishSmallSvg()
{
Test(@"TestImages\small.svg",
@"..\..\..\..\Dianoga\Dianoga Tools\SVGO\svgop.exe",
"");
"", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

[Fact]
public void ShouldSquishLargeSvg()
{
Test(@"TestImages\large.svg",
@"..\..\..\..\Dianoga\Dianoga Tools\SVGO\svgop.exe",
"");
"", out var args, out var startingSize);
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
}

private void Test(string imagePath, string exePath, string exeArgs)
[Fact]
public void ShouldNotSquishCorruptedJpeg()
{
Test(@"TestImages\corrupted.jpg",
@"..\..\..\..\Dianoga\Dianoga Tools\SVGO\svgop.exe",
"", out var args, out var startingSize);
args.Stream.Length.Should().IsSameOrEqualTo(startingSize);
args.IsOptimized.Should().BeFalse();
}

private void Test(string imagePath, string exePath, string exeArgs, out OptimizerArgs argsOut, out long startingSize)
{
var inputStream = new MemoryStream();

Expand All @@ -47,16 +62,15 @@ private void Test(string imagePath, string exePath, string exeArgs)

var args = new OptimizerArgs(inputStream);

var startingSize = args.Stream.Length;
startingSize = args.Stream.Length;

var stopwatch = new Stopwatch();
stopwatch.Start();
sut.Process(args);
stopwatch.Stop();
output.WriteLine($"Time: {stopwatch.ElapsedMilliseconds}ms Size: {args.Stream.Length}");

args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0);
args.IsOptimized.Should().BeTrue();
argsOut = args;
}
}
}
Loading

0 comments on commit 87f4e58

Please sign in to comment.