spiegel-keyman/core/tests/unit/helpers/emscripten_filesystem.cpp
Marc Durdin d61e5bbb18 refactor(core): clean up unit tests
Consolidate test frameworks and cleanup, including:
* all tests use Google Test
* reorganize folders, esp. kmnkbd -> api
* move all api tests into api folder
* replace references to test_assert or test_color with gtest equivalents
* leverage gtest patterns to remove boilerplate code
* dramatically simplify meson.build files and localize variables
* move shared helper code into helpers/ folder
* make test names unique and add gtest protocol for reporting back to
  teamcity

Fixes:
* CLDR test keyboards had invalid unicodeset escapes (needs to be raised
  upstream also) - was not failing tests because files were not loading
  but returning success

Test-bot: skip
2026-04-28 12:17:44 +02:00

75 lines
1.9 KiB
C++

/*
* Keyman is copyright (C) SIL International. MIT License.
*
* Keyman Core - Helper functions for referencing fixtures in WASM builds
*/
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <iostream>
#include "path.hpp"
bool get_wasm_file_path(const std::string& filename, km::core::path& result) {
// 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
if(!(
(filename.length() > 0 && filename.at(0) == '/') ||
(filename.length() > 1 && filename.at(1) == ':')
)) {
return false;
}
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
result = f;
return true;
}
#endif