Skip to content

Commit 57c1a0a

Browse files
authored
Merge pull request #102 from codingthat/remove-jq-dependency
Remove jq dependency
2 parents 2fd04bb + 205d2c0 commit 57c1a0a

File tree

4 files changed

+99
-44
lines changed

4 files changed

+99
-44
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ jobs:
3232
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
3333

3434
- name: Verify all exercises
35-
run: bin/verify-exercises
35+
run: godot --headless -s bin/verify-exercises.gd

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ Exercism exercises in GDScript.
66

77
## Testing
88

9-
To set up for testing, clone https://github.com/exercism/gdscript-test-runner and move its contents to `/opt/test-runner`.
9+
To set up for testing, clone https://github.com/exercism/gdscript-test-runner and move its contents to `/opt/exercism/gdscript/test-runner`:
1010

11-
To test the exercises, run `./bin/verify-exercises`.
11+
```sh
12+
git clone https://github.com/exercism/gdscript-test-runner.git
13+
sudo mkdir -p /opt/exercism/gdscript/
14+
sudo mv gdscript-test-runner/ /opt/exercism/gdscript/test-runner/
15+
```
16+
17+
To test the exercises, run `godot --headless -s bin/verify-exercises.gd`.
1218
This command will iterate over all exercises and check to see if their exemplar/example implementation passes all the tests.
1319

1420
### Track linting

bin/verify-exercises

Lines changed: 0 additions & 41 deletions
This file was deleted.

bin/verify-exercises.gd

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)