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