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

realm / realm-core / github_pull_request_312964

19 Feb 2025 07:31PM UTC coverage: 90.814% (-0.3%) from 91.119%
github_pull_request_312964

Pull #8071

Evergreen

web-flow
Bump serialize-javascript and mocha

Bumps [serialize-javascript](https://github.com/yahoo/serialize-javascript) to 6.0.2 and updates ancestor dependency [mocha](https://github.com/mochajs/mocha). These dependencies need to be updated together.


Updates `serialize-javascript` from 6.0.0 to 6.0.2
- [Release notes](https://github.com/yahoo/serialize-javascript/releases)
- [Commits](https://github.com/yahoo/serialize-javascript/compare/v6.0.0...v6.0.2)

Updates `mocha` from 10.2.0 to 10.8.2
- [Release notes](https://github.com/mochajs/mocha/releases)
- [Changelog](https://github.com/mochajs/mocha/blob/main/CHANGELOG.md)
- [Commits](https://github.com/mochajs/mocha/compare/v10.2.0...v10.8.2)

---
updated-dependencies:
- dependency-name: serialize-javascript
  dependency-type: indirect
- dependency-name: mocha
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #8071: Bump serialize-javascript and mocha

96552 of 179126 branches covered (53.9%)

212672 of 234185 relevant lines covered (90.81%)

3115802.0 hits per line

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

74.67
/test/util/test_path.cpp
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
#include "test_path.hpp"
20

21
#include "misc.hpp"
22
#include "spawned_process.hpp"
23

24
#include <realm/util/file.hpp>
25
#include <realm/db.hpp>
26
#include <realm/history.hpp>
27

28
#include <algorithm>
29
#include <string>
30

31
#if REALM_PLATFORM_APPLE
32
#include <realm/util/cf_ptr.hpp>
33

34
#include <CoreFoundation/CoreFoundation.h>
35
#include <sys/mount.h>
36
#include <sys/param.h>
37
#elif defined(_WIN32)
38
#include <Windows.h>
39
// PathCchRemoveFileSpec()
40
#include <pathcch.h>
41
#pragma comment(lib, "Pathcch.lib")
42
#else
43
#include <unistd.h>
44
#include <libgen.h>
45
#endif
46

47
using namespace realm::util;
48

49
namespace {
50

51
bool g_keep_files = false;
52

53
std::string g_path_prefix;
54
std::string g_resource_path;
55
std::string g_exe_name;
56

57
#ifdef _WIN32
58
std::string sanitize_for_file_name(std::string str)
59
{
60
    static const std::string invalid("<>:\"|?*\\/");
61
    std::transform(str.begin(), str.end(), str.begin(), [](char c) {
62
        if (invalid.find(c) != std::string::npos)
63
            return '-';
64
        return c;
65
    });
66
    return str;
67
}
68
#else
69
std::string sanitize_for_file_name(const std::string& str)
70
{
4,017✔
71
    return str;
4,017✔
72
}
4,017✔
73
#endif
74

75
#if REALM_PLATFORM_APPLE
76
std::string url_to_path(CFURLRef url)
77
{
78
    auto absolute = adoptCF(CFURLCopyAbsoluteURL(url));
79
    auto path = adoptCF(CFURLCopyPath(absolute.get()));
80
    auto length = CFStringGetLength(path.get());
81
    std::string ret;
82
    ret.resize(CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8));
83
    CFIndex bytes_written;
84
    CFStringGetBytes(path.get(), {0, length}, kCFStringEncodingUTF8, 0, false, reinterpret_cast<uint8_t*>(ret.data()),
85
                     ret.size(), &bytes_written);
86
    REALM_ASSERT(bytes_written);
87
    ret.resize(bytes_written);
88
    return ret;
89
}
90
#endif
91

92
} // anonymous namespace
93

94
namespace realm::test_util {
95

96
void keep_test_files()
97
{
×
98
    g_keep_files = true;
×
99
}
×
100

101
std::string get_test_path(const std::string& test_name, const std::string& suffix)
102
{
4,017✔
103
    return g_path_prefix + sanitize_for_file_name(test_name) + suffix;
4,017✔
104
}
4,017✔
105

106
std::string get_test_path_prefix()
107
{
402✔
108
    return g_path_prefix;
402✔
109
}
402✔
110

111
bool initialize_test_path(int argc, const char* argv[])
112
{
12✔
113
#if REALM_PLATFORM_APPLE
114
    // On Apple platforms we copy everything into a read-only bundle containing
115
    // the test executable and resource files, and have to create test files in
116
    // a temporary directory.
117
#if REALM_APPLE_DEVICE || TARGET_OS_SIMULATOR
118
    auto home = adoptCF(CFCopyHomeDirectoryURL());
119
    g_path_prefix = url_to_path(home.get()) + "Documents/";
120
#else
121
    g_path_prefix = util::make_temp_dir() + "/";
122
#endif
123

124
    auto resources_url = adoptCF(CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()));
125
    g_resource_path = url_to_path(resources_url.get());
126

127
    // On other platforms we can write to the executable's directory, so we use
128
    // that as the base path.
129
#elif defined(_MSC_VER)
130
    wchar_t path[MAX_PATH];
131
    if (GetModuleFileName(NULL, path, MAX_PATH) == 0) {
132
        fprintf(stderr, "Failed to retrieve path to exectuable.\n");
133
        return false;
134
    }
135
    PathCchRemoveFileSpec(path, MAX_PATH);
136
    SetCurrentDirectory(path);
137
    g_path_prefix = std::string(reinterpret_cast<const char*>(std::filesystem::path(path).u8string().c_str()));
138
    g_resource_path = g_path_prefix + "\\resources\\";
139
#else
140
    char executable[PATH_MAX];
12✔
141
    if (realpath(argv[0], executable) == nullptr) {
12✔
142
        fprintf(stderr, "Failed to retrieve path to exectuable.\n");
×
143
        return false;
×
144
    }
×
145
    const char* directory = dirname(executable);
12✔
146
    if (chdir(directory) < 0) {
12✔
147
        fprintf(stderr, "Failed to change directory.\n");
×
148
        return false;
×
149
    }
×
150
    g_resource_path = File::resolve("resources", directory) + "/";
12✔
151
    g_path_prefix = std::string(directory) + "/";
12✔
152
#endif
12✔
153

154
    if (argc > 0) {
12✔
155
        g_exe_name = argv[0];
12✔
156
    }
12✔
157

158
    if (argc > 1) {
12✔
159
        g_path_prefix = argv[1];
×
160
    }
×
161

162
    return true;
12✔
163
}
12✔
164

165
bool test_dir_is_exfat()
166
{
3✔
167
#if REALM_PLATFORM_APPLE
168
    if (test_util::get_test_path_prefix().empty())
169
        return false;
170

171
    struct statfs fsbuf;
172
    int ret = statfs(test_util::get_test_path_prefix().c_str(), &fsbuf);
173
    REALM_ASSERT_RELEASE(ret == 0);
174
    // The documentation and headers helpfully don't list any of the values of
175
    // f_type or provide constants for them
176
    std::string fs_typename = fsbuf.f_fstypename;
177
    std::transform(fs_typename.begin(), fs_typename.end(), fs_typename.begin(), toLowerAscii);
178
    return fs_typename.find(std::string("exfat")) != std::string::npos ||
179
           fs_typename.find(std::string("msdos")) != std::string::npos;
180
#else
181
    return false;
3✔
182
#endif
3✔
183
}
3✔
184

185
std::string get_test_resource_path()
186
{
1,785✔
187
    return g_resource_path;
1,785✔
188
}
1,785✔
189

190
std::string get_test_exe_name()
191
{
369✔
192
    return g_exe_name;
369✔
193
}
369✔
194

195
TestPathGuard::TestPathGuard(const std::string& path)
196
    : m_path(path)
3,630✔
197
    , m_do_remove(test_util::SpawnedProcess::is_parent())
3,630✔
198
{
3,630✔
199
    if (m_do_remove) {
3,630✔
200
        File::try_remove(m_path);
3,630✔
201
    }
3,630✔
202
}
3,630✔
203

204
TestPathGuard::~TestPathGuard() noexcept
205
{
3,630✔
206
    if (g_keep_files)
3,630✔
207
        return;
×
208
    if (!m_do_remove)
3,630✔
209
        return;
×
210
    try {
3,630✔
211
        if (!m_path.empty())
3,630✔
212
            File::try_remove(m_path);
3,630✔
213
    }
3,630✔
214
    catch (...) {
3,630✔
215
        // Exception deliberately ignored
216
    }
×
217
}
3,630✔
218

219
TestPathGuard::TestPathGuard(TestPathGuard&& other) noexcept
220
    : m_path(std::move(other.m_path))
×
221
{
×
222
    other.m_path.clear();
×
223
}
×
224

225
TestPathGuard& TestPathGuard::operator=(TestPathGuard&& other) noexcept
226
{
×
227
    m_path = std::move(other.m_path);
×
228
    other.m_path.clear();
×
229
    return *this;
×
230
}
×
231

232

233
TestDirGuard::TestDirGuard(const std::string& path, bool init_clean)
234
    : m_path(path)
600✔
235
{
600✔
236
    if (!try_make_dir(path)) {
600✔
237
        if (init_clean)
54✔
238
            clean_dir(path);
3✔
239
    }
54✔
240
}
600✔
241

242
TestDirGuard::~TestDirGuard() noexcept
243
{
600✔
244
    if (g_keep_files)
600✔
245
        return;
×
246

247
    if (!do_remove)
600✔
248
        return;
×
249

250
    try {
600✔
251
        clean_dir(m_path);
600✔
252
        remove_dir(m_path);
600✔
253
    }
600✔
254
    catch (...) {
600✔
255
        // Exception deliberately ignored
256
    }
18✔
257
}
600✔
258

259
namespace {
260
void do_clean_dir(const std::string& path, const std::string& guard_string)
261
{
9,009✔
262
    DirScanner ds(path, true);
9,009✔
263
    std::string name;
9,009✔
264
    while (ds.next(name)) {
11,634✔
265
        std::string subpath = File::resolve(name, path);
2,643✔
266
        if (File::is_dir(subpath)) {
2,643✔
267
            do_clean_dir(subpath, guard_string);
1,554✔
268
            remove_dir(subpath);
1,554✔
269
        }
1,554✔
270
        else {
1,089✔
271
            // Try to avoid accidental removal of precious files due to bugs in
272
            // TestDirGuard or TEST_DIR macro.
273
            if (subpath.find(guard_string) == std::string::npos)
1,089✔
274
                throw std::runtime_error("Bad test dir path: " + path + ", guard: " + guard_string);
18✔
275
            File::remove(subpath);
1,071✔
276
        }
1,071✔
277
    }
2,643✔
278
}
9,009✔
279
} // namespace
280

281
void TestDirGuard::clean_dir(const std::string& path)
282
{
633✔
283
    do_clean_dir(path, ".test-dir");
633✔
284
}
633✔
285

286

287
DBTestPathGuard::DBTestPathGuard(const std::string& path)
288
    : TestPathGuard(path)
3,411✔
289
{
3,411✔
290
    cleanup();
3,411✔
291
}
3,411✔
292

293
DBTestPathGuard::~DBTestPathGuard() noexcept
294
{
3,411✔
295
    if (!g_keep_files && !m_path.empty())
3,411✔
296
        cleanup();
3,411✔
297
}
3,411✔
298

299
void DBTestPathGuard::cleanup() const noexcept
300
{
6,822✔
301
    if (!m_do_remove)
6,822✔
302
        return;
×
303
    try {
6,822✔
304
        do_clean_dir(m_path + ".management", ".management");
6,822✔
305
        if (File::is_dir(m_path + ".management"))
6,822✔
306
            remove_dir(m_path + ".management");
3,309✔
307
        File::try_remove(get_lock_path());
6,822✔
308
    }
6,822✔
309
    catch (...) {
6,822✔
310
        // Exception deliberately ignored
311
    }
×
312
}
6,822✔
313

314
TestDirNameGenerator::TestDirNameGenerator(std::string path)
315
    : m_path{std::move(path)}
×
316
{
×
317
}
×
318

319
std::string TestDirNameGenerator::next()
320
{
×
321
    return m_path + "/" + std::to_string(m_counter++);
×
322
}
×
323

324
std::shared_ptr<DB> get_test_db(const std::string& path, const char* crypt_key)
325
{
54✔
326
    const char* str = getenv("UNITTEST_LOG_LEVEL");
54✔
327
    realm::util::Logger::Level core_log_level = realm::util::Logger::Level::off;
54✔
328
    if (str && strlen(str) != 0) {
54!
329
        std::istringstream in(str);
×
330
        in.imbue(std::locale::classic());
×
331
        in.flags(in.flags() & ~std::ios_base::skipws); // Do not accept white space
×
332
        in >> core_log_level;
×
333
    }
×
334

335
    DBOptions options;
54✔
336
    options.logger = std::make_shared<util::StderrLogger>(core_log_level);
54✔
337
    options.encryption_key = crypt_key;
54✔
338
    return DB::create(make_in_realm_history(), path, options);
54✔
339
}
54✔
340

341
} // namespace realm::test_util
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

© 2025 Coveralls, Inc