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