1b39c5158Smillert#!/usr/bin/perl -w 2b39c5158Smillert 3*256a93a4Safresh1use strict; 4*256a93a4Safresh1use warnings; 5*256a93a4Safresh1 6b39c5158SmillertBEGIN { 7b39c5158Smillert unshift @INC, 't/lib/'; 8b39c5158Smillert} 9b39c5158Smillertchdir 't'; 10b39c5158Smillert 11b39c5158Smillertuse vars qw( $required ); 12b39c5158Smillertuse Test::More tests => 18; 13b39c5158Smillert 14b39c5158SmillertBEGIN { use_ok( 'ExtUtils::Mkbootstrap' ) } 15b39c5158Smillert 16b39c5158Smillert# Mkbootstrap makes a backup copy of "$_[0].bs" if it exists and is non-zero 17b39c5158Smillertmy $file_is_ready; 18b39c5158Smillertlocal *OUT; 19b39c5158Smillertif (open(OUT, '>mkboot.bs')) { 20b39c5158Smillert $file_is_ready = 1; 21b39c5158Smillert print OUT 'meaningless text'; 22b39c5158Smillert close OUT; 23b39c5158Smillert} 24b39c5158Smillert 25b39c5158SmillertSKIP: { 26b39c5158Smillert skip("could not make dummy .bs file: $!", 2) unless $file_is_ready; 27b39c5158Smillert 28b39c5158Smillert Mkbootstrap('mkboot'); 29b39c5158Smillert ok( -s 'mkboot.bso', 'Mkbootstrap should backup the .bs file' ); 30b39c5158Smillert local *IN; 31b39c5158Smillert if (open(IN, 'mkboot.bso')) { 32b39c5158Smillert chomp ($file_is_ready = <IN>); 33b39c5158Smillert close IN; 34b39c5158Smillert } 35b39c5158Smillert 36b39c5158Smillert is( $file_is_ready, 'meaningless text', 'backup should be a perfect copy' ); 37b39c5158Smillert} 38b39c5158Smillert 39b39c5158Smillert 40b39c5158Smillert# if it doesn't exist or is zero bytes in size, it won't be backed up 41b39c5158SmillertMkbootstrap('fakeboot'); 42b39c5158Smillertok( !( -f 'fakeboot.bso' ), 'Mkbootstrap should not backup an empty file' ); 43b39c5158Smillert 44b39c5158Smillertuse TieOut; 45b39c5158Smillertmy $out = tie *STDOUT, 'TieOut'; 46b39c5158Smillert 47b39c5158Smillert# with $Verbose set, it should print status messages about libraries 48b39c5158Smillert$ExtUtils::Mkbootstrap::Verbose = 1; 49b39c5158SmillertMkbootstrap(''); 50b39c5158Smillertis( $out->read, "\tbsloadlibs=\n", 'should report libraries in Verbose mode' ); 51b39c5158Smillert 52b39c5158SmillertMkbootstrap('', 'foo'); 53b39c5158Smillertlike( $out->read, qr/bsloadlibs=foo/, 'should still report libraries' ); 54b39c5158Smillert 55b39c5158Smillert 56b39c5158Smillert# if ${_[0]}_BS exists, require it 57b39c5158Smillert$file_is_ready = open(OUT, '>boot_BS'); 58b39c5158Smillert 59b39c5158SmillertSKIP: { 60b39c5158Smillert skip("cannot open boot_BS for writing: $!", 1) unless $file_is_ready; 61b39c5158Smillert 62b39c5158Smillert print OUT '$main::required = 1'; 63b39c5158Smillert close OUT; 64b39c5158Smillert Mkbootstrap('boot'); 65b39c5158Smillert 66b39c5158Smillert ok( $required, 'baseext_BS file should be require()d' ); 67b39c5158Smillert} 68b39c5158Smillert 69b39c5158Smillert 70b39c5158Smillert# if there are any arguments, open a file named baseext.bs 71b39c5158Smillert$file_is_ready = open(OUT, '>dasboot.bs'); 72b39c5158Smillert 73b39c5158SmillertSKIP: { 74b39c5158Smillert skip("cannot make dasboot.bs: $!", 5) unless $file_is_ready; 75b39c5158Smillert 76b39c5158Smillert # if it can't be opened for writing, we want to prove that it'll die 77b39c5158Smillert close OUT; 78b39c5158Smillert chmod 0444, 'dasboot.bs'; 79b39c5158Smillert 80b39c5158Smillert SKIP: { 815759b3d2Safresh1 skip("cannot write readonly files", 1) if -w 'dasboot.bs' || $^O eq 'cygwin'; 82b39c5158Smillert 83b39c5158Smillert eval{ Mkbootstrap('dasboot', 1) }; 84b39c5158Smillert like( $@, qr/Unable to open dasboot\.bs/, 'should die given bad filename' ); 85b39c5158Smillert } 86b39c5158Smillert 87b39c5158Smillert # now put it back like it was 88b39c5158Smillert chmod 0777, 'dasboot.bs'; 89b39c5158Smillert eval{ Mkbootstrap('dasboot', 'myarg') }; 90b39c5158Smillert is( $@, '', 'should not die, given good filename' ); 91b39c5158Smillert 92b39c5158Smillert # red and reed (a visual pun makes tests worth reading) 93b39c5158Smillert my $read = $out->read(); 94b39c5158Smillert like( $read, qr/Writing dasboot.bs/, 'should print status' ); 95b39c5158Smillert like( $read, qr/containing: my/, 'should print verbose status on request' ); 96b39c5158Smillert 97b39c5158Smillert # now be tricky, and set the status for the next skip block 98b39c5158Smillert $file_is_ready = open(IN, 'dasboot.bs'); 99b39c5158Smillert ok( $file_is_ready, 'should have written a new .bs file' ); 100b39c5158Smillert} 101b39c5158Smillert 102b39c5158Smillert 103b39c5158SmillertSKIP: { 104b39c5158Smillert skip("cannot read .bs file: $!", 2) unless $file_is_ready; 105b39c5158Smillert 106b39c5158Smillert my $file = do { local $/ = <IN> }; 107b39c5158Smillert 108b39c5158Smillert # filename should be in header 109b39c5158Smillert like( $file, qr/# dasboot DynaLoader/, 'file should have boilerplate' ); 110b39c5158Smillert 111b39c5158Smillert # should print arguments within this array 112b39c5158Smillert like( $file, qr/qw\(myarg\);/, 'should have written array to file' ); 113b39c5158Smillert} 114b39c5158Smillert 115b39c5158Smillert 116b39c5158Smillert# overwrite this file (may whack portability, but the name's too good to waste) 117b39c5158Smillert$file_is_ready = open(OUT, '>dasboot.bs'); 118b39c5158Smillert 119b39c5158SmillertSKIP: { 120b39c5158Smillert skip("cannot make dasboot.bs again: $!", 1) unless $file_is_ready; 121b39c5158Smillert close OUT; 122b39c5158Smillert 123b39c5158Smillert # if $DynaLoader::bscode is set, write its contents to the file 124b39c5158Smillert local $DynaLoader::bscode; 125b39c5158Smillert $DynaLoader::bscode = 'Wall'; 126b39c5158Smillert $ExtUtils::Mkbootstrap::Verbose = 0; 127b39c5158Smillert 128b39c5158Smillert # if arguments contain '-l' or '-L' or '-R' print dl_findfile message 129b39c5158Smillert eval{ Mkbootstrap('dasboot', '-Larry') }; 130b39c5158Smillert is( $@, '', 'should be able to open a file again'); 131b39c5158Smillert 132b39c5158Smillert $file_is_ready = open(IN, 'dasboot.bs'); 133b39c5158Smillert} 134b39c5158Smillert 135b39c5158SmillertSKIP: { 136b39c5158Smillert skip("cannot open dasboot.bs for reading: $!", 3) unless $file_is_ready; 137b39c5158Smillert 138b39c5158Smillert my $file = do { local $/ = <IN> }; 139b39c5158Smillert is( $out->read, "Writing dasboot.bs\n", 'should hush without Verbose set' ); 140b39c5158Smillert 141b39c5158Smillert # and find our hidden tribute to a fine example 142b39c5158Smillert like( $file, qr/dl_findfile.+Larry/s, 'should load libraries if needed' ); 143b39c5158Smillert like( $file, qr/Wall\n1;\n/ms, 'should write $DynaLoader::bscode if set' ); 144b39c5158Smillert} 145b39c5158Smillert 146b39c5158Smillertclose IN; 147b39c5158Smillertclose OUT; 148b39c5158Smillert 149b39c5158SmillertEND { 150b39c5158Smillert # clean things up, even on VMS 151b39c5158Smillert 1 while unlink(qw( mkboot.bso boot_BS dasboot.bs .bs )); 152b39c5158Smillert} 153