|
| 1 | +extends SceneTree |
| 2 | + |
| 3 | +# godot --headless -s bin/verify-exercises.gd |
| 4 | + |
| 5 | +func _init(): |
| 6 | + # Calling `quit(1)` doesn't stop the program immediately, so a `return` is necessary. |
| 7 | + # That's why errors are checked directly in `_init()`, instead of calling `quit(1)` |
| 8 | + # in each method. |
| 9 | + var temp_dir_base = DirAccess.create_temp("gdscript-verify-exercises") |
| 10 | + if temp_dir_base == null: |
| 11 | + push_error("Failed to create base temporary directory: %s", DirAccess.get_open_error()) |
| 12 | + quit(1) |
| 13 | + return |
| 14 | + var temp_dir_base_path = temp_dir_base.get_current_dir() |
| 15 | + |
| 16 | + for exercise_base in [ |
| 17 | + "exercises/practice", |
| 18 | + "exercises/concept" |
| 19 | + ]: |
| 20 | + if run_tests(exercise_base, temp_dir_base_path) != OK: |
| 21 | + quit(1) |
| 22 | + return |
| 23 | + quit() |
| 24 | + |
| 25 | +func run_tests(exercise_base: String, temp_dir_base_path: String) -> Error: |
| 26 | + for slug in DirAccess.get_directories_at(exercise_base): |
| 27 | + var result = run_test(slug, exercise_base + "/" + slug, temp_dir_base_path) |
| 28 | + if result != OK: |
| 29 | + return result |
| 30 | + return OK |
| 31 | + |
| 32 | + |
| 33 | +func run_test(slug: String, exercise_dir: String, temp_dir_base_path: String) -> Error: |
| 34 | + var temp_dir: String = temp_dir_base_path + "/" + slug |
| 35 | + DirAccess.make_dir_recursive_absolute(temp_dir) |
| 36 | + |
| 37 | + var config_path = exercise_dir + "/.meta/config.json" |
| 38 | + var config_file = FileAccess.open(config_path, FileAccess.READ) |
| 39 | + if not config_file: |
| 40 | + push_error("Failed to read config: " + config_path) |
| 41 | + return DirAccess.get_open_error() |
| 42 | + |
| 43 | + var json = JSON.new() |
| 44 | + var parse_return_value = json.parse(config_file.get_as_text()) |
| 45 | + var config |
| 46 | + if parse_return_value == OK: |
| 47 | + config = json.data |
| 48 | + if typeof(config) != TYPE_DICTIONARY: |
| 49 | + push_error("Expected TYPE_DICTIONARY for JSON in: " + config_path) |
| 50 | + return ERR_UNCONFIGURED |
| 51 | + else: |
| 52 | + push_error("JSON Parse Error: ", json.get_error_message(), " in ", config_path, " at line ", json.get_error_line()) |
| 53 | + return parse_return_value |
| 54 | + |
| 55 | + var solution_name: String = config.get("files", {}).get("solution", [])[0] |
| 56 | + var test_path = exercise_dir + "/" + config.get("files", {}).get("test", [])[0] |
| 57 | + var example_path = exercise_dir + "/" + config.get("files", {}).get("example", [])[0] |
| 58 | + var dest_solution = temp_dir + "/" + solution_name |
| 59 | + |
| 60 | + # Copy test and example files into temp dir |
| 61 | + DirAccess.copy_absolute(test_path, temp_dir + "/" + test_path.get_file()) |
| 62 | + DirAccess.copy_absolute(example_path, dest_solution) |
| 63 | + |
| 64 | + # Run external test script |
| 65 | + var args = [slug, temp_dir, temp_dir] |
| 66 | + var output = [] |
| 67 | + var exit_code = OS.execute("/opt/exercism/gdscript/test-runner/bin/run.sh", args, output, true) |
| 68 | + if exit_code != 0: |
| 69 | + push_error("Test runner failed for ", slug, " with ", output) |
| 70 | + return ERR_SCRIPT_FAILED |
| 71 | + print(output[0]) |
| 72 | + |
| 73 | + # Check test result |
| 74 | + var results_path = temp_dir + "/results.json" |
| 75 | + if not FileAccess.file_exists(results_path): |
| 76 | + push_error("Missing results.json for ", slug, " at ", results_path) |
| 77 | + return ERR_FILE_NOT_FOUND |
| 78 | + var results_file = FileAccess.open(results_path, FileAccess.READ) |
| 79 | + parse_return_value = json.parse(results_file.get_as_text()) |
| 80 | + var results |
| 81 | + if parse_return_value == OK: |
| 82 | + results = json.data |
| 83 | + if results.get("status") != "pass": |
| 84 | + push_error("Tests for ", slug, " have failed:") |
| 85 | + push_error(JSON.stringify(results, "\t")) |
| 86 | + return FAILED |
| 87 | + else: |
| 88 | + push_error("JSON Parse Error: ", json.get_error_message(), " in ", config_path, " at line ", json.get_error_line()) |
| 89 | + return parse_return_value |
| 90 | + return OK |
0 commit comments