packages icon indicating copy to clipboard operation
packages copied to clipboard

[go_router] Don't log if `hierarchicalLoggingEnabled` is `true`

Open ValentinVignal opened this issue 1 year ago • 3 comments

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

If you need help, consider asking for advice on the #hackers-new channel on Discord.

ValentinVignal avatar Jan 31 '24 09:01 ValentinVignal

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.

flutter-dashboard[bot] avatar Jan 31 '24 09:01 flutter-dashboard[bot]

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 hierarchicalLoggingEnabled set to false and verify that developer.log is called or something is logged in the console.
  • 1 with hierarchicalLoggingEnabled set to true and verify that developer.log is 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?

ValentinVignal avatar Jan 31 '24 09:01 ValentinVignal

@chunhtai Any idea how I can write a test for that?

ValentinVignal avatar Feb 26 '24 12:02 ValentinVignal

Hello @chunhtai, do you have any thoughts on this?

ValentinVignal avatar Mar 15 '24 07:03 ValentinVignal

Hello @chunhtai @johnpryan @hangyujin , do you have any idea/feedback for the tests?

ValentinVignal avatar Apr 01 '24 10:04 ValentinVignal

I think you have to get someone on discord for the review

cedvdb avatar Apr 02 '24 08:04 cedvdb

@cedvdb Would you know which channel I should use ? I tried hackers-routing-🛤️ , but no one answered

ValentinVignal avatar Apr 02 '24 09:04 ValentinVignal

@ValentinVignal I will take a look at this PR, also @chunhtai will be back in a week

hannah-hyj avatar Apr 02 '24 19:04 hannah-hyj

@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?

ValentinVignal avatar Apr 13 '24 15:04 ValentinVignal

Hi @ValentinVignal, can you bump version?

hannah-hyj avatar Apr 24 '24 22:04 hannah-hyj

@ValentinVignal looks like the ci is still not happy

chunhtai avatar Apr 25 '24 21:04 chunhtai

@chunhtai It should be good now

ValentinVignal avatar Apr 29 '24 10:04 ValentinVignal