]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/img.t
Run autopkgtest tests using autodep8 and the pkg-perl team's infrastructure
[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 $installed = $ENV{INSTALLED_TESTS};
23 my @command;
24 if ($installed) {
25         @command = qw(ikiwiki);
26 }
27 else {
28         ok(! system("make -s ikiwiki.out"));
29         @command = qw(perl -I. ./ikiwiki.out
30                 --underlaydir=underlays/basewiki
31                 --set underlaydirbase=underlays
32                 --templatedir=templates);
33 }
35 push @command, qw(--set usedirs=0 --plugin img t/tmp/in t/tmp/out --verbose);
37 my $installed = $ENV{INSTALLED_TESTS};
39 my @command;
40 if ($installed) {
41         @command = qw(ikiwiki);
42 }
43 else {
44         ok(! system("make -s ikiwiki.out"));
45         @command = qw(perl -I. ./ikiwiki.out
46                 --underlaydir=underlays/basewiki
47                 --set underlaydirbase=underlays
48                 --templatedir=templates);
49 }
51 push @command, qw(--set usedirs=0 --plugin img t/tmp/in t/tmp/out --verbose);
53 my $magick = new Image::Magick;
55 $magick->Read("t/img/twopages.pdf");
56 my $PDFS_WORK = defined $magick->Get("width");
58 ok(! system("rm -rf t/tmp; mkdir -p t/tmp/in"));
60 ok(! system("cp t/img/redsquare.png t/tmp/in/redsquare.png"));
61 ok(! system("cp t/img/redsquare.jpg t/tmp/in/redsquare.jpg"));
62 ok(! system("cp t/img/redsquare.jpg t/tmp/in/redsquare.jpeg"));
63 ok(! system("cp t/img/redsquare.jpg t/tmp/in/SHOUTY.JPG"));
64 # colons in filenames are a corner case for img
65 ok(! system("cp t/img/redsquare.png t/tmp/in/hello:world.png"));
66 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c.png"));
67 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d.png"));
68 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d:e:f:g:h:i:j.png"));
70 writefile("bluesquare.svg", "t/tmp/in",
71         '<svg width="30" height="30"><rect x="0" y="0" width="30" height="30" fill="blue"/></svg>');
72 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.png"));
73 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.bmp"));
74 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.pdf"));
76 # using different image sizes for different pages, so the pagenumber selection can be tested easily
77 ok(! system("cp t/img/twopages.pdf t/tmp/in/twopages.pdf"));
78 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.JPEG"));
79 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.jpg"));
80 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.png"));
81 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.svg"));
83 my $maybe_pdf_img = "";
84 if ($PDFS_WORK) {
85         $maybe_pdf_img = <<EOF;
86 [[!img twopages.pdf size=12x]]
87 [[!img twopages.pdf size=16x pagenumber=1]]
88 EOF
89 }
91 writefile("imgconversions.mdwn", "t/tmp/in", <<EOF
92 [[!img redsquare.png]]
93 [[!img redsquare.jpg size=11x]]
94 [[!img redsquare.jpeg size=12x]]
95 [[!img SHOUTY.JPG size=13x]]
96 [[!img redsquare.png size=10x]]
97 [[!img redsquare.png size=30x50]] expecting 30x30
98 [[!img hello:world.png size=x8]] expecting 8x8
99 [[!img a:b:c.png size=x4]]
100 [[!img a:b:c:d:e:f:g:h:i:j.png size=x6]]
101 [[!img bluesquare.svg size=42x]] expecting 42x
102 [[!img bluesquare.svg size=x43]] expecting x43
103 [[!img bluesquare.svg size=42x43]] expecting 42x43 because aspect rario not preserved
104 $maybe_pdf_img
106 # bad ideas
107 [[!img really-svg.png size=666x]]
108 [[!img really-svg.bmp size=666x]]
109 [[!img really-svg.pdf size=666x]]
110 [[!img really-pdf.JPEG size=666x]]
111 [[!img really-pdf.jpg size=666x]]
112 [[!img really-pdf.png size=666x]]
113 [[!img really-pdf.svg size=666x]]
114 EOF
115 );
116 ok(utime(333333333, 333333333, "t/tmp/in/imgconversions.mdwn"));
118 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[JPEG, PNG, svg, pdf]'));
120 sub size($) {
121         my $filename = shift;
122         my $im = Image::Magick->new();
123         my $r = $im->Read(":$filename");
124         return "no image" if $r;
125         my $w = $im->Get("width");
126         my $h = $im->Get("height");
127         return "${w}x${h}";
130 my $outpath = "t/tmp/out/imgconversions";
131 my $outhtml = readfile("$outpath.html");
133 is(size("$outpath/10x-redsquare.png"), "10x10");
134 ok(! -e "$outpath/30x-redsquare.png");
135 ok($outhtml =~ /width="30" height="30".*expecting 30x30/);
136 ok($outhtml =~ /width="42".*expecting 42x/);
137 ok($outhtml =~ /height="43".*expecting x43/);
138 ok($outhtml =~ /width="42" height="43".*expecting 42x43/);
140 SKIP: {
141         skip "PDF support not installed (try ghostscript)", 2
142                 unless $PDFS_WORK;
143         is(size("$outpath/12x-twopages.png"), "12x12");
144         is(size("$outpath/16x-p1-twopages.png"), "16x2");
147 ok($outhtml =~ /width="8" height="8".*expecting 8x8/);
148 is(size("$outpath/x8-hello:world.png"), "8x8");
149 is(size("$outpath/x4-a:b:c.png"), "4x4");
150 is(size("$outpath/x6-a:b:c:d:e:f:g:h:i:j.png"), "6x6");
152 is(size("$outpath/11x-redsquare.jpg"), "11x11");
153 is(size("$outpath/12x-redsquare.jpeg"), "12x12");
154 is(size("$outpath/13x-SHOUTY.JPG"), "13x13");
155 like($outhtml, qr{src="(\./)?imgconversions/11x-redsquare\.jpg" width="11" height="11"});
156 like($outhtml, qr{src="(\./)?imgconversions/12x-redsquare\.jpeg" width="12" height="12"});
157 like($outhtml, qr{src="(\./)?imgconversions/13x-SHOUTY\.JPG" width="13" height="13"});
159 # We do not misinterpret images
160 my $quot = qr/(?:"|&quot;)/;
161 like($outhtml, qr/${quot}really-svg\.png${quot} does not seem to be a valid png file/);
162 ok(! -e "$outpath/666x-really-svg.png");
163 ok(! -e "$outpath/666x-really-svg.bmp");
164 like($outhtml, qr/${quot}really-pdf\.JPEG${quot} does not seem to be a valid jpeg file/);
165 ok(! -e "$outpath/666x-really-pdf.jpeg");
166 ok(! -e "$outpath/666x-really-pdf.JPEG");
167 like($outhtml, qr/${quot}really-pdf\.jpg${quot} does not seem to be a valid jpeg file/);
168 ok(! -e "$outpath/666x-really-pdf.jpg");
169 like($outhtml, qr/${quot}really-pdf\.png${quot} does not seem to be a valid png file/);
170 ok(! -e "$outpath/666x-really-pdf.png");
172 # disable support for uncommon formats and try again
173 ok(! system(@command, "--rebuild"));
174 ok(! -e "$outpath/10x-bluesquare.png");
175 ok(! -e "$outpath/12x-twopages.png");
176 ok(! -e "$outpath/16x-p1-twopages.png");
178 # now let's remove them again
180 if (1) { # for easier testing
181         writefile("imgconversions.mdwn", "t/tmp/in", "nothing to see here");
183         ok(! system(@command, "--refresh"));
185         ok(! -e "$outpath/10x-redsquare.png");
186         ok(! -e "$outpath/10x-bluesquare.png");
187         ok(! -e "$outpath/12x-twopages.png");
188         ok(! -e "$outpath/13x-SHOUTY.JPG");
189         ok(! -e "$outpath/16x-p1-twopages.png");
190         ok(! -e "$outpath/x8-hello:world.png");
191         ok(! -e "$outpath/x4-a:b:c.png");
192         ok(! -e "$outpath/x6-a:b:c:d:e:f:g:h:i:j.png");
194         # cleanup
195         ok(! system("rm -rf t/tmp"));
197 done_testing;
199 1;