From 2ec28a1848a851c83f68995b36614be9c8e30fce Mon Sep 17 00:00:00 2001 From: esainane Date: Tue, 10 Dec 2024 10:22:46 +0000 Subject: [PATCH] GD-607: Fix discovery of root test suites (#604) # Why The GdUnit4 Godot Editor plugin currently does not find any tests in the project root directory, even when configured to do so. Fixes #607. # What - Refactors external calls to `scan_test_directories` into a new `scan_all_test_directories` instead. These external calls all had the form `scan_test_directorys("res://", GdUnitSettings.test_root_folder(), [])`. This provides a cleaner API, and a logical place to add handling for the "include all resources" special case. - When `scan_all_test_directories` is called to determine which resource paths should be included, and the project is configured to include all resources, it early returns `["res://"]`. Otherwise, it performs the current subfolder scanning logic via `scan_test_directories("res://", GdUnitSettings.test_root_folder(), [])`. - Fixes a typo: `suits` -> `suites` - Fixes a typo: `scan_test_directorys` -> `scan_test_directories` --------- Co-authored-by: Nullpointer --- addons/gdUnit4/src/core/GdUnitRunner.gd | 4 +-- addons/gdUnit4/src/core/GdUnitSettings.gd | 2 +- .../src/core/command/GdUnitCommandHandler.gd | 12 ++++++-- .../core/discovery/GdUnitTestDiscoverer.gd | 2 +- .../src/ui/parts/InspectorTreeMainPanel.gd | 3 +- .../core/command/GdUnitCommandHandlerTest.gd | 29 +++++++++++++++---- .../ui/parts/resources/tree/tree_example.json | 10 +++---- .../tree/tree_sorted_by_EXECUTION_TIME.json | 10 +++---- .../tree/tree_sorted_by_NAME_ASCENDING.json | 10 +++---- .../tree/tree_sorted_by_NAME_DESCENDING.json | 10 +++---- .../tree/tree_sorted_by_UNSORTED.json | 10 +++---- 11 files changed, 63 insertions(+), 39 deletions(-) diff --git a/addons/gdUnit4/src/core/GdUnitRunner.gd b/addons/gdUnit4/src/core/GdUnitRunner.gd index 239bea6d..7f3d7702 100644 --- a/addons/gdUnit4/src/core/GdUnitRunner.gd +++ b/addons/gdUnit4/src/core/GdUnitRunner.gd @@ -65,7 +65,7 @@ func _process(_delta :float) -> void: if _client.is_client_connected(): var time := LocalTime.now() prints("Scan for test suites.") - _test_suites_to_process = load_test_suits() + _test_suites_to_process = load_test_suites() prints("Scanning of %d test suites took" % _test_suites_to_process.size(), time.elapsed_since()) gdUnitInit() _state = RUN @@ -95,7 +95,7 @@ func _process(_delta :float) -> void: get_tree().quit(0) -func load_test_suits() -> Array[Node]: +func load_test_suites() -> Array[Node]: var to_execute := _config.to_execute() if to_execute.is_empty(): prints("No tests selected to execute!") diff --git a/addons/gdUnit4/src/core/GdUnitSettings.gd b/addons/gdUnit4/src/core/GdUnitSettings.gd index e4bf4781..2fc1cff2 100644 --- a/addons/gdUnit4/src/core/GdUnitSettings.gd +++ b/addons/gdUnit4/src/core/GdUnitSettings.gd @@ -396,7 +396,7 @@ static func migrate_property(old_property :String, new_property :String, default ProjectSettings.set_initial_value(new_property, default_value) set_help(new_property, value, help) ProjectSettings.clear(old_property) - prints("Succesfull migrated property '%s' -> '%s' value: %s" % [old_property, new_property, value]) + prints("Successfully migrated property '%s' -> '%s' value: %s" % [old_property, new_property, value]) static func dump_to_tmp() -> void: diff --git a/addons/gdUnit4/src/core/command/GdUnitCommandHandler.gd b/addons/gdUnit4/src/core/command/GdUnitCommandHandler.gd index fa24abdd..76a138af 100644 --- a/addons/gdUnit4/src/core/command/GdUnitCommandHandler.gd +++ b/addons/gdUnit4/src/core/command/GdUnitCommandHandler.gd @@ -192,7 +192,7 @@ func cmd_run_test_case(test_suite_resource_path: String, test_case: String, test func cmd_run_overall(debug: bool) -> void: - var test_suite_paths: PackedStringArray = GdUnitCommandHandler.scan_test_directorys("res://" , GdUnitSettings.test_root_folder(), []) + var test_suite_paths: PackedStringArray = GdUnitCommandHandler.scan_all_test_directories(GdUnitSettings.test_root_folder()) var result := _runner_config.clear()\ .add_test_suites(test_suite_paths)\ .save_config() @@ -273,8 +273,14 @@ func cmd_create_test() -> void: func cmd_discover_tests() -> void: await GdUnitTestDiscoverer.run() +static func scan_all_test_directories(root: String) -> PackedStringArray: + var base_directory := "res://" + # If the test root folder is configured as blank, "/", or "res://", use the root folder as described in the settings panel + if root.is_empty() or root == "/" or root == base_directory: + return [base_directory] + return scan_test_directories(base_directory, root, []) -static func scan_test_directorys(base_directory: String, test_directory: String, test_suite_paths: PackedStringArray) -> PackedStringArray: +static func scan_test_directories(base_directory: String, test_directory: String, test_suite_paths: PackedStringArray) -> PackedStringArray: print_verbose("Scannning for test directory '%s' at %s" % [test_directory, base_directory]) for directory in DirAccess.get_directories_at(base_directory): if directory.begins_with("."): @@ -287,7 +293,7 @@ static func scan_test_directorys(base_directory: String, test_directory: String, test_suite_paths.append(current_directory) else: @warning_ignore("return_value_discarded") - scan_test_directorys(current_directory, test_directory, test_suite_paths) + scan_test_directories(current_directory, test_directory, test_suite_paths) return test_suite_paths diff --git a/addons/gdUnit4/src/core/discovery/GdUnitTestDiscoverer.gd b/addons/gdUnit4/src/core/discovery/GdUnitTestDiscoverer.gd index 22b36d15..febe0687 100644 --- a/addons/gdUnit4/src/core/discovery/GdUnitTestDiscoverer.gd +++ b/addons/gdUnit4/src/core/discovery/GdUnitTestDiscoverer.gd @@ -11,7 +11,7 @@ static func run() -> void: var t:= Thread.new() @warning_ignore("return_value_discarded") t.start(func () -> void: - var test_suite_directories :PackedStringArray = GdUnitCommandHandler.scan_test_directorys("res://" , GdUnitSettings.test_root_folder(), []) + var test_suite_directories :PackedStringArray = GdUnitCommandHandler.scan_all_test_directories(GdUnitSettings.test_root_folder()) var scanner := GdUnitTestSuiteScanner.new() var _test_suites_to_process :Array[Node] = [] diff --git a/addons/gdUnit4/src/ui/parts/InspectorTreeMainPanel.gd b/addons/gdUnit4/src/ui/parts/InspectorTreeMainPanel.gd index cb94fe88..205105e2 100644 --- a/addons/gdUnit4/src/ui/parts/InspectorTreeMainPanel.gd +++ b/addons/gdUnit4/src/ui/parts/InspectorTreeMainPanel.gd @@ -608,7 +608,8 @@ func create_tree_item(test_suite: GdUnitTestSuiteDto) -> TreeItem: var elements := test_relative_path.split("/") if elements[0] == "res://" or elements[0] == "": elements.remove_at(0) - elements.remove_at(elements.size() - 1) + if elements.size() > 0: + elements.remove_at(elements.size() - 1) for element in elements: test_base_path += "/" + element parent = create_or_find_item(parent, test_base_path, element) diff --git a/addons/gdUnit4/test/core/command/GdUnitCommandHandlerTest.gd b/addons/gdUnit4/test/core/command/GdUnitCommandHandlerTest.gd index 9500ecc2..de606c5d 100644 --- a/addons/gdUnit4/test/core/command/GdUnitCommandHandlerTest.gd +++ b/addons/gdUnit4/test/core/command/GdUnitCommandHandlerTest.gd @@ -51,19 +51,36 @@ func _test__check_test_run_stopped_manually() -> void: verify(inspector, 1).cmd_stop(inspector._client_id) -func test_scan_test_directorys() -> void: - assert_array(GdUnitCommandHandler.scan_test_directorys("res://", "test", [])).contains_exactly([ +func test_scan_test_directories() -> void: + assert_array(GdUnitCommandHandler.scan_test_directories("res://", "test", [])).contains_exactly([ "res://addons/gdUnit4/test" ]) # for root folders - assert_array(GdUnitCommandHandler.scan_test_directorys("res://", "", [])).contains_exactly([ + assert_array(GdUnitCommandHandler.scan_test_directories("res://", "", [])).contains_exactly([ "res://addons", "res://assets", "res://gdUnit3-examples" ]) - assert_array(GdUnitCommandHandler.scan_test_directorys("res://", "/", [])).contains_exactly([ + assert_array(GdUnitCommandHandler.scan_test_directories("res://", "/", [])).contains_exactly([ "res://addons", "res://assets", "res://gdUnit3-examples" ]) - assert_array(GdUnitCommandHandler.scan_test_directorys("res://", "res://", [])).contains_exactly([ + assert_array(GdUnitCommandHandler.scan_test_directories("res://", "res://", [])).contains_exactly([ "res://addons", "res://assets", "res://gdUnit3-examples" ]) # a test folder not exists - assert_array(GdUnitCommandHandler.scan_test_directorys("res://", "notest", [])).is_empty() + assert_array(GdUnitCommandHandler.scan_test_directories("res://", "notest", [])).is_empty() + + +func test_scan_all_test_directories() -> void: + # Test when test_root_folder is empty + assert_array(GdUnitCommandHandler.scan_all_test_directories("")).contains_exactly(["res://"]) + + # Test when test_root_folder is "/" + assert_array(GdUnitCommandHandler.scan_all_test_directories("/")).contains_exactly(["res://"]) + + # Test when test_root_folder is "res://" + assert_array(GdUnitCommandHandler.scan_all_test_directories("res://")).contains_exactly(["res://"]) + + # Test when test_root_folder is set to a specific folder + assert_array(GdUnitCommandHandler.scan_all_test_directories("test")).contains_exactly(["res://addons/gdUnit4/test"]) + + # Test when test_root_folder is set to something which doesn't exist + assert_array(GdUnitCommandHandler.scan_all_test_directories("notest")).is_empty() diff --git a/addons/gdUnit4/test/ui/parts/resources/tree/tree_example.json b/addons/gdUnit4/test/ui/parts/resources/tree/tree_example.json index ea3fc237..f814bed5 100644 --- a/addons/gdUnit4/test/ui/parts/resources/tree/tree_example.json +++ b/addons/gdUnit4/test/ui/parts/resources/tree/tree_example.json @@ -12530,7 +12530,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -77439,7 +77439,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -91204,7 +91204,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -91440,7 +91440,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -91823,7 +91823,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, diff --git a/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_EXECUTION_TIME.json b/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_EXECUTION_TIME.json index 6ff3dc1a..a29bb00a 100644 --- a/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_EXECUTION_TIME.json +++ b/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_EXECUTION_TIME.json @@ -5770,7 +5770,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -40771,7 +40771,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -67760,7 +67760,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -67996,7 +67996,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -68379,7 +68379,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, diff --git a/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_NAME_ASCENDING.json b/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_NAME_ASCENDING.json index cdabc5c0..32e73004 100644 --- a/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_NAME_ASCENDING.json +++ b/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_NAME_ASCENDING.json @@ -12530,7 +12530,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -77439,7 +77439,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -91204,7 +91204,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -91440,7 +91440,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -91823,7 +91823,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, diff --git a/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_NAME_DESCENDING.json b/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_NAME_DESCENDING.json index ad7cc6c4..e54062d2 100644 --- a/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_NAME_DESCENDING.json +++ b/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_NAME_DESCENDING.json @@ -14111,7 +14111,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -66602,7 +66602,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -94261,7 +94261,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -94497,7 +94497,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -94710,7 +94710,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, diff --git a/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_UNSORTED.json b/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_UNSORTED.json index 194f5e8e..553cf454 100644 --- a/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_UNSORTED.json +++ b/addons/gdUnit4/test/ui/parts/resources/tree/tree_sorted_by_UNSORTED.json @@ -12530,7 +12530,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -77439,7 +77439,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -91204,7 +91204,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -91440,7 +91440,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0, @@ -91823,7 +91823,7 @@ "disable_folding": false, "metadata/__focus_rect": "[P: (471, 276), S: (100, 28)]", "metadata/gdUnit_execution_time": 21, - "metadata/gdUnit_name": "test_scan_test_directorys", + "metadata/gdUnit_name": "test_scan_test_directories", "metadata/gdUnit_state": 2, "metadata/gdUnit_suite_success_tests": 0, "metadata/gdUnit_suite_total_tests": 0,