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