test-utils.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2025 Ivan Baidakou
  3. #pragma once
  4. #include <catch2/catch_all.hpp>
  5. #include <filesystem>
  6. #include "model/device.h"
  7. #include "model/diff/apply_controller.h"
  8. #include "model/diff/cluster_diff.h"
  9. #include "syncspirit-test-export.h"
  10. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
  11. #define SYNCSPIRIT_WIN
  12. #endif
  13. namespace syncspirit::test {
  14. namespace bfs = std::filesystem;
  15. namespace sys = boost::system;
  16. struct SYNCSPIRIT_TEST_API path_guard_t {
  17. bfs::path &path;
  18. path_guard_t(bfs::path &path_) : path{path_} {}
  19. ~path_guard_t() {
  20. if (!getenv("SYNCSPIRIT_TEST_KEEP_PATH")) {
  21. sys::error_code ec;
  22. if (bfs::exists(path, ec)) {
  23. bfs::permissions(path, bfs::perms::owner_all, ec);
  24. if (ec) {
  25. printf("error setting permissions : %s: %s\n", path.string().c_str(), ec.message().c_str());
  26. }
  27. }
  28. bfs::remove_all(path, ec);
  29. if (ec) {
  30. printf("error removing %s : %s\n", path.string().c_str(), ec.message().c_str());
  31. }
  32. }
  33. }
  34. };
  35. SYNCSPIRIT_TEST_API bfs::path unique_path();
  36. SYNCSPIRIT_TEST_API model::diff::apply_controller_t &get_apply_controller();
  37. SYNCSPIRIT_TEST_API void init_logging();
  38. SYNCSPIRIT_TEST_API bfs::path locate_path(const char *test_file);
  39. SYNCSPIRIT_TEST_API std::string read_file(const bfs::path &path);
  40. SYNCSPIRIT_TEST_API void write_file(const bfs::path &path, std::string_view content);
  41. SYNCSPIRIT_TEST_API std::string device_id2sha256(std::string_view device_id);
  42. SYNCSPIRIT_TEST_API model::device_ptr_t make_device(std::string_view device_id, std::string_view name = "");
  43. SYNCSPIRIT_TEST_API std::string hash_string(const std::string_view &hash) noexcept;
  44. } // namespace syncspirit::test