[go_router] Don't log if `hierarchicalLoggingEnabled` is `true`
Fixes https://github.com/flutter/flutter/issues/139667
If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.
Pre-launch Checklist
- [x] I read the Contributor Guide and followed the process outlined there for submitting PRs.
- [x] I read the Tree Hygiene wiki page, which explains my responsibilities.
- [x] I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use
dart format.) - [x] I signed the CLA.
- [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g.
[shared_preferences] - [x] I linked to at least one issue that this PR fixes in the description above.
- [x] I updated
pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes. - [x] I updated
CHANGELOG.mdto add a description of the change, following repository CHANGELOG style. - [x] I updated/added relevant documentation (doc comments with
///). - [ ] I added new tests to check the change I am making, or this PR is test-exempt.
- [x] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel on Discord.
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact "@test-exemption-reviewer" in the #hackers channel in Chat (don't just cc them here, they won't see it! Use Discord!).
If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?
Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.
I would like to ask for some help with the tests here.
Here is the setLogging method:
https://github.com/flutter/packages/blob/5b48c446976fd5f61316d58ab3fcc9888423c2e6/packages/go_router/lib/src/logging.dart#L27-L62
My idea was to write 2 tests (both with _enabled set to true):
- 1 with
hierarchicalLoggingEnabledset tofalseand verify thatdeveloper.logis called or something is logged in the console. - 1 with
hierarchicalLoggingEnabledset totrueand verify thatdeveloper.logis not called or nothing is logged in the console.
I first wrote this test:
testWidgets(
'It should log the known routes and the initial route if debugLogDiagnostics is false',
(WidgetTester tester) async {
final _FakeStdout stdout = _FakeStdout();
IOOverrides.runZoned(
() {
final List<String> logs = <String>[];
Logger.root.onRecord.listen(
(LogRecord event) => logs.add(event.message),
);
GoRouter(
debugLogDiagnostics: true,
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (_, GoRouterState state) => const Text('home'),
),
],
);
expect(
logs,
const <String>[
'Full paths for routes:\n => /\n',
'setting initial location null'
],
);
expect(stdout.writtenObjects, isNotEmpty);
},
stdout: () => stdout,
);
},
);
But stdout.writtenObjects is empty. I cannot find a way to verify that developer.log is called.
Do you have any suggestion?
@chunhtai Any idea how I can write a test for that?
Hello @chunhtai, do you have any thoughts on this?
Hello @chunhtai @johnpryan @hangyujin , do you have any idea/feedback for the tests?
I think you have to get someone on discord for the review
@cedvdb Would you know which channel I should use ? I tried hackers-routing-🛤️ , but no one answered
@ValentinVignal I will take a look at this PR, also @chunhtai will be back in a week
@chunhtai I refactored logging to be able to mock developer.log in refactor: Add mockable developerLog method and added some tests in test: Update the tests. Is that what you were expecting?
Hi @ValentinVignal, can you bump version?
@ValentinVignal looks like the ci is still not happy
@chunhtai It should be good now