]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/img.t
unit test for bugs/image_rescaling_distorts_with_small_pictures
[git.ikiwiki.info.git] / t / img.t
1 #!/usr/bin/perl
2 #
3 # unit test that creates test images (png, svg, multi-page pdf), runs ikiwiki
4 # on them, checks the resulting images for plausibility based on their image
5 # sizes, and checks if they vanish when not required in the build process any
6 # more
7 #
8 # if you have trouble here, be aware that there are three debian packages that
9 # can provide Image::Magick: perlmagick, libimage-magick-perl and
10 # graphicsmagick-libmagick-dev-compat
11 #
12 package IkiWiki;
14 use warnings;
15 use strict;
16 use Test::More;
18 BEGIN { use_ok("IkiWiki"); }
19 BEGIN { use_ok("Image::Magick"); }
21 ok(! system("rm -rf t/tmp; mkdir -p t/tmp/in"));
23 ok(! system("cp t/img/redsquare.png t/tmp/in/redsquare.png"));
24 writefile("emptysquare.svg", "t/tmp/in", '<svg width="30" height="30"/>');
25 # using different image sizes for different pages, so the pagenumber selection can be tested easily
26 ok(! system("cp t/img/twopages.pdf t/tmp/in/twopages.pdf"));
28 writefile("imgconversions.mdwn", "t/tmp/in", <<EOF
29 [[!img redsquare.png]]
30 [[!img redsquare.png size=10x]]
31 [[!img redsquare.png size=30x50]] expecting 30x30
32 [[!img emptysquare.svg size=10x]]
33 [[!img twopages.pdf size=12x]]
34 [[!img twopages.pdf size=16x pagenumber=1]]
35 EOF
36 );
38 ok(! system("make -s ikiwiki.out"));
40 my $command = "perl -I. ./ikiwiki.out -set usedirs=0 -plugin img t/tmp/in t/tmp/out -verbose";
42 ok(! system($command));
44 sub size($) {
45         my $filename = shift;
46         my $im = Image::Magick->new();
47         my $r = $im->Read($filename);
48         return "no image" if $r;
49         my $w = $im->Get("width");
50         my $h = $im->Get("height");
51         return "${w}x${h}";
52 }
54 my $outpath = "t/tmp/out/imgconversions";
55 open (my $outhtmlfd, "<", "$outpath.html");
56 local $/=undef;
57 my $outhtml = <$outhtmlfd>;
58 close $outhtmlfd;
60 is(size("$outpath/10x-redsquare.png"), "10x10");
61 ok(! -e "$outpath/30x-redsquare.png");
62 ok($outhtml =~ /width="30" height="30".*expecting 30x30/);
63 # if this fails, you need libmagickcore-6.q16-2-extra installed
64 is(size("$outpath/10x-emptysquare.png"), "10x10");
65 is(size("$outpath/12x-twopages.png"), "12x12");
66 is(size("$outpath/16x-p1-twopages.png"), "16x2");
68 # now let's remove them again
70 if (1) { # for easier testing
71         writefile("imgconversions.mdwn", "t/tmp/in", "nothing to see here");
73         ok(! system("$command --refresh"));
75         ok(! -e "$outpath/10x-simple.png");
76         ok(! -e "$outpath/10x-simple-svg.png");
77         ok(! -e "$outpath/10x-simple-pdf.png");
78         ok(! -e "$outpath/10x-p1-simple-pdf.png");
80         # cleanup
81         ok(! system("rm -rf t/tmp"));
82 }
83 done_testing;
85 1;