Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

tests for IO filehandle stat times (changed/modified/accessed)

  • Loading branch information...
commit a45e1b21d8529fe2a3dd3d3940a2941fa5869d43 1 parent d9d5118
Geir Amdal authored April 21, 2012

Showing 1 changed file with 93 additions and 0 deletions. Show diff stats Hide diff stats

  1. 93  S16-filehandles/filestat.t
93  S16-filehandles/filestat.t
... ...
@@ -0,0 +1,93 @@
  1
+use v6;
  2
+
  3
+use Test;
  4
+
  5
+=begin pod
  6
+
  7
+=head1 DESCRIPTION
  8
+
  9
+This test tests various file stat methods.
  10
+
  11
+=end pod
  12
+
  13
+plan 13;
  14
+
  15
+if $*OS eq "browser" {
  16
+  skip_rest "Programs running in browsers don't have access to regular IO.";
  17
+  exit;
  18
+}
  19
+
  20
+# time stat tests (modify/change/access)
  21
+{
  22
+    my $before_creation = time - 1;
  23
+
  24
+    my $tmpfile1 = create_temporary_file(1);
  25
+    my $original1_modified = $tmpfile1.IO.modified;
  26
+    my $original1_changed = $tmpfile1.IO.changed;
  27
+    my $original1_accessed = $tmpfile1.IO.accessed;
  28
+
  29
+    my $tmpfile2 = create_temporary_file(2);
  30
+    my $original2_modified = $tmpfile2.IO.modified;
  31
+    my $original2_changed = $tmpfile2.IO.changed;
  32
+    my $original2_accessed = $tmpfile2.IO.accessed;
  33
+
  34
+    ok ($before_creation < $original1_modified), 'IO.modified should be greater than pre-creation timestamp';
  35
+    ok ($before_creation < $original1_changed),  'IO.changed should be greater than pre-creation timestamp';
  36
+    ok ($before_creation < $original1_accessed), 'IO.accessed should be greater than pre-creation timestamp';
  37
+
  38
+    sleep 2; # tick for time comparisons
  39
+
  40
+
  41
+    # altering content
  42
+    my $fh1 = open $tmpfile1, :w orelse die "Could not open $tmpfile1 for writing";
  43
+    $fh1.print("example content");
  44
+    $fh1.close;
  45
+
  46
+    ok ($original1_modified < $tmpfile1.IO.modified), 'IO.modified should be updated when file content changes';
  47
+    ok ($original1_changed  < $tmpfile1.IO.changed),  'IO.changed should be updated when file content changes';
  48
+    ok ($original1_accessed == $tmpfile1.IO.accessed), 'IO.accessed should NOT be updated when file is opened for writing';
  49
+   
  50
+    # opening for read
  51
+    $fh1 = open $tmpfile1, :r orelse die "Could not open $tmpfile1 for reading";
  52
+    $fh1.close;
  53
+
  54
+    ok ($original1_accessed == $tmpfile1.IO.accessed), 'IO.accessed should NOT be updated when file is opened for reading';
  55
+
  56
+    # reading contents of file 
  57
+    slurp $tmpfile1;
  58
+    ok ($original1_accessed < $tmpfile1.IO.accessed), 'IO.accessed should be updated when contents of file is read';
  59
+
  60
+
  61
+    # changing file permissions
  62
+
  63
+    $tmpfile2.IO.chmod(0o000);
  64
+    my $post_chmod_modified = $tmpfile2.IO.modified;
  65
+    my $post_chmod_changed = $tmpfile2.IO.changed;
  66
+
  67
+    ok ($original2_changed == $post_chmod_modified), 'IO.modified should NOT be updated when file mode is altered';
  68
+    ok ($original2_changed  < $post_chmod_changed),  'IO.changed should be updated when file mode is altered';
  69
+    ok ($post_chmod_modified != $post_chmod_changed),  'IO.changed and IO.modified should differ after file mode change';
  70
+
  71
+
  72
+    # accessing file
  73
+
  74
+    remove_file $tmpfile1;
  75
+    remove_file $tmpfile2;
  76
+}
  77
+
  78
+
  79
+sub create_temporary_file($id) {
  80
+    my $time = time;
  81
+    my $file = "temp-16-filehandles-filestat-" ~ $*PID ~ "-" ~ $id ~ ".temp";
  82
+    my $fh = open $file, :w orelse die "Could not create $file";   #OK not used
  83
+    $fh.print($time);                            # store pre-creation timestamp
  84
+    diag "Using file $file";
  85
+    return $file;
  86
+}
  87
+
  88
+sub remove_file ($file) {
  89
+    unlink $file;
  90
+    ok($file.IO ~~ :!e, "Test file $file was successfully removed");
  91
+}
  92
+
  93
+# vim: ft=perl6

0 notes on commit a45e1b2

Please sign in to comment.
Something went wrong with that request. Please try again.