Test-MockFile

 view release on metacpan or  search on metacpan

t/stat_timestamps.t  view on Meta::CPAN

note "-------------- READLINE UPDATES atime --------------";
{
    my $mock = Test::MockFile->file( '/ts/readline', "line1\nline2\n" );

    $mock->{'atime'} = 1000;
    $mock->{'mtime'} = 1000;
    $mock->{'ctime'} = 1000;

    open my $fh, '<', '/ts/readline' or die "open: $!";
    my $line = <$fh>;
    close $fh;

    isnt( $mock->atime(), 1000, 'readline updates atime' );
    is( $mock->mtime(), 1000, 'readline does not update mtime' );
    is( $mock->ctime(), 1000, 'readline does not update ctime' );
}

note "-------------- READLINE (list context) UPDATES atime --------------";
{
    my $mock = Test::MockFile->file( '/ts/slurp', "a\nb\nc\n" );

    $mock->{'atime'} = 1000;

    open my $fh, '<', '/ts/slurp' or die "open: $!";
    my @lines = <$fh>;
    close $fh;

    isnt( $mock->atime(), 1000, 'readline in list context updates atime' );
    is( scalar @lines, 3, 'read all three lines' );
}

note "-------------- GETC UPDATES atime --------------";
{
    my $mock = Test::MockFile->file( '/ts/getc', 'XY' );

    $mock->{'atime'} = 1000;

    open my $fh, '<', '/ts/getc' or die "open: $!";
    my $c = getc($fh);
    close $fh;

    is( $c, 'X', 'getc returns first character' );
    isnt( $mock->atime(), 1000, 'getc updates atime' );
}

note "-------------- CHMOD UPDATES ctime --------------";
{
    my $mock = Test::MockFile->file( '/ts/chmod', 'data' );

    $mock->{'ctime'} = 1000;
    $mock->{'mtime'} = 1000;

    chmod 0644, '/ts/chmod';

    isnt( $mock->ctime(), 1000, 'chmod updates ctime' );
    is( $mock->mtime(), 1000, 'chmod does not update mtime' );
}

note "-------------- CHOWN UPDATES ctime --------------";
{
    my $mock = Test::MockFile->file( '/ts/chown', 'data' );

    $mock->{'ctime'} = 1000;
    $mock->{'mtime'} = 1000;

    my ($primary_gid) = split /\s/, $);
    chown $>, $primary_gid, '/ts/chown';

    isnt( $mock->ctime(), 1000, 'chown updates ctime' );
    is( $mock->mtime(), 1000, 'chown does not update mtime' );
}

note "-------------- OPEN > UPDATES mtime/ctime (truncate) --------------";
{
    my $mock = Test::MockFile->file( '/ts/trunc', 'existing content' );

    $mock->{'mtime'} = 1000;
    $mock->{'ctime'} = 1000;

    open my $fh, '>', '/ts/trunc' or die "open: $!";
    close $fh;

    isnt( $mock->mtime(), 1000, 'open > updates mtime' );
    isnt( $mock->ctime(), 1000, 'open > updates ctime' );
    is( $mock->contents(), '', 'open > truncated contents' );
}

note "-------------- SYSOPEN O_TRUNC UPDATES mtime/ctime --------------";
{
    my $mock = Test::MockFile->file( '/ts/systrunc', 'existing' );

    $mock->{'mtime'} = 1000;
    $mock->{'ctime'} = 1000;

    sysopen my $fh, '/ts/systrunc', O_WRONLY | O_TRUNC or die "sysopen: $!";
    close $fh;

    isnt( $mock->mtime(), 1000, 'sysopen O_TRUNC updates mtime' );
    isnt( $mock->ctime(), 1000, 'sysopen O_TRUNC updates ctime' );
}

note "-------------- SYSOPEN O_CREAT UPDATES mtime/ctime --------------";
{
    my $mock = Test::MockFile->file('/ts/creat');

    $mock->{'mtime'} = 1000;
    $mock->{'ctime'} = 1000;

    sysopen my $fh, '/ts/creat', O_WRONLY | O_CREAT or die "sysopen: $!";
    close $fh;

    isnt( $mock->mtime(), 1000, 'sysopen O_CREAT on new file updates mtime' );
    isnt( $mock->ctime(), 1000, 'sysopen O_CREAT on new file updates ctime' );
}

note "-------------- OPEN >> does NOT update until write --------------";
{
    my $mock = Test::MockFile->file( '/ts/append', 'data' );

    $mock->{'mtime'} = 1000;
    $mock->{'ctime'} = 1000;

    open my $fh, '>>', '/ts/append' or die "open: $!";

    # Opening in append mode alone shouldn't update timestamps
    is( $mock->mtime(), 1000, 'open >> alone does not update mtime' );
    is( $mock->ctime(), 1000, 'open >> alone does not update ctime' );

    print $fh "more";
    close $fh;



( run in 0.604 second using v1.01-cache-2.11-cpan-5511b514fd6 )