From 60c51eb9a350a24ee09b69d95107230020c960b5 Mon Sep 17 00:00:00 2001
From: Thomas Goyne <plorkyeran@aegisub.org>
Date: Mon, 21 Jul 2014 09:34:33 -0700
Subject: [PATCH] Ensure the lua state gets closed when running tests

This is required for the gc metamethods to actually get run.
---
 automation/tests/aegisub.cpp | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/automation/tests/aegisub.cpp b/automation/tests/aegisub.cpp
index 197ab0320..ea667cd94 100644
--- a/automation/tests/aegisub.cpp
+++ b/automation/tests/aegisub.cpp
@@ -33,6 +33,12 @@ void check(lua_State *L, int status) {
 		exit(status);
 	}
 }
+
+int close_and_exit(lua_State *L) {
+	int status = lua_tointeger(L, 1);
+	lua_close(L);
+	exit(status);
+}
 }
 
 int main(int argc, char **argv) {
@@ -54,6 +60,13 @@ int main(int argc, char **argv) {
 	preload_modules(L);
 	Install(L, {"include"});
 
+	// Patch os.exit to close the lua state first since busted calls it when
+	// it's done
+	lua_getglobal(L, "os");
+	lua_pushcfunction(L, close_and_exit);
+	lua_setfield(L, -2, "exit");
+	lua_pop(L, 1);
+
 	// Build arg table for scripts
 	lua_createtable(L, argc - 1, 0);
 	for (int i = 1; i < argc; ++i) {
@@ -76,5 +89,6 @@ int main(int argc, char **argv) {
 
 	int base = lua_gettop(L) - argc + 1;
 	check(L, lua_pcall(L, argc - 2, LUA_MULTRET, base));
+	lua_close(L);
 }
 
-- 
GitLab