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 tests => 49;
14	} else {
15		plan skip_all => 'This is not Win32';
16	}
17}
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_others(): check if all keys are created and set?
108# qw( TOUCH CHMOD CP RM_F RM_RF MV NOOP TEST_F LD AR LDLOADLIBS DEV_NUL )
109{
110    my $mm_w32 = bless( { BASEEXT => 'Foo' }, 'MM' );
111    $mm_w32->init_others();
112    my @keys = qw( TOUCH CHMOD CP RM_F RM_RF MV NOOP
113                   TEST_F LD AR LDLOADLIBS DEV_NULL );
114    for my $key ( @keys ) {
115        ok( $mm_w32->{ $key }, "init_others: $key" );
116    }
117}
118
119# constants()
120# XXX this test is probably useless now that we can call individual
121# init_* methods and check the keys in $mm_w32 directly
122{
123    my $mm_w32 = bless {
124        NAME         => 'TestMM_Win32',
125        VERSION      => '1.00',
126        PM           => { 'MM_Win32.pm' => 1 },
127    }, 'MM';
128
129    # XXX Hack until we have a proper init method.
130    # Flesh out some necessary keys in the MM object.
131    @{$mm_w32}{qw(XS MAN1PODS MAN3PODS)} = ({}) x 3;
132    @{$mm_w32}{qw(C O_FILES H)}          = ([]) x 3;
133    @{$mm_w32}{qw(PARENT_NAME)}          = ('') x 3;
134    $mm_w32->{FULLEXT} = 'TestMM_Win32';
135    $mm_w32->{BASEEXT} = 'TestMM_Win32';
136
137    $mm_w32->init_VERSION;
138    $mm_w32->init_linker;
139    $mm_w32->init_INST;
140    $mm_w32->init_xs;
141
142    my $s_PM = join( " \\\n\t", sort keys %{$mm_w32->{PM}} );
143    my $k_PM = join( " \\\n\t", %{$mm_w32->{PM}} );
144
145    my $constants = $mm_w32->constants;
146
147    foreach my $regex (
148         qr|^NAME       \s* = \s* TestMM_Win32 \s* $|xms,
149         qr|^VERSION    \s* = \s* 1\.00 \s* $|xms,
150         qr|^MAKEMAKER  \s* = \s* \Q$INC{'ExtUtils/MakeMaker.pm'}\E \s* $|xms,
151         qr|^MM_VERSION \s* = \s* \Q$ExtUtils::MakeMaker::VERSION\E \s* $|xms,
152         qr|^TO_INST_PM \s* = \s* \Q$s_PM\E \s* $|xms,
153         qr|^PM_TO_BLIB \s* = \s* \Q$k_PM\E \s* $|xms,
154        )
155    {
156        like( $constants, $regex, 'constants() check' );
157    }
158}
159
160# path()
161{
162    ok( eq_array( [ $MM->path() ], [ File::Spec->path ] ),
163        'path() [preset]' );
164}
165
166# static_lib() should look into that
167# dynamic_bs() should look into that
168# dynamic_lib() should look into that
169
170# init_linker
171{
172    my $libperl = File::Spec->catfile('$(PERL_INC)',
173                                      $Config{libperl} || 'libperl.a');
174    my $export  = '$(BASEEXT).def';
175    my $after   = '';
176    $MM->init_linker;
177
178    is( $MM->{PERL_ARCHIVE},        $libperl,   'PERL_ARCHIVE' );
179    is( $MM->{PERL_ARCHIVE_AFTER},  $after,     'PERL_ARCHIVE_AFTER' );
180    is( $MM->{EXPORT_LIST},         $export,    'EXPORT_LIST' );
181}
182
183# canonpath()
184{
185    my $path = 'c:\\Program Files/SomeApp\\Progje.exe';
186    is( $MM->canonpath( $path ), File::Spec->canonpath( $path ),
187	    'canonpath() eq File::Spec->canonpath' );
188}
189
190# perl_script()
191my $script_ext  = '';
192my $script_name = 'mm_w32tmp';
193SKIP: {
194    local *SCRIPT;
195    skip( "Can't create temp file: $!", 4 )
196        unless open SCRIPT, "> $script_name";
197    print SCRIPT <<'EOSCRIPT';
198#! perl
199__END__
200EOSCRIPT
201    skip( "Can't write to temp file: $!", 4 )
202        unless close SCRIPT;
203    # now start tests:
204    is( $MM->perl_script( $script_name ),
205        "${script_name}$script_ext", "perl_script ($script_ext)" );
206
207    skip( "Can't rename temp file: $!", 3 )
208        unless rename $script_name, "${script_name}.pl";
209    $script_ext = '.pl';
210    is( $MM->perl_script( $script_name ),
211        "${script_name}$script_ext", "perl_script ($script_ext)" );
212
213    skip( "Can't rename temp file: $!", 2 )
214        unless rename "${script_name}$script_ext", "${script_name}.bat";
215    $script_ext = '.bat';
216    is( $MM->perl_script( $script_name ),
217        "${script_name}$script_ext", "perl_script ($script_ext)" );
218
219    skip( "Can't rename temp file: $!", 1 )
220        unless rename "${script_name}$script_ext", "${script_name}.noscript";
221    $script_ext = '.noscript';
222
223    isnt( $MM->perl_script( $script_name ),
224          "${script_name}$script_ext",
225          "not a perl_script anymore ($script_ext)" );
226    is( $MM->perl_script( $script_name ), undef,
227        "perl_script ($script_ext) returns empty" );
228}
229unlink "${script_name}$script_ext" if -f "${script_name}$script_ext";
230
231# is_make_type()
232{
233    # Check for literal nmake
234    SKIP: {
235        skip("Not using 'nmake'", 2) unless $Config{make} eq 'nmake';
236        ok(   $MM->is_make_type('nmake'), '->is_make_type(nmake) true'  );
237	ok( ! $MM->is_make_type('dmake'), '->is_make_type(dmake) false' );
238    }
239
240    # Check for literal nmake
241    SKIP: {
242        skip("Not using /nmake/", 2) unless $Config{make} =~ /nmake/;
243        ok(   $MM->is_make_type('nmake'), '->is_make_type(nmake) true'  );
244	ok( ! $MM->is_make_type('dmake'), '->is_make_type(dmake) false' );
245    }
246
247    # Check for literal dmake
248    SKIP: {
249        skip("Not using 'dmake'", 2) unless $Config{make} eq 'dmake';
250        ok(   $MM->is_make_type('dmake'), '->is_make_type(dmake) true'  );
251	ok( ! $MM->is_make_type('nmake'), '->is_make_type(nmake) false' );
252    }
253
254    # Check for literal dmake
255    SKIP: {
256        skip("Not using /dmake/", 2) unless $Config{make} =~ /dmake/;
257        ok(   $MM->is_make_type('dmake'), '->is_make_type(dmake) true'  );
258	ok( ! $MM->is_make_type('nmake'), '->is_make_type(nmake) false' );
259    }
260
261}
262
263# xs_o() should look into that
264# top_targets() should look into that
265
266# dist_ci() should look into that
267# dist_core() should look into that
268
269# pasthru()
270{
271    my $pastru = "PASTHRU = " . ($Config{make} =~ /^nmake/i ? "-nologo" : "");
272    is( $MM->pasthru(), $pastru, 'pasthru()' );
273}
274
275package FakeOut;
276
277sub TIEHANDLE {
278	bless(\(my $scalar), $_[0]);
279}
280
281sub PRINT {
282	my $self = shift;
283	$$self .= shift;
284}
285
286__END__
287
288=head1 NAME
289
290MM_Win32.t - Tests for ExtUtils::MM_Win32
291
292=head1 TODO
293
294 - Methods to still be checked:
295 # static_lib() should look into that
296 # dynamic_bs() should look into that
297 # dynamic_lib() should look into that
298 # xs_o() should look into that
299 # top_targets() should look into that
300 # dist_ci() should look into that
301 # dist_core() should look into that
302
303=head1 AUTHOR
304
30520011228 Abe Timmerman <abe@ztreet.demon.nl>
306
307=cut
308