File: 02-basic-usage.t

package info (click to toggle)
libfile-touch-perl 0.12-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 112 kB
  • sloc: perl: 186; makefile: 2
file content (77 lines) | stat: -rw-r--r-- 2,211 bytes parent folder | download | duplicates (3)
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
#! perl

use strict;
use warnings;

use Test::More 0.88 tests => 18;
use File::Touch;

my $testfilename = 't/example-file.txt';
my $new_atime    = 1399242863;
my $new_mtime    = 1399156463;

if (-f $testfilename) {
    unlink($testfilename)
    || BAIL_OUT("test file ($testfilename) already exists and I can't delete it: $!");
}

foreach my $set_atime ( 0 .. 1 ) {
    foreach my $set_mtime ( 0 .. 1 ) {
        next unless $set_mtime || $set_atime;

        my %args;
        my $toucher;

        open(my $fh, '>', $testfilename)
            || BAIL_OUT("can't create test file [set_atime=$set_atime set_mtime=$set_mtime]");
        print $fh "set_atime=$set_atime set_mtime=$set_mtime\n";
        close($fh);

        my ($original_atime, $original_mtime) = (stat($testfilename))[8,9];

        ok($original_atime > $new_atime && $original_mtime > $new_mtime,
           "atime & mtime on the new file should be in the future compared to when I wrote this test");

        if ($set_mtime) {
            $args{mtime} = $new_mtime;
        }
        else {
            $args{atime_only} = 1;
        }

        if ($set_atime) {
            $args{atime} = $new_atime;
        }
        else {
            $args{mtime_only} = 1;
        }

        $toucher = File::Touch->new(%args);
        ok(defined($toucher), "We should get an instance of File::Touch");

        ok($toucher->touch($testfilename) == 1, "touch() should say that it updated 1 file");

        my ($updated_atime, $updated_mtime) = (stat($testfilename))[8,9];

        if ($set_mtime) {
            ok($updated_mtime == $new_mtime,
               "stat() should return a changed mtime");
        }
        else {
            ok($updated_mtime == $original_mtime,
               "stat() should return the same mtime as when the file was created");
        }

        if ($set_atime) {
            ok($updated_atime == $new_atime,
               "stat() should return a changed atime");
        }
        else {
            ok($updated_atime == $original_atime,
               "stat() should return the same atime as when the file was created");
        }

        ok(unlink($testfilename), "delete the file after running the test");

    }
}