• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / 2392

06 Jun 2024 05:02PM UTC coverage: 90.987% (+0.1%) from 90.855%
2392

push

Evergreen

web-flow
Merge pull request #7698 from realm/tg/file-map-cache

RCORE-2141 RCORE-2142 Clean up a bunch of old encryption cruft

101862 of 180058 branches covered (56.57%)

1048 of 1098 new or added lines in 27 files covered. (95.45%)

118 existing lines in 19 files now uncovered.

214614 of 235872 relevant lines covered (90.99%)

5960870.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.97
/test/util/test_path.hpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#ifndef REALM_TEST_UTIL_TEST_PATH_HPP
20
#define REALM_TEST_UTIL_TEST_PATH_HPP
21

22
#include <string>
23
#include <memory>
24

25
#include <realm/string_data.hpp>
26
#include <realm/util/features.h>
27

28
#define TEST_PATH_HELPER(class_name, var_name, suffix)                                                               \
29
    class_name var_name(realm::test_util::get_test_path(test_context.get_test_name(), "." #var_name "." suffix))
1,604✔
30

31
#if REALM_PLATFORM_APPLE
32
// Apple doesn't support file names with "�"
33
#define TEST_PATH(var_name) TEST_PATH_HELPER(realm::test_util::TestPathGuard, var_name, "test");
49✔
34
#else
35
#define TEST_PATH(var_name) TEST_PATH_HELPER(realm::test_util::TestPathGuard, var_name, "tempor�re");
49✔
36
#endif
37

38
#define TEST_DIR(var_name) TEST_PATH_HELPER(realm::test_util::TestDirGuard, var_name, "test-dir");
254✔
39

40
#define GROUP_TEST_PATH(var_name) TEST_PATH_HELPER(realm::test_util::TestPathGuard, var_name, "realm");
46✔
41

42
#define SHARED_GROUP_TEST_PATH(var_name) TEST_PATH_HELPER(realm::test_util::DBTestPathGuard, var_name, "realm");
1,206✔
43

44
namespace realm {
45

46
class DB;
47

48
namespace test_util {
49

50
/// Disable automatic removal of test files.
51
///
52
/// This function is **not** thread-safe. If you call it, be sure to call it
53
/// prior to any execution of the TEST_PATH or TEST_DIR family of macros.
54
void keep_test_files();
55

56

57
/// This function is thread-safe as long as there are no concurrent invocations
58
/// of set_test_path_prefix().
59
std::string get_test_path_prefix();
60

61
/// This function is thread-safe as long as there are no concurrent invocations
62
/// of set_test_path_prefix().
63
std::string get_test_path(const std::string& path, const std::string& suffix);
64

65
/// Initialize the test path prefix, resource path, and working directory. This function is not thread-safe and should
66
/// be called exactly once on startup.
67
bool initialize_test_path(int argc, const char* argv[]);
68

69
/// Check if get_test_path_prefix() will give a path located on an exFAT
70
/// filesystem, which does not support all of the features a typical unix
71
/// filesystem does.
72
bool test_dir_is_exfat();
73

74

75
/// This function is thread-safe as long as there are no concurrent invocations
76
/// of set_test_resource_path().
77
std::string get_test_resource_path();
78

79
/// This function is thread-safe as long as there are no concurrent invocations
80
/// of initialize_test_path
81
std::string get_test_exe_name();
82

83
// This is an adapter class which replaces dragging in the whole test framework
84
// by implementing the `get_test_name()` method from the TestContext class.
85
// It allows use of TestPathGuard and friends outside of a unit test:
86
// RealmPathInfo test_context { path };
87
struct RealmPathInfo {
88
    std::string m_path;
89
    std::string get_test_name() const
90
    {
×
91
        return m_path;
×
92
    }
×
93
};
94

95

96
/// Constructor and destructor removes file if it exists.
97
class TestPathGuard {
98
public:
99
    TestPathGuard(const std::string& path);
100
    ~TestPathGuard() noexcept;
101
    operator const std::string&() const noexcept
102
    {
5,656✔
103
        return m_path;
5,656✔
104
    }
5,656✔
105
    operator StringData() const noexcept
NEW
106
    {
×
NEW
107
        return m_path;
×
NEW
108
    }
×
109
    operator std::string_view() const noexcept
110
    {
460✔
111
        return m_path;
460✔
112
    }
460✔
113
    const char* c_str() const noexcept
114
    {
24✔
115
        return m_path.c_str();
24✔
116
    }
24✔
117
    TestPathGuard(const TestPathGuard&) = delete;
118
    TestPathGuard& operator=(const TestPathGuard&) = delete;
119
    TestPathGuard(TestPathGuard&&) noexcept;
120
    TestPathGuard& operator=(TestPathGuard&&) noexcept;
121

122
protected:
123
    std::string m_path;
124
    bool m_do_remove;
125
};
126

127
/// The constructor creates the directory if it does not already exist, then
128
/// removes any files already in it. The destructor removes files in the
129
/// directory, then removes the directory.
130
class TestDirGuard {
131
public:
132
    TestDirGuard(const std::string& path, bool init_clean = true);
133
    ~TestDirGuard() noexcept;
134
    operator std::string() const
135
    {
1,449✔
136
        return m_path;
1,449✔
137
    }
1,449✔
138
    operator StringData() const
139
    {
×
140
        return m_path;
×
141
    }
×
142
    const char* c_str() const
143
    {
22✔
144
        return m_path.c_str();
22✔
145
    }
22✔
146

147
    bool do_remove = true;
148

149
    void clean_dir()
150
    {
20✔
151
        clean_dir(m_path);
20✔
152
    }
20✔
153

154
private:
155
    std::string m_path;
156
    void clean_dir(const std::string& path);
157
};
158

159
class DBTestPathGuard : public TestPathGuard {
160
public:
161
    DBTestPathGuard(const std::string& path);
162
    std::string get_lock_path() const
163
    {
13,620✔
164
        return m_path + ".lock"; // ".management/access_control";
13,620✔
165
    }
13,620✔
166
    ~DBTestPathGuard() noexcept;
167
    DBTestPathGuard(DBTestPathGuard&&) = default;
×
168
    DBTestPathGuard& operator=(DBTestPathGuard&&) = default;
169

170
private:
171
    void cleanup() const noexcept;
172
};
173

174
class TestDirNameGenerator {
175
public:
176
    TestDirNameGenerator(std::string path);
177

178
    std::string next();
179

180
private:
181
    std::string m_path;
182
    std::size_t m_counter = 0;
183
};
184

185
std::shared_ptr<DB> get_test_db(const std::string& path, const char* crypt_key = nullptr);
186

187
} // namespace test_util
188
} // namespace realm
189

190
#endif // REALM_TEST_UTIL_TEST_PATH_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc