1*0Sstevel@tonic-gate#!/usr/bin/perl -w 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateBEGIN { 4*0Sstevel@tonic-gate if( $ENV{PERL_CORE} ) { 5*0Sstevel@tonic-gate chdir 't'; 6*0Sstevel@tonic-gate @INC = ('../lib', 'lib/'); 7*0Sstevel@tonic-gate } 8*0Sstevel@tonic-gate else { 9*0Sstevel@tonic-gate unshift @INC, 't/lib/'; 10*0Sstevel@tonic-gate } 11*0Sstevel@tonic-gate} 12*0Sstevel@tonic-gatechdir 't'; 13*0Sstevel@tonic-gate 14*0Sstevel@tonic-gateBEGIN { 15*0Sstevel@tonic-gate $Testfile = 'testfile.foo'; 16*0Sstevel@tonic-gate} 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gateBEGIN { 19*0Sstevel@tonic-gate 1 while unlink $Testfile, 'newfile'; 20*0Sstevel@tonic-gate # forcibly remove ecmddir/temp2, but don't import mkpath 21*0Sstevel@tonic-gate use File::Path (); 22*0Sstevel@tonic-gate File::Path::rmtree( 'ecmddir' ); 23*0Sstevel@tonic-gate} 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gateBEGIN { 26*0Sstevel@tonic-gate use Test::More tests => 26; 27*0Sstevel@tonic-gate use File::Spec; 28*0Sstevel@tonic-gate} 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gateBEGIN { 31*0Sstevel@tonic-gate # bad neighbor, but test_f() uses exit() 32*0Sstevel@tonic-gate *CORE::GLOBAL::exit = ''; # quiet 'only once' warning. 33*0Sstevel@tonic-gate *CORE::GLOBAL::exit = sub { return @_ }; 34*0Sstevel@tonic-gate use_ok( 'ExtUtils::Command' ); 35*0Sstevel@tonic-gate} 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate{ 38*0Sstevel@tonic-gate # concatenate this file with itself 39*0Sstevel@tonic-gate # be extra careful the regex doesn't match itself 40*0Sstevel@tonic-gate use TieOut; 41*0Sstevel@tonic-gate my $out = tie *STDOUT, 'TieOut'; 42*0Sstevel@tonic-gate my $self = $0; 43*0Sstevel@tonic-gate unless (-f $self) { 44*0Sstevel@tonic-gate my ($vol, $dirs, $file) = File::Spec->splitpath($self); 45*0Sstevel@tonic-gate my @dirs = File::Spec->splitdir($dirs); 46*0Sstevel@tonic-gate unshift(@dirs, File::Spec->updir); 47*0Sstevel@tonic-gate $dirs = File::Spec->catdir(@dirs); 48*0Sstevel@tonic-gate $self = File::Spec->catpath($vol, $dirs, $file); 49*0Sstevel@tonic-gate } 50*0Sstevel@tonic-gate @ARGV = ($self, $self); 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate cat(); 53*0Sstevel@tonic-gate is( scalar( $$out =~ s/use_ok\( 'ExtUtils::Command'//g), 2, 54*0Sstevel@tonic-gate 'concatenation worked' ); 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate # the truth value here is reversed -- Perl true is C false 57*0Sstevel@tonic-gate @ARGV = ( $Testfile ); 58*0Sstevel@tonic-gate ok( test_f(), 'testing non-existent file' ); 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate @ARGV = ( $Testfile ); 61*0Sstevel@tonic-gate cmp_ok( ! test_f(), '==', (-f $Testfile), 'testing non-existent file' ); 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate # these are destructive, have to keep setting @ARGV 64*0Sstevel@tonic-gate @ARGV = ( $Testfile ); 65*0Sstevel@tonic-gate touch(); 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate @ARGV = ( $Testfile ); 68*0Sstevel@tonic-gate ok( test_f(), 'now creating that file' ); 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate @ARGV = ( $Testfile ); 71*0Sstevel@tonic-gate ok( -e $ARGV[0], 'created!' ); 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate my ($now) = time; 74*0Sstevel@tonic-gate utime ($now, $now, $ARGV[0]); 75*0Sstevel@tonic-gate sleep 2; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate # Just checking modify time stamp, access time stamp is set 78*0Sstevel@tonic-gate # to the beginning of the day in Win95. 79*0Sstevel@tonic-gate # There's a small chance of a 1 second flutter here. 80*0Sstevel@tonic-gate my $stamp = (stat($ARGV[0]))[9]; 81*0Sstevel@tonic-gate cmp_ok( abs($now - $stamp), '<=', 1, 'checking modify time stamp' ) || 82*0Sstevel@tonic-gate diag "mtime == $stamp, should be $now"; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate @ARGV = qw(newfile); 85*0Sstevel@tonic-gate touch(); 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate my $new_stamp = (stat('newfile'))[9]; 88*0Sstevel@tonic-gate cmp_ok( abs($new_stamp - $stamp), '>=', 2, 'newer file created' ); 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate @ARGV = ('newfile', $Testfile); 91*0Sstevel@tonic-gate eqtime(); 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate $stamp = (stat($Testfile))[9]; 94*0Sstevel@tonic-gate cmp_ok( abs($new_stamp - $stamp), '<=', 1, 'eqtime' ); 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate # eqtime use to clear the contents of the file being equalized! 97*0Sstevel@tonic-gate open(FILE, ">>$Testfile") || die $!; 98*0Sstevel@tonic-gate print FILE "Foo"; 99*0Sstevel@tonic-gate close FILE; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate @ARGV = ('newfile', $Testfile); 102*0Sstevel@tonic-gate eqtime(); 103*0Sstevel@tonic-gate ok( -s $Testfile, "eqtime doesn't clear the file being equalized" ); 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate SKIP: { 106*0Sstevel@tonic-gate if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || 107*0Sstevel@tonic-gate $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' || 108*0Sstevel@tonic-gate $^O eq 'MacOS' 109*0Sstevel@tonic-gate ) { 110*0Sstevel@tonic-gate skip( "different file permission semantics on $^O", 3); 111*0Sstevel@tonic-gate } 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate # change a file to execute-only 114*0Sstevel@tonic-gate @ARGV = ( '0100', $Testfile ); 115*0Sstevel@tonic-gate ExtUtils::Command::chmod(); 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate is( ((stat($Testfile))[2] & 07777) & 0700, 118*0Sstevel@tonic-gate 0100, 'change a file to execute-only' ); 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate # change a file to read-only 121*0Sstevel@tonic-gate @ARGV = ( '0400', $Testfile ); 122*0Sstevel@tonic-gate ExtUtils::Command::chmod(); 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate is( ((stat($Testfile))[2] & 07777) & 0700, 125*0Sstevel@tonic-gate ($^O eq 'vos' ? 0500 : 0400), 'change a file to read-only' ); 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate # change a file to write-only 128*0Sstevel@tonic-gate @ARGV = ( '0200', $Testfile ); 129*0Sstevel@tonic-gate ExtUtils::Command::chmod(); 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate is( ((stat($Testfile))[2] & 07777) & 0700, 132*0Sstevel@tonic-gate ($^O eq 'vos' ? 0700 : 0200), 'change a file to write-only' ); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate # change a file to read-write 136*0Sstevel@tonic-gate @ARGV = ( '0600', $Testfile ); 137*0Sstevel@tonic-gate ExtUtils::Command::chmod(); 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate is( ((stat($Testfile))[2] & 07777) & 0700, 140*0Sstevel@tonic-gate ($^O eq 'vos' ? 0700 : 0600), 'change a file to read-write' ); 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate # mkpath 143*0Sstevel@tonic-gate @ARGV = ( File::Spec->join( 'ecmddir', 'temp2' ) ); 144*0Sstevel@tonic-gate ok( ! -e $ARGV[0], 'temp directory not there yet' ); 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate mkpath(); 147*0Sstevel@tonic-gate ok( -e $ARGV[0], 'temp directory created' ); 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate # copy a file to a nested subdirectory 150*0Sstevel@tonic-gate unshift @ARGV, $Testfile; 151*0Sstevel@tonic-gate cp(); 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate ok( -e File::Spec->join( 'ecmddir', 'temp2', $Testfile ), 'copied okay' ); 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate # cp should croak if destination isn't directory (not a great warning) 156*0Sstevel@tonic-gate @ARGV = ( $Testfile ) x 3; 157*0Sstevel@tonic-gate eval { cp() }; 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate like( $@, qr/Too many arguments/, 'cp croaks on error' ); 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate # move a file to a subdirectory 162*0Sstevel@tonic-gate @ARGV = ( $Testfile, 'ecmddir' ); 163*0Sstevel@tonic-gate mv(); 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate ok( ! -e $Testfile, 'moved file away' ); 166*0Sstevel@tonic-gate ok( -e File::Spec->join( 'ecmddir', $Testfile ), 'file in new location' ); 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate # mv should also croak with the same wacky warning 169*0Sstevel@tonic-gate @ARGV = ( $Testfile ) x 3; 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate eval { mv() }; 172*0Sstevel@tonic-gate like( $@, qr/Too many arguments/, 'mv croaks on error' ); 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate # Test expand_wildcards() 175*0Sstevel@tonic-gate { 176*0Sstevel@tonic-gate my $file = $Testfile; 177*0Sstevel@tonic-gate @ARGV = (); 178*0Sstevel@tonic-gate chdir 'ecmddir'; 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate # % means 'match one character' on VMS. Everything else is ? 181*0Sstevel@tonic-gate my $match_char = $^O eq 'VMS' ? '%' : '?'; 182*0Sstevel@tonic-gate ($ARGV[0] = $file) =~ s/.\z/$match_char/; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate # this should find the file 185*0Sstevel@tonic-gate ExtUtils::Command::expand_wildcards(); 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate is_deeply( \@ARGV, [$file], 'expanded wildcard ? successfully' ); 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate # try it with the asterisk now 190*0Sstevel@tonic-gate ($ARGV[0] = $file) =~ s/.{3}\z/\*/; 191*0Sstevel@tonic-gate ExtUtils::Command::expand_wildcards(); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate is_deeply( \@ARGV, [$file], 'expanded wildcard * successfully' ); 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate chdir File::Spec->updir; 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate # remove some files 199*0Sstevel@tonic-gate my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', $Testfile ), 200*0Sstevel@tonic-gate File::Spec->catfile( 'ecmddir', 'temp2', $Testfile ) ); 201*0Sstevel@tonic-gate rm_f(); 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate ok( ! -e $_, "removed $_ successfully" ) for (@ARGV); 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate # rm_f dir 206*0Sstevel@tonic-gate @ARGV = my $dir = File::Spec->catfile( 'ecmddir' ); 207*0Sstevel@tonic-gate rm_rf(); 208*0Sstevel@tonic-gate ok( ! -e $dir, "removed $dir successfully" ); 209*0Sstevel@tonic-gate} 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gateEND { 212*0Sstevel@tonic-gate 1 while unlink $Testfile, 'newfile'; 213*0Sstevel@tonic-gate File::Path::rmtree( 'ecmddir' ); 214*0Sstevel@tonic-gate} 215