aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/entry-time/buildfile8
-rw-r--r--tests/entry-time/driver.cxx151
-rw-r--r--tests/entry-time/testscript63
3 files changed, 222 insertions, 0 deletions
diff --git a/tests/entry-time/buildfile b/tests/entry-time/buildfile
new file mode 100644
index 0000000..7219149
--- /dev/null
+++ b/tests/entry-time/buildfile
@@ -0,0 +1,8 @@
+# file : tests/entry-time/buildfile
+# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+import libs = libbutl%lib{butl}
+libs += $stdmod_lib
+
+exe{driver}: {hxx cxx}{*} $libs test{testscript}
diff --git a/tests/entry-time/driver.cxx b/tests/entry-time/driver.cxx
new file mode 100644
index 0000000..91e9bbc
--- /dev/null
+++ b/tests/entry-time/driver.cxx
@@ -0,0 +1,151 @@
+// file : tests/entry-time/driver.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <cassert>
+
+#ifndef __cpp_lib_modules
+#include <string>
+#include <chrono>
+#include <iostream>
+#endif
+
+// Other includes.
+
+#ifdef __cpp_modules
+#ifdef __cpp_lib_modules
+import std.core;
+import std.io;
+#endif
+import butl.path;
+import butl.filesystem;
+
+import butl.optional; // @@ MOD Clang should not be necessary.
+#else
+#include <libbutl/path.mxx>
+#include <libbutl/optional.mxx>
+#include <libbutl/timestamp.mxx>
+#include <libbutl/filesystem.mxx>
+#endif
+
+using namespace std;
+using namespace butl;
+
+// Usage: argv[0] (-p|-s <time>) (-f|-d) (-m|-a) <path>
+//
+// Prints or sets the modification or access time for the specified filesystem
+// entry.
+//
+// -p
+// Print the filesystem entry time as as a number of milliseconds passed
+// since UNIX epoch.
+//
+// -s
+// Set the filesystem entry time that is represented as a number of
+// milliseconds passed since UNIX epoch.
+//
+// -f
+// The specified path is a file.
+//
+// -d
+// The specified path is a directory.
+//
+// -m
+// Print or set the filesystem entry modification time.
+//
+// -a
+// Print or set the filesystem entry access time.
+//
+int
+main (int argc, const char* argv[])
+{
+ using butl::optional;
+
+ optional<bool> set;
+ optional<bool> dir;
+ optional<bool> mod;
+ optional<path> p;
+ timestamp tm;
+
+ assert (argc > 0);
+
+ for (int i (1); i != argc; ++i)
+ {
+ string v (argv[i]);
+
+ if (v == "-p")
+ {
+ assert (!set);
+ set = false;
+ }
+ else if (v == "-s")
+ {
+ assert (!set);
+ set = true;
+
+ assert (i + 1 != argc);
+ tm = timestamp (chrono::duration_cast<duration> (
+ chrono::milliseconds (stoull (argv[++i]))));
+ }
+ else if (v == "-f")
+ {
+ assert (!dir);
+ dir = false;
+ }
+ else if (v == "-d")
+ {
+ assert (!dir);
+ dir = true;
+ }
+ else if (v == "-m")
+ {
+ assert (!mod);
+ mod = true;
+ }
+ else if (v == "-a")
+ {
+ assert (!mod);
+ mod = false;
+ }
+ else
+ {
+ assert (set && dir && mod && !p);
+ p = path (move (v));
+ }
+ }
+
+ assert (set);
+ assert (dir);
+ assert (mod);
+ assert (p);
+
+ if (*set)
+ {
+ if (*dir)
+ {
+ if (*mod)
+ dir_mtime (path_cast<dir_path> (*p), tm);
+ else
+ dir_atime (path_cast<dir_path> (*p), tm);
+ }
+ else
+ {
+ if (*mod)
+ file_mtime (*p, tm);
+ else
+ file_atime (*p, tm);
+ }
+ }
+ else
+ {
+ if (*dir)
+ tm = *mod
+ ? dir_mtime (path_cast<dir_path> (*p))
+ : dir_atime (path_cast<dir_path> (*p));
+ else
+ tm = *mod ? file_mtime (*p) : file_atime (*p);
+
+ cout << chrono::duration_cast<chrono::milliseconds> (
+ tm.time_since_epoch ()).count () << endl;
+ }
+}
diff --git a/tests/entry-time/testscript b/tests/entry-time/testscript
new file mode 100644
index 0000000..933ae3a
--- /dev/null
+++ b/tests/entry-time/testscript
@@ -0,0 +1,63 @@
+# file : tests/entry-time/testscript
+# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+# Filesystem entry times resolution may vary across OSes and underlying APIs,
+# so the timestamp set for the enty may differ a bit from the one we get for
+# it afterwards. Thus we will compare with the milliseconds resolution which
+# seems to be reasonable to expect being supported on the platforms we are
+# testing on.
+#
+t = 1521456316789 # Some time point since epoch expressed in milliseconds.
+
+: modification
+:
+{
+ test.options += -m
+
+ : dir
+ :
+ {
+ test.options += -d;
+
+ mkdir d;
+ $* -s $t d;
+ $* -p d >"$t"
+ }
+
+ : file
+ :
+ {
+ test.options += -f;
+
+ touch f;
+ $* -s $t f;
+ $* -p f >"$t"
+ }
+}
+
+: access
+:
+{
+ test.options += -a
+
+ : dir
+ :
+ {
+ test.options += -d;
+
+ mkdir d;
+ $* -s $t d;
+ $* -p d >"$t"
+ }
+
+ : file
+ :
+ {
+ test.options += -f;
+
+ touch f;
+ $* -s $t f;
+ $* -p f >"$t"
+ }
+}