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