Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 94 lines (64 sloc) 3.084 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
use v6;

use Test;

=begin pod

=head1 DESCRIPTION

This test tests various file stat methods.

=end pod

plan 13;

if $*OS eq "browser" {
  skip_rest "Programs running in browsers don't have access to regular IO.";
  exit;
}

# time stat tests (modify/change/access)
{
    my $before_creation = time - 1;

    my $tmpfile1 = create_temporary_file(1);
    my $original1_modified = $tmpfile1.IO.modified;
    my $original1_changed = $tmpfile1.IO.changed;
    my $original1_accessed = $tmpfile1.IO.accessed;

    my $tmpfile2 = create_temporary_file(2);
    my $original2_modified = $tmpfile2.IO.modified;
    my $original2_changed = $tmpfile2.IO.changed;
    my $original2_accessed = $tmpfile2.IO.accessed;

    ok ($before_creation < $original1_modified), 'IO.modified should be greater than pre-creation timestamp';
    ok ($before_creation < $original1_changed), 'IO.changed should be greater than pre-creation timestamp';
    ok ($before_creation < $original1_accessed), 'IO.accessed should be greater than pre-creation timestamp';

    sleep 2; # tick for time comparisons


    # altering content
    my $fh1 = open $tmpfile1, :w orelse die "Could not open $tmpfile1 for writing";
    $fh1.print("example content");
    $fh1.close;

    ok ($original1_modified < $tmpfile1.IO.modified), 'IO.modified should be updated when file content changes';
    ok ($original1_changed < $tmpfile1.IO.changed), 'IO.changed should be updated when file content changes';
    ok ($original1_accessed == $tmpfile1.IO.accessed), 'IO.accessed should NOT be updated when file is opened for writing';
   
    # opening for read
    $fh1 = open $tmpfile1, :r orelse die "Could not open $tmpfile1 for reading";
    $fh1.close;

    ok ($original1_accessed == $tmpfile1.IO.accessed), 'IO.accessed should NOT be updated when file is opened for reading';

    # reading contents of file
    slurp $tmpfile1;
    ok ($original1_accessed < $tmpfile1.IO.accessed), 'IO.accessed should be updated when contents of file is read';


    # changing file permissions

    $tmpfile2.IO.chmod(0o000);
    my $post_chmod_modified = $tmpfile2.IO.modified;
    my $post_chmod_changed = $tmpfile2.IO.changed;

    ok ($original2_changed == $post_chmod_modified), 'IO.modified should NOT be updated when file mode is altered';
    ok ($original2_changed < $post_chmod_changed), 'IO.changed should be updated when file mode is altered';
    ok ($post_chmod_modified != $post_chmod_changed), 'IO.changed and IO.modified should differ after file mode change';


    # accessing file

    remove_file $tmpfile1;
    remove_file $tmpfile2;
}


sub create_temporary_file($id) {
    my $time = time;
    my $file = "temp-16-filehandles-filestat-" ~ $*PID ~ "-" ~ $id ~ ".temp";
    my $fh = open $file, :w orelse die "Could not create $file"; #OK not used
    $fh.print($time); # store pre-creation timestamp
    diag "Using file $file";
    return $file;
}

sub remove_file ($file) {
    unlink $file;
    ok($file.IO ~~ :!e, "Test file $file was successfully removed");
}

# vim: ft=perl6
Something went wrong with that request. Please try again.