xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/ExtUtils/t/Command.t (revision 0:68f95e015346)
1#!/usr/bin/perl -w
2
3BEGIN {
4    if( $ENV{PERL_CORE} ) {
5        chdir 't';
6        @INC = ('../lib', 'lib/');
7    }
8    else {
9        unshift @INC, 't/lib/';
10    }
11}
12chdir 't';
13
14BEGIN {
15    $Testfile = 'testfile.foo';
16}
17
18BEGIN {
19    1 while unlink $Testfile, 'newfile';
20    # forcibly remove ecmddir/temp2, but don't import mkpath
21    use File::Path ();
22    File::Path::rmtree( 'ecmddir' );
23}
24
25BEGIN {
26    use Test::More tests => 26;
27    use File::Spec;
28}
29
30BEGIN {
31    # bad neighbor, but test_f() uses exit()
32        *CORE::GLOBAL::exit = '';   # quiet 'only once' warning.
33    *CORE::GLOBAL::exit = sub { return @_ };
34    use_ok( 'ExtUtils::Command' );
35}
36
37{
38    # concatenate this file with itself
39    # be extra careful the regex doesn't match itself
40    use TieOut;
41    my $out = tie *STDOUT, 'TieOut';
42    my $self = $0;
43    unless (-f $self) {
44        my ($vol, $dirs, $file) = File::Spec->splitpath($self);
45        my @dirs = File::Spec->splitdir($dirs);
46        unshift(@dirs, File::Spec->updir);
47        $dirs = File::Spec->catdir(@dirs);
48        $self = File::Spec->catpath($vol, $dirs, $file);
49    }
50    @ARGV = ($self, $self);
51
52    cat();
53    is( scalar( $$out =~ s/use_ok\( 'ExtUtils::Command'//g), 2,
54        'concatenation worked' );
55
56    # the truth value here is reversed -- Perl true is C false
57    @ARGV = ( $Testfile );
58    ok( test_f(), 'testing non-existent file' );
59
60    @ARGV = ( $Testfile );
61    cmp_ok( ! test_f(), '==', (-f $Testfile), 'testing non-existent file' );
62
63    # these are destructive, have to keep setting @ARGV
64    @ARGV = ( $Testfile );
65    touch();
66
67    @ARGV = ( $Testfile );
68    ok( test_f(), 'now creating that file' );
69
70    @ARGV = ( $Testfile );
71    ok( -e $ARGV[0], 'created!' );
72
73    my ($now) = time;
74    utime ($now, $now, $ARGV[0]);
75    sleep 2;
76
77    # Just checking modify time stamp, access time stamp is set
78    # to the beginning of the day in Win95.
79    # There's a small chance of a 1 second flutter here.
80    my $stamp = (stat($ARGV[0]))[9];
81    cmp_ok( abs($now - $stamp), '<=', 1, 'checking modify time stamp' ) ||
82      diag "mtime == $stamp, should be $now";
83
84    @ARGV = qw(newfile);
85    touch();
86
87    my $new_stamp = (stat('newfile'))[9];
88    cmp_ok( abs($new_stamp - $stamp), '>=', 2,  'newer file created' );
89
90    @ARGV = ('newfile', $Testfile);
91    eqtime();
92
93    $stamp = (stat($Testfile))[9];
94    cmp_ok( abs($new_stamp - $stamp), '<=', 1, 'eqtime' );
95
96    # eqtime use to clear the contents of the file being equalized!
97    open(FILE, ">>$Testfile") || die $!;
98    print FILE "Foo";
99    close FILE;
100
101    @ARGV = ('newfile', $Testfile);
102    eqtime();
103    ok( -s $Testfile, "eqtime doesn't clear the file being equalized" );
104
105    SKIP: {
106        if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
107            $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin'  ||
108            $^O eq 'MacOS'
109           ) {
110            skip( "different file permission semantics on $^O", 3);
111        }
112
113        # change a file to execute-only
114        @ARGV = ( '0100', $Testfile );
115        ExtUtils::Command::chmod();
116
117        is( ((stat($Testfile))[2] & 07777) & 0700,
118            0100, 'change a file to execute-only' );
119
120        # change a file to read-only
121        @ARGV = ( '0400', $Testfile );
122        ExtUtils::Command::chmod();
123
124        is( ((stat($Testfile))[2] & 07777) & 0700,
125            ($^O eq 'vos' ? 0500 : 0400), 'change a file to read-only' );
126
127        # change a file to write-only
128        @ARGV = ( '0200', $Testfile );
129        ExtUtils::Command::chmod();
130
131        is( ((stat($Testfile))[2] & 07777) & 0700,
132            ($^O eq 'vos' ? 0700 : 0200), 'change a file to write-only' );
133    }
134
135    # change a file to read-write
136    @ARGV = ( '0600', $Testfile );
137    ExtUtils::Command::chmod();
138
139    is( ((stat($Testfile))[2] & 07777) & 0700,
140        ($^O eq 'vos' ? 0700 : 0600), 'change a file to read-write' );
141
142    # mkpath
143    @ARGV = ( File::Spec->join( 'ecmddir', 'temp2' ) );
144    ok( ! -e $ARGV[0], 'temp directory not there yet' );
145
146    mkpath();
147    ok( -e $ARGV[0], 'temp directory created' );
148
149    # copy a file to a nested subdirectory
150    unshift @ARGV, $Testfile;
151    cp();
152
153    ok( -e File::Spec->join( 'ecmddir', 'temp2', $Testfile ), 'copied okay' );
154
155    # cp should croak if destination isn't directory (not a great warning)
156    @ARGV = ( $Testfile ) x 3;
157    eval { cp() };
158
159    like( $@, qr/Too many arguments/, 'cp croaks on error' );
160
161    # move a file to a subdirectory
162    @ARGV = ( $Testfile, 'ecmddir' );
163    mv();
164
165    ok( ! -e $Testfile, 'moved file away' );
166    ok( -e File::Spec->join( 'ecmddir', $Testfile ), 'file in new location' );
167
168    # mv should also croak with the same wacky warning
169    @ARGV = ( $Testfile ) x 3;
170
171    eval { mv() };
172    like( $@, qr/Too many arguments/, 'mv croaks on error' );
173
174    # Test expand_wildcards()
175    {
176        my $file = $Testfile;
177        @ARGV = ();
178        chdir 'ecmddir';
179
180        # % means 'match one character' on VMS.  Everything else is ?
181        my $match_char = $^O eq 'VMS' ? '%' : '?';
182        ($ARGV[0] = $file) =~ s/.\z/$match_char/;
183
184        # this should find the file
185        ExtUtils::Command::expand_wildcards();
186
187        is_deeply( \@ARGV, [$file], 'expanded wildcard ? successfully' );
188
189        # try it with the asterisk now
190        ($ARGV[0] = $file) =~ s/.{3}\z/\*/;
191        ExtUtils::Command::expand_wildcards();
192
193        is_deeply( \@ARGV, [$file], 'expanded wildcard * successfully' );
194
195        chdir File::Spec->updir;
196    }
197
198    # remove some files
199    my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', $Testfile ),
200    File::Spec->catfile( 'ecmddir', 'temp2', $Testfile ) );
201    rm_f();
202
203    ok( ! -e $_, "removed $_ successfully" ) for (@ARGV);
204
205    # rm_f dir
206    @ARGV = my $dir = File::Spec->catfile( 'ecmddir' );
207    rm_rf();
208    ok( ! -e $dir, "removed $dir successfully" );
209}
210
211END {
212    1 while unlink $Testfile, 'newfile';
213    File::Path::rmtree( 'ecmddir' );
214}
215