// Copyright Epic Games, Inc. All Rights Reserved. #include "RunLuaFrameworkTestsCommandlet.h" #include "UnrealLuaDemoEditor.h" #include "Core/LuaGameInstance.h" #include "Async/TaskGraphInterfaces.h" #include "Containers/Ticker.h" #include "Engine/Engine.h" #include "Engine/World.h" #include "GameFramework/WorldSettings.h" #include "Tickable.h" #include "UObject/UObjectGlobals.h" #include "UnLua.h" #include "UnLuaModule.h" #include "LuaEnv.h" #include "lua.hpp" namespace { UWorld* GFrameworkTestWorld = nullptr; int LuaFrameworkTestTick(lua_State* L) { const float DeltaSeconds = lua_gettop(L) >= 1 ? static_cast(luaL_checknumber(L, 1)) : 0.016f; ++GFrameCounter; if (IsAsyncLoading()) { FPlatformProcess::Sleep(0.005f); } FTaskGraphInterface::Get().ProcessThreadUntilIdle(ENamedThreads::GameThread); ProcessAsyncLoading(false, false, 0.0f); FTSTicker::GetCoreTicker().Tick(DeltaSeconds); FTickableGameObject::TickObjects(nullptr, LEVELTICK_All, false, DeltaSeconds); if (GFrameworkTestWorld && IsValid(GFrameworkTestWorld)) { GFrameworkTestWorld->Tick(LEVELTICK_All, DeltaSeconds); } return 0; } int LuaFrameworkTestCollectGarbage(lua_State* L) { lua_gc(L, LUA_GCCOLLECT, 0); CollectGarbage(GARBAGE_COLLECTION_KEEPFLAGS, true); return 0; } } URunLuaFrameworkTestsCommandlet::URunLuaFrameworkTestsCommandlet() { IsClient = true; IsEditor = true; IsServer = false; LogToConsole = true; ShowErrorCount = true; } int32 URunLuaFrameworkTestsCommandlet::Main(const FString& Params) { UE_LOG(LogUnrealLuaDemoEditor, Display, TEXT("[RunLuaFrameworkTests] Starting UE 5.8 + UnLua framework test runner...")); IUnLuaModule& UnLuaModule = IUnLuaModule::Get(); const bool bWasActive = UnLuaModule.IsActive(); if (!bWasActive) { UnLuaModule.SetActive(true); } UWorld* TestWorld = UWorld::CreateWorld(EWorldType::Game, false, TEXT("LuaFrameworkTestWorld")); if (!TestWorld) { UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[RunLuaFrameworkTests] Failed to create UWorld.")); if (!bWasActive) { UnLuaModule.SetActive(false); } return 1; } ULuaGameInstance* TestGameInstance = NewObject(GEngine); TestGameInstance->AddToRoot(); FWorldContext& WorldContext = GEngine->CreateNewWorldContext(EWorldType::Game); WorldContext.OwningGameInstance = TestGameInstance; WorldContext.SetCurrentWorld(TestWorld); TestWorld->SetGameInstance(TestGameInstance); TestGameInstance->Init(); TestWorld->InitializeActorsForPlay(FURL()); if (AWorldSettings* WorldSettings = TestWorld->GetWorldSettings()) { WorldSettings->NotifyBeginPlay(); WorldSettings->NotifyMatchStarted(); } TestWorld->BeginPlay(); GFrameworkTestWorld = TestWorld; int32 ExitCode = 1; UnLua::FLuaEnv* Env = UnLuaModule.GetEnv(TestGameInstance); if (!Env) { Env = UnLuaModule.GetEnv(TestWorld); } if (!Env) { TMap& AllEnvs = UnLua::FLuaEnv::GetAll(); for (const TPair& Pair : AllEnvs) { if (Pair.Value) { Env = Pair.Value; break; } } } if (!Env) { UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[RunLuaFrameworkTests] Failed to obtain UnLua FLuaEnv.")); } else if (lua_State* L = Env->GetMainState()) { UnLua::PushUObject(L, TestGameInstance); lua_setglobal(L, "__UE_TEST_GAME_INSTANCE"); UnLua::PushUObject(L, TestWorld); lua_setglobal(L, "__UE_TEST_WORLD"); lua_pushcfunction(L, &LuaFrameworkTestTick); lua_setglobal(L, "__UE_TEST_TICK"); lua_pushcfunction(L, &LuaFrameworkTestCollectGarbage); lua_setglobal(L, "__UE_TEST_GC"); const FString RunChunk = TEXT( "local suite = __MODULE__.Import('tests.ue.run_ue_tests')\n" "local runner = __MODULE__.Import('tests.harness.test_runner')\n" "local summary = suite.RunAllUETests(_G.__UE_TEST_GAME_INSTANCE, _G.__UE_TEST_WORLD)\n" "_G.__UE_TEST_TOTAL = summary.total or 0\n" "_G.__UE_TEST_PASSED = summary.passed or 0\n" "_G.__UE_TEST_FAILED = summary.failed or 0\n" "_G.__UE_TEST_REPORT = runner.FormatReport(summary)\n" ); const bool bExecOk = Env->DoString(RunChunk, TEXT("RunLuaFrameworkTestsCommandlet")); lua_getglobal(L, "__UE_TEST_REPORT"); const char* ReportUtf8 = lua_tostring(L, -1); const FString ReportText = ReportUtf8 ? UTF8_TO_TCHAR(ReportUtf8) : FString(); lua_pop(L, 1); if (!ReportText.IsEmpty()) { TArray Lines; ReportText.ParseIntoArrayLines(Lines, false); for (const FString& Line : Lines) { UE_LOG(LogUnrealLuaDemoEditor, Display, TEXT("%s"), *Line); } } lua_getglobal(L, "__UE_TEST_TOTAL"); const int32 Total = static_cast(lua_tointeger(L, -1)); lua_pop(L, 1); lua_getglobal(L, "__UE_TEST_PASSED"); const int32 Passed = static_cast(lua_tointeger(L, -1)); lua_pop(L, 1); lua_getglobal(L, "__UE_TEST_FAILED"); const int32 Failed = static_cast(lua_tointeger(L, -1)); lua_pop(L, 1); lua_pushnil(L); lua_setglobal(L, "__UE_TEST_GAME_INSTANCE"); lua_pushnil(L); lua_setglobal(L, "__UE_TEST_WORLD"); lua_pushnil(L); lua_setglobal(L, "__UE_TEST_TICK"); lua_pushnil(L); lua_setglobal(L, "__UE_TEST_GC"); lua_pushnil(L); lua_setglobal(L, "__UE_TEST_REPORT"); if (!bExecOk) { UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[RunLuaFrameworkTests] Lua execution error in tests.ue.run_ue_tests.")); ExitCode = 1; } else if (Total <= 0 || Failed > 0) { UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[RunLuaFrameworkTests] FAILED (Total=%d, Passed=%d, Failed=%d)."), Total, Passed, Failed); ExitCode = 1; } else { UE_LOG(LogUnrealLuaDemoEditor, Display, TEXT("[RunLuaFrameworkTests] PASSED (Total=%d, Passed=%d, Failed=%d)."), Total, Passed, Failed); ExitCode = 0; } } GFrameworkTestWorld = nullptr; TestWorld->EndPlay(EEndPlayReason::Quit); TestGameInstance->Shutdown(); TestGameInstance->RemoveFromRoot(); GEngine->DestroyWorldContext(TestWorld); TestWorld->DestroyWorld(false); if (!bWasActive) { UnLuaModule.SetActive(false); } return ExitCode; }