只接 std::exception 的话,第三方库或跨 FFI 边界抛出来的东西照样会逃出 main 走 abort, 和上一个 commit 修掉的是同一类故障:ctest 看不到原因,栈也不展开。引擎内现有的抛出确实 都是 std::exception 派生,但这一层缺的是「无论什么异常都到不了 terminate」的保证,补上它 才让 main 的边界真正闭合。 只加 catch 分支,输出通道沿用各文件原来那一个(fprintf / cerr / fmt::println), 不改任何断言。
61 lines
1.6 KiB
C++
61 lines
1.6 KiB
C++
#include "../../core/data_path.h"
|
|
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <stdexcept>
|
|
#include <cstdio>
|
|
|
|
namespace
|
|
{
|
|
void set_data_directory(const std::filesystem::path &path)
|
|
{
|
|
#ifdef _WIN32
|
|
if (_wputenv_s(L"METASEQUOIA_IME_DATA_DIR", path.c_str()) != 0)
|
|
#else
|
|
if (setenv("METASEQUOIA_IME_DATA_DIR", metasequoia::path_to_utf8(path).c_str(), 1) != 0)
|
|
#endif
|
|
{
|
|
throw std::runtime_error("Failed to set the data directory override.");
|
|
}
|
|
}
|
|
} // namespace
|
|
|
|
int run_test()
|
|
{
|
|
const std::filesystem::path expected =
|
|
std::filesystem::temp_directory_path() / std::filesystem::u8path("metasequoia-ime-词库");
|
|
set_data_directory(expected);
|
|
if (metasequoia::data_directory() != expected)
|
|
{
|
|
throw std::runtime_error("The data directory override was ignored.");
|
|
}
|
|
if (metasequoia::data_file_path("msime.db") != expected / "msime.db")
|
|
{
|
|
throw std::runtime_error("The data file path was not joined with the platform separator.");
|
|
}
|
|
if (metasequoia::path_to_utf8(metasequoia::data_file_path(std::filesystem::u8path("词库.db"))) !=
|
|
metasequoia::path_to_utf8(expected / std::filesystem::u8path("词库.db")))
|
|
{
|
|
throw std::runtime_error("The data file path was not preserved as UTF-8.");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
try
|
|
{
|
|
return run_test();
|
|
}
|
|
catch (const std::exception &error)
|
|
{
|
|
std::fprintf(stderr, "%s\n", error.what());
|
|
return 1;
|
|
}
|
|
catch (...)
|
|
{
|
|
std::fprintf(stderr, "An exception escaped the test body.\n");
|
|
return 1;
|
|
}
|
|
}
|