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