This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix warning.
[perl5.git] / lib / ExtUtils / t / MM_Win32.t
1 #!/usr/bin/perl
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
7     }
8     else {
9         unshift @INC, 't/lib';
10     }
11 }
12 chdir 't';
13
14 use strict;
15 use Test::More;
16
17 BEGIN {
18         if ($^O =~ /MSWin32/i) {
19                 plan tests => 41;
20         } else {
21                 plan skip_all => 'This is not Win32';
22         }
23 }
24
25 use Config;
26 use File::Spec;
27 use File::Basename;
28 use ExtUtils::MM;
29
30 require_ok( 'ExtUtils::MM_Win32' );
31
32 # Dummy MM object until we have a real MM init method.
33 my $MM = bless {
34                 DIR     => [],
35                 NOECHO  => '@',
36                 XS      => {},
37                 MAKEFILE => 'Makefile',
38                 RM_RF   => 'rm -rf',
39                 MV      => 'mv',
40                 MAKE    => $Config{make}
41                }, 'MM';
42
43
44 # replace_manpage_separator() => tr|/|.|s ?
45 {
46     my $man = 'a/path/to//something';
47     ( my $replaced = $man ) =~ tr|/|.|s;
48     is( $MM->replace_manpage_separator( $man ),
49         $replaced, 'replace_manpage_separator()' );
50 }
51
52 # maybe_command()
53 SKIP: {
54     skip( '$ENV{COMSPEC} not set', 2 )
55         unless $ENV{COMSPEC} =~ m!((?:[a-z]:)?[^|<>]+)!i;
56     my $comspec = $1;
57     is( $MM->maybe_command( $comspec ), 
58         $comspec, 'COMSPEC is a maybe_command()' );
59     ( my $comspec2 = $comspec ) =~ s|\..{3}$||;
60     like( $MM->maybe_command( $comspec2 ), 
61           qr/\Q$comspec/i, 
62           'maybe_command() without extension' );
63 }
64
65 my $had_pathext = exists $ENV{PATHEXT};
66 {
67     local $ENV{PATHEXT} = '.exe';
68     ok( ! $MM->maybe_command( 'not_a_command.com' ), 
69         'not a maybe_command()' );
70 }
71 # Bug in Perl.  local $ENV{FOO} won't delete the key afterward.
72 delete $ENV{PATHEXT} unless $had_pathext;
73
74 # file_name_is_absolute() [Does not support UNC-paths]
75 {
76     ok( $MM->file_name_is_absolute( 'C:/' ), 
77         'file_name_is_absolute()' );
78     ok( ! $MM->file_name_is_absolute( 'some/path/' ),
79         'not file_name_is_absolute()' );
80
81 }
82
83 # find_perl() 
84 # Should be able to find running perl... $^X is OK on Win32
85 {
86     my $my_perl = $1 if $^X  =~ /(.*)/; # are we in -T or -t?
87     my( $perl, $path ) = fileparse( $my_perl );
88     like( $MM->find_perl( $], [ $perl ], [ $path ], 0 ),
89           qr/^\Q$my_perl\E$/i, 'find_perl() finds this perl' );
90 }
91
92 # catdir() (calls MM_Win32->canonpath)
93 {
94     my @path_eg = qw( c: trick dir/now_OK );
95
96     is( $MM->catdir( @path_eg ), 
97          'C:\\trick\\dir\\now_OK', 'catdir()' );
98     is( $MM->catdir( @path_eg ), 
99         File::Spec->catdir( @path_eg ), 
100         'catdir() eq File::Spec->catdir()' );
101
102 # catfile() (calls MM_Win32->catdir)
103     push @path_eg, 'file.ext';
104
105     is( $MM->catfile( @path_eg ),
106         'C:\\trick\\dir\\now_OK\\file.ext', 'catfile()' );
107
108     is( $MM->catfile( @path_eg ), 
109         File::Spec->catfile( @path_eg ), 
110         'catfile() eq File::Spec->catfile()' );
111 }
112
113 # init_others(): check if all keys are created and set?
114 # qw( TOUCH CHMOD CP RM_F RM_RF MV NOOP TEST_F LD AR LDLOADLIBS DEV_NUL )
115 {
116     my $mm_w32 = bless( { BASEEXT => 'Foo' }, 'MM' );
117     $mm_w32->init_others();
118     my @keys = qw( TOUCH CHMOD CP RM_F RM_RF MV NOOP 
119                    TEST_F LD AR LDLOADLIBS DEV_NULL );
120     for my $key ( @keys ) {
121         ok( $mm_w32->{ $key }, "init_others: $key" );
122     }
123 }
124
125 # constants()
126 # XXX this test is probably useless now that we can call individual
127 # init_* methods and check the keys in $mm_w32 directly
128 {
129     my $mm_w32 = bless {
130         NAME         => 'TestMM_Win32', 
131         VERSION      => '1.00',
132         PM           => { 'MM_Win32.pm' => 1 },
133     }, 'MM';
134
135     # XXX Hack until we have a proper init method.
136     # Flesh out some necessary keys in the MM object.
137     @{$mm_w32}{qw(XS MAN1PODS MAN3PODS)} = ({}) x 3;
138     @{$mm_w32}{qw(C O_FILES H)}          = ([]) x 3;
139     @{$mm_w32}{qw(PARENT_NAME)}          = ('') x 3;
140     $mm_w32->{FULLEXT} = 'TestMM_Win32';
141     $mm_w32->{BASEEXT} = 'TestMM_Win32';
142
143     $mm_w32->init_VERSION;
144     $mm_w32->init_linker;
145     $mm_w32->init_INST;
146     $mm_w32->init_xs;
147
148     my $s_PM = join( " \\\n\t", sort keys %{$mm_w32->{PM}} );
149     my $k_PM = join( " \\\n\t", %{$mm_w32->{PM}} );
150
151     my $constants = $mm_w32->constants;
152
153     foreach my $regex (
154          qr|^NAME       \s* = \s* TestMM_Win32 \s* $|xms,
155          qr|^VERSION    \s* = \s* 1\.00 \s* $|xms,
156          qr|^MAKEMAKER  \s* = \s* \Q$INC{'ExtUtils/MakeMaker.pm'}\E \s* $|xms,
157          qr|^MM_VERSION \s* = \s* \Q$ExtUtils::MakeMaker::VERSION\E \s* $|xms,
158          qr|^TO_INST_PM \s* = \s* \Q$s_PM\E \s* $|xms,
159          qr|^PM_TO_BLIB \s* = \s* \Q$k_PM\E \s* $|xms,
160         )
161     {
162         like( $constants, $regex, 'constants() check' );
163     }
164 }
165
166 # path()
167 {
168     ok( eq_array( [ $MM->path() ], [ File::Spec->path ] ),
169         'path() [preset]' );
170 }
171
172 # static_lib() should look into that
173 # dynamic_bs() should look into that
174 # dynamic_lib() should look into that
175
176 # init_linker
177 {
178     my $libperl = File::Spec->catfile('$(PERL_INC)', 
179                                       $Config{libperl} || 'libperl.a');
180     my $export  = '$(BASEEXT).def';
181     my $after   = '';
182     $MM->init_linker;
183
184     is( $MM->{PERL_ARCHIVE},        $libperl,   'PERL_ARCHIVE' );
185     is( $MM->{PERL_ARCHIVE_AFTER},  $after,     'PERL_ARCHIVE_AFTER' );
186     is( $MM->{EXPORT_LIST},         $export,    'EXPORT_LIST' );
187 }
188
189 # canonpath()
190 {
191     my $path = 'c:\\Program Files/SomeApp\\Progje.exe';
192     is( $MM->canonpath( $path ), File::Spec->canonpath( $path ),
193             'canonpath() eq File::Spec->canonpath' );
194 }
195
196 # perl_script()
197 my $script_ext  = '';
198 my $script_name = 'mm_w32tmp';
199 SKIP: {
200     local *SCRIPT;
201     skip( "Can't create temp file: $!", 4 )
202         unless open SCRIPT, "> $script_name";
203     print SCRIPT <<'EOSCRIPT';
204 #! perl
205 __END__
206 EOSCRIPT
207     skip( "Can't write to temp file: $!", 4 )
208         unless close SCRIPT;
209     # now start tests:
210     is( $MM->perl_script( $script_name ), 
211         "${script_name}$script_ext", "perl_script ($script_ext)" );
212
213     skip( "Can't rename temp file: $!", 3 )
214         unless rename $script_name, "${script_name}.pl";
215     $script_ext = '.pl';
216     is( $MM->perl_script( $script_name ), 
217         "${script_name}$script_ext", "perl_script ($script_ext)" );
218
219     skip( "Can't rename temp file: $!", 2 )
220         unless rename "${script_name}$script_ext", "${script_name}.bat";
221     $script_ext = '.bat';
222     is( $MM->perl_script( $script_name ), 
223         "${script_name}$script_ext", "perl_script ($script_ext)" );
224
225     skip( "Can't rename temp file: $!", 1 )
226         unless rename "${script_name}$script_ext", "${script_name}.noscript";
227     $script_ext = '.noscript';
228
229     isnt( $MM->perl_script( $script_name ),
230           "${script_name}$script_ext", 
231           "not a perl_script anymore ($script_ext)" );
232     is( $MM->perl_script( $script_name ), undef,
233         "perl_script ($script_ext) returns empty" );
234 }
235 unlink "${script_name}$script_ext" if -f "${script_name}$script_ext";
236
237
238 # xs_o() should look into that
239 # top_targets() should look into that
240
241 # dist_ci() should look into that
242 # dist_core() should look into that
243
244 # pasthru()
245 {
246     my $pastru = "PASTHRU = " . ($Config{make} =~ /^nmake/i ? "-nologo" : "");
247     is( $MM->pasthru(), $pastru, 'pasthru()' );
248 }
249
250 package FakeOut;
251
252 sub TIEHANDLE {
253         bless(\(my $scalar), $_[0]);
254 }
255
256 sub PRINT {
257         my $self = shift;
258         $$self .= shift;
259 }
260
261 __END__
262
263 =head1 NAME
264
265 MM_Win32.t - Tests for ExtUtils::MM_Win32
266
267 =head1 TODO
268
269  - Methods to still be checked:
270  # static_lib() should look into that
271  # dynamic_bs() should look into that
272  # dynamic_lib() should look into that
273  # xs_o() should look into that
274  # top_targets() should look into that
275  # dist_ci() should look into that
276  # dist_core() should look into that
277
278 =head1 AUTHOR
279
280 20011228 Abe Timmerman <abe@ztreet.demon.nl>
281
282 =cut