gdscript_test_runner.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /**************************************************************************/
  2. /* gdscript_test_runner.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gdscript_test_runner.h"
  31. #include "../gdscript.h"
  32. #include "../gdscript_analyzer.h"
  33. #include "../gdscript_compiler.h"
  34. #include "../gdscript_parser.h"
  35. #include "core/config/project_settings.h"
  36. #include "core/core_globals.h"
  37. #include "core/core_string_names.h"
  38. #include "core/io/dir_access.h"
  39. #include "core/io/file_access_pack.h"
  40. #include "core/os/os.h"
  41. #include "core/string/string_builder.h"
  42. #include "scene/resources/packed_scene.h"
  43. #include "tests/test_macros.h"
  44. namespace GDScriptTests {
  45. void init_autoloads() {
  46. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  47. // First pass, add the constants so they exist before any script is loaded.
  48. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  49. const ProjectSettings::AutoloadInfo &info = E.value;
  50. if (info.is_singleton) {
  51. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  52. ScriptServer::get_language(i)->add_global_constant(info.name, Variant());
  53. }
  54. }
  55. }
  56. // Second pass, load into global constants.
  57. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  58. const ProjectSettings::AutoloadInfo &info = E.value;
  59. if (!info.is_singleton) {
  60. // Skip non-singletons since we don't have a scene tree here anyway.
  61. continue;
  62. }
  63. Node *n = nullptr;
  64. if (ResourceLoader::get_resource_type(info.path) == "PackedScene") {
  65. // Cache the scene reference before loading it (for cyclic references)
  66. Ref<PackedScene> scn;
  67. scn.instantiate();
  68. scn->set_path(info.path);
  69. scn->reload_from_file();
  70. ERR_CONTINUE_MSG(!scn.is_valid(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path));
  71. if (scn.is_valid()) {
  72. n = scn->instantiate();
  73. }
  74. } else {
  75. Ref<Resource> res = ResourceLoader::load(info.path);
  76. ERR_CONTINUE_MSG(res.is_null(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path));
  77. Ref<Script> scr = res;
  78. if (scr.is_valid()) {
  79. StringName ibt = scr->get_instance_base_type();
  80. bool valid_type = ClassDB::is_parent_class(ibt, "Node");
  81. ERR_CONTINUE_MSG(!valid_type, vformat("Failed to instantiate an autoload, script '%s' does not inherit from 'Node'.", info.path));
  82. Object *obj = ClassDB::instantiate(ibt);
  83. ERR_CONTINUE_MSG(!obj, vformat("Failed to instantiate an autoload, cannot instantiate '%s'.", ibt));
  84. n = Object::cast_to<Node>(obj);
  85. n->set_script(scr);
  86. }
  87. }
  88. ERR_CONTINUE_MSG(!n, vformat("Failed to instantiate an autoload, path is not pointing to a scene or a script: %s.", info.path));
  89. n->set_name(info.name);
  90. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  91. ScriptServer::get_language(i)->add_global_constant(info.name, n);
  92. }
  93. }
  94. }
  95. void init_language(const String &p_base_path) {
  96. // Setup project settings since it's needed by the languages to get the global scripts.
  97. // This also sets up the base resource path.
  98. Error err = ProjectSettings::get_singleton()->setup(p_base_path, String(), true);
  99. if (err) {
  100. print_line("Could not load project settings.");
  101. // Keep going since some scripts still work without this.
  102. }
  103. // Initialize the language for the test routine.
  104. GDScriptLanguage::get_singleton()->init();
  105. init_autoloads();
  106. }
  107. void finish_language() {
  108. GDScriptLanguage::get_singleton()->finish();
  109. ScriptServer::global_classes_clear();
  110. }
  111. StringName GDScriptTestRunner::test_function_name;
  112. GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames) {
  113. test_function_name = StaticCString::create("test");
  114. do_init_languages = p_init_language;
  115. print_filenames = p_print_filenames;
  116. source_dir = p_source_dir;
  117. if (!source_dir.ends_with("/")) {
  118. source_dir += "/";
  119. }
  120. if (do_init_languages) {
  121. init_language(p_source_dir);
  122. }
  123. #ifdef DEBUG_ENABLED
  124. // Set all warning levels to "Warn" in order to test them properly, even the ones that default to error.
  125. ProjectSettings::get_singleton()->set_setting("debug/gdscript/warnings/enable", true);
  126. for (int i = 0; i < (int)GDScriptWarning::WARNING_MAX; i++) {
  127. if (i == GDScriptWarning::UNTYPED_DECLARATION || i == GDScriptWarning::INFERRED_DECLARATION) {
  128. // TODO: Add ability for test scripts to specify which warnings to enable/disable for testing.
  129. continue;
  130. }
  131. String warning_setting = GDScriptWarning::get_settings_path_from_code((GDScriptWarning::Code)i);
  132. ProjectSettings::get_singleton()->set_setting(warning_setting, (int)GDScriptWarning::WARN);
  133. }
  134. #endif
  135. // Enable printing to show results
  136. CoreGlobals::print_line_enabled = true;
  137. CoreGlobals::print_error_enabled = true;
  138. }
  139. GDScriptTestRunner::~GDScriptTestRunner() {
  140. test_function_name = StringName();
  141. if (do_init_languages) {
  142. finish_language();
  143. }
  144. }
  145. #ifndef DEBUG_ENABLED
  146. static String strip_warnings(const String &p_expected) {
  147. // On release builds we don't have warnings. Here we remove them from the output before comparison
  148. // so it doesn't fail just because of difference in warnings.
  149. String expected_no_warnings;
  150. for (String line : p_expected.split("\n")) {
  151. if (line.begins_with(">> ")) {
  152. continue;
  153. }
  154. expected_no_warnings += line + "\n";
  155. }
  156. return expected_no_warnings.strip_edges() + "\n";
  157. }
  158. #endif
  159. int GDScriptTestRunner::run_tests() {
  160. if (!make_tests()) {
  161. FAIL("An error occurred while making the tests.");
  162. return -1;
  163. }
  164. if (!generate_class_index()) {
  165. FAIL("An error occurred while generating class index.");
  166. return -1;
  167. }
  168. int failed = 0;
  169. for (int i = 0; i < tests.size(); i++) {
  170. GDScriptTest test = tests[i];
  171. if (print_filenames) {
  172. print_line(test.get_source_relative_filepath());
  173. }
  174. GDScriptTest::TestResult result = test.run_test();
  175. String expected = FileAccess::get_file_as_string(test.get_output_file());
  176. #ifndef DEBUG_ENABLED
  177. expected = strip_warnings(expected);
  178. #endif
  179. INFO(test.get_source_file());
  180. if (!result.passed) {
  181. INFO(expected);
  182. failed++;
  183. }
  184. CHECK_MESSAGE(result.passed, (result.passed ? String() : result.output));
  185. }
  186. return failed;
  187. }
  188. bool GDScriptTestRunner::generate_outputs() {
  189. is_generating = true;
  190. if (!make_tests()) {
  191. print_line("Failed to generate a test output.");
  192. return false;
  193. }
  194. if (!generate_class_index()) {
  195. return false;
  196. }
  197. for (int i = 0; i < tests.size(); i++) {
  198. GDScriptTest test = tests[i];
  199. if (print_filenames) {
  200. print_line(test.get_source_relative_filepath());
  201. } else {
  202. OS::get_singleton()->print(".");
  203. }
  204. bool result = test.generate_output();
  205. if (!result) {
  206. print_line("\nCould not generate output for " + test.get_source_file());
  207. return false;
  208. }
  209. }
  210. print_line("\nGenerated output files for " + itos(tests.size()) + " tests successfully.");
  211. return true;
  212. }
  213. bool GDScriptTestRunner::make_tests_for_dir(const String &p_dir) {
  214. Error err = OK;
  215. Ref<DirAccess> dir(DirAccess::open(p_dir, &err));
  216. if (err != OK) {
  217. return false;
  218. }
  219. String current_dir = dir->get_current_dir();
  220. dir->list_dir_begin();
  221. String next = dir->get_next();
  222. while (!next.is_empty()) {
  223. if (dir->current_is_dir()) {
  224. if (next == "." || next == "..") {
  225. next = dir->get_next();
  226. continue;
  227. }
  228. if (!make_tests_for_dir(current_dir.path_join(next))) {
  229. return false;
  230. }
  231. } else {
  232. if (next.ends_with(".notest.gd")) {
  233. next = dir->get_next();
  234. continue;
  235. } else if (next.get_extension().to_lower() == "gd") {
  236. #ifndef DEBUG_ENABLED
  237. // On release builds, skip tests marked as debug only.
  238. Error open_err = OK;
  239. Ref<FileAccess> script_file(FileAccess::open(current_dir.path_join(next), FileAccess::READ, &open_err));
  240. if (open_err != OK) {
  241. ERR_PRINT(vformat(R"(Couldn't open test file "%s".)", next));
  242. next = dir->get_next();
  243. continue;
  244. } else {
  245. if (script_file->get_line() == "#debug-only") {
  246. next = dir->get_next();
  247. continue;
  248. }
  249. }
  250. #endif
  251. String out_file = next.get_basename() + ".out";
  252. if (!is_generating && !dir->file_exists(out_file)) {
  253. ERR_FAIL_V_MSG(false, "Could not find output file for " + next);
  254. }
  255. GDScriptTest test(current_dir.path_join(next), current_dir.path_join(out_file), source_dir);
  256. tests.push_back(test);
  257. }
  258. }
  259. next = dir->get_next();
  260. }
  261. dir->list_dir_end();
  262. return true;
  263. }
  264. bool GDScriptTestRunner::make_tests() {
  265. Error err = OK;
  266. Ref<DirAccess> dir(DirAccess::open(source_dir, &err));
  267. ERR_FAIL_COND_V_MSG(err != OK, false, "Could not open specified test directory.");
  268. source_dir = dir->get_current_dir() + "/"; // Make it absolute path.
  269. return make_tests_for_dir(dir->get_current_dir());
  270. }
  271. bool GDScriptTestRunner::generate_class_index() {
  272. StringName gdscript_name = GDScriptLanguage::get_singleton()->get_name();
  273. for (int i = 0; i < tests.size(); i++) {
  274. GDScriptTest test = tests[i];
  275. String base_type;
  276. String class_name = GDScriptLanguage::get_singleton()->get_global_class_name(test.get_source_file(), &base_type);
  277. if (class_name.is_empty()) {
  278. continue;
  279. }
  280. ERR_FAIL_COND_V_MSG(ScriptServer::is_global_class(class_name), false,
  281. "Class name '" + class_name + "' from " + test.get_source_file() + " is already used in " + ScriptServer::get_global_class_path(class_name));
  282. ScriptServer::add_global_class(class_name, base_type, gdscript_name, test.get_source_file());
  283. }
  284. return true;
  285. }
  286. GDScriptTest::GDScriptTest(const String &p_source_path, const String &p_output_path, const String &p_base_dir) {
  287. source_file = p_source_path;
  288. output_file = p_output_path;
  289. base_dir = p_base_dir;
  290. _print_handler.printfunc = print_handler;
  291. _error_handler.errfunc = error_handler;
  292. }
  293. void GDScriptTestRunner::handle_cmdline() {
  294. List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
  295. for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
  296. String &cmd = E->get();
  297. if (cmd == "--gdscript-generate-tests") {
  298. String path;
  299. if (E->next()) {
  300. path = E->next()->get();
  301. } else {
  302. path = "modules/gdscript/tests/scripts";
  303. }
  304. GDScriptTestRunner runner(path, false, cmdline_args.find("--print-filenames") != nullptr);
  305. bool completed = runner.generate_outputs();
  306. int failed = completed ? 0 : -1;
  307. exit(failed);
  308. }
  309. }
  310. }
  311. void GDScriptTest::enable_stdout() {
  312. // TODO: this could likely be handled by doctest or `tests/test_macros.h`.
  313. OS::get_singleton()->set_stdout_enabled(true);
  314. OS::get_singleton()->set_stderr_enabled(true);
  315. }
  316. void GDScriptTest::disable_stdout() {
  317. // TODO: this could likely be handled by doctest or `tests/test_macros.h`.
  318. OS::get_singleton()->set_stdout_enabled(false);
  319. OS::get_singleton()->set_stderr_enabled(false);
  320. }
  321. void GDScriptTest::print_handler(void *p_this, const String &p_message, bool p_error, bool p_rich) {
  322. TestResult *result = (TestResult *)p_this;
  323. result->output += p_message + "\n";
  324. }
  325. void GDScriptTest::error_handler(void *p_this, const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_explanation, bool p_editor_notify, ErrorHandlerType p_type) {
  326. ErrorHandlerData *data = (ErrorHandlerData *)p_this;
  327. GDScriptTest *self = data->self;
  328. TestResult *result = data->result;
  329. result->status = GDTEST_RUNTIME_ERROR;
  330. StringBuilder builder;
  331. builder.append(">> ");
  332. // Only include the function, file and line for script errors, otherwise the
  333. // test outputs changes based on the platform/compiler.
  334. bool include_source_info = false;
  335. switch (p_type) {
  336. case ERR_HANDLER_ERROR:
  337. builder.append("ERROR");
  338. break;
  339. case ERR_HANDLER_WARNING:
  340. builder.append("WARNING");
  341. break;
  342. case ERR_HANDLER_SCRIPT:
  343. builder.append("SCRIPT ERROR");
  344. include_source_info = true;
  345. break;
  346. case ERR_HANDLER_SHADER:
  347. builder.append("SHADER ERROR");
  348. break;
  349. default:
  350. builder.append("Unknown error type");
  351. break;
  352. }
  353. if (include_source_info) {
  354. builder.append("\n>> on function: ");
  355. builder.append(String::utf8(p_function));
  356. builder.append("()\n>> ");
  357. builder.append(String::utf8(p_file).trim_prefix(self->base_dir).replace("\\", "/"));
  358. builder.append("\n>> ");
  359. builder.append(itos(p_line));
  360. }
  361. builder.append("\n>> ");
  362. builder.append(String::utf8(p_error));
  363. if (strlen(p_explanation) > 0) {
  364. builder.append("\n>> ");
  365. builder.append(String::utf8(p_explanation));
  366. }
  367. builder.append("\n");
  368. result->output = builder.as_string();
  369. }
  370. bool GDScriptTest::check_output(const String &p_output) const {
  371. Error err = OK;
  372. String expected = FileAccess::get_file_as_string(output_file, &err);
  373. ERR_FAIL_COND_V_MSG(err != OK, false, "Error when opening the output file.");
  374. String got = p_output.strip_edges(); // TODO: may be hacky.
  375. got += "\n"; // Make sure to insert newline for CI static checks.
  376. #ifndef DEBUG_ENABLED
  377. expected = strip_warnings(expected);
  378. #endif
  379. return got == expected;
  380. }
  381. String GDScriptTest::get_text_for_status(GDScriptTest::TestStatus p_status) const {
  382. switch (p_status) {
  383. case GDTEST_OK:
  384. return "GDTEST_OK";
  385. case GDTEST_LOAD_ERROR:
  386. return "GDTEST_LOAD_ERROR";
  387. case GDTEST_PARSER_ERROR:
  388. return "GDTEST_PARSER_ERROR";
  389. case GDTEST_ANALYZER_ERROR:
  390. return "GDTEST_ANALYZER_ERROR";
  391. case GDTEST_COMPILER_ERROR:
  392. return "GDTEST_COMPILER_ERROR";
  393. case GDTEST_RUNTIME_ERROR:
  394. return "GDTEST_RUNTIME_ERROR";
  395. }
  396. return "";
  397. }
  398. GDScriptTest::TestResult GDScriptTest::execute_test_code(bool p_is_generating) {
  399. disable_stdout();
  400. TestResult result;
  401. result.status = GDTEST_OK;
  402. result.output = String();
  403. result.passed = false;
  404. Error err = OK;
  405. // Create script.
  406. Ref<GDScript> script;
  407. script.instantiate();
  408. script->set_path(source_file);
  409. err = script->load_source_code(source_file);
  410. if (err != OK) {
  411. enable_stdout();
  412. result.status = GDTEST_LOAD_ERROR;
  413. result.passed = false;
  414. ERR_FAIL_V_MSG(result, "\nCould not load source code for: '" + source_file + "'");
  415. }
  416. // Test parsing.
  417. GDScriptParser parser;
  418. err = parser.parse(script->get_source_code(), source_file, false);
  419. if (err != OK) {
  420. enable_stdout();
  421. result.status = GDTEST_PARSER_ERROR;
  422. result.output = get_text_for_status(result.status) + "\n";
  423. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  424. if (!errors.is_empty()) {
  425. // Only the first error since the following might be cascading.
  426. result.output += errors[0].message + "\n"; // TODO: line, column?
  427. }
  428. if (!p_is_generating) {
  429. result.passed = check_output(result.output);
  430. }
  431. return result;
  432. }
  433. // Test type-checking.
  434. GDScriptAnalyzer analyzer(&parser);
  435. err = analyzer.analyze();
  436. if (err != OK) {
  437. enable_stdout();
  438. result.status = GDTEST_ANALYZER_ERROR;
  439. result.output = get_text_for_status(result.status) + "\n";
  440. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  441. if (!errors.is_empty()) {
  442. // Only the first error since the following might be cascading.
  443. result.output += errors[0].message + "\n"; // TODO: line, column?
  444. }
  445. if (!p_is_generating) {
  446. result.passed = check_output(result.output);
  447. }
  448. return result;
  449. }
  450. #ifdef DEBUG_ENABLED
  451. StringBuilder warning_string;
  452. for (const GDScriptWarning &E : parser.get_warnings()) {
  453. const GDScriptWarning warning = E;
  454. warning_string.append(">> WARNING");
  455. warning_string.append("\n>> Line: ");
  456. warning_string.append(itos(warning.start_line));
  457. warning_string.append("\n>> ");
  458. warning_string.append(warning.get_name());
  459. warning_string.append("\n>> ");
  460. warning_string.append(warning.get_message());
  461. warning_string.append("\n");
  462. }
  463. result.output += warning_string.as_string();
  464. #endif
  465. // Test compiling.
  466. GDScriptCompiler compiler;
  467. err = compiler.compile(&parser, script.ptr(), false);
  468. if (err != OK) {
  469. enable_stdout();
  470. result.status = GDTEST_COMPILER_ERROR;
  471. result.output = get_text_for_status(result.status) + "\n";
  472. result.output = compiler.get_error();
  473. if (!p_is_generating) {
  474. result.passed = check_output(result.output);
  475. }
  476. return result;
  477. }
  478. // Script files matching this pattern are allowed to not contain a test() function.
  479. if (source_file.match("*.notest.gd")) {
  480. enable_stdout();
  481. result.passed = check_output(result.output);
  482. return result;
  483. }
  484. // Test running.
  485. const HashMap<StringName, GDScriptFunction *>::ConstIterator test_function_element = script->get_member_functions().find(GDScriptTestRunner::test_function_name);
  486. if (!test_function_element) {
  487. enable_stdout();
  488. result.status = GDTEST_LOAD_ERROR;
  489. result.output = "";
  490. result.passed = false;
  491. ERR_FAIL_V_MSG(result, "\nCould not find test function on: '" + source_file + "'");
  492. }
  493. // Setup output handlers.
  494. ErrorHandlerData error_data(&result, this);
  495. _print_handler.userdata = &result;
  496. _error_handler.userdata = &error_data;
  497. add_print_handler(&_print_handler);
  498. add_error_handler(&_error_handler);
  499. script->reload();
  500. // Create object instance for test.
  501. Object *obj = ClassDB::instantiate(script->get_native()->get_name());
  502. Ref<RefCounted> obj_ref;
  503. if (obj->is_ref_counted()) {
  504. obj_ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
  505. }
  506. obj->set_script(script);
  507. GDScriptInstance *instance = static_cast<GDScriptInstance *>(obj->get_script_instance());
  508. // Call test function.
  509. Callable::CallError call_err;
  510. instance->callp(GDScriptTestRunner::test_function_name, nullptr, 0, call_err);
  511. // Tear down output handlers.
  512. remove_print_handler(&_print_handler);
  513. remove_error_handler(&_error_handler);
  514. // Check results.
  515. if (call_err.error != Callable::CallError::CALL_OK) {
  516. enable_stdout();
  517. result.status = GDTEST_LOAD_ERROR;
  518. result.passed = false;
  519. ERR_FAIL_V_MSG(result, "\nCould not call test function on: '" + source_file + "'");
  520. }
  521. result.output = get_text_for_status(result.status) + "\n" + result.output;
  522. if (!p_is_generating) {
  523. result.passed = check_output(result.output);
  524. }
  525. if (obj_ref.is_null()) {
  526. memdelete(obj);
  527. }
  528. enable_stdout();
  529. GDScriptCache::remove_script(script->get_path());
  530. return result;
  531. }
  532. GDScriptTest::TestResult GDScriptTest::run_test() {
  533. return execute_test_code(false);
  534. }
  535. bool GDScriptTest::generate_output() {
  536. TestResult result = execute_test_code(true);
  537. if (result.status == GDTEST_LOAD_ERROR) {
  538. return false;
  539. }
  540. Error err = OK;
  541. Ref<FileAccess> out_file = FileAccess::open(output_file, FileAccess::WRITE, &err);
  542. if (err != OK) {
  543. return false;
  544. }
  545. String output = result.output.strip_edges(); // TODO: may be hacky.
  546. output += "\n"; // Make sure to insert newline for CI static checks.
  547. out_file->store_string(output);
  548. return true;
  549. }
  550. } // namespace GDScriptTests