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 Test::More;
18
19plan skip_all => "no toolchain installed when cross-compiling"
20    if $ENV{PERL_CORE} && $Config{'usecrosscompile'};
21
22#--------------------- Setup
23
24my $cwd  = abs_path;
25my $perl = which_perl;
26my $make = make_run();
27my $mm = bless { NAME => "Foo", MAKE => $Config{make}, PARENT_NAME => '' }, "MM";
28$mm->init_INST;   # *PERLRUN needs INIT_*
29$mm->init_PERL;   # generic ECHO needs ABSPERLRUN
30$mm->init_tools;  # need ECHO
31
32# Run Perl with the currently installing MakeMaker
33$mm->{$_} .= q[ "-I$(INST_ARCHLIB)" "-I$(INST_LIB)"] for qw( PERLRUN FULLPERLRUN ABSPERLRUN );
34
35#------------------- Testing functions
36
37sub test_for_echo {
38    my($calls, $want, $name) = @_;
39    my $output_file = $calls->[0][1];
40
41    note "Testing $name";
42
43    my $dir = File::Temp->newdir();
44    chdir $dir;
45    note "Temp dir: $dir";
46
47    # Write a Makefile to test the output of echo
48    {
49        open my $makefh, ">", "Makefile" or croak "Can't open Makefile: $!";
50        print $makefh "FOO=42\n";       # a variable to test with
51
52        for my $key (qw(INST_ARCHLIB INST_LIB PERL ABSPERL ABSPERLRUN ECHO)) {
53            print $makefh "$key=$mm->{$key}\n";
54        }
55
56        print $makefh "all :\n";
57        for my $args (@$calls) {
58            print $makefh map { "\t$_\n" } $mm->echo(@$args);
59        }
60    }
61
62    # Run the Makefile
63    ok run($make), "make: $name";
64
65    # Check it made the file in question
66    ok -e $output_file, "$output_file exists";
67    open my $fh, "<", $output_file or croak "Can't open $output_file: $!";
68    is join("", <$fh>), $want, "contents";
69
70    chdir $cwd;
71}
72
73
74#---------------- Tests begin
75
76test_for_echo(
77    [["Foo", "bar.txt"]],
78    "Foo\n",
79    "simple echo"
80);
81
82test_for_echo(
83    [["Foo\nBar\nBaz Biff\n", "something.txt"]],
84    "Foo\nBar\nBaz Biff\n",
85    "multiline echo"
86);
87
88test_for_echo(
89    [['$something$', "something.txt"]],
90    '$something$'."\n",
91    "dollar signs escaped"
92);
93
94test_for_echo(
95    [['$(something)', "something.txt"]],
96    '$(something)'."\n",
97    "variables escaped"
98);
99
100test_for_echo(
101    [['Answer: $(FOO)', "bar.txt", { allow_variables => 1 }]],
102    "Answer: 42\n",
103    "allow_variables"
104);
105
106test_for_echo(
107    [
108        ["Foo", "bar.txt"],
109        ["Bar", "bar.txt", { append => 1 }],
110        ["Baz", "bar.txt", 1],
111    ],
112    "Foo\nBar\nBaz\n",
113    "append"
114);
115
116done_testing;
117