Skip to content
This repository
branch: mls/new-autocl…
Fetching contributors…

Cannot retrieve contributors at this time

file 260 lines (229 sloc) 9.059 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
#!perl
# Copyright (C) 2001-2009, Parrot Foundation.

use strict;
use warnings;

use Test::More tests => 15;
use Carp;
use Cwd;
use File::Temp 0.13 qw/ tempdir /;
use lib qw( lib t/configure/testlib );
use Parrot::Configure;
use IO::CaptureOutput qw | capture |;

my $cwd = cwd();
my $conf = Parrot::Configure->new;
$conf->data->set( make => 'make' );

my $nonexistent = 'config/gen/makefiles/foobar';
eval { $conf->genfile( $nonexistent => 'CFLAGS', comment_type => '#', ); };
like(
    $@, qr/Can't open $nonexistent/, #'
    "Got expected error message when non-existent file provided as argument to genfile()."
);

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    print $IN qq{Hello world\n};
    close $IN or croak "Unable to close temp file";
    ok(
        $conf->genfile(
            $dummy => 'CFLAGS',
            file_type => 'makefile',
        ),
        "genfile() returned true value with 'file_type' option being set to 'makefile'"
    );
    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    print $IN qq{Hello world\n};
    close $IN or croak "Unable to close temp file";
    eval { $conf->genfile( $dummy => 'CFLAGS', file_type => 'makefile', comment_type => q{<!--}, ); };
    like(
        $@,
        qr/^Unknown comment type/,
        "genfile() failed due to unrecognized comment type with expected message"
    );
    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    print $IN qq{#perl Hello world\n};
    close $IN or croak "Unable to close temp file";
    ok(
        $conf->genfile(
            $dummy => 'CFLAGS',
            file_type => 'makefile',
            feature_file => 0,
        ),
        "genfile() returned true value with false value for 'feature_file' option"
    );
    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    print $IN q{#perl\nuse strict;\n$something = 'something';\n};
    print $IN <<'END_DUMMY';
#perl
if (@verbose@) { sprint "Hello world\n"; }
END_DUMMY
    close $IN or croak "Unable to close temp file";
    my ($stdout, $stderr);
    capture ( sub { eval { $conf->genfile( $dummy => 'CFLAGS', feature_file => 1, ) } },
        \$stdout, \$stderr );
    ok( $stderr, "Error message caught" );
    like( $stderr, qr/sprint/, "Error message had expected content" );
    ok( $@, "Bad Perl code caught by genfile()" );

    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    print $IN q{@foobar@\n};
    close $IN or croak "Unable to close temp file";
    my ($rv, $stdout, $stderr) ;
    capture (
        sub { $rv = $conf->genfile( $dummy => 'CFLAGS' ) },
        \$stdout,
        \$stderr
    );
    ok($rv, "genfile() returned true when warning expected" );
    like( $stderr, qr/value for '\@foobar\@'/, "got expected warning" );

    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    print $IN q{This line ends in a slash/}, qq{\n};
    close $IN or croak "Unable to close temp file";
    eval { $conf->genfile( $dummy => 'CFLAGS', replace_slashes => 1, ); };
    like( $@, qr//,
        "genfile() died as expected with replace_slashes option and line ending in trailing slash"
    );

    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    my $line = q{$(basename morgan/lefay/abra.ca.dabra src/foo.c hacks)};
    print $IN $line, "\n";
    close $IN or croak "Unable to close temp file";
    ok(
        $conf->genfile(
            $dummy => 'CFLAGS',
            expand_gmake_syntax => 1,
        ),
        "genfile() did transformation of 'make' 'basename' as expected"
    );
    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    my $line = q{$(notdir morgan/lefay/abra.ca.dabra src/foo.c hacks)};

    print $IN $line, "\n";
    close $IN or croak "Unable to close temp file";
    ok(
        $conf->genfile(
            $dummy => 'CFLAGS',
            expand_gmake_syntax => 1,
        ),
        "genfile() did transformation of 'make' 'notdir' as expected"
    );
    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    my $line = q{$(addprefix src/,morgan/lefay/abra.ca.dabra foo bar)};

    print $IN $line, "\n";
    close $IN or croak "Unable to close temp file";
    ok(
        $conf->genfile(
            $dummy => 'CFLAGS',
            expand_gmake_syntax => 1,
        ),
        "genfile() did transformation of 'make' 'addprefix' as expected"
    );
    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    my $line = q{$(wildcard *.c *.o)};

    print $IN $line, "\n";
    close $IN or croak "Unable to close temp file";
    ok(
        $conf->genfile(
            $dummy => 'CFLAGS',
            expand_gmake_syntax => 1,
        ),
        "genfile() did transformation of 'make' 'wildcard' as expected"
    );
    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

{
    my $tdir = tempdir( CLEANUP => 1 );
    chdir $tdir or croak "Unable to change to temporary directory";
    my $dummy = 'dummy';
    open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
    print $IN qq{Hello world\n};
    close $IN or croak "Unable to close temp file";
    my $file_type = q{foobar};
    eval { $conf->genfile( $dummy => 'CFLAGS', file_type => $file_type ); };
    like(
        $@,
        qr/^Unknown file_type '$file_type'/,
        "genfile() failed due to unrecognized file type with expected message"
    );
    unlink $dummy or croak "Unable to delete file after testing";
    chdir $cwd or croak "Unable to change back to starting directory";
}

################### DOCUMENTATION ###################

=head1 NAME

t/configure/034-step.t - tests Parrot::Configure::Compiler

=head1 SYNOPSIS

prove t/configure/034-step.t

=head1 DESCRIPTION

Regression tests for the L<Parrot::Configure::Compiler> module. This file holds
tests for Parrot::Configure::Compiler::genfile().

Thanks to http://start.it.uts.edu.au/w/doc/solaris/gmake/make_8.html for
instruction in using F<gmake> functions.

=cut

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
Something went wrong with that request. Please try again.