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