This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Command.t patch had some errors... sorry.
[perl5.git] / lib / ExtUtils / Command.t
CommitLineData
e38fdfdb 1#!./perl -w
2
3BEGIN {
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
b488a12a 12use Test::More tests => 21;
e38fdfdb 13use File::Spec;
14
15SKIP: {
b488a12a
JH
16 skip( 'ExtUtils::Command is a Win32 module', 21 )
17 unless $^O =~ /Win32/;
e38fdfdb 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 touch();
68
69 @ARGV = ( 'ecmdfile' );
70 ok( test_f(), 'now creating that file' );
71
72 @ARGV = ( 'ecmdfile' );
73 ok( -e $ARGV[0], 'created!' );
74
851f5327
NK
75 my ($now) = time;
76 utime ($now, $now, $ARGV[0]);
77
fbac1b85
NK
78 # Just checking modify time stamp, access time stamp is set
79 # to the beginning of the day in Win95
e38fdfdb 80 is( (stat($ARGV[0]))[9], $now, 'checking modify time stamp' );
81
82 # change a file to read-only
83 @ARGV = ( 0600, 'ecmdfile' );
84 ExtUtils::Command::chmod();
85
86 is( (stat('ecmdfile'))[2] & 07777, 0600, 'removed non-owner permissions' );
87
88 # mkpath
89 @ARGV = ( File::Spec->join( 'ecmddir', 'temp2' ) );
90 ok( ! -e $ARGV[0], 'temp directory not there yet' );
91
92 mkpath();
93 ok( -e $ARGV[0], 'temp directory created' );
94
95 # copy a file to a nested subdirectory
96 unshift @ARGV, 'ecmdfile';
97 cp();
98
99 ok( -e File::Spec->join( 'ecmddir', 'temp2', 'ecmdfile' ), 'copied okay' );
100
101 # cp should croak if destination isn't directory (not a great warning)
102 @ARGV = ( 'ecmdfile' ) x 3;
103 eval { cp() };
104
105 like( $@, qr/Too many arguments/, 'cp croaks on error' );
106
107 # move a file to a subdirectory
108 @ARGV = ( 'ecmdfile', 'ecmddir' );
109 mv();
110
111 ok( ! -e 'ecmdfile', 'moved file away' );
112 ok( -e File::Spec->join( 'ecmddir', 'ecmdfile' ), 'file in new location' );
113
114 # mv should also croak with the same wacky warning
115 @ARGV = ( 'ecmdfile' ) x 3;
116
117 eval { mv() };
118 like( $@, qr/Too many arguments/, 'mv croaks on error' );
119
120 # remove some files
121 my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', 'ecmdfile' ),
122 File::Spec->catfile( 'ecmddir', 'temp2', 'ecmdfile' ) );
123 rm_f();
124
125 ok( ! -e $_, "removed $_ successfully" ) for (@ARGV);
126
127 # rm_f dir
128 @ARGV = my $dir = File::Spec->catfile( 'ecmddir' );
129 rm_rf();
130 ok( ! -e $dir, "removed $dir successfully" );
131}
132
133END {
134 1 while unlink 'ecmdfile';
135 File::Path::rmtree( 'ecmddir' );
136}
137
138package TieOut;
139
140sub TIEHANDLE {
141 bless( \(my $text), $_[0] );
142}
143
144sub PRINT {
145 ${ $_[0] } .= join($/, @_);
146}