]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/img.t
img: make img_allowed_formats case-insensitive
[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 $magick = new Image::Magick;
39 $magick->Read("t/img/twopages.pdf");
40 my $PDFS_WORK = defined $magick->Get("width");
42 ok(! system("rm -rf t/tmp; mkdir -p t/tmp/in"));
44 ok(! system("cp t/img/redsquare.png t/tmp/in/redsquare.png"));
45 ok(! system("cp t/img/redsquare.jpg t/tmp/in/redsquare.jpg"));
46 ok(! system("cp t/img/redsquare.jpg t/tmp/in/redsquare.jpeg"));
47 # colons in filenames are a corner case for img
48 ok(! system("cp t/img/redsquare.png t/tmp/in/hello:world.png"));
49 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c.png"));
50 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d.png"));
51 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d:e:f:g:h:i:j.png"));
53 writefile("bluesquare.svg", "t/tmp/in",
54         '<svg width="30" height="30"><rect x="0" y="0" width="30" height="30" fill="blue"/></svg>');
55 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.png"));
56 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.bmp"));
57 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.pdf"));
59 # using different image sizes for different pages, so the pagenumber selection can be tested easily
60 ok(! system("cp t/img/twopages.pdf t/tmp/in/twopages.pdf"));
61 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.jpeg"));
62 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.jpg"));
63 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.png"));
64 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.svg"));
66 my $maybe_pdf_img = "";
67 if ($PDFS_WORK) {
68         $maybe_pdf_img = <<EOF;
69 [[!img twopages.pdf size=12x]]
70 [[!img twopages.pdf size=16x pagenumber=1]]
71 EOF
72 }
74 writefile("imgconversions.mdwn", "t/tmp/in", <<EOF
75 [[!img redsquare.png]]
76 [[!img redsquare.jpg size=11x]]
77 [[!img redsquare.jpeg size=12x]]
78 [[!img redsquare.png size=10x]]
79 [[!img redsquare.png size=30x50]] expecting 30x30
80 [[!img hello:world.png size=x8]] expecting 8x8
81 [[!img a:b:c.png size=x4]]
82 [[!img a:b:c:d:e:f:g:h:i:j.png size=x6]]
83 [[!img bluesquare.svg size=42x]] expecting 42x
84 [[!img bluesquare.svg size=x43]] expecting x43
85 [[!img bluesquare.svg size=42x43]] expecting 42x43 because aspect rario not preserved
86 $maybe_pdf_img
88 # bad ideas
89 [[!img really-svg.png size=666x]]
90 [[!img really-svg.bmp size=666x]]
91 [[!img really-svg.pdf size=666x]]
92 [[!img really-pdf.jpeg size=666x]]
93 [[!img really-pdf.jpg size=666x]]
94 [[!img really-pdf.png size=666x]]
95 [[!img really-pdf.svg size=666x]]
96 EOF
97 );
98 ok(utime(333333333, 333333333, "t/tmp/in/imgconversions.mdwn"));
100 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[JPEG, PNG, svg, pdf]'));
102 sub size($) {
103         my $filename = shift;
104         my $im = Image::Magick->new();
105         my $r = $im->Read(":$filename");
106         return "no image" if $r;
107         my $w = $im->Get("width");
108         my $h = $im->Get("height");
109         return "${w}x${h}";
112 my $outpath = "t/tmp/out/imgconversions";
113 my $outhtml = readfile("$outpath.html");
115 is(size("$outpath/10x-redsquare.png"), "10x10");
116 ok(! -e "$outpath/30x-redsquare.png");
117 ok($outhtml =~ /width="30" height="30".*expecting 30x30/);
118 ok($outhtml =~ /width="42".*expecting 42x/);
119 ok($outhtml =~ /height="43".*expecting x43/);
120 ok($outhtml =~ /width="42" height="43".*expecting 42x43/);
122 SKIP: {
123         skip "PDF support not installed (try ghostscript)", 2
124                 unless $PDFS_WORK;
125         is(size("$outpath/12x-twopages.png"), "12x12");
126         is(size("$outpath/16x-p1-twopages.png"), "16x2");
129 ok($outhtml =~ /width="8" height="8".*expecting 8x8/);
130 is(size("$outpath/x8-hello:world.png"), "8x8");
131 is(size("$outpath/x4-a:b:c.png"), "4x4");
132 is(size("$outpath/x6-a:b:c:d:e:f:g:h:i:j.png"), "6x6");
134 is(size("$outpath/11x-redsquare.jpg"), "11x11");
135 is(size("$outpath/12x-redsquare.jpeg"), "12x12");
136 like($outhtml, qr{src="(\./)?imgconversions/11x-redsquare\.jpg" width="11" height="11"});
137 like($outhtml, qr{src="(\./)?imgconversions/12x-redsquare\.jpeg" width="12" height="12"});
139 # We do not misinterpret images
140 my $quot = qr/(?:"|&quot;)/;
141 like($outhtml, qr/${quot}really-svg\.png${quot} does not seem to be a valid png file/);
142 ok(! -e "$outpath/666x-really-svg.png");
143 ok(! -e "$outpath/666x-really-svg.bmp");
144 like($outhtml, qr/${quot}really-pdf\.jpeg${quot} does not seem to be a valid jpeg file/);
145 ok(! -e "$outpath/666x-really-pdf.jpeg");
146 like($outhtml, qr/${quot}really-pdf\.jpg${quot} does not seem to be a valid jpeg file/);
147 ok(! -e "$outpath/666x-really-pdf.jpg");
148 like($outhtml, qr/${quot}really-pdf\.png${quot} does not seem to be a valid png file/);
149 ok(! -e "$outpath/666x-really-pdf.png");
151 # disable support for uncommon formats and try again
152 ok(! system(@command, "--rebuild"));
153 ok(! -e "$outpath/10x-bluesquare.png");
154 ok(! -e "$outpath/12x-twopages.png");
155 ok(! -e "$outpath/16x-p1-twopages.png");
157 # now let's remove them again
159 if (1) { # for easier testing
160         writefile("imgconversions.mdwn", "t/tmp/in", "nothing to see here");
162         ok(! system(@command, "--refresh"));
164         ok(! -e "$outpath/10x-redsquare.png");
165         ok(! -e "$outpath/10x-bluesquare.png");
166         ok(! -e "$outpath/12x-twopages.png");
167         ok(! -e "$outpath/16x-p1-twopages.png");
168         ok(! -e "$outpath/x8-hello:world.png");
169         ok(! -e "$outpath/x4-a:b:c.png");
170         ok(! -e "$outpath/x6-a:b:c:d:e:f:g:h:i:j.png");
172         # cleanup
173         ok(! system("rm -rf t/tmp"));
175 done_testing;
177 1;