]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/img.t
a7b92531f5c9c71b4f8a6122d66f02af9dd2b123
[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 my $magick = new Image::Magick;
22 my $SVGS_WORK = defined $magick->QueryFormat("svg");
24 ok(! system("rm -rf t/tmp; mkdir -p t/tmp/in"));
26 ok(! system("cp t/img/redsquare.png t/tmp/in/redsquare.png"));
27 # colons in filenames are a corner case for img
28 ok(! system("cp t/img/redsquare.png t/tmp/in/hello:world.png"));
29 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c.png"));
30 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d.png"));
31 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d:e:f:g:h:i:j.png"));
33 if ($SVGS_WORK) {
34         writefile("emptysquare.svg", "t/tmp/in",
35                 '<svg width="30" height="30"><rect x="0" y="0" width="30" height="30" fill="blue"/></svg>');
36 }
38 # using different image sizes for different pages, so the pagenumber selection can be tested easily
39 ok(! system("cp t/img/twopages.pdf t/tmp/in/twopages.pdf"));
41 my $maybe_svg_img = "";
42 if ($SVGS_WORK) {
43         $maybe_svg_img = "[[!img emptysquare.svg size=10x]]";
44 }
46 writefile("imgconversions.mdwn", "t/tmp/in", <<EOF
47 [[!img redsquare.png]]
48 [[!img redsquare.png size=10x]]
49 [[!img redsquare.png size=30x50]] expecting 30x30
50 [[!img hello:world.png size=x8]] expecting 8x8
51 [[!img a:b:c.png size=x4]]
52 [[!img a:b:c:d:e:f:g:h:i:j.png size=x6]]
53 $maybe_svg_img
54 [[!img twopages.pdf size=12x]]
55 [[!img twopages.pdf size=16x pagenumber=1]]
56 EOF
57 );
59 ok(! system("make -s ikiwiki.out"));
61 my $command = "perl -I. ./ikiwiki.out -set usedirs=0 -templatedir=templates -plugin img t/tmp/in t/tmp/out -verbose";
63 ok(! system($command));
65 sub size($) {
66         my $filename = shift;
67         my $im = Image::Magick->new();
68         my $r = $im->Read(":$filename");
69         return "no image" if $r;
70         my $w = $im->Get("width");
71         my $h = $im->Get("height");
72         return "${w}x${h}";
73 }
75 my $outpath = "t/tmp/out/imgconversions";
76 my $outhtml = readfile("$outpath.html");
78 is(size("$outpath/10x-redsquare.png"), "10x10");
79 ok(! -e "$outpath/30x-redsquare.png");
80 ok($outhtml =~ /width="30" height="30".*expecting 30x30/);
82 if ($SVGS_WORK) {
83         # if this fails, you need libmagickcore-6.q16-2-extra installed
84         is(size("$outpath/10x-emptysquare.png"), "10x10");
85 }
87 is(size("$outpath/12x-twopages.png"), "12x12");
88 is(size("$outpath/16x-p1-twopages.png"), "16x2");
89 ok($outhtml =~ /width="8" height="8".*expecting 8x8/);
90 is(size("$outpath/x8-hello:world.png"), "8x8");
91 is(size("$outpath/x4-a:b:c.png"), "4x4");
92 is(size("$outpath/x6-a:b:c:d:e:f:g:h:i:j.png"), "6x6");
94 # now let's remove them again
96 if (1) { # for easier testing
97         writefile("imgconversions.mdwn", "t/tmp/in", "nothing to see here");
99         ok(! system("$command --refresh"));
101         ok(! -e "$outpath/10x-simple.png");
102         ok(! -e "$outpath/10x-simple-svg.png");
103         ok(! -e "$outpath/10x-simple-pdf.png");
104         ok(! -e "$outpath/10x-p1-simple-pdf.png");
105         ok(! -e "$outpath/x8-hello:world.png");
106         ok(! -e "$outpath/x4-a:b:c.png");
107         ok(! -e "$outpath/x6-a:b:c:d:e:f:g:h:i:j.png");
109         # cleanup
110         ok(! system("rm -rf t/tmp"));
112 done_testing;
114 1;