]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/img.t
e42d85353c8f33aabc7da4a96f5f2104ed19c598
[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 Cwd qw(getcwd);
17 use Test::More;
18 plan(skip_all => "Image::Magick not available")
19         unless eval q{use Image::Magick; 1};
21 use IkiWiki;
23 my $installed = $ENV{INSTALLED_TESTS};
25 my @command;
26 if ($installed) {
27         @command = qw(ikiwiki);
28 }
29 else {
30         ok(! system("make -s ikiwiki.out"));
31         @command = ("perl", "-I".getcwd, qw(./ikiwiki.out
32                 --underlaydir=underlays/basewiki
33                 --set underlaydirbase=underlays
34                 --templatedir=templates));
35 }
37 push @command, qw(--set usedirs=0 --plugin img t/tmp/in t/tmp/out --verbose);
39 my $magick = new Image::Magick;
41 $magick->Read("t/img/twopages.pdf");
42 my $PDFS_WORK = defined $magick->Get("width");
44 ok(! system("rm -rf t/tmp; mkdir -p t/tmp/in"));
46 ok(! system("cp t/img/redsquare.png t/tmp/in/redsquare.png"));
47 ok(! system("cp t/img/redsquare.jpg t/tmp/in/redsquare.jpg"));
48 ok(! system("cp t/img/redsquare.jpg t/tmp/in/redsquare.jpeg"));
49 ok(! system("cp t/img/redsquare.jpg t/tmp/in/SHOUTY.JPG"));
50 # colons in filenames are a corner case for img
51 ok(! system("cp t/img/redsquare.png t/tmp/in/hello:world.png"));
52 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c.png"));
53 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d.png"));
54 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d:e:f:g:h:i:j.png"));
56 writefile("bluesquare.svg", "t/tmp/in",
57         '<svg width="30" height="30"><rect x="0" y="0" width="30" height="30" fill="blue"/></svg>');
58 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.png"));
59 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.bmp"));
60 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.pdf"));
62 # using different image sizes for different pages, so the pagenumber selection can be tested easily
63 ok(! system("cp t/img/twopages.pdf t/tmp/in/twopages.pdf"));
64 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.JPEG"));
65 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.jpg"));
66 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.png"));
67 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.svg"));
69 my $maybe_pdf_img = "";
70 if ($PDFS_WORK) {
71         $maybe_pdf_img = <<EOF;
72 [[!img twopages.pdf size=12x]]
73 [[!img twopages.pdf size=16x pagenumber=1]]
74 EOF
75 }
77 writefile("imgconversions.mdwn", "t/tmp/in", <<EOF
78 [[!img redsquare.png]]
79 [[!img redsquare.jpg size=11x]]
80 [[!img redsquare.jpeg size=12x]]
81 [[!img SHOUTY.JPG size=13x]]
82 [[!img redsquare.png size=10x]]
83 [[!img redsquare.png size=30x50]] expecting 30x30
84 [[!img hello:world.png size=x8]] expecting 8x8
85 [[!img a:b:c.png size=x4]]
86 [[!img a:b:c:d:e:f:g:h:i:j.png size=x6]]
87 [[!img bluesquare.svg size=42x]] expecting 42x
88 [[!img bluesquare.svg size=x43]] expecting x43
89 [[!img bluesquare.svg size=42x43]] expecting 42x43 because aspect ratio not preserved
90 $maybe_pdf_img
92 # bad ideas
93 [[!img really-svg.png size=666x]]
94 [[!img really-svg.bmp size=666x]]
95 [[!img really-svg.pdf size=666x]]
96 [[!img really-pdf.JPEG size=666x]]
97 [[!img really-pdf.jpg size=666x]]
98 [[!img really-pdf.png size=666x]]
99 [[!img really-pdf.svg size=666x]]
100 EOF
101 );
102 ok(utime(333333333, 333333333, "t/tmp/in/imgconversions.mdwn"));
104 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[JPEG, PNG, svg, pdf]'));
106 sub size($) {
107         my $filename = shift;
108         my $decoder;
109         if ($filename =~ m/\.png$/i) {
110                 $decoder = 'png';
111         }
112         elsif ($filename =~ m/\.jpe?g$/i) {
113                 $decoder = 'jpeg';
114         }
115         elsif ($filename =~ m/\.bmp$/i) {
116                 $decoder = 'bmp';
117         }
118         else {
119                 die "Unexpected extension in '$filename'";
120         }
121         my $im = Image::Magick->new();
122         my $r = $im->Read("$decoder:$filename");
123         return "no image: $r" if $r;
124         my $w = $im->Get("width");
125         my $h = $im->Get("height");
126         return "${w}x${h}";
129 my $outpath = "t/tmp/out/imgconversions";
130 my $outhtml = readfile("$outpath.html");
132 is(size("$outpath/10x-redsquare.png"), "10x10");
133 ok(! -e "$outpath/30x-redsquare.png");
134 ok($outhtml =~ /width="30" height="30".*expecting 30x30/);
135 ok($outhtml =~ /width="42".*expecting 42x/);
136 ok($outhtml =~ /height="43".*expecting x43/);
137 ok($outhtml =~ /width="42" height="43".*expecting 42x43/);
139 SKIP: {
140         skip "PDF support not installed (try ghostscript)", 2
141                 unless $PDFS_WORK;
142         is(size("$outpath/12x-twopages.png"), "12x12");
143         is(size("$outpath/16x-p1-twopages.png"), "16x2");
146 ok($outhtml =~ /width="8" height="8".*expecting 8x8/);
147 is(size("$outpath/x8-hello:world.png"), "8x8");
148 is(size("$outpath/x4-a:b:c.png"), "4x4");
149 is(size("$outpath/x6-a:b:c:d:e:f:g:h:i:j.png"), "6x6");
151 is(size("$outpath/11x-redsquare.jpg"), "11x11");
152 is(size("$outpath/12x-redsquare.jpeg"), "12x12");
153 is(size("$outpath/13x-SHOUTY.JPG"), "13x13");
154 like($outhtml, qr{src="(\./)?imgconversions/11x-redsquare\.jpg" width="11" height="11"});
155 like($outhtml, qr{src="(\./)?imgconversions/12x-redsquare\.jpeg" width="12" height="12"});
156 like($outhtml, qr{src="(\./)?imgconversions/13x-SHOUTY\.JPG" width="13" height="13"});
158 # We do not misinterpret images
159 my $quot = qr/(?:"|&quot;)/;
160 like($outhtml, qr/${quot}really-svg\.png${quot} does not seem to be a valid png file/);
161 ok(! -e "$outpath/666x-really-svg.png");
162 ok(! -e "$outpath/666x-really-svg.bmp");
163 like($outhtml, qr/${quot}really-pdf\.JPEG${quot} does not seem to be a valid jpeg file/);
164 ok(! -e "$outpath/666x-really-pdf.jpeg");
165 ok(! -e "$outpath/666x-really-pdf.JPEG");
166 like($outhtml, qr/${quot}really-pdf\.jpg${quot} does not seem to be a valid jpeg file/);
167 ok(! -e "$outpath/666x-really-pdf.jpg");
168 like($outhtml, qr/${quot}really-pdf\.png${quot} does not seem to be a valid png file/);
169 ok(! -e "$outpath/666x-really-pdf.png");
171 # resize is deterministic when deterministic=1
172 ok(utime(333333333, 333333333, "t/tmp/in/redsquare.png"));
173 ok(! system("rm $outpath/10x-redsquare.png"));
174 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[JPEG, PNG, svg, pdf]', '--set', 'deterministic=1', "--rebuild"));
175 ok(! system("cp $outpath/10x-redsquare.png $outpath/10x-redsquare.png.orig"));
176 ok(utime(444444444, 444444444, "t/tmp/in/redsquare.png"));
177 ok(! system("rm $outpath/10x-redsquare.png"));
178 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[JPEG, PNG, svg, pdf]', '--set', 'deterministic=1', "--rebuild"));
179 ok(
180         ! system("cmp $outpath/10x-redsquare.png $outpath/10x-redsquare.png.orig"),
181         "resize is deterministic when configured with deterministic=1"
182 );
184 # disable support for uncommon formats and try again
185 ok(! system(@command, "--rebuild"));
186 ok(! -e "$outpath/10x-bluesquare.png");
187 ok(! -e "$outpath/12x-twopages.png");
188 ok(! -e "$outpath/16x-p1-twopages.png");
190 # now let's remove them again
192 if (1) { # for easier testing
193         writefile("imgconversions.mdwn", "t/tmp/in", "nothing to see here");
195         ok(! system(@command, "--refresh"));
197         ok(! -e "$outpath/10x-redsquare.png");
198         ok(! -e "$outpath/10x-bluesquare.png");
199         ok(! -e "$outpath/12x-twopages.png");
200         ok(! -e "$outpath/13x-SHOUTY.JPG");
201         ok(! -e "$outpath/16x-p1-twopages.png");
202         ok(! -e "$outpath/x8-hello:world.png");
203         ok(! -e "$outpath/x4-a:b:c.png");
204         ok(! -e "$outpath/x6-a:b:c:d:e:f:g:h:i:j.png");
206         # cleanup
207         ok(! system("rm -rf t/tmp"));
209 done_testing;
211 1;