This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Cant spl wirth a dam.
[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' );
fbac1b85 67 my $now = time;
e38fdfdb 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
fbac1b85
NK
76 # Just checking modify time stamp, access time stamp is set
77 # to the beginning of the day in Win95
e38fdfdb 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
131END {
132 1 while unlink 'ecmdfile';
133 File::Path::rmtree( 'ecmddir' );
134}
135
136package TieOut;
137
138sub TIEHANDLE {
139 bless( \(my $text), $_[0] );
140}
141
142sub PRINT {
143 ${ $_[0] } .= join($/, @_);
144}