]> sipb.mit.edu Git - ikiwiki.git/blob - t/img.t
Merge remote-tracking branch 'origin/master'
[ikiwiki.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;
13
14 use warnings;
15 use strict;
16 use Test::More;
17
18 BEGIN { use_ok("IkiWiki"); }
19 BEGIN { use_ok("Image::Magick"); }
20
21 my $magick = new Image::Magick;
22 my $SVGS_WORK = defined $magick->QueryFormat("svg");
23
24 ok(! system("rm -rf t/tmp; mkdir -p t/tmp/in"));
25
26 ok(! system("cp t/img/redsquare.png t/tmp/in/redsquare.png"));
27
28 if ($SVGS_WORK) {
29         writefile("emptysquare.svg", "t/tmp/in",
30                 '<svg width="30" height="30"><rect x="0" y="0" width="30" height="30" fill="blue"/></svg>');
31 }
32
33 # using different image sizes for different pages, so the pagenumber selection can be tested easily
34 ok(! system("cp t/img/twopages.pdf t/tmp/in/twopages.pdf"));
35
36 my $maybe_svg_img = "";
37 if ($SVGS_WORK) {
38         $maybe_svg_img = "[[!img emptysquare.svg size=10x]]";
39 }
40
41 writefile("imgconversions.mdwn", "t/tmp/in", <<EOF
42 [[!img redsquare.png]]
43 [[!img redsquare.png size=10x]]
44 [[!img redsquare.png size=30x50]] expecting 30x30
45 $maybe_svg_img
46 [[!img twopages.pdf size=12x]]
47 [[!img twopages.pdf size=16x pagenumber=1]]
48 EOF
49 );
50
51 ok(! system("make -s ikiwiki.out"));
52
53 my $command = "perl -I. ./ikiwiki.out -set usedirs=0 -templatedir=templates -plugin img t/tmp/in t/tmp/out -verbose";
54
55 ok(! system($command));
56
57 sub size($) {
58         my $filename = shift;
59         my $im = Image::Magick->new();
60         my $r = $im->Read($filename);
61         return "no image" if $r;
62         my $w = $im->Get("width");
63         my $h = $im->Get("height");
64         return "${w}x${h}";
65 }
66
67 my $outpath = "t/tmp/out/imgconversions";
68 my $outhtml = readfile("$outpath.html");
69
70 is(size("$outpath/10x-redsquare.png"), "10x10");
71 ok(! -e "$outpath/30x-redsquare.png");
72 ok($outhtml =~ /width="30" height="30".*expecting 30x30/);
73
74 if ($SVGS_WORK) {
75         # if this fails, you need libmagickcore-6.q16-2-extra installed
76         is(size("$outpath/10x-emptysquare.png"), "10x10");
77 }
78
79 is(size("$outpath/12x-twopages.png"), "12x12");
80 is(size("$outpath/16x-p1-twopages.png"), "16x2");
81
82 # now let's remove them again
83
84 if (1) { # for easier testing
85         writefile("imgconversions.mdwn", "t/tmp/in", "nothing to see here");
86
87         ok(! system("$command --refresh"));
88
89         ok(! -e "$outpath/10x-simple.png");
90         ok(! -e "$outpath/10x-simple-svg.png");
91         ok(! -e "$outpath/10x-simple-pdf.png");
92         ok(! -e "$outpath/10x-p1-simple-pdf.png");
93
94         # cleanup
95         ok(! system("rm -rf t/tmp"));
96 }
97 done_testing;
98
99 1;