1#!/usr/bin/perl
2
3BEGIN {
4    unshift @INC, 't/lib';
5}
6chdir 't';
7
8use strict;
9use warnings;
10use Test::More;
11
12BEGIN {
13    if ($^O !~ /MSWin32/i) {
14        plan skip_all => 'This is not Win32';
15    }
16}
17plan 'no_plan'; # BinGOs says there are 63 but I can only see 62
18
19use Config;
20use File::Spec;
21use File::Basename;
22use ExtUtils::MM;
23
24require_ok( 'ExtUtils::MM_Win32' );
25
26# Dummy MM object until we have a real MM init method.
27my $MM = bless {
28                DIR     => [],
29                NOECHO  => '@',
30                XS      => {},
31                MAKEFILE => 'Makefile',
32                RM_RF   => 'rm -rf',
33                MV      => 'mv',
34                MAKE    => $Config{make}
35               }, 'MM';
36
37
38# replace_manpage_separator() => tr|/|.|s ?
39{
40    my $man = 'a/path/to//something';
41    ( my $replaced = $man ) =~ tr|/|.|s;
42    is( $MM->replace_manpage_separator( $man ),
43        $replaced, 'replace_manpage_separator()' );
44}
45
46# maybe_command()
47SKIP: {
48    skip( '$ENV{COMSPEC} not set', 2 )
49        unless $ENV{COMSPEC} =~ m!((?:[a-z]:)?[^|<>]+)!i;
50    my $comspec = $1;
51    is( $MM->maybe_command( $comspec ),
52        $comspec, 'COMSPEC is a maybe_command()' );
53    ( my $comspec2 = $comspec ) =~ s|\..{3}$||;
54    like( $MM->maybe_command( $comspec2 ),
55          qr/\Q$comspec/i,
56          'maybe_command() without extension' );
57}
58
59my $had_pathext = exists $ENV{PATHEXT};
60{
61    local $ENV{PATHEXT} = '.exe';
62    ok( ! $MM->maybe_command( 'not_a_command.com' ),
63        'not a maybe_command()' );
64}
65# Bug in Perl.  local $ENV{FOO} won't delete the key afterward.
66delete $ENV{PATHEXT} unless $had_pathext;
67
68# file_name_is_absolute() [Does not support UNC-paths]
69{
70    ok( $MM->file_name_is_absolute( 'C:/' ),
71        'file_name_is_absolute()' );
72    ok( ! $MM->file_name_is_absolute( 'some/path/' ),
73        'not file_name_is_absolute()' );
74
75}
76
77# find_perl()
78# Should be able to find running perl... $^X is OK on Win32
79{
80    my $my_perl = $1 if $^X  =~ /(.*)/; # are we in -T or -t?
81    my( $perl, $path ) = fileparse( $my_perl );
82    like( $MM->find_perl( $], [ $perl ], [ $path ], 0 ),
83          qr/^\Q$my_perl\E$/i, 'find_perl() finds this perl' );
84}
85
86# catdir() (calls MM_Win32->canonpath)
87{
88    my @path_eg = qw( c: trick dir/now_OK );
89
90    is( $MM->catdir( @path_eg ),
91         'C:\\trick\\dir\\now_OK', 'catdir()' );
92    is( $MM->catdir( @path_eg ),
93        File::Spec->catdir( @path_eg ),
94        'catdir() eq File::Spec->catdir()' );
95
96# catfile() (calls MM_Win32->catdir)
97    push @path_eg, 'file.ext';
98
99    is( $MM->catfile( @path_eg ),
100        'C:\\trick\\dir\\now_OK\\file.ext', 'catfile()' );
101
102    is( $MM->catfile( @path_eg ),
103        File::Spec->catfile( @path_eg ),
104        'catfile() eq File::Spec->catfile()' );
105}
106
107# init_tools(): check if all keys are created and set?
108note "init_tools creates expected keys"; {
109    my $mm_w32 = bless( { BASEEXT => 'Foo', MAKE => $Config{make} }, 'MM' );
110    $mm_w32->init_tools();
111    my @keys = qw( TOUCH CHMOD CP RM_F RM_RF MV NOOP NOECHO ECHO ECHO_N TEST_F DEV_NULL );
112    for my $key ( @keys ) {
113        ok( $mm_w32->{ $key }, "init_tools: $key" );
114    }
115}
116
117note "init_others creates expected keys"; {
118    my $mm_w32 = bless( { BASEEXT => 'Foo', MAKE => $Config{make} }, 'MM' );
119    $mm_w32->init_others();
120    my @keys = qw( LD AR LDLOADLIBS );
121    for my $key ( @keys ) {
122        ok( $mm_w32->{ $key }, "init_others: $key" );
123    }
124}
125
126# constants()
127# XXX this test is probably useless now that we can call individual
128# init_* methods and check the keys in $mm_w32 directly
129{
130    my $mm_w32 = bless {
131        NAME         => 'TestMM_Win32',
132        VERSION      => '1.00',
133        PM           => { 'MM_Win32.pm' => 1 },
134        MAKE         => $Config{make},
135    }, 'MM';
136
137    # XXX Hack until we have a proper init method.
138    # Flesh out some necessary keys in the MM object.
139    @{$mm_w32}{qw(XS MAN1PODS MAN3PODS)} = ({}) x 3;
140    @{$mm_w32}{qw(C O_FILES H)}          = ([]) x 3;
141    @{$mm_w32}{qw(PARENT_NAME)}          = ('') x 3;
142    $mm_w32->{FULLEXT} = 'TestMM_Win32';
143    $mm_w32->{BASEEXT} = 'TestMM_Win32';
144
145    $mm_w32->init_VERSION;
146    $mm_w32->init_linker;
147    $mm_w32->init_INST;
148    $mm_w32->init_xs;
149
150    my $s_PM = join( " \\\n\t", sort keys %{$mm_w32->{PM}} );
151
152    my $constants = $mm_w32->constants;
153
154    foreach my $regex (
155         qr|^NAME       \s* = \s* TestMM_Win32 \s* $|xms,
156         qr|^VERSION    \s* = \s* 1\.00 \s* $|xms,
157         qr|^MAKEMAKER  \s* = \s* \Q$INC{'ExtUtils/MakeMaker.pm'}\E \s* $|xms,
158         qr|^MM_VERSION \s* = \s* \Q$ExtUtils::MakeMaker::VERSION\E \s* $|xms,
159         qr|^TO_INST_PM \s* = \s* \Q$s_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()
197my $script_ext  = '';
198my $script_name = 'mm_w32tmp';
199SKIP: {
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__
206EOSCRIPT
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}
235unlink "${script_name}$script_ext" if -f "${script_name}$script_ext";
236
237# is_make_type()
238{
239    # Check for literal nmake
240    SKIP: {
241        skip("Not using 'nmake'", 2) unless $Config{make} eq 'nmake';
242        ok(   $MM->is_make_type('nmake'), '->is_make_type(nmake) true'  );
243        ok( ! $MM->is_make_type('dmake'), '->is_make_type(dmake) false' );
244    }
245
246    # Check for literal nmake
247    SKIP: {
248        skip("Not using /nmake/", 2) unless $Config{make} =~ /nmake/;
249        ok(   $MM->is_make_type('nmake'), '->is_make_type(nmake) true'  );
250        ok( ! $MM->is_make_type('dmake'), '->is_make_type(dmake) false' );
251    }
252
253    # Check for literal dmake
254    SKIP: {
255        skip("Not using 'dmake'", 2) unless $Config{make} eq 'dmake';
256        ok(   $MM->is_make_type('dmake'), '->is_make_type(dmake) true'  );
257        ok( ! $MM->is_make_type('nmake'), '->is_make_type(nmake) false' );
258    }
259
260    # Check for literal dmake
261    SKIP: {
262        skip("Not using /dmake/", 2) unless $Config{make} =~ /dmake/;
263        ok(   $MM->is_make_type('dmake'), '->is_make_type(dmake) true'  );
264        ok( ! $MM->is_make_type('nmake'), '->is_make_type(nmake) false' );
265    }
266
267}
268
269# xs_o() should look into that
270# top_targets() should look into that
271
272# dist_ci() should look into that
273# dist_core() should look into that
274
275# _identify_compiler_environment()
276{
277    sub _run_cc_id {
278        my ( $config ) = @_;
279
280        $config->{cc} ||= '';
281
282        my @cc_env = ExtUtils::MM_Win32::_identify_compiler_environment( $config );
283
284        my %cc_env = ( BORLAND => $cc_env[0], GCC => $cc_env[1], MSVC => $cc_env[2] );
285
286        return \%cc_env;
287    }
288
289    sub _check_cc_id_value {
290        my ( $test ) = @_;
291
292        my $res = _run_cc_id( $test->{config} );
293
294        fail( "unknown key '$test->{key}'" ) if !exists $res->{$test->{key}};
295        my $val = $res->{$test->{key}};
296
297        is( $val, $test->{expect}, $test->{desc} );
298
299        return;
300    }
301
302    my @tests = (
303        {
304            config => {},
305            key => 'GCC', expect => 0,
306            desc => 'empty cc is not recognized as gcc',
307        },
308        {
309            config => { cc => 'gcc' },
310            key => 'GCC', expect => 1,
311            desc => 'plain "gcc" is recognized',
312        },
313        {
314            config => { cc => 'C:/MinGW/bin/gcc.exe' },
315            key => 'GCC', expect => 1,
316            desc => 'fully qualified "gcc" is recognized',
317        },
318        {
319            config => { cc => 'C:/MinGW/bin/gcc-1.exe' },
320            key => 'GCC', expect => 1,
321            desc => 'dash-extended gcc is recognized',
322        },
323        {
324            config => { cc => 'C:/MinGW/bin/gcc_1.exe' },
325            key => 'GCC', expect => 0,
326            desc => 'underscore-extended gcc is not recognized',
327        },
328        {
329            config => {},
330            key => 'BORLAND', expect => 0,
331            desc => 'empty cc is not recognized as borland',
332        },
333        {
334            config => { cc => 'bcc' },
335            key => 'BORLAND', expect => 1,
336            desc => 'plain "bcc" is recognized',
337        },
338        {
339            config => { cc => 'C:/Borland/bin/bcc.exe' },
340            key => 'BORLAND', expect => 1,
341            desc => 'fully qualified borland cc is recognized',
342        },
343        {
344            config => { cc => 'bcc-1.exe' },
345            key => 'BORLAND', expect => 1,
346            desc => 'dash-extended borland cc is recognized',
347        },
348        {
349            config => { cc => 'bcc_1.exe' },
350            key => 'BORLAND', expect => 1,
351            desc => 'underscore-extended borland cc is recognized',
352        },
353    );
354
355    _check_cc_id_value($_) for @tests;
356}
357
358package FakeOut;
359
360sub TIEHANDLE {
361    bless(\(my $scalar), $_[0]);
362}
363
364sub PRINT {
365    my $self = shift;
366    $$self .= shift;
367}
368