]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/img.t
6f822bbd5a669479208f851def42022d899be138
[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;
17 plan(skip_all => "Image::Magick not available")
18         unless eval q{use Image::Magick; 1};
20 use IkiWiki;
22 my $installed = $ENV{INSTALLED_TESTS};
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 }
36 push @command, qw(--set usedirs=0 --plugin img t/tmp/in t/tmp/out --verbose);
38 my $magick = new Image::Magick;
39 my $SVGS_WORK = defined $magick->QueryFormat("svg");
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 # 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"));
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"));
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"));
68 my $maybe_svg_img = "";
69 if ($SVGS_WORK) {
70         $maybe_svg_img = "[[!img bluesquare.svg size=10x]]";
71 }
73 my $maybe_pdf_img = "";
74 if ($PDFS_WORK) {
75         $maybe_pdf_img = <<EOF;
76 [[!img twopages.pdf size=12x]]
77 [[!img twopages.pdf size=16x pagenumber=1]]
78 EOF
79 }
81 writefile("imgconversions.mdwn", "t/tmp/in", <<EOF
82 [[!img redsquare.png]]
83 [[!img redsquare.jpg size=11x]]
84 [[!img redsquare.jpeg size=12x]]
85 [[!img redsquare.png size=10x]]
86 [[!img redsquare.png size=30x50]] expecting 30x30
87 [[!img hello:world.png size=x8]] expecting 8x8
88 [[!img a:b:c.png size=x4]]
89 [[!img a:b:c:d:e:f:g:h:i:j.png size=x6]]
90 $maybe_svg_img
91 $maybe_pdf_img
93 # bad ideas
94 [[!img really-svg.png size=666x]]
95 [[!img really-svg.bmp size=666x]]
96 [[!img really-svg.pdf size=666x]]
97 [[!img really-pdf.jpeg size=666x]]
98 [[!img really-pdf.jpg size=666x]]
99 [[!img really-pdf.png size=666x]]
100 [[!img really-pdf.svg size=666x]]
101 EOF
102 );
103 ok(utime(333333333, 333333333, "t/tmp/in/imgconversions.mdwn"));
105 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[jpeg, png, svg, pdf]'));
107 sub size($) {
108         my $filename = shift;
109         my $im = Image::Magick->new();
110         my $r = $im->Read(":$filename");
111         return "no image" if $r;
112         my $w = $im->Get("width");
113         my $h = $im->Get("height");
114         return "${w}x${h}";
117 my $outpath = "t/tmp/out/imgconversions";
118 my $outhtml = readfile("$outpath.html");
120 is(size("$outpath/10x-redsquare.png"), "10x10");
121 ok(! -e "$outpath/30x-redsquare.png");
122 ok($outhtml =~ /width="30" height="30".*expecting 30x30/);
124 SKIP: {
125         skip "SVG support not installed (try libmagickcore-extra)", 1
126                 unless $SVGS_WORK;
127         # if this fails, you need libmagickcore-6.q16-2-extra installed
128         is(size("$outpath/10x-bluesquare.png"), "10x10");
131 SKIP: {
132         skip "PDF support not installed (try ghostscript)", 2
133                 unless $PDFS_WORK;
134         is(size("$outpath/12x-twopages.png"), "12x12");
135         is(size("$outpath/16x-p1-twopages.png"), "16x2");
138 ok($outhtml =~ /width="8" height="8".*expecting 8x8/);
139 is(size("$outpath/x8-hello:world.png"), "8x8");
140 is(size("$outpath/x4-a:b:c.png"), "4x4");
141 is(size("$outpath/x6-a:b:c:d:e:f:g:h:i:j.png"), "6x6");
143 is(size("$outpath/11x-redsquare.jpg"), "11x11");
144 is(size("$outpath/12x-redsquare.jpeg"), "12x12");
145 like($outhtml, qr{src="(\./)?imgconversions/11x-redsquare\.jpg" width="11" height="11"});
146 like($outhtml, qr{src="(\./)?imgconversions/12x-redsquare\.jpeg" width="12" height="12"});
148 # We do not misinterpret images
149 ok(! -e "$outpath/666x-really-svg.png");
150 ok(! -e "$outpath/666x-really-svg.bmp");
151 ok(! -e "$outpath/666x-really-pdf.jpeg");
152 ok(! -e "$outpath/666x-really-pdf.jpg");
153 ok(! -e "$outpath/666x-really-pdf.png");
155 # disable support for uncommon formats and try again
156 ok(! system(@command, "--rebuild"));
157 ok(! -e "$outpath/10x-bluesquare.png");
158 ok(! -e "$outpath/12x-twopages.png");
159 ok(! -e "$outpath/16x-p1-twopages.png");
161 # now let's remove them again
163 if (1) { # for easier testing
164         writefile("imgconversions.mdwn", "t/tmp/in", "nothing to see here");
166         ok(! system(@command, "--refresh"));
168         ok(! -e "$outpath/10x-redsquare.png");
169         ok(! -e "$outpath/10x-bluesquare.png");
170         ok(! -e "$outpath/12x-twopages.png");
171         ok(! -e "$outpath/16x-p1-twopages.png");
172         ok(! -e "$outpath/x8-hello:world.png");
173         ok(! -e "$outpath/x4-a:b:c.png");
174         ok(! -e "$outpath/x6-a:b:c:d:e:f:g:h:i:j.png");
176         # cleanup
177         ok(! system("rm -rf t/tmp"));
179 done_testing;
181 1;