gdscript_test_runner.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*************************************************************************/
  2. /* gdscript_test_runner.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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("Can't autoload: %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("Can't autoload: %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("Script does not inherit from Node: %s.", info.path));
  82. Object *obj = ClassDB::instantiate(ibt);
  83. ERR_CONTINUE_MSG(!obj, vformat("Cannot instance script for Autoload, expected 'Node' inheritance, got: %s.", ibt));
  84. n = Object::cast_to<Node>(obj);
  85. n->set_script(scr);
  86. }
  87. }
  88. ERR_CONTINUE_MSG(!n, vformat("Path in autoload not a node or 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) {
  113. test_function_name = StaticCString::create("test");
  114. do_init_languages = p_init_language;
  115. source_dir = p_source_dir;
  116. if (!source_dir.ends_with("/")) {
  117. source_dir += "/";
  118. }
  119. if (do_init_languages) {
  120. init_language(p_source_dir);
  121. }
  122. #ifdef DEBUG_ENABLED
  123. // Enable all warnings for GDScript, so we can test them.
  124. ProjectSettings::get_singleton()->set_setting("debug/gdscript/warnings/enable", true);
  125. for (int i = 0; i < (int)GDScriptWarning::WARNING_MAX; i++) {
  126. String warning = GDScriptWarning::get_name_from_code((GDScriptWarning::Code)i).to_lower();
  127. ProjectSettings::get_singleton()->set_setting("debug/gdscript/warnings/" + warning, true);
  128. }
  129. #endif
  130. // Enable printing to show results
  131. CoreGlobals::print_line_enabled = true;
  132. CoreGlobals::print_error_enabled = true;
  133. }
  134. GDScriptTestRunner::~GDScriptTestRunner() {
  135. test_function_name = StringName();
  136. if (do_init_languages) {
  137. finish_language();
  138. }
  139. }
  140. #ifndef DEBUG_ENABLED
  141. static String strip_warnings(const String &p_expected) {
  142. // On release builds we don't have warnings. Here we remove them from the output before comparison
  143. // so it doesn't fail just because of difference in warnings.
  144. String expected_no_warnings;
  145. for (String line : p_expected.split("\n")) {
  146. if (line.begins_with(">> ")) {
  147. continue;
  148. }
  149. expected_no_warnings += line + "\n";
  150. }
  151. return expected_no_warnings.strip_edges() + "\n";
  152. }
  153. #endif
  154. int GDScriptTestRunner::run_tests() {
  155. if (!make_tests()) {
  156. FAIL("An error occurred while making the tests.");
  157. return -1;
  158. }
  159. if (!generate_class_index()) {
  160. FAIL("An error occurred while generating class index.");
  161. return -1;
  162. }
  163. int failed = 0;
  164. for (int i = 0; i < tests.size(); i++) {
  165. GDScriptTest test = tests[i];
  166. GDScriptTest::TestResult result = test.run_test();
  167. String expected = FileAccess::get_file_as_string(test.get_output_file());
  168. #ifndef DEBUG_ENABLED
  169. expected = strip_warnings(expected);
  170. #endif
  171. INFO(test.get_source_file());
  172. if (!result.passed) {
  173. INFO(expected);
  174. failed++;
  175. }
  176. CHECK_MESSAGE(result.passed, (result.passed ? String() : result.output));
  177. }
  178. return failed;
  179. }
  180. bool GDScriptTestRunner::generate_outputs() {
  181. is_generating = true;
  182. if (!make_tests()) {
  183. print_line("Failed to generate a test output.");
  184. return false;
  185. }
  186. if (!generate_class_index()) {
  187. return false;
  188. }
  189. for (int i = 0; i < tests.size(); i++) {
  190. OS::get_singleton()->print(".");
  191. GDScriptTest test = tests[i];
  192. bool result = test.generate_output();
  193. if (!result) {
  194. print_line("\nCould not generate output for " + test.get_source_file());
  195. return false;
  196. }
  197. }
  198. print_line("\nGenerated output files for " + itos(tests.size()) + " tests successfully.");
  199. return true;
  200. }
  201. bool GDScriptTestRunner::make_tests_for_dir(const String &p_dir) {
  202. Error err = OK;
  203. Ref<DirAccess> dir(DirAccess::open(p_dir, &err));
  204. if (err != OK) {
  205. return false;
  206. }
  207. String current_dir = dir->get_current_dir();
  208. dir->list_dir_begin();
  209. String next = dir->get_next();
  210. while (!next.is_empty()) {
  211. if (dir->current_is_dir()) {
  212. if (next == "." || next == "..") {
  213. next = dir->get_next();
  214. continue;
  215. }
  216. if (!make_tests_for_dir(current_dir.path_join(next))) {
  217. return false;
  218. }
  219. } else {
  220. if (next.ends_with(".notest.gd")) {
  221. next = dir->get_next();
  222. continue;
  223. } else if (next.get_extension().to_lower() == "gd") {
  224. #ifndef DEBUG_ENABLED
  225. // On release builds, skip tests marked as debug only.
  226. Error open_err = OK;
  227. Ref<FileAccess> script_file(FileAccess::open(current_dir.path_join(next), FileAccess::READ, &open_err));
  228. if (open_err != OK) {
  229. ERR_PRINT(vformat(R"(Couldn't open test file "%s".)", next));
  230. next = dir->get_next();
  231. continue;
  232. } else {
  233. if (script_file->get_line() == "#debug-only") {
  234. next = dir->get_next();
  235. continue;
  236. }
  237. }
  238. #endif
  239. String out_file = next.get_basename() + ".out";
  240. if (!is_generating && !dir->file_exists(out_file)) {
  241. ERR_FAIL_V_MSG(false, "Could not find output file for " + next);
  242. }
  243. GDScriptTest test(current_dir.path_join(next), current_dir.path_join(out_file), source_dir);
  244. tests.push_back(test);
  245. }
  246. }
  247. next = dir->get_next();
  248. }
  249. dir->list_dir_end();
  250. return true;
  251. }
  252. bool GDScriptTestRunner::make_tests() {
  253. Error err = OK;
  254. Ref<DirAccess> dir(DirAccess::open(source_dir, &err));
  255. ERR_FAIL_COND_V_MSG(err != OK, false, "Could not open specified test directory.");
  256. source_dir = dir->get_current_dir() + "/"; // Make it absolute path.
  257. return make_tests_for_dir(dir->get_current_dir());
  258. }
  259. bool GDScriptTestRunner::generate_class_index() {
  260. StringName gdscript_name = GDScriptLanguage::get_singleton()->get_name();
  261. for (int i = 0; i < tests.size(); i++) {
  262. GDScriptTest test = tests[i];
  263. String base_type;
  264. String class_name = GDScriptLanguage::get_singleton()->get_global_class_name(test.get_source_file(), &base_type);
  265. if (class_name.is_empty()) {
  266. continue;
  267. }
  268. ERR_FAIL_COND_V_MSG(ScriptServer::is_global_class(class_name), false,
  269. "Class name '" + class_name + "' from " + test.get_source_file() + " is already used in " + ScriptServer::get_global_class_path(class_name));
  270. ScriptServer::add_global_class(class_name, base_type, gdscript_name, test.get_source_file());
  271. }
  272. return true;
  273. }
  274. GDScriptTest::GDScriptTest(const String &p_source_path, const String &p_output_path, const String &p_base_dir) {
  275. source_file = p_source_path;
  276. output_file = p_output_path;
  277. base_dir = p_base_dir;
  278. _print_handler.printfunc = print_handler;
  279. _error_handler.errfunc = error_handler;
  280. }
  281. void GDScriptTestRunner::handle_cmdline() {
  282. List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
  283. // TODO: this could likely be ported to use test commands:
  284. // https://github.com/godotengine/godot/pull/41355
  285. // Currently requires to startup the whole engine, which is slow.
  286. String test_cmd = "--gdscript-test";
  287. String gen_cmd = "--gdscript-generate-tests";
  288. for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
  289. String &cmd = E->get();
  290. if (cmd == test_cmd || cmd == gen_cmd) {
  291. if (E->next() == nullptr) {
  292. ERR_PRINT("Needed a path for the test files.");
  293. exit(-1);
  294. }
  295. const String &path = E->next()->get();
  296. GDScriptTestRunner runner(path, false);
  297. int failed = 0;
  298. if (cmd == test_cmd) {
  299. failed = runner.run_tests();
  300. } else {
  301. bool completed = runner.generate_outputs();
  302. failed = completed ? 0 : -1;
  303. }
  304. exit(failed);
  305. }
  306. }
  307. }
  308. void GDScriptTest::enable_stdout() {
  309. // TODO: this could likely be handled by doctest or `tests/test_macros.h`.
  310. OS::get_singleton()->set_stdout_enabled(true);
  311. OS::get_singleton()->set_stderr_enabled(true);
  312. }
  313. void GDScriptTest::disable_stdout() {
  314. // TODO: this could likely be handled by doctest or `tests/test_macros.h`.
  315. OS::get_singleton()->set_stdout_enabled(false);
  316. OS::get_singleton()->set_stderr_enabled(false);
  317. }
  318. void GDScriptTest::print_handler(void *p_this, const String &p_message, bool p_error, bool p_rich) {
  319. TestResult *result = (TestResult *)p_this;
  320. result->output += p_message + "\n";
  321. }
  322. 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) {
  323. ErrorHandlerData *data = (ErrorHandlerData *)p_this;
  324. GDScriptTest *self = data->self;
  325. TestResult *result = data->result;
  326. result->status = GDTEST_RUNTIME_ERROR;
  327. StringBuilder builder;
  328. builder.append(">> ");
  329. switch (p_type) {
  330. case ERR_HANDLER_ERROR:
  331. builder.append("ERROR");
  332. break;
  333. case ERR_HANDLER_WARNING:
  334. builder.append("WARNING");
  335. break;
  336. case ERR_HANDLER_SCRIPT:
  337. builder.append("SCRIPT ERROR");
  338. break;
  339. case ERR_HANDLER_SHADER:
  340. builder.append("SHADER ERROR");
  341. break;
  342. default:
  343. builder.append("Unknown error type");
  344. break;
  345. }
  346. builder.append("\n>> on function: ");
  347. builder.append(String::utf8(p_function));
  348. builder.append("()\n>> ");
  349. builder.append(String::utf8(p_file).trim_prefix(self->base_dir));
  350. builder.append("\n>> ");
  351. builder.append(itos(p_line));
  352. builder.append("\n>> ");
  353. builder.append(String::utf8(p_error));
  354. if (strlen(p_explanation) > 0) {
  355. builder.append("\n>> ");
  356. builder.append(String::utf8(p_explanation));
  357. }
  358. builder.append("\n");
  359. result->output = builder.as_string();
  360. }
  361. bool GDScriptTest::check_output(const String &p_output) const {
  362. Error err = OK;
  363. String expected = FileAccess::get_file_as_string(output_file, &err);
  364. ERR_FAIL_COND_V_MSG(err != OK, false, "Error when opening the output file.");
  365. String got = p_output.strip_edges(); // TODO: may be hacky.
  366. got += "\n"; // Make sure to insert newline for CI static checks.
  367. #ifndef DEBUG_ENABLED
  368. expected = strip_warnings(expected);
  369. #endif
  370. return got == expected;
  371. }
  372. String GDScriptTest::get_text_for_status(GDScriptTest::TestStatus p_status) const {
  373. switch (p_status) {
  374. case GDTEST_OK:
  375. return "GDTEST_OK";
  376. case GDTEST_LOAD_ERROR:
  377. return "GDTEST_LOAD_ERROR";
  378. case GDTEST_PARSER_ERROR:
  379. return "GDTEST_PARSER_ERROR";
  380. case GDTEST_ANALYZER_ERROR:
  381. return "GDTEST_ANALYZER_ERROR";
  382. case GDTEST_COMPILER_ERROR:
  383. return "GDTEST_COMPILER_ERROR";
  384. case GDTEST_RUNTIME_ERROR:
  385. return "GDTEST_RUNTIME_ERROR";
  386. }
  387. return "";
  388. }
  389. GDScriptTest::TestResult GDScriptTest::execute_test_code(bool p_is_generating) {
  390. disable_stdout();
  391. TestResult result;
  392. result.status = GDTEST_OK;
  393. result.output = String();
  394. result.passed = false;
  395. Error err = OK;
  396. // Create script.
  397. Ref<GDScript> script;
  398. script.instantiate();
  399. script->set_path(source_file);
  400. err = script->load_source_code(source_file);
  401. if (err != OK) {
  402. enable_stdout();
  403. result.status = GDTEST_LOAD_ERROR;
  404. result.passed = false;
  405. ERR_FAIL_V_MSG(result, "\nCould not load source code for: '" + source_file + "'");
  406. }
  407. // Test parsing.
  408. GDScriptParser parser;
  409. err = parser.parse(script->get_source_code(), source_file, false);
  410. if (err != OK) {
  411. enable_stdout();
  412. result.status = GDTEST_PARSER_ERROR;
  413. result.output = get_text_for_status(result.status) + "\n";
  414. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  415. if (!errors.is_empty()) {
  416. // Only the first error since the following might be cascading.
  417. result.output += errors[0].message + "\n"; // TODO: line, column?
  418. }
  419. if (!p_is_generating) {
  420. result.passed = check_output(result.output);
  421. }
  422. return result;
  423. }
  424. // Test type-checking.
  425. GDScriptAnalyzer analyzer(&parser);
  426. err = analyzer.analyze();
  427. if (err != OK) {
  428. enable_stdout();
  429. result.status = GDTEST_ANALYZER_ERROR;
  430. result.output = get_text_for_status(result.status) + "\n";
  431. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  432. if (!errors.is_empty()) {
  433. // Only the first error since the following might be cascading.
  434. result.output += errors[0].message + "\n"; // TODO: line, column?
  435. }
  436. if (!p_is_generating) {
  437. result.passed = check_output(result.output);
  438. }
  439. return result;
  440. }
  441. #ifdef DEBUG_ENABLED
  442. StringBuilder warning_string;
  443. for (const GDScriptWarning &E : parser.get_warnings()) {
  444. const GDScriptWarning warning = E;
  445. warning_string.append(">> WARNING");
  446. warning_string.append("\n>> Line: ");
  447. warning_string.append(itos(warning.start_line));
  448. warning_string.append("\n>> ");
  449. warning_string.append(warning.get_name());
  450. warning_string.append("\n>> ");
  451. warning_string.append(warning.get_message());
  452. warning_string.append("\n");
  453. }
  454. result.output += warning_string.as_string();
  455. #endif
  456. // Test compiling.
  457. GDScriptCompiler compiler;
  458. err = compiler.compile(&parser, script.ptr(), false);
  459. if (err != OK) {
  460. enable_stdout();
  461. result.status = GDTEST_COMPILER_ERROR;
  462. result.output = get_text_for_status(result.status) + "\n";
  463. result.output = compiler.get_error();
  464. if (!p_is_generating) {
  465. result.passed = check_output(result.output);
  466. }
  467. return result;
  468. }
  469. // Script files matching this pattern are allowed to not contain a test() function.
  470. if (source_file.match("*.notest.gd")) {
  471. enable_stdout();
  472. result.passed = check_output(result.output);
  473. return result;
  474. }
  475. // Test running.
  476. const HashMap<StringName, GDScriptFunction *>::ConstIterator test_function_element = script->get_member_functions().find(GDScriptTestRunner::test_function_name);
  477. if (!test_function_element) {
  478. enable_stdout();
  479. result.status = GDTEST_LOAD_ERROR;
  480. result.output = "";
  481. result.passed = false;
  482. ERR_FAIL_V_MSG(result, "\nCould not find test function on: '" + source_file + "'");
  483. }
  484. script->reload();
  485. // Create object instance for test.
  486. Object *obj = ClassDB::instantiate(script->get_native()->get_name());
  487. Ref<RefCounted> obj_ref;
  488. if (obj->is_ref_counted()) {
  489. obj_ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
  490. }
  491. obj->set_script(script);
  492. GDScriptInstance *instance = static_cast<GDScriptInstance *>(obj->get_script_instance());
  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. // Call test function.
  500. Callable::CallError call_err;
  501. instance->callp(GDScriptTestRunner::test_function_name, nullptr, 0, call_err);
  502. // Tear down output handlers.
  503. remove_print_handler(&_print_handler);
  504. remove_error_handler(&_error_handler);
  505. // Check results.
  506. if (call_err.error != Callable::CallError::CALL_OK) {
  507. enable_stdout();
  508. result.status = GDTEST_LOAD_ERROR;
  509. result.passed = false;
  510. ERR_FAIL_V_MSG(result, "\nCould not call test function on: '" + source_file + "'");
  511. }
  512. result.output = get_text_for_status(result.status) + "\n" + result.output;
  513. if (!p_is_generating) {
  514. result.passed = check_output(result.output);
  515. }
  516. if (obj_ref.is_null()) {
  517. memdelete(obj);
  518. }
  519. enable_stdout();
  520. GDScriptCache::remove_script(script->get_path());
  521. return result;
  522. }
  523. GDScriptTest::TestResult GDScriptTest::run_test() {
  524. return execute_test_code(false);
  525. }
  526. bool GDScriptTest::generate_output() {
  527. TestResult result = execute_test_code(true);
  528. if (result.status == GDTEST_LOAD_ERROR) {
  529. return false;
  530. }
  531. Error err = OK;
  532. Ref<FileAccess> out_file = FileAccess::open(output_file, FileAccess::WRITE, &err);
  533. if (err != OK) {
  534. return false;
  535. }
  536. String output = result.output.strip_edges(); // TODO: may be hacky.
  537. output += "\n"; // Make sure to insert newline for CI static checks.
  538. out_file->store_string(output);
  539. return true;
  540. }
  541. } // namespace GDScriptTests