TestRunnerUtil.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibCore/DirIterator.h>
  8. #include <fcntl.h>
  9. #include <sys/stat.h>
  10. #include <sys/time.h>
  11. namespace Test {
  12. inline double get_time_in_ms()
  13. {
  14. struct timeval tv1;
  15. auto return_code = gettimeofday(&tv1, nullptr);
  16. VERIFY(return_code >= 0);
  17. return static_cast<double>(tv1.tv_sec) * 1000.0 + static_cast<double>(tv1.tv_usec) / 1000.0;
  18. }
  19. template<typename Callback>
  20. inline void iterate_directory_recursively(ByteString const& directory_path, Callback callback)
  21. {
  22. Core::DirIterator directory_iterator(directory_path, Core::DirIterator::Flags::SkipDots);
  23. while (directory_iterator.has_next()) {
  24. auto name = directory_iterator.next_path();
  25. struct stat st = {};
  26. if (fstatat(directory_iterator.fd(), name.characters(), &st, AT_SYMLINK_NOFOLLOW) < 0)
  27. continue;
  28. bool is_directory = S_ISDIR(st.st_mode);
  29. auto full_path = ByteString::formatted("{}/{}", directory_path, name);
  30. if (is_directory && name != "/Fixtures"sv) {
  31. iterate_directory_recursively(full_path, callback);
  32. } else if (!is_directory) {
  33. callback(full_path);
  34. }
  35. }
  36. }
  37. }