Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarin committed Aug 1, 2024
1 parent d32a0e0 commit 46e953c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/HTFeed/Image/Grok.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ sub compress {
"-q 32",
"> /dev/null 2>&1"
);

my $sys_ret_val = system($full_cmd);

return $sys_ret_val;
return !$sys_ret_val;
}

sub decompress {
Expand All @@ -67,7 +68,7 @@ sub decompress {
);
my $sys_ret_val = system($full_cmd);

return $sys_ret_val;
return !$sys_ret_val;
}

1;
3 changes: 2 additions & 1 deletion lib/HTFeed/Image/Magick.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ sub compress {
"-strip",
"'$outfile'"
);
print $full_cmd . "\n";
my $sys_ret_val = system($full_cmd);

return $sys_ret_val;
return !$sys_ret_val;
}

1;
7 changes: 6 additions & 1 deletion lib/HTFeed/Image/Shared.pm
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package HTFeed::Image::Shared;

# Shared functionality for the classes under HTFeed::Image.

use strict;
use warnings;

use Data::Dumper;
use Log::Log4perl qw(get_logger);

# Take a hashref, check that all values are defined and truthy
# Take a hashref,
# check that all values are defined and truthy,
# or use key to tell you which value was invalid.
sub check_set {
my $validate = shift || {};

foreach my $key (keys %$validate) {
my $val = $validate->{$key};
if (!defined $val || !$val) {
Expand All @@ -25,6 +29,7 @@ sub check_set {
sub _invalid_input {
my $input_name = shift;
my $input_value = shift;

my $is_undef = !defined $input_value;

if ($is_undef) {
Expand Down
Binary file added t/fixtures/reference_images/autumn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added t/fixtures/reference_images/autumn.tif
Binary file not shown.
1 change: 1 addition & 0 deletions t/fixtures/reference_images/blah.tif
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blah
Binary file not shown.
Binary file added t/fixtures/reference_images/plywood.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions t/image.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/lib";

use Image::ExifTool;
use Test::Spec;
use Test::Exception;
# classes under test:
use HTFeed::Image::Grok;
use HTFeed::Image::Magick;

my $test_dir = "/tmp/imgtest";
my $reference_dir = "/usr/local/feed/t/fixtures/reference_images";

describe "HTFeed::Image" => sub {
before each => sub {
# Clean copy of reference images for each test.
system("rm -rf '$test_dir'");
system("mkdir '$test_dir'");
};
# Put a fresh copy of the requested reference input file
# in the testing directory before running a test.
sub cp_to_test {
my $imgname = shift;
system("cp $reference_dir/$imgname $test_dir/$imgname");
}
# Assert that the 1st file is bigger than the 2nd (input order matters).
sub bigger_than {
ok(-s $_[0] > -s $_[1]);
}
# We want to fail a test that e.g. thinks it wrote a tiff but didn't.
sub check_outfile {
my $outfile = shift;
# Check that outfile exists & isn't empty.
ok(-f $outfile);
ok(-s $outfile > 0);

# Check file ext on outfile...
my $ext = "";
if ($outfile =~ m/\.(\S+)$/) {
$ext = $1;
} else {
die "no ext on outfile\n";
}

# ... and compare file ext against exiftool's idea of filetype.
my $exifTool = new Image::ExifTool;
$exifTool->ExtractInfo($outfile, {Binary => 1});
my $exif_filetype = $exifTool->GetValue("FileType");
if ($ext eq "jp2") {
ok($exif_filetype eq "JP2");
} elsif ($ext eq "tif") {
ok($exif_filetype eq "TIFF");
} else {
ok(0);
}
}
it "can decompress a jpeg2000 to a tiff w/ Grok" => sub {
#
};
it "can compress a tiff to a jpeg2000 w/ Grok" => sub {
cp_to_test("autumn.tif");
my $in = "$test_dir/autumn.tif";
my $out = "$test_dir/autumn_out.jp2";
my $res = HTFeed::Image::Grok::compress($in, $out);
ok($res);
check_outfile($out);
bigger_than($in, $out);
};
it "can convert a png to a tiff w/ Magick" => sub {
cp_to_test("autumn.png");
my $in = "$test_dir/autumn.png";
my $out = "$test_dir/autumn_out.tif";
my %args = (-compress => 'None', '-type' => 'TrueColor');
my $res = HTFeed::Image::Magick::compress($in, $out, %args);
ok($res);
check_outfile($out);
bigger_than($out, $in);
};
it "can compress a jpg to a tiff w/ Magick" => sub {
cp_to_test("plywood.jpg");
my $in = "$test_dir/plywood.jpg";
my $out = "$test_dir/plywood_out.tif";
my %args = (-compress => 'None', '-type' => 'TrueColor');
my $res = HTFeed::Image::Magick::compress($in, $out, %args);
ok($res);
check_outfile($out);
bigger_than($out, $in);
};
it "can convert a truecolor tiff to a jpeg2000 w/ Magick" => sub {
cp_to_test("autumn.tif");
my $in = "$test_dir/autumn.tif";
my $out = "$test_dir/autumn_out.jp2";
my %args = (-compress => 'None', '-type' => 'TrueColor');
my $res = HTFeed::Image::Magick::compress($in, $out, %args);
ok($res);
check_outfile($out);
bigger_than($in, $out);
};
it "can convert a grayscale tiff to a jpeg2000 w/ Magick" => sub {
cp_to_test("circuit_grayscale.tif");
my $in = "$test_dir/circuit_grayscale.tif";
my $out = "$test_dir/circuit_grayscale_out.jp2";
my %args = (-compress => 'None', '-type' => 'Grayscale');
my $res = HTFeed::Image::Magick::compress($in, $out, %args);
ok($res);
check_outfile($out);
bigger_than($in, $out);
};
};

runtests unless caller;

0 comments on commit 46e953c

Please sign in to comment.