img_html.t (2093B)
1 # 2 # Verify the img_html suffix generates a thumbnail and correct HTML 3 # fragment. 4 # 5 use File::Spec::Functions; 6 use Imager; 7 use Modern::Perl; 8 use Test::Cmd; 9 use Test::File; 10 use Test::File::Contents; 11 use Test::More tests => 19; 12 13 14 # Currently supported suffixes. 15 my @sfxs = ( 'jpg', 'jpeg', 'png' ); 16 17 my $cmd = Test::Cmd->new( prog => '/usr/bin/make', workdir => '' ); 18 my $html_mk = $cmd->here . "/bsd.html.mk"; 19 20 # Write smallest possible image for quick processing. 21 my $img = Imager->new( xsize => 1, ysize => 1, channels => 4 ); 22 for my $sfx (@sfxs) { 23 my $ret = $img->write( file => $cmd->workdir . "/pic_$sfx.$sfx" ); 24 isnt( $ret, undef, "$sfx write (err = $img->errstr)" ); 25 } 26 27 $cmd->write( 'index.in', <<EOF ); 28 #include "pic_jpg.img_html" 29 #include "pic_jpeg.img_html" 30 #include "pic_png.img_html" 31 EOF 32 33 $cmd->write( 'Makefile', <<EOF ); 34 .include "$html_mk" 35 EOF 36 37 $cmd->run( args => 'depend', chdir => $cmd->curdir ); 38 like( $cmd->stdout, qr/.*/, 'make depend stdout' ); 39 is( $cmd->stderr, '', 'make depend stderr' ); 40 is( $? >> 8, 0, 'make depend exit status' ); 41 42 $cmd->run( chdir => $cmd->curdir ); 43 like( $cmd->stdout, qr/.*/, 'make stdout' ); 44 is( $cmd->stderr, '', 'make stderr' ); 45 is( $? >> 8, 0, 'make exit status' ); 46 47 # 48 # Verify: 49 # - thumbnail generated 50 # - original copied 51 # - html generated 52 # - final html file looks good 53 # 54 my $index_good; 55 56 for my $sfx (@sfxs) { 57 my $cp_file = "pic_${sfx}.cp.${sfx}"; 58 my $thumb_file = "pic_${sfx}.thumb.${sfx}"; 59 60 file_exists_ok( catfile( $cmd->workdir, $cp_file ) ); 61 file_exists_ok( catfile( $cmd->workdir, $thumb_file ) ); 62 63 my $html_good = <<EOF; 64 <a href='$cp_file'> 65 <img loading='lazy' src='$thumb_file' 66 EOF 67 68 my $html_file = catfile( $cmd->workdir, "pic_${sfx}.img_html" ); 69 file_contents_eq_or_diff( $html_file, $html_good ); 70 71 $index_good .= $html_good; 72 } 73 74 # Adjust for different preprocessors adding different amounts of whitespace. 75 my $index_html; 76 $cmd->read( \$index_html, 'index.html' ); 77 $index_html =~ s/^\n//mg; 78 $cmd->write( 'index.html', $index_html ); 79 80 file_contents_eq_or_diff( catfile( $cmd->workdir, 'index.html' ), $index_good );