1#!/usr/bin/perl
2
3BEGIN {
4    unshift @INC, 't/lib';
5}
6chdir 't';
7
8use strict;
9use Test::More;
10
11BEGIN {
12    if ($^O !~ /MSWin32/i) {
13        plan skip_all => 'This is not Win32';
14    }
15}
16plan 'no_plan'; # BinGOs says there are 63 but I can only see 62
17
18use Config;
19use File::Spec;
20use File::Basename;
21use ExtUtils::MM;
22
23require_ok( 'ExtUtils::MM_Win32' );
24
25# Dummy MM object until we have a real MM init method.
26my $MM = bless {
27                DIR     => [],
28                NOECHO  => '@',
29                XS      => {},
30                MAKEFILE => 'Makefile',
31                RM_RF   => 'rm -rf',
32                MV      => 'mv',
33                MAKE    => $Config{make}
34               }, 'MM';
35
36
37# replace_manpage_separator() => tr|/|.|s ?
38{
39    my $man = 'a/path/to//something';
40    ( my $replaced = $man ) =~ tr|/|.|s;
41    is( $MM->replace_manpage_separator( $man ),
42        $replaced, 'replace_manpage_separator()' );
43}
44
45# maybe_command()
46SKIP: {
47    skip( '$ENV{COMSPEC} not set', 2 )
48        unless $ENV{COMSPEC} =~ m!((?:[a-z]:)?[^|<>]+)!i;
49    my $comspec = $1;
50    is( $MM->maybe_command( $comspec ),
51        $comspec, 'COMSPEC is a maybe_command()' );
52    ( my $comspec2 = $comspec ) =~ s|\..{3}$||;
53    like( $MM->maybe_command( $comspec2 ),
54          qr/\Q$comspec/i,
55          'maybe_command() without extension' );
56}
57
58my $had_pathext = exists $ENV{PATHEXT};
59{
60    local $ENV{PATHEXT} = '.exe';
61    ok( ! $MM->maybe_command( 'not_a_command.com' ),
62        'not a maybe_command()' );
63}
64# Bug in Perl.  local $ENV{FOO} won't delete the key afterward.
65delete $ENV{PATHEXT} unless $had_pathext;
66
67# file_name_is_absolute() [Does not support UNC-paths]
68{
69    ok( $MM->file_name_is_absolute( 'C:/' ),
70        'file_name_is_absolute()' );
71    ok( ! $MM->file_name_is_absolute( 'some/path/' ),
72        'not file_name_is_absolute()' );
73
74}
75
76# find_perl()
77# Should be able to find running perl... $^X is OK on Win32
78{
79    my $my_perl = $1 if $^X  =~ /(.*)/; # are we in -T or -t?
80    my( $perl, $path ) = fileparse( $my_perl );
81    like( $MM->find_perl( $], [ $perl ], [ $path ], 0 ),
82          qr/^\Q$my_perl\E$/i, 'find_perl() finds this perl' );
83}
84
85# catdir() (calls MM_Win32->canonpath)
86{
87    my @path_eg = qw( c: trick dir/now_OK );
88
89    is( $MM->catdir( @path_eg ),
90         'C:\\trick\\dir\\now_OK', 'catdir()' );
91    is( $MM->catdir( @path_eg ),
92        File::Spec->catdir( @path_eg ),
93        'catdir() eq File::Spec->catdir()' );
94
95# catfile() (calls MM_Win32->catdir)
96    push @path_eg, 'file.ext';
97
98    is( $MM->catfile( @path_eg ),
99        'C:\\trick\\dir\\now_OK\\file.ext', 'catfile()' );
100
101    is( $MM->catfile( @path_eg ),
102        File::Spec->catfile( @path_eg ),
103        'catfile() eq File::Spec->catfile()' );
104}
105
106# init_tools(): check if all keys are created and set?
107note "init_tools creates expected keys"; {
108    my $mm_w32 = bless( { BASEEXT => 'Foo', MAKE => $Config{make} }, 'MM' );
109    $mm_w32->init_tools();
110    my @keys = qw( TOUCH CHMOD CP RM_F RM_RF MV NOOP NOECHO ECHO ECHO_N TEST_F DEV_NULL );
111    for my $key ( @keys ) {
112        ok( $mm_w32->{ $key }, "init_tools: $key" );
113    }
114}
115
116note "init_others creates expected keys"; {
117    my $mm_w32 = bless( { BASEEXT => 'Foo', MAKE => $Config{make} }, 'MM' );
118    $mm_w32->init_others();
119    my @keys = qw( LD AR LDLOADLIBS );
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        MAKE         => $Config{make},
134    }, 'MM';
135
136    # XXX Hack until we have a proper init method.
137    # Flesh out some necessary keys in the MM object.
138    @{$mm_w32}{qw(XS MAN1PODS MAN3PODS)} = ({}) x 3;
139    @{$mm_w32}{qw(C O_FILES H)}          = ([]) x 3;
140    @{$mm_w32}{qw(PARENT_NAME)}          = ('') x 3;
141    $mm_w32->{FULLEXT} = 'TestMM_Win32';
142    $mm_w32->{BASEEXT} = 'TestMM_Win32';
143
144    $mm_w32->init_VERSION;
145    $mm_w32->init_linker;
146    $mm_w32->init_INST;
147    $mm_w32->init_xs;
148
149    my $s_PM = join( " \\\n\t", sort keys %{$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        )
160    {
161        like( $constants, $regex, 'constants() check' );
162    }
163}
164
165# path()
166{
167    ok( eq_array( [ $MM->path() ], [ File::Spec->path ] ),
168        'path() [preset]' );
169}
170
171# static_lib() should look into that
172# dynamic_bs() should look into that
173# dynamic_lib() should look into that
174
175# init_linker
176{
177    my $libperl = File::Spec->catfile('$(PERL_INC)',
178                                      $Config{libperl} || 'libperl.a');
179    my $export  = '$(BASEEXT).def';
180    my $after   = '';
181    $MM->init_linker;
182
183    is( $MM->{PERL_ARCHIVE},        $libperl,   'PERL_ARCHIVE' );
184    is( $MM->{PERL_ARCHIVE_AFTER},  $after,     'PERL_ARCHIVE_AFTER' );
185    is( $MM->{EXPORT_LIST},         $export,    'EXPORT_LIST' );
186}
187
188# canonpath()
189{
190    my $path = 'c:\\Program Files/SomeApp\\Progje.exe';
191    is( $MM->canonpath( $path ), File::Spec->canonpath( $path ),
192        'canonpath() eq File::Spec->canonpath' );
193}
194
195# perl_script()
196my $script_ext  = '';
197my $script_name = 'mm_w32tmp';
198SKIP: {
199    local *SCRIPT;
200    skip( "Can't create temp file: $!", 4 )
201        unless open SCRIPT, "> $script_name";
202    print SCRIPT <<'EOSCRIPT';
203#! perl
204__END__
205EOSCRIPT
206    skip( "Can't write to temp file: $!", 4 )
207        unless close SCRIPT;
208    # now start tests:
209    is( $MM->perl_script( $script_name ),
210        "${script_name}$script_ext", "perl_script ($script_ext)" );
211
212    skip( "Can't rename temp file: $!", 3 )
213        unless rename $script_name, "${script_name}.pl";
214    $script_ext = '.pl';
215    is( $MM->perl_script( $script_name ),
216        "${script_name}$script_ext", "perl_script ($script_ext)" );
217
218    skip( "Can't rename temp file: $!", 2 )
219        unless rename "${script_name}$script_ext", "${script_name}.bat";
220    $script_ext = '.bat';
221    is( $MM->perl_script( $script_name ),
222        "${script_name}$script_ext", "perl_script ($script_ext)" );
223
224    skip( "Can't rename temp file: $!", 1 )
225        unless rename "${script_name}$script_ext", "${script_name}.noscript";
226    $script_ext = '.noscript';
227
228    isnt( $MM->perl_script( $script_name ),
229          "${script_name}$script_ext",
230          "not a perl_script anymore ($script_ext)" );
231    is( $MM->perl_script( $script_name ), undef,
232        "perl_script ($script_ext) returns empty" );
233}
234unlink "${script_name}$script_ext" if -f "${script_name}$script_ext";
235
236# is_make_type()
237{
238    # Check for literal nmake
239    SKIP: {
240        skip("Not using 'nmake'", 2) unless $Config{make} eq 'nmake';
241        ok(   $MM->is_make_type('nmake'), '->is_make_type(nmake) true'  );
242        ok( ! $MM->is_make_type('dmake'), '->is_make_type(dmake) false' );
243    }
244
245    # Check for literal nmake
246    SKIP: {
247        skip("Not using /nmake/", 2) unless $Config{make} =~ /nmake/;
248        ok(   $MM->is_make_type('nmake'), '->is_make_type(nmake) true'  );
249        ok( ! $MM->is_make_type('dmake'), '->is_make_type(dmake) false' );
250    }
251
252    # Check for literal dmake
253    SKIP: {
254        skip("Not using 'dmake'", 2) unless $Config{make} eq 'dmake';
255        ok(   $MM->is_make_type('dmake'), '->is_make_type(dmake) true'  );
256        ok( ! $MM->is_make_type('nmake'), '->is_make_type(nmake) false' );
257    }
258
259    # Check for literal dmake
260    SKIP: {
261        skip("Not using /dmake/", 2) unless $Config{make} =~ /dmake/;
262        ok(   $MM->is_make_type('dmake'), '->is_make_type(dmake) true'  );
263        ok( ! $MM->is_make_type('nmake'), '->is_make_type(nmake) false' );
264    }
265
266}
267
268# xs_o() should look into that
269# top_targets() should look into that
270
271# dist_ci() should look into that
272# dist_core() should look into that
273
274# _identify_compiler_environment()
275{
276    sub _run_cc_id {
277        my ( $config ) = @_;
278
279        $config->{cc} ||= '';
280
281        my @cc_env = ExtUtils::MM_Win32::_identify_compiler_environment( $config );
282
283        my %cc_env = ( BORLAND => $cc_env[0], GCC => $cc_env[1], MSVC => $cc_env[2] );
284
285        return \%cc_env;
286    }
287
288    sub _check_cc_id_value {
289        my ( $test ) = @_;
290
291        my $res = _run_cc_id( $test->{config} );
292
293        fail( "unknown key '$test->{key}'" ) if !exists $res->{$test->{key}};
294        my $val = $res->{$test->{key}};
295
296        is( $val, $test->{expect}, $test->{desc} );
297
298        return;
299    }
300
301    my @tests = (
302        {
303            config => {},
304            key => 'GCC', expect => 0,
305            desc => 'empty cc is not recognized as gcc',
306        },
307        {
308            config => { cc => 'gcc' },
309            key => 'GCC', expect => 1,
310            desc => 'plain "gcc" is recognized',
311        },
312        {
313            config => { cc => 'C:/MinGW/bin/gcc.exe' },
314            key => 'GCC', expect => 1,
315            desc => 'fully qualified "gcc" is recognized',
316        },
317        {
318            config => { cc => 'C:/MinGW/bin/gcc-1.exe' },
319            key => 'GCC', expect => 1,
320            desc => 'dash-extended gcc is recognized',
321        },
322        {
323            config => { cc => 'C:/MinGW/bin/gcc_1.exe' },
324            key => 'GCC', expect => 0,
325            desc => 'underscore-extended gcc is not recognized',
326        },
327        {
328            config => {},
329            key => 'BORLAND', expect => 0,
330            desc => 'empty cc is not recognized as borland',
331        },
332        {
333            config => { cc => 'bcc' },
334            key => 'BORLAND', expect => 1,
335            desc => 'plain "bcc" is recognized',
336        },
337        {
338            config => { cc => 'C:/Borland/bin/bcc.exe' },
339            key => 'BORLAND', expect => 1,
340            desc => 'fully qualified borland cc is recognized',
341        },
342        {
343            config => { cc => 'bcc-1.exe' },
344            key => 'BORLAND', expect => 1,
345            desc => 'dash-extended borland cc is recognized',
346        },
347        {
348            config => { cc => 'bcc_1.exe' },
349            key => 'BORLAND', expect => 1,
350            desc => 'underscore-extended borland cc is recognized',
351        },
352    );
353
354    _check_cc_id_value($_) for @tests;
355}
356
357package FakeOut;
358
359sub TIEHANDLE {
360    bless(\(my $scalar), $_[0]);
361}
362
363sub PRINT {
364    my $self = shift;
365    $$self .= shift;
366}
367