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 =~ /cygwin/i) {
14		plan tests => 14;
15	} else {
16		plan skip_all => "This is not cygwin";
17	}
18}
19
20use Config;
21use File::Spec;
22use ExtUtils::MM;
23use Config;
24
25use_ok( 'ExtUtils::MM_Cygwin' );
26
27# test canonpath
28my $path = File::Spec->canonpath('/a/../../c');
29is( MM->canonpath('/a/../../c'), $path,
30	'canonpath() method should work just like the one in File::Spec' );
31
32# test cflags, with the fake package below
33my $MM = bless({
34	CFLAGS	=> 'fakeflags',
35	CCFLAGS	=> '',
36}, 'MM');
37
38# with CFLAGS set, it should be returned
39is( $MM->cflags(), 'fakeflags',
40	'cflags() should return CFLAGS member data, if set' );
41
42delete $MM->{CFLAGS};
43
44# ExtUtils::MM_Cygwin::cflags() calls this, fake the output
45{
46    local $SIG{__WARN__} = sub {
47        warn @_ unless $_[0] =~ /^Subroutine .* redefined/;
48    };
49    *ExtUtils::MM_Unix::cflags = sub { return $_[1] };
50}
51
52# respects the config setting, should ignore whitespace around equal sign
53my $ccflags = $Config{useshrplib} eq 'true' ? ' -DUSEIMPORTLIB' : '';
54{
55    local $MM->{NEEDS_LINKING} = 1;
56    $MM->cflags(<<FLAGS);
57OPTIMIZE = opt
58PERLTYPE  =pt
59FLAGS
60}
61
62like( $MM->{CFLAGS}, qr/OPTIMIZE = opt/, '... should set OPTIMIZE' );
63like( $MM->{CFLAGS}, qr/PERLTYPE = pt/, '... should set PERLTYPE' );
64like( $MM->{CFLAGS}, qr/CCFLAGS = $ccflags/, '... should set CCFLAGS' );
65
66# test manifypods
67$MM = bless({
68	NOECHO => 'noecho',
69	MAN3PODS => {},
70	MAN1PODS => {},
71    MAKEFILE => 'Makefile',
72}, 'MM');
73unlike( $MM->manifypods(), qr/foo/,
74	'manifypods() should return without PODS values set' );
75
76$MM->{MAN3PODS} = { foo => 'foo.1' };
77my $res = $MM->manifypods();
78like( $res, qr/manifypods.*foo.*foo.1/s, '... should add MAN3PODS targets' );
79
80
81# init_linker
82{
83    my $libperl = $Config{libperl} || 'libperl.a';
84    $libperl =~ s/\.a/.dll.a/ if "$]" >= 5.006002;
85    $libperl = "\$(PERL_INC)/$libperl";
86
87    my $export  = '';
88    my $after   = '';
89    $MM->init_linker;
90
91    is( $MM->{PERL_ARCHIVE},        $libperl,   'PERL_ARCHIVE' );
92    is( $MM->{PERL_ARCHIVE_AFTER},  $after,     'PERL_ARCHIVE_AFTER' );
93    is( $MM->{EXPORT_LIST},         $export,    'EXPORT_LIST' );
94}
95
96# Tests for correct handling of maybe_command in /cygdrive/*
97# and c:/*.  $ENV{COMSPEC}, if it exists, should always be executable.
98SKIP: {
99    skip "Needs Cygwin::win_to_posix_path()", 2 unless defined &Cygwin::win_to_posix_path;
100
101    SKIP: {
102        my $comspec = $ENV{COMSPEC};
103        skip(q[$ENV{COMSPEC} does not exist], 1) unless $comspec;
104
105        $comspec = Cygwin::win_to_posix_path($comspec);
106
107        ok(MM->maybe_command($comspec), qq{'$comspec' should be executable"});
108    }
109
110    # 'C:/' should *never* be executable, it's a directory.
111    {
112        my $cdrive = Cygwin::win_to_posix_path("C:/");
113
114        ok(!MM->maybe_command($cdrive), qq{'$cdrive' should never be executable});
115    }
116}
117
118# Our copy of Perl (with a unix-path) should always be executable.
119SKIP: {
120  skip "The Perl may not be installed yet when in core", 1 if $ENV{PERL_CORE};
121  ok(MM->maybe_command($Config{perlpath}), qq{'$Config{perlpath}' should be executable});
122}
123
124package FakeOut;
125
126sub TIEHANDLE {
127	bless(\(my $scalar), $_[0]);
128}
129
130sub PRINT {
131	my $self = shift;
132	$$self .= shift;
133}
134