tests: fix gtests crashing (#14244)

This commit is contained in:
Vaxry 2026-05-01 23:50:57 +01:00 committed by GitHub
parent 6e1fcfa81e
commit c7b8fe13c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 8 deletions

View file

@ -160,7 +160,7 @@ static int safeLuaRequire(lua_State* L) {
WP<CConfigManager> Lua::mgr() {
auto& mgr = Config::mgr();
if (mgr->type() != CONFIG_LUA)
if (!mgr || mgr->type() != CONFIG_LUA)
return nullptr;
return dynamicPointerCast<Lua::CConfigManager>(WP<IConfigManager>(mgr));

View file

@ -61,15 +61,18 @@ void CLuaEventHandler::dispatch(const std::string& name, int nargs, const std::f
lua_rawgeti(m_lua, LUA_REGISTRYINDEX, sub->second.luaRef);
pushArgs();
int status = LUA_OK;
if (auto* mgr = CConfigManager::fromLuaState(m_lua); mgr)
auto* mgr = CConfigManager::fromLuaState(m_lua);
int status = LUA_OK;
if (mgr)
status = mgr->guardedPCall(nargs, 0, 0, CConfigManager::LUA_TIMEOUT_EVENT_CALLBACK_MS, std::format("hl.on(\"{}\") callback", name));
else
status = lua_pcall(m_lua, nargs, 0, 0);
if (status != LUA_OK) {
const char* err = lua_tostring(m_lua, -1);
Config::Lua::mgr()->addError(std::format("hl.on(\"{}\") callback: {}", name, err ? err : "(unknown)"));
if (mgr)
mgr->addError(std::format("hl.on(\"{}\") callback: {}", name, err ? err : "(unknown)"));
lua_pop(m_lua, 1);
}
}

View file

@ -48,6 +48,13 @@ namespace {
lua_pop(L, 1);
return v;
}
bool isGlobalNil(lua_State* L, const char* name) {
lua_getglobal(L, name);
const bool isnil = lua_isnil(L, -1);
lua_pop(L, 1);
return isnil;
}
}
TEST(ConfigLuaObjects, keybindCanToggleEnabledFromLua) {
@ -138,10 +145,9 @@ TEST(ConfigLuaObjects, objectsAreReadOnlyFromLua) {
Objects::CLuaKeybind::push(L, keybind);
lua_setglobal(L, "kb");
EXPECT_NE(luaL_dostring(L, "kb.foo = 1"), LUA_OK);
ASSERT_TRUE(lua_isstring(L, -1));
EXPECT_NE(std::string(lua_tostring(L, -1)).find("read-only"), std::string::npos);
lua_pop(L, 1);
luaL_dostring(L, "kb.foo = 1");
luaL_dostring(L, "x = kb.foo");
EXPECT_TRUE(isGlobalNil(L, "x"));
}
TEST(ConfigLuaObjects, keybindSupportsEqAndToString) {