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}
16
17use Config;
18use File::Spec;
19use File::Basename;
20use ExtUtils::MM;
21
22require_ok( 'ExtUtils::MM_Win32' );
23
24# Dummy MM object until we have a real MM init method.
25my $MM = bless {
26                DIR     => [],
27                NOECHO  => '@',
28                XS      => {},
29                MAKEFILE => 'Makefile',
30                RM_RF   => 'rm -rf',
31                MV      => 'mv',
32                MAKE    => $Config{make}
33               }, 'MM';
34
35
36# replace_manpage_separator() => tr|/|.|s ?
37{
38    my $man = 'a/path/to//something';
39    ( my $replaced = $man ) =~ tr|/|.|s;
40    is( $MM->replace_manpage_separator( $man ),
41        $replaced, 'replace_manpage_separator()' );
42}
43
44# maybe_command()
45SKIP: {
46    skip( '$ENV{COMSPEC} not set', 2 )
47        unless $ENV{COMSPEC} =~ m!((?:[a-z]:)?[^|<>]+)!i;
48    my $comspec = $1;
49    is( $MM->maybe_command( $comspec ),
50        $comspec, 'COMSPEC is a maybe_command()' );
51    ( my $comspec2 = $comspec ) =~ s|\..{3}$||;
52    like( $MM->maybe_command( $comspec2 ),
53          qr/\Q$comspec/i,
54          'maybe_command() without extension' );
55}
56
57my $had_pathext = exists $ENV{PATHEXT};
58{
59    local $ENV{PATHEXT} = '.exe';
60    ok( ! $MM->maybe_command( 'not_a_command.com' ),
61        'not a maybe_command()' );
62}
63# Bug in Perl.  local $ENV{FOO} won't delete the key afterward.
64delete $ENV{PATHEXT} unless $had_pathext;
65
66# file_name_is_absolute() [Does not support UNC-paths]
67{
68    ok( $MM->file_name_is_absolute( 'C:/' ),
69        'file_name_is_absolute()' );
70    ok( ! $MM->file_name_is_absolute( 'some/path/' ),
71        'not file_name_is_absolute()' );
72
73}
74
75# find_perl()
76# Should be able to find running perl... $^X is OK on Win32
77{
78    my $my_perl = $1 if $^X  =~ /(.*)/; # are we in -T or -t?
79    my( $perl, $path ) = fileparse( $my_perl );
80    like( $MM->find_perl( $], [ $perl ], [ $path ], 0 ),
81          qr/^\Q$my_perl\E$/i, 'find_perl() finds this perl' );
82}
83
84# catdir() (calls MM_Win32->canonpath)
85{
86    my @path_eg = qw( c: trick dir/now_OK );
87
88    is( $MM->catdir( @path_eg ),
89         'C:\\trick\\dir\\now_OK', 'catdir()' );
90    is( $MM->catdir( @path_eg ),
91        File::Spec->catdir( @path_eg ),
92        'catdir() eq File::Spec->catdir()' );
93
94# catfile() (calls MM_Win32->catdir)
95    push @path_eg, 'file.ext';
96
97    is( $MM->catfile( @path_eg ),
98        'C:\\trick\\dir\\now_OK\\file.ext', 'catfile()' );
99
100    is( $MM->catfile( @path_eg ),
101        File::Spec->catfile( @path_eg ),
102        'catfile() eq File::Spec->catfile()' );
103}
104
105# init_tools(): check if all keys are created and set?
106note "init_tools creates expected keys"; {
107    my $mm_w32 = bless( { BASEEXT => 'Foo', MAKE => $Config{make} }, 'MM' );
108    $mm_w32->init_tools();
109    my @keys = qw( TOUCH CHMOD CP RM_F RM_RF MV NOOP NOECHO ECHO ECHO_N TEST_F DEV_NULL );
110    for my $key ( @keys ) {
111        ok( $mm_w32->{ $key }, "init_tools: $key" );
112    }
113}
114
115note "init_others creates expected keys"; {
116    my $mm_w32 = bless( { BASEEXT => 'Foo', MAKE => $Config{make} }, 'MM' );
117    $mm_w32->init_others();
118    my @keys = qw( LD AR LDLOADLIBS );
119    for my $key ( @keys ) {
120        ok( $mm_w32->{ $key }, "init_others: $key" );
121    }
122}
123
124# constants()
125# XXX this test is probably useless now that we can call individual
126# init_* methods and check the keys in $mm_w32 directly
127{
128    my $mm_w32 = bless {
129        NAME         => 'TestMM_Win32',
130        VERSION      => '1.00',
131        PM           => { 'MM_Win32.pm' => 1 },
132        MAKE         => $Config{make},
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()
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], DLLTOOL => $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 => 'DLLTOOL', expect => 'dlltool',
306            desc => 'empty dlltool defaults to "dlltool"',
307        },
308        {
309            config => { dlltool => 'test' },
310            key => 'DLLTOOL', expect => 'test',
311            desc => 'dlltool value is taken over verbatim from %Config, if set',
312        },
313        {
314            config => {},
315            key => 'GCC', expect => 0,
316            desc => 'empty cc is not recognized as gcc',
317        },
318        {
319            config => { cc => 'gcc' },
320            key => 'GCC', expect => 1,
321            desc => 'plain "gcc" is recognized',
322        },
323        {
324            config => { cc => 'C:/MinGW/bin/gcc.exe' },
325            key => 'GCC', expect => 1,
326            desc => 'fully qualified "gcc" is recognized',
327        },
328        {
329            config => { cc => 'C:/MinGW/bin/gcc-1.exe' },
330            key => 'GCC', expect => 1,
331            desc => 'dash-extended gcc is recognized',
332        },
333        {
334            config => { cc => 'C:/MinGW/bin/gcc_1.exe' },
335            key => 'GCC', expect => 0,
336            desc => 'underscore-extended gcc is not recognized',
337        },
338        {
339            config => {},
340            key => 'BORLAND', expect => 0,
341            desc => 'empty cc is not recognized as borland',
342        },
343        {
344            config => { cc => 'bcc' },
345            key => 'BORLAND', expect => 1,
346            desc => 'plain "bcc" is recognized',
347        },
348        {
349            config => { cc => 'C:/Borland/bin/bcc.exe' },
350            key => 'BORLAND', expect => 0,
351            desc => 'fully qualified borland cc is not recognized',
352        },
353        {
354            config => { cc => 'bcc-1.exe' },
355            key => 'BORLAND', expect => 1,
356            desc => 'dash-extended borland cc is recognized',
357        },
358        {
359            config => { cc => 'bcc_1.exe' },
360            key => 'BORLAND', expect => 1,
361            desc => 'underscore-extended borland cc is recognized',
362        },
363    );
364
365    _check_cc_id_value($_) for @tests;
366}
367
368
369done_testing;
370
371
372package FakeOut;
373
374sub TIEHANDLE {
375    bless(\(my $scalar), $_[0]);
376}
377
378sub PRINT {
379    my $self = shift;
380    $$self .= shift;
381}
382