-
Notifications
You must be signed in to change notification settings - Fork 29.8k
fix issue 12999: Make assets available during tests #31207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change will leave behind a folder 'test_assets' in root directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets put this under build
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but it is not necessary to have build folder when running test, should I just create the build folder here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like test already write something into build. fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will run at the beginning of every test. I make it so it will only mock the first time it is called, after that will be no op
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to move this into the binding initialization code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
This will potential be a breaking change. If developer also mocking the message channel in their code. It might cause unexpected behavior during the test. Not sure if we need to care about this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use fs.path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to happen after the pub check, so that any assets from package dependencies will already exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this function. Previously, it only mock when the first time it is called, and no op at later call.
That will pose an issue when some of our tests also mock the message channel for some specific test cases.
I updated it so it will mock or override existing mock every time it is called. To ensure the performance, I implement _ensureInitialized method to do the expansive operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these always strings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be. will you suggest using Set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set<String> would be preferable to dynamic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should call this something other than mock. mockFlutterAssets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, since all fields are static I would remove the class entirely and place them under the test bindings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this add measurable overhead when running the test over and over again? If so, we could potentially reuse the existing pubGet check, make it return a bool for whether or not the pubspec.yaml file had updated. Only if the pubspec file is updated, or if the asset directory doesn't exist, do we need to re-run ot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will only run once within a flutter test. The overhead is depends on the assets size.
I check the code in flutter run. the run cold will build it everytime, while run hot will check if the pubspec.yaml change or not. I think it is ok to always rebuild during test. if we want to be smart, we not only need to check the pubspec.yaml, we also need to check ant of assets files changed or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, thinking about it more - we can't really do this, since a user may edit an asset which would then not be copied.
We should expose a flag in the test command to disable this functionality though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean expose a flag to skip rebuilding asset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, similar to how we have a --pub,--no-pub flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the setMethodCallHandler method, then it wouldn't be a breaking change as the mocks would still take precedence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, settMockMessageHandler is setting _mockHandlers, which is used for outgoing message. setMethodCallHandler is setting _handlers, which is for in coming message. The assets request is outgoing message. I think setting the _handlers will not be able to intercept the request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh good point, my bad!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a flag.
Two thing concerns me here.
- Should we build the assets by default?
2, When no-assets, should we still try to see if there is test assets built in previous test and use it if it exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My current implementation is yes for both
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my two general concerns:
- Test startup speed - does building the asset bundle slow it down measurably? Try
time flutter test <<something_test.dart>>in the gallery and compare. - Users that are already mocking assets. Maybe
--no-assetsshould also not set the env variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't see too much difference. maybe 1 or 2 seconds slower out of 2mins. But I do want to point out one thing. The existing test that involve assets load are currently passed because of our fake async. The assets load never finished without my fix. Otherwise they will all fail. This might add some more running time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would name this something more specific like --no-test-assets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
|
Also, if you're concerned about rolling this out, you can always default it to off. though you'll want to make sure it is used by the gallery tests |
jonahwilliams
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I have no other concerns from a tooling perspective. This implementation leaves us some room to optimize later if we need to, without breaking any users.
|
@jonahwilliams I will like to enable the assets load by default since it is more intuitive to have the functionality enabled. After think a little bit more, this should not be a breaking change, either. We should be good to merge this pr |
|
This hurt test performance (see e.g. |
|
@chunhtai Did this PR cover |
|
this is only for flutter test. I believe flutter drive is using the really device and assets is already available? @jonahwilliams |
|
@gadfly361 flutter drive runs your real application - assets are already loaded |
|
@jonahwilliams @chunhtai Thank you for the quick reply! |
I found flutter drive can't load assets when running. |
Hi @leishuai |
|
@chunhtai |
|
FYI this PR seems to have caused #35907 |
Description
Make assets available during tests. This PR add a step to build asset bundle during test setup.
The test will load the bundle and mock the message channel to bypass the engine.
Related Issues
#12999
Tests
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]). This will ensure a smooth and quick review process.///).flutter analyze --flutter-repo) does not report any problems on my PR.Breaking Change
Does your PR require Flutter developers to manually update their apps to accommodate your change?