1#!/usr/bin/perl -w
2
3BEGIN {
4    unshift @INC, 't/lib';
5}
6
7use strict;
8use warnings;
9
10use Carp;
11use Config;
12use ExtUtils::MM;
13use MakeMaker::Test::Utils;
14use File::Temp;
15use Cwd 'abs_path';
16
17use ExtUtils::MM;
18use Test::More
19    !MM->can_run(make()) && $ENV{PERL_CORE} && $Config{'usecrosscompile'}
20    ? (skip_all => "cross-compiling and make not available")
21    : (tests => 18);
22
23#--------------------- Setup
24
25my $cwd  = abs_path;
26my $perl = which_perl;
27my $make = make_run();
28my $mm = bless { NAME => "Foo", MAKE => $Config{make}, PARENT_NAME => '' }, "MM";
29$mm->init_INST;   # *PERLRUN needs INIT_*
30$mm->init_PERL;   # generic ECHO needs ABSPERLRUN
31$mm->init_tools;  # need ECHO
32
33# Run Perl with the currently installing MakeMaker
34$mm->{$_} .= q[ "-I$(INST_ARCHLIB)" "-I$(INST_LIB)"] for qw( PERLRUN FULLPERLRUN ABSPERLRUN );
35
36#see sub specify_shell
37my $shell = $^O eq 'MSWin32' && $mm->is_make_type('gmake') ? $ENV{COMSPEC} : undef;
38
39#------------------- Testing functions
40
41sub test_for_echo {
42    my($calls, $want, $name) = @_;
43    my $output_file = $calls->[0][1];
44
45    note "Testing $name";
46
47    my $dir = File::Temp->newdir();
48    chdir $dir;
49    note "Temp dir: $dir";
50
51    # Write a Makefile to test the output of echo
52    {
53        open my $makefh, ">", "Makefile" or croak "Can't open Makefile: $!";
54        print $makefh "FOO=42\n";       # a variable to test with
55
56        for my $key (qw(INST_ARCHLIB INST_LIB PERL ABSPERL ABSPERLRUN ECHO)) {
57            print $makefh "$key=$mm->{$key}\n";
58        }
59        print $makefh "SHELL=$shell\n" if defined $shell;
60
61        print $makefh "all :\n";
62        for my $args (@$calls) {
63            print $makefh map { "\t$_\n" } $mm->echo(@$args);
64        }
65    }
66
67    # Run the Makefile
68    ok run($make), "make: $name";
69
70    # Check it made the file in question
71    ok -e $output_file, "$output_file exists";
72    open my $fh, "<", $output_file or croak "Can't open $output_file: $!";
73    is join("", <$fh>), $want, "contents";
74
75    chdir $cwd;
76}
77
78
79#---------------- Tests begin
80
81test_for_echo(
82    [["Foo", "bar.txt"]],
83    "Foo\n",
84    "simple echo"
85);
86
87test_for_echo(
88    [["Foo\nBar\nBaz Biff\n", "something.txt"]],
89    "Foo\nBar\nBaz Biff\n",
90    "multiline echo"
91);
92
93test_for_echo(
94    [['$something$', "something.txt"]],
95    '$something$'."\n",
96    "dollar signs escaped"
97);
98
99test_for_echo(
100    [['$(something)', "something.txt"]],
101    '$(something)'."\n",
102    "variables escaped"
103);
104
105test_for_echo(
106    [['Answer: $(FOO)', "bar.txt", { allow_variables => 1 }]],
107    "Answer: 42\n",
108    "allow_variables"
109);
110
111test_for_echo(
112    [
113        ["Foo", "bar.txt"],
114        ["Bar", "bar.txt", { append => 1 }],
115        ["Baz", "bar.txt", 1],
116    ],
117    "Foo\nBar\nBaz\n",
118    "append"
119);
120