summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibTest/TestRunnerUtil.h
diff options
context:
space:
mode:
authordavidot <davidot@serenityos.org>2022-09-09 23:52:21 +0200
committerLinus Groh <mail@linusgroh.de>2022-09-11 20:25:51 +0100
commitb6094b7a3de5ff0364c196485b4dc0c1f6b1f6ec (patch)
treed36da4e22e1008e500fe4ac7c3961d3ce6cf7a09 /Userland/Libraries/LibTest/TestRunnerUtil.h
parent6b437c576f1c1c1ef62a462c130a5c02b3d4df6e (diff)
downloadserenity-b6094b7a3de5ff0364c196485b4dc0c1f6b1f6ec.zip
LibTest: Extract some useful functions from TestRunner.h
This allows other test like programs to use these without needing to link to LibTest.
Diffstat (limited to 'Userland/Libraries/LibTest/TestRunnerUtil.h')
-rw-r--r--Userland/Libraries/LibTest/TestRunnerUtil.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/Userland/Libraries/LibTest/TestRunnerUtil.h b/Userland/Libraries/LibTest/TestRunnerUtil.h
new file mode 100644
index 0000000000..7f4eed89c4
--- /dev/null
+++ b/Userland/Libraries/LibTest/TestRunnerUtil.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2022, the SerenityOS developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibCore/DirIterator.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+
+namespace Test {
+
+inline double get_time_in_ms()
+{
+ struct timeval tv1;
+ auto return_code = gettimeofday(&tv1, nullptr);
+ VERIFY(return_code >= 0);
+ return static_cast<double>(tv1.tv_sec) * 1000.0 + static_cast<double>(tv1.tv_usec) / 1000.0;
+}
+
+template<typename Callback>
+inline void iterate_directory_recursively(String const& directory_path, Callback callback)
+{
+ Core::DirIterator directory_iterator(directory_path, Core::DirIterator::Flags::SkipDots);
+
+ while (directory_iterator.has_next()) {
+ auto name = directory_iterator.next_path();
+ struct stat st = {};
+ if (fstatat(directory_iterator.fd(), name.characters(), &st, AT_SYMLINK_NOFOLLOW) < 0)
+ continue;
+ bool is_directory = S_ISDIR(st.st_mode);
+ auto full_path = String::formatted("{}/{}", directory_path, name);
+ if (is_directory && name != "/Fixtures"sv) {
+ iterate_directory_recursively(full_path, callback);
+ } else if (!is_directory) {
+ callback(full_path);
+ }
+ }
+}
+
+}