<commit_before>#! perl
# Copyright (C) 2001-2006, The Perl Foundation.
# $Id$

use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );

use Test::More;
use Parrot::Test tests => 7;

use Parrot::Config;

use Cwd;
use File::Temp;
use File::Spec::Functions;

my $tempdir = File::Temp::tempdir( CLEANUP => 1 );

our ( $MSWin32, $cygwin );
$MSWin32 = 1 if $^O =~ m!MSWin32!;
$cygwin  = 1 if $^O =~ m!cygwin!;

=head1 NAME

t/pmc/file.t - Files functions

=head1 SYNOPSIS

    % prove t/pmc/file.t

=head1 DESCRIPTION

Tests the C<File> PMC.

=cut

my $xpto = catdir( $tempdir, 'xpto' );
mkdir $xpto unless -d $xpto;

my $otpx = catfile( $xpto, 'otpx' );

open my $fh, '>', $otpx or die $!;
print $fh 'xpto';
close $fh;

# test is_dir
pir_output_is( <<"CODE", <<"OUT", "Test is_dir" );
.sub main :main
        \$P1 = new 'File'

        \$S1 = '$xpto'
        \$I1 = \$P1."is_dir"(\$S1)

        if \$I1 goto ok1
        print "not "

ok1:
        print "ok 1\\n"

        \$S1 = '$otpx'
        \$I1 = \$P1."is_dir"(\$S1)
        \$I1 = !\$I1

        if \$I1 goto ok2
        print "not "

ok2:
        print "ok 2\\n"

        end
.end
CODE
ok 1
ok 2
OUT

# test is_file
pir_output_is( <<"CODE", <<"OUT", "Test is_file" );
.sub main :main
        \$P1 = new 'File'

        \$S1 = '$xpto'
        \$I1 = \$P1."is_file"(\$S1)
        \$I1 = !\$I1

        if \$I1 goto ok1
        print "not "

ok1:
        print "ok 1\\n"

        \$S1 = '$otpx'
        \$I1 = \$P1."is_file"(\$S1)

        if \$I1 goto ok2
        print "not "

ok2:
        print "ok 2\\n"

        end
.end
CODE
ok 1
ok 2
OUT

SKIP: {
    skip "Links not available under Windows", 1 if $MSWin32;

    my $lotpx = catfile( $xpto, 'lotpx' );
    symlink $otpx, $lotpx;

    # test is_link
    pir_output_is( <<"CODE", <<"OUT", "Test is_link with links to files" );<commit_msg>[pmc] add error condition tests to t/pmc/file.t to increase code coverage  and make testing #46681 more reliable<commit_after>#! perl
# Copyright (C) 2001-2006, The Perl Foundation.
# $Id$

use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );

use Test::More;
use Parrot::Test tests => 9;

use Parrot::Config;

use Cwd;
use File::Temp;
use File::Spec::Functions;

my $tempdir = File::Temp::tempdir( CLEANUP => 1 );

our ( $MSWin32, $cygwin );
$MSWin32 = 1 if $^O =~ m!MSWin32!;
$cygwin  = 1 if $^O =~ m!cygwin!;

=head1 NAME

t/pmc/file.t - Files functions

=head1 SYNOPSIS

    % prove t/pmc/file.t

=head1 DESCRIPTION

Tests the C<File> PMC.

=cut

my $xpto = catdir( $tempdir, 'xpto' );
mkdir $xpto unless -d $xpto;

my $otpx = catfile( $xpto, 'otpx' );

open my $fh, '>', $otpx or die $!;
print $fh 'xpto';
close $fh;

# test is_dir
pir_output_is( <<"CODE", <<"OUT", "Test is_dir" );
.sub main :main
        \$P1 = new 'File'

        \$S1 = '$xpto'
        \$I1 = \$P1."is_dir"(\$S1)

        if \$I1 goto ok1
        print "not "

ok1:
        print "ok 1\\n"

        \$S1 = '$otpx'
        \$I1 = \$P1."is_dir"(\$S1)
        \$I1 = !\$I1

        if \$I1 goto ok2
        print "not "

ok2:
        print "ok 2\\n"

        end
.end
CODE
ok 1
ok 2
OUT

# test is_dir
pir_error_output_like( <<"CODE", <<"OUT", "Test is_dir error" );
.sub main :main
        \$P1 = new 'File'

        #make a filename that's long enough to cause lstat to fail
        \$I0 = 1000
loop:
        \$S0 = concat \$S0, "1234567890"
        \$I0 = \$I0 - 1
        if \$I0 goto loop

        \$I1 = \$P1."is_dir"(\$S0)

        end
.end
CODE
/^[\\w \t\r\n]+current instr\.:/
OUT

# test is_file
pir_output_is( <<"CODE", <<"OUT", "Test is_file" );
.sub main :main
        \$P1 = new 'File'

        \$S1 = '$xpto'
        \$I1 = \$P1."is_file"(\$S1)
        \$I1 = !\$I1

        if \$I1 goto ok1
        print "not "

ok1:
        print "ok 1\\n"

        \$S1 = '$otpx'
        \$I1 = \$P1."is_file"(\$S1)

        if \$I1 goto ok2
        print "not "

ok2:
        print "ok 2\\n"

        end
.end
CODE
ok 1
ok 2
OUT

# test is_file
pir_error_output_like( <<"CODE", <<"OUT", "Test is_file error" );
.sub main :main
        \$P1 = new 'File'

        #make a filename that's long enough to cause lstat to fail
        \$I0 = 1000
loop:
        \$S0 = concat \$S0, "1234567890"
        \$I0 = \$I0 - 1
        if \$I0 goto loop

        \$I1 = \$P1."is_file"(\$S0)

        end
.end
CODE
/^[\\w \t\r\n]+current instr\.:/
OUT

SKIP: {
    skip "Links not available under Windows", 1 if $MSWin32;

    my $lotpx = catfile( $xpto, 'lotpx' );
    symlink $otpx, $lotpx;

    # test is_link
    pir_output_is( <<"CODE", <<"OUT", "Test is_link with links to files" );
{'fraction_non_alphanumeric': 0.18055555555555555, 'fraction_numerical': 0.040581597222222224, 'mean_word_length': 2.038233355306526, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7408736', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1892, 'n_words': 560}