spiegel-keyman/core/tests/unit/emscripten_filesystem.cpp
Marc Durdin d71cb56ca7 fix(core): rename assert() to test_assert() in unit tests
Had a real yak shave this morning with disabling assertions in release
builds in our C/C++ code. It turns out that our unit tests use
`assert()` which we intended to use from `test_assert.h`, but in some
cases `cassert` or `assert.h` had been #included after `test_assert.h`,
overriding our special `assert()` macro. The chain of includes is
somewhat hard to puzzle out -- it's often buried several levels deep.
This meant that a release build would drop all test assertions, meaning
most tests passed, unsurprisingly, as there were no assertions left to
fail ... but some tests failed with crashes because we optimized out
important lines such as `assert(some_important_function())`.

I was quite unhappy with this fragility, so I have opted to rename
`assert()` to `test_assert()` in all of our home-grown C/C++ unit tests,
which further highlighted unit tests which were only using the C/C++
`assert()` and not ours, so then had to figure out which unit test
executables needed to have `test_assert` added, and then ... then ...
discovered a bug in `test_color.h`, where we were #including
`io.h`/`unistd.h` inside a `namespace console_color {}` block, which
just happened to be the first ref to those beautiful headers, and thus
(because `#pragma once`) meant that useful little functions like
`access()` were no longer accessible to us in the global namespace.

I have also audited Every Single Call to `assert()` to verify that we do
not do Important Work inside the parentheses, and, apart from those
offending unit tests, now resolved with `test_assert()`, it looks like
all is good.

I would like to present one very well-shaved yak in this commit.

Fixes: #12619
2024-11-28 09:46:01 +07:00

72 lines
1.8 KiB
C++

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <iostream>
#include <test_assert.h>
const std::string get_wasm_file_path(const std::string& filename) {
// Verify that we are passing a fully-qualified path
// TODO: we need to support relative paths based on CWD
#if _DEBUG_FOPEN
std::cout << "get_wasm_file_path ENTER (" << filename << ")" << std::endl;
#endif
test_assert(
(filename.length() > 0 && filename.at(0) == '/') ||
(filename.length() > 1 && filename.at(1) == ':')
);
#if _DEBUG_FOPEN
std::cout << "get_wasm_file_path test_assert passed " << std::endl;
#endif
EM_ASM_({
//console.log('EM_ASM enter');
let path = Module.UTF8ToString($0);
const isWin32Path = path.match(/^[a-zA-Z]:/);
//console.log('EM_ASM path = '+path);
let root = '/nodefs-mount';
if(!FS.analyzePath(root).exists) {
//console.log('EM_ASM mkdir '+root);
FS.mkdir(root);
if(!isWin32Path) {
//console.log('EM_ASM mount '+root);
FS.mount(NODEFS, {root : '/'}, root);
}
}
if(isWin32Path) {
// Win32 path, one mount per drive
root += "/" + path.charAt(0);
if(!FS.analyzePath(root).exists) {
//console.log('EM_ASM mkdir '+root);
FS.mkdir(root);
//console.log('EM_ASM mount '+root);
FS.mount(NODEFS, {root : path.substr(0,3) }, root);
}
}
}, filename.c_str());
#if _DEBUG_FOPEN
std::cout << "get_wasm_file_path EM_ASM_ passed " << std::endl;
#endif
std::string f = std::string("/nodefs-mount");
if(filename.length() > 2 && filename.at(1) == ':') {
f += std::string("/") + filename.at(0) + filename.substr(2);
} else {
f += filename;
}
#if _DEBUG_FOPEN
std::cout << "get_wasm_file_path opening virtual path: " << f << std::endl;
#endif
return f;
}
#endif