1# Test problems in Makefile.PL's and hint files.
2
3BEGIN {
4    unshift @INC, 't/lib';
5}
6chdir 't';
7
8use strict;
9use ExtUtils::MM;
10use MakeMaker::Test::Utils qw(makefile_name make make_run run hash2files);
11use Test::More;
12use Config;
13use File::Path;
14use utf8;
15BEGIN {
16  plan skip_all => 'Need perlio and perl 5.8+.'
17    if $] < 5.008 or !$Config{useperlio};
18  plan skip_all => 'cross-compiling and make not available'
19    if !MM->can_run(make()) && $ENV{PERL_CORE} && $Config{'usecrosscompile'};
20
21  plan tests => 8;
22}
23use TieOut;
24
25my $MM = bless { DIR => ['.'] }, 'MM';
26
27my $DIRNAME = 'Problem-Module';
28my %FILES = (
29    'Makefile.PL'   => <<'PL_END',
30use ExtUtils::MakeMaker;
31use utf8;
32WriteMakefile(
33    NAME          => 'Problem::Module',
34    ABSTRACT_FROM => 'lib/Problem/Module.pm',
35    AUTHOR        => q{Danijel Tašov},
36    EXE_FILES     => [ qw(bin/probscript) ],
37    INSTALLMAN1DIR => "some", # even if disabled in $Config{man1dir}
38    MAN1EXT       => 1, # set to 0 if man pages disabled
39);
40PL_END
41
42    'lib/Problem/Module.pm'  => <<'pm_END',
43use utf8;
44
45=pod
46
47=encoding utf8
48
49=head1 NAME
50
51Problem::Module - Danijel Tašov's great new module
52
53=cut
54
551;
56pm_END
57
58    'bin/probscript'  => <<'pl_END',
59#!/usr/bin/perl
60use utf8;
61
62=encoding utf8
63
64=head1 NAME
65
66文档 - Problem script
67pl_END
68);
69
70hash2files($DIRNAME, \%FILES);
71END {
72    ok( chdir File::Spec->updir, 'chdir updir' );
73    ok( rmtree($DIRNAME), 'teardown' );
74}
75
76ok( chdir $DIRNAME, "chdir'd to $DIRNAME" ) ||
77  diag("chdir failed: $!");
78
79if ($] >= 5.008) {
80  eval { require ExtUtils::MakeMaker::Locale; };
81  note "ExtUtils::MakeMaker::Locale vars: $ExtUtils::MakeMaker::Locale::ENCODING_LOCALE;$ExtUtils::MakeMaker::Locale::ENCODING_LOCALE_FS;$ExtUtils::MakeMaker::Locale::ENCODING_CONSOLE_IN;$ExtUtils::MakeMaker::Locale::ENCODING_CONSOLE_OUT\n" unless $@;
82  note "Locale env vars: " . join(';', map {
83    "$_=$ENV{$_}"
84  } grep { /LANG|LC/ } keys %ENV) . "\n";
85}
86
87# Make sure when Makefile.PL's break, they issue a warning.
88# Also make sure Makefile.PL's in subdirs still have '.' in @INC.
89{
90    my $stdout = tie *STDOUT, 'TieOut' or die;
91
92    my $warning = '';
93    local $SIG{__WARN__} = sub { $warning .= join '', @_ };
94    $MM->eval_in_subdirs;
95    my $warnlines = grep { !/does not map to/ } split "\n", $warning;
96    is $warnlines, 0, 'no warning' or diag $warning;
97
98    open my $json_fh, '<:utf8', 'MYMETA.json' or die $!;
99    my $json = do { local $/; <$json_fh> };
100    close $json_fh;
101
102    no utf8; # leave the data below as bytes and let Encode sort it out
103    require Encode;
104    my $str = Encode::decode( 'utf8', "Danijel Tašov's" );
105    like( $json, qr/$str/, 'utf8 abstract' );
106
107    untie *STDOUT;
108}
109
110my $make = make_run();
111my $make_out = run($make);
112diag $make_out unless is $? >> 8, 0, 'Exit code of make == 0';
113
114my $manfile = File::Spec->catfile(qw(blib man1 probscript.1));
115SKIP: {
116  skip 'Manpage not generated', 1 unless -f $manfile;
117  skip 'Pod::Man >= 2.17 needed', 1 unless do {
118    require Pod::Man; $Pod::Man::VERSION >= 2.17;
119  };
120  open my $man_fh, '<:utf8', $manfile or die "open $manfile: $!";
121  my $man = do { local $/; <$man_fh> };
122  close $man_fh;
123
124  no utf8; # leave the data below as bytes and let Encode sort it out
125  require Encode;
126  my $str = Encode::decode( 'utf8', "文档" );
127  like( $man, qr/$str/, 'utf8 man-snippet' );
128}
129
130$make_out = run("$make realclean");
131diag $make_out unless is $? >> 8, 0, 'Exit code of make == 0';
132
133sub makefile_content {
134  open my $fh, '<', makefile_name or die;
135  return <$fh>;
136}
137