a21661f2b2778cdd54b70b21589b47873782a00f
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Command.t
1 #!/usr/bin/perl -w
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't';
6         @INC = ('../lib', 'lib/');
7     }
8     else {
9         unshift @INC, 't/lib/';
10     }
11 }
12 chdir 't';
13
14 BEGIN {
15         1 while unlink 'ecmdfile';
16         # forcibly remove ecmddir/temp2, but don't import mkpath
17         use File::Path ();
18         File::Path::rmtree( 'ecmddir' );
19 }
20
21 BEGIN {
22         use Test::More tests => 21;
23         use File::Spec;
24 }
25
26 {
27         # bad neighbor, but test_f() uses exit()
28     *CORE::GLOBAL::exit = '';   # quiet 'only once' warning.
29         *CORE::GLOBAL::exit = sub { return @_ };
30
31         use_ok( 'ExtUtils::Command' );
32
33         # get a file in the current directory, replace last char with wildcard 
34         my $file;
35         {
36                 local *DIR;
37                 opendir(DIR, File::Spec->curdir());
38                 while ($file = readdir(DIR)) {
39                         $file =~ s/\.\z// if $^O eq 'VMS';
40                         last if $file =~ /^\w/;
41                 }
42         }
43
44         # this should find the file
45         ($ARGV[0] = $file) =~ s/.\z/\?/;
46         ExtUtils::Command::expand_wildcards();
47
48         is( scalar @ARGV, 1, 'found one file' );
49         like( $ARGV[0], qr/$file/, 'expanded wildcard ? successfully' );
50
51         # try it with the asterisk now
52         ($ARGV[0] = $file) =~ s/.{3}\z/\*/;
53         ExtUtils::Command::expand_wildcards();
54
55         ok( (grep { qr/$file/ } @ARGV), 'expanded wildcard * successfully' );
56
57         # concatenate this file with itself
58         # be extra careful the regex doesn't match itself
59     use TieOut;
60         my $out = tie *STDOUT, 'TieOut';
61         my $self = $0;
62         unless (-f $self) {
63             my ($vol, $dirs, $file) = File::Spec->splitpath($self);
64             my @dirs = File::Spec->splitdir($dirs);
65             unshift(@dirs, File::Spec->updir);
66             $dirs = File::Spec->catdir(@dirs);
67             $self = File::Spec->catpath($vol, $dirs, $file);
68         }
69         @ARGV = ($self, $self);
70
71         cat();
72         is( scalar( $$out =~ s/use_ok\( 'ExtUtils::Command'//g), 2, 
73                 'concatenation worked' );
74
75         # the truth value here is reversed -- Perl true is C false
76         @ARGV = ( 'ecmdfile' );
77         ok( test_f(), 'testing non-existent file' );
78
79         @ARGV = ( 'ecmdfile' );
80         cmp_ok( ! test_f(), '==', (-f 'ecmdfile'), 'testing non-existent file' );
81
82         # these are destructive, have to keep setting @ARGV
83         @ARGV = ( 'ecmdfile' );
84         touch();
85
86         @ARGV = ( 'ecmdfile' );
87         ok( test_f(), 'now creating that file' );
88
89         @ARGV = ( 'ecmdfile' );
90         ok( -e $ARGV[0], 'created!' );
91
92         my ($now) = time;
93         utime ($now, $now, $ARGV[0]);
94     sleep 2;
95
96         # Just checking modify time stamp, access time stamp is set
97         # to the beginning of the day in Win95.
98     # There's a small chance of a 1 second flutter here.
99     my $stamp = (stat($ARGV[0]))[9];
100         ok( abs($now - $stamp) <= 1, 'checking modify time stamp' ) ||
101       print "# mtime == $stamp, should be $now\n";
102
103         # change a file to read-only
104         @ARGV = ( 0600, 'ecmdfile' );
105         ExtUtils::Command::chmod();
106
107         is( ((stat('ecmdfile'))[2] & 07777) & 0700, 0600, 'change a file to read-only' );
108
109         # mkpath
110         @ARGV = ( File::Spec->join( 'ecmddir', 'temp2' ) );
111         ok( ! -e $ARGV[0], 'temp directory not there yet' );
112
113         mkpath();
114         ok( -e $ARGV[0], 'temp directory created' );
115
116         # copy a file to a nested subdirectory
117         unshift @ARGV, 'ecmdfile';
118         cp();
119
120         ok( -e File::Spec->join( 'ecmddir', 'temp2', 'ecmdfile' ), 'copied okay' );
121
122         # cp should croak if destination isn't directory (not a great warning)
123         @ARGV = ( 'ecmdfile' ) x 3;
124         eval { cp() };
125
126         like( $@, qr/Too many arguments/, 'cp croaks on error' );
127
128         # move a file to a subdirectory
129         @ARGV = ( 'ecmdfile', 'ecmddir' );
130         mv();
131
132         ok( ! -e 'ecmdfile', 'moved file away' );
133         ok( -e File::Spec->join( 'ecmddir', 'ecmdfile' ), 'file in new location' );
134
135         # mv should also croak with the same wacky warning
136         @ARGV = ( 'ecmdfile' ) x 3;
137
138         eval { mv() };
139         like( $@, qr/Too many arguments/, 'mv croaks on error' );
140
141         # remove some files
142         my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', 'ecmdfile' ),
143         File::Spec->catfile( 'ecmddir', 'temp2', 'ecmdfile' ) );
144         rm_f();
145
146         ok( ! -e $_, "removed $_ successfully" ) for (@ARGV);
147
148         # rm_f dir
149         @ARGV = my $dir = File::Spec->catfile( 'ecmddir' );
150         rm_rf();
151         ok( ! -e $dir, "removed $dir successfully" );
152 }
153
154 END {
155         1 while unlink 'ecmdfile';
156         File::Path::rmtree( 'ecmddir' );
157 }