MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
Utils.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <cstdlib>
7#include <filesystem>
8#include <fstream>
9#include <sstream>
10#include <string>
12
13#ifdef __APPLE__
14# include <stdexcept>
15# include <unistd.h> // For mkdtemp
16#endif
17
18namespace mxl::tests
19{
20
21 std::string readFile(std::filesystem::path const& filepath);
22
29 std::filesystem::path getDomainPath();
30
31 // Helper to make a unique temp domain
32 std::filesystem::path makeTempDomain();
33
34 //
35 // RAII helper to prepare and cleanup a domain for the duration of a test
36 //
38 {
39 public:
43 {
45 std::filesystem::create_directories(domain);
46 }
47
53
54 protected:
56 std::filesystem::path domain;
57
58 [[nodiscard]]
59 bool flowDirectoryExists(std::string const& id) const
60 {
61 return std::filesystem::exists(lib::makeFlowDirectoryName(domain, id));
62 }
63
64 private:
67 {
68 if (std::filesystem::exists(domain))
69 {
70 std::filesystem::remove_all(domain);
71 }
72 }
73 };
74
75 /**************************************************************************/
76 /* Inline implementation. */
77 /**************************************************************************/
78 inline std::string readFile(std::filesystem::path const& filepath)
79 {
80 if (auto file = std::ifstream{filepath, std::ios::in | std::ios::binary}; file)
81 {
82 auto reader = std::stringstream{};
83 reader << file.rdbuf();
84 return reader.str();
85 }
86
87 throw std::runtime_error("Failed to open file: " + filepath.string());
88 }
89
90 inline std::filesystem::path getDomainPath()
91 {
92#ifdef __linux__
93 return std::filesystem::path{"/dev/shm/mxl_domain"};
94#elif __APPLE__
95 if (auto const home = std::getenv("HOME"); home != nullptr)
96 {
97 return std::filesystem::path{home} / "mxl_domain";
98 }
99 else
100 {
101 throw std::runtime_error{"Environment variable HOME is not set."};
102 }
103#else
104# error "Unsupported platform. This is only implemented for Linux and macOS."
105#endif
106 }
107
108 inline std::filesystem::path makeTempDomain()
109 {
110#ifdef __linux__
111 char tmpl[] = "/dev/shm/mxl_test_domainXXXXXX";
112#elif __APPLE__
113 char tmpl[] = "/tmp/mxl_test_domainXXXXXX";
114#else
115# error "Unsupported platform. This is only implemented for Linux and macOS."
116#endif
117 if (::mkdtemp(tmpl) == nullptr)
118 {
119 throw std::runtime_error("Failed to create temporary directory");
120 }
121 return std::filesystem::path{tmpl};
122 }
123
124} // namespace mxl::tests
Paths and filenames utilities used by the MXL SDK.
std::filesystem::path domain
The path to the domain.
Definition Utils.hpp:56
mxlDomainFixture()
Create the fixture. Will delete the domain if it exists and create a new one.
Definition Utils.hpp:41
bool flowDirectoryExists(std::string const &id) const
Definition Utils.hpp:59
void removeDomain()
Remove the domain folder if it exists.
Definition Utils.hpp:66
~mxlDomainFixture()
Delete the domain folder.
Definition Utils.hpp:49
std::filesystem::path makeFlowDirectoryName(std::filesystem::path const &domain, std::string const &uuid)
Definition PathUtils.cpp:11
std::filesystem::path makeTempDomain()
Definition Utils.hpp:108
std::filesystem::path getDomainPath()
Definition Utils.hpp:90
std::string readFile(std::filesystem::path const &filepath)
Definition Utils.hpp:78
mxlFlowReader reader