Doubling a `Resource` subclass breaks
Attempting to use double() and partial_double() but am receiving the following error on creation of the double no matter what I do.
Parser Error: The method "set_path" overrides a method from native class "Resource". This won't be called by the engine and may not work as expected. (Warning treated as error.)
Further, the double method then returns an object without the expected new() method on it the documentation suggests should be there (e.g. var doubled_script = double(MyScript).new()).
Minimal reproduction
Script foo.gd (just an empty script that extends Resource):
extends Resource
class_name Foo
Test Script test_foo.gd:
extends GutTest
var FooScript = preload("res://path/to/foo.gd")
func test_foo_double():
var foo = double(FooScript).new()
I found the exact line where the error surfaces, but I can't seem to link to it. It's in addons/gut/util.gd, but that file in this repo is very different than the one published in my release, despite that release being dated March 12, 2023 and the latest commit to that file being August 15, 2022. I don't know what is happening to cause this difference but it makes it very hard to communicate the breakpoint. Here is the breaking snippet on my end:
var _created_script_count = 0
func create_script_from_source(source, override_path=null):
_created_script_count += 1
var r_path = ''#str('workaround for godot issue #65263 (', _created_script_count, ')')
if(override_path != null):
r_path = override_path
var DynamicScript = GDScript.new()
DynamicScript.source_code = source
# The resource_path must be unique or Godot thinks it is trying
# to load something it has already loaded and generates an error like
# ERROR: Another resource is loaded from path 'workaround for godot issue #65263' (possible cyclic resource inclusion).
DynamicScript.resource_path = r_path
var result = DynamicScript.reload() # <= THIS IS THE LINE THAT PAUSES EXECUTION WITH THE ERROR
return DynamicScript
Godot version: 4.0.1 GUT version: 9.0.1
I ran into this problem this morning. I was able to get my tests to run with doubling by switching the GDScript debugging setting "Native Method Override" from Error to Warning. I don't know yet if this will have other negative downstream effects, and I don't like switching this option this way. However, it may be a helpful workaround for the time being.
For what it's worth, I redesigned my Resource types to be Objects instead, and I am still getting this error unless I fiddle with the debug settings as described above. I mention this because it's possible I have a different root cause. I have not dived into the GUT code as the OP has.
Godot 4.0.2 GUT 9.0.1
I must have the warning disabled. I was not able to cause any issues with the following cursory check. I checked doubling, partial doubling, stubbing the instance, and stubbing the class.
Class under test
class_name TestI482
extends Resource
func get_something():
return 'something'
func add(p1, p2):
return p1 + p2
Tests
extends GutTest
func test_double_extends_resource():
var dbl = double(TestI482).new()
assert_not_null(dbl)
func test_double_extends_resource_get_something():
var dbl = double(TestI482).new()
assert_null(dbl.get_something())
func test_double_extends_resource_add():
var dbl = double(TestI482).new()
assert_null(dbl.add(1, 'a'))
func test_double_extends_resource_get_something_stubbed_instance():
var dbl = double(TestI482).new()
stub(dbl, 'get_something').to_return('nothing')
assert_eq(dbl.get_something(), 'nothing')
func test_double_extends_resource_add_stubbed_class():
stub(TestI482, 'add').to_return('foo')
var dbl = double(TestI482).new()
assert_eq(dbl.add(1, 'a'), 'foo')
func test_partial_double_extends_resource_get_something():
var dbl = partial_double(TestI482).new()
assert_eq(dbl.get_something(), 'something')
func test_partial_double_extends_resource_add():
var dbl = partial_double(TestI482).new()
assert_eq(dbl.add(1, 2), 3)
Output
--- Gut ---
[INFO]: using [/Users/butchwesley/Library/Application Support/Godot/app_userdata/Gut] for temporary output.
Godot version: 4.0.0
GUT version: 9.0.1
res://test/unit/test_bugs/test_i482_double_resource.gd.
* test_double_extends_resource
[Passed]: Expected [<Resource#-9223371994358086027>(double of i482_class.gd)] to be anything but NULL:
* test_double_extends_resource_get_something
[INFO]: Unstubbed call to get_something::<Resource#-9223371992076384644>(double of i482_class.gd)
[WARNING]: Call to [get_something] was not stubbed for the supplied parameters []. Null was returned.
[Passed]: Expected [<null>] to be NULL:
* test_double_extends_resource_add
[INFO]: Unstubbed call to add::<Resource#-9223371989979232636>(double of i482_class.gd)
[WARNING]: Call to [add] was not stubbed for the supplied parameters [1, "a"]. Null was returned.
[Passed]: Expected [<null>] to be NULL:
* test_double_extends_resource_get_something_stubbed_instance
[Passed]: ["nothing"] expected to equal ["nothing"]:
* test_double_extends_resource_add_stubbed_class
[Passed]: ["foo"] expected to equal ["foo"]:
* test_partial_double_extends_resource_get_something
[Passed]: ["something"] expected to equal ["something"]:
* test_partial_double_extends_resource_add
[Passed]: [3] expected to equal [3]:
7/7 passed.
==============================================
= Run Summary
==============================================
All tests passed
Totals
Scripts: 1
Passing tests 7
Failing tests 0
Risky tests 0
Pending: 0
Asserts: 7 of 7 passed
Warnings/Errors:
* 2 Warnings.
7 passed 0 failed. Tests finished in 0.074s
I just noticed you are using 4.0.1. I'll try again with that later. This attempt was with 4.0.0.
Ran same test with 4.0.2 without error:
--- Gut ---
[INFO]: using [/Users/butchwesley/Library/Application Support/Godot/app_userdata/Gut] for temporary output.
Godot version: 4.0.2
GUT version: 9.0.1
res://test/unit/test_bugs/test_i482_double_resource.gd.
* test_double_extends_resource
[Passed]: Expected [<Resource#-9223371994358086027>(double of i482_class.gd)] to be anything but NULL:
* test_double_extends_resource_get_something
[INFO]: Unstubbed call to get_something::<Resource#-9223371992076384644>(double of i482_class.gd)
[WARNING]: Call to [get_something] was not stubbed for the supplied parameters []. Null was returned.
[Passed]: Expected [<null>] to be NULL:
* test_double_extends_resource_add
[INFO]: Unstubbed call to add::<Resource#-9223371989979232636>(double of i482_class.gd)
[WARNING]: Call to [add] was not stubbed for the supplied parameters [1, "a"]. Null was returned.
[Passed]: Expected [<null>] to be NULL:
* test_double_extends_resource_get_something_stubbed_instance
[Passed]: ["nothing"] expected to equal ["nothing"]:
* test_double_extends_resource_add_stubbed_class
[Passed]: ["foo"] expected to equal ["foo"]:
* test_partial_double_extends_resource_get_something
[Passed]: ["something"] expected to equal ["something"]:
* test_partial_double_extends_resource_add
[Passed]: [3] expected to equal [3]:
7/7 passed.
==============================================
= Run Summary
==============================================
All tests passed
Totals
Scripts: 1
Passing tests 7
Failing tests 0
Risky tests 0
Pending: 0
Asserts: 7 of 7 passed
Warnings/Errors:
* 2 Warnings.
7 passed 0 failed. Tests finished in 0.075s
I can confirm that those tests provided by @bitwes run and pass but only if you modify the "Native Method override" setting. Seeing it requires turning on the "Advanced" option within the project settings. The default value is "Error," but changing it to "Warning" or "Ignore" allows the tests to pass. I'm still seeing a lot of warnings in the console though, many dealing with redundant awaits.
The problem is that I don't want to have to disable useful errors and warnings just to be able to use GUT. Are there alternative approaches that would allow doubling of resources without having to disable notification in my other code, or is there a way to suppress the warnings within the plugin source?
@bitwes I will attempt your sample across multiple versions when I get a moment.
In the meantime, can you please explain the other half of my issue description where the contents in the github release zip and the godot editor's asset library do not match the contents under the addons folder in the repo? When stepping through execution on my end, the line that breaks is in the release zip but it's not in the folder on the repo.
Why does that discrepancy exist? Are you testing with the code under the release zip or the code directly in the repo? Because if you're testing with direct repo code the offending line (or at least where the issue surfaces) just isn't in the repo code as it's presented. addons/gut/utils.gd has 70 more lines in the zip than in the repo including the breaking one and I don't understand why that is.
Discrepancy
@JSchrepp for a second there I thought something crazy had happened...then I remembered that we are talking Godot 4. The release you are using is from the godot_4 branch, not master. Here's the link to the line in question:
https://github.com/bitwes/Gut/blob/godot_4/addons/gut/utils.gd#L464
Issue
I was able to replicate the issue and I started adding methods to GUT's internal list of methods to ignore. After about 15 or so I changed the error to warning and it turns out that there are about 266 methods that should be ignored. So the blacklist isn't the right approach.
I tried changing the doubling strategy to DOUBLE_STRATEGY.SCRIPT_ONLY, but that didn't seem to have any effect, which is odd.
Places to start looking:
script_parser._parse_methodsmight be mislabeling methods as "local" for Resources.- There might be a bug with the doubling strategy as I would have expected that to fix it.
- I think something can be done in
script_parser.gdwhen the thing being doubled is a resource.
Godot 4 code is not in master okay that explains so much thank you. Yes that is the exact line breaking for me. This is a helpful initial assessment thank you.
So I think someone has confused the term "double strategy" between script/super and whole/partial. This block expects opts.double_strategy to be either 'include super' or 'script only'. But its default value has been set to 'partial' here. To be clear, there is not another instance of the strings 'include super' or 'script only' in the entire code base. Something is wrong here, and this may explain why @bitwes options weren't properly set.
Meanwhile, gut.gd has its double strategy as just an int instead of the enum from utils, and its defaulting to 1 (which would be INCLUDE_SUPER when converted to enum). Since the config code won't see either super or script indicators, it left it as is, leaving this to always be set to super.
What is not clear to me as a newcomer to this code is how this options dictionary is getting values set by the end user, and so I don't know exactly what the fix should look like.
Additionally, changing the default options to use script only does not change the actual options passed into the if/else block.
DOUBLE_STRATEGY determines whether the doubler should create overloads all the methods a script has (including any defined in the parent script) and whether it should only those defined in the script. In the TestI482 class above, a INCLUDE_SUPER will overload all methods in Resource and all methods defined in the class. SCRIPT_ONLY would only generate overloads for get_something and add. At least that is how it should work.
A double stubs all the overloads created by the doubler to_do_nothing. A partial_double stubs all the overloads created to_call_super.
DOUBLE_STRATEGY came before partial_double and originally the flags were FULL or PARTIAL. This got a little confusing once partial_double was introduced. When converting to Godot 4, I decided to rename the strategies to INCLUDE_SUPER and SCRIPT_ONLY so it could be a little less confusing.
I suspect I missed a few things in the conversion.
This block expects opts.double_strategy to be either 'include super' or 'script only'. But its default value has been set to 'partial' here. To be clear, there is not another instance of the strings 'include super' or 'script only' in the entire code base.
That block is converting human readable options in a gutconfig file to their enum equivalents.
For reference, here is the GUT 7.4.x documentation for double strategy: https://github.com/bitwes/Gut/wiki/Doubles#setting-the-doubling-strategy. I haven't updated this yet for GUT 9.0.x.
When testing (not shown in the code I pasted) I tried to set the double strategy on each of the calls to double and partial double and it did not seem to change anything, though I did stop trying when the first test failed.
I believe you can work around this issue by changing the double strategy in gutconfig or at the script level. For some reason overriding the strategy when calling double/partial_double is not working when changing it from INCLUDE_SUPER -> SCRIPT_ONLY, but it does work the other way.
If you are using the command line you can do this by adding the following to your .gutconfig.json
"double_strategy": "script only",
If you are using the editor, or if you do not want to change the strategy everywhere (not a good idea if you have the warning enabled) you must set it per script/inner-test-class in before_all. GUT will reset it to the default after each script, and there is currently no way to set the double strategy in the editor.
func before_all():
set_double_strategy(DOUBLE_STRATEGY.SCRIPT_ONLY)
Let me know if this works
Edit You could set the default double strategy in a pre-hook script. You must configure this script to be run either through the editor or gutconfig.
extends GutHookScript
func run():
gut.double_strategy = GutTest.DOUBLE_STRATEGY.SCRIPT_ONLY # or .INCLUDE_SUPER
Hook info here: https://github.com/bitwes/Gut/wiki/Hooks
I read enough of this issue to think this is the cause of the new headache: https://github.com/godotengine/godot/issues/55024
The gist is, if you overload set_position then if you use something like thing.call('set_position') then the engine won't be using your overload. I think (if not, you get the idea). For Doubles, this is not really an issue. You won't be using them with the engine unless your integration tests get really complicated. But you may want to verify that something called set_position on your double. If you can't/don't use DOUBLE_STRATEGY.INCLUDE_SUPER then you cannot spy on these methods and use assert_called and the like.
You can set Project settings at runtime though, and I tested the code below and it worked as expected. It saves off the native_method_override value. Then it disables native_method_override and changes the double strategy before all tests are run. After all the tests are run it restores the previous value of native_method_override.
var orig_override_val = ProjectSettings.get_setting('debug/gdscript/warnings/native_method_override')
func before_all():
set_double_strategy(DOUBLE_STRATEGY.INCLUDE_SUPER)
ProjectSettings.set_setting('debug/gdscript/warnings/native_method_override', 0)
func after_all():
ProjectSettings.set_setting('debug/gdscript/warnings/native_method_override', orig_override_val)
# This test will create a double including all super functions and there won't be any
# warnings or errors
func test_double_extends_resource():
var dbl = double(TestI482).new()
assert_not_null(dbl)
You could also set native_method_override the in a pre-hook script (changing at runtime does not change the setting permanently). This allows GUT to do "illegal" stuff for testing purposes but helps protect you from doing it.
Now the question is how much should GUT try to help you through this. I think changing the default DOUBLE_STRATEGY is probably a good thing. GUT might be able to toggle the native_method_override before/after creating doubles. An ugly amount of documentation is probably needed too. It won't affect most people, but those edge cases are going to be horribly difficult to debug.
This before_all/after_all trick worked for a little code I was working on.
I don't have an immediate answer to your final question, about how much GUT should help. A concern I have, though I have not tested it, is that my project settings might get quietly changed if I run a test that doesn't exit properly and an implicit after_all does not get run.
Thank you for your prompt attention to this issue.
I verified that changing a ProjectSetting at runtime does not save the value. You must call ProjectSettings.save() for the change to be permanent.
If you are worried about the value leaking from one test script to another, the only way that I can think that would happen is if after_all has a an error and exits before it resets the value. If that happens you'll at least see the error in the logs. Tests that exit early will not prevent after_all from running.
I created another issue (#484 ) to consolidate the info here and give me a place up top to track what changes need to be done. I'm closing this out to avoid any confusion going forward.
FYI, this has been fixed and will be in the next release. See #488 and CHANGES.md in the godot_4 branch for info.