# Copyright (C) 2008, Parrot Foundation.

=head1 NAME

Parrot::Test::Util - utilities for Parrot tests

=head1 SYNOPSIS

    use Parrot::Test::Util 'create_tempfile';

    my ($FOO, $temp_pir) = create_tempfile( SUFFIX => '.pir', UNLINK => 1 );


=head1 DESCRIPTION

This module provides basic utilities for Parrot test scripts. So far, there's
only one utility, C<create_tempfile>, which must be requested for import.

=head1 AUTHOR

Written by Jerry Gay.

=cut

package Parrot::Test::Util;

use strict;
use warnings;

use File::Temp 'tempfile';
use Exporter;

our @ISA = qw( Exporter );
our @EXPORT_OK = qw( create_tempfile );


=head1 Functions

=over 4

=item C<create_tempfile>

Creates a tempfile using File::Temp::tempfile, passing parameters through.
Returns a Perl-friendly path using forward-slashes, rather than a platform-
specific path that may contain unescaped backslashes which may be interpreted
as (likely invalid) unicode escape codes.

=cut

sub create_tempfile {
    # platform quirks (GH 1077, cygwin 5.18 - Cwd 3.40 only):
    # we cannot get Cwd::abs_path on a non-existing file, and we don't
    # need to unlink it.
    my ($filehandle, $filename, $winfixup);
    if ($^O eq 'cygwin' and join(' ',@_) eq 'UNLINK 1 OPEN 0') {
        ($filehandle, $filename) = tempfile('UNLINK' => 0, 'OPEN' => 0);
    }
    else {
        ($filehandle, $filename) = &tempfile;
    }

    # expand msys virtual paths
    if($^O eq 'msys') {
        my $tmpdir = `cd /tmp && pwd -W`;
        chomp $tmpdir;
        $filename =~ s/^\/tmp\//$tmpdir\//;
    }

    $filename =~ s/\\/\//g;
    return ($filehandle, $filename);
}

=back

=cut


1;

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
