Skip to content
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

[go_router] Don't log if hierarchicalLoggingEnabled is true #6019

Conversation

ValentinVignal
Copy link
Contributor

Fixes flutter/flutter#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.

@flutter-dashboard
Copy link

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.

@ValentinVignal
Copy link
Contributor Author

I would like to ask for some help with the tests here.

Here is the setLogging method:

/// Forwards diagnostic messages to the dart:developer log() API.
void setLogging({bool enabled = false}) {
_subscription?.cancel();
_enabled = enabled;
if (!enabled) {
return;
}
_subscription = logger.onRecord.listen((LogRecord e) {
// use `dumpErrorToConsole` for severe messages to ensure that severe
// exceptions are formatted consistently with other Flutter examples and
// avoids printing duplicate exceptions
if (e.level >= Level.SEVERE) {
final Object? error = e.error;
FlutterError.dumpErrorToConsole(
FlutterErrorDetails(
exception: error is Exception ? error : Exception(error),
stack: e.stackTrace,
library: e.loggerName,
context: ErrorDescription(e.message),
),
);
} else {
developer.log(
e.message,
time: e.time,
sequenceNumber: e.sequenceNumber,
level: e.level.value,
name: e.loggerName,
zone: e.zone,
error: e.error,
stackTrace: e.stackTrace,
);
}
});
}

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?

…-if-hierarchical-logging-enabled-is-true

# Conflicts:
#	packages/go_router/CHANGELOG.md
#	packages/go_router/pubspec.yaml
…enabled-is-true

# Conflicts:
#	packages/go_router/CHANGELOG.md
@ValentinVignal
Copy link
Contributor Author

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

@ValentinVignal
Copy link
Contributor Author

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

@ValentinVignal ValentinVignal force-pushed the go-router/don-t-log-if-hierarchical-logging-enabled-is-true branch from df6f51d to 998a25e Compare March 16, 2024 09:29
@ValentinVignal
Copy link
Contributor Author

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

@cedvdb
Copy link
Contributor

cedvdb commented Apr 2, 2024

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

@ValentinVignal
Copy link
Contributor Author

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

@hangyujin
Copy link
Contributor

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

/// Called when this route is removed from GoRouter's route history.
///
/// Corresponds to [GoRoute.onExit].
FutureOr<bool> onExit(BuildContext context) => true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this from other patch? we should probably separate this out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yeah, that's a mistake. I reverted it

@@ -72,6 +72,8 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
// This should be the only place where the last GoRoute exit the screen.
final GoRoute lastRoute = currentConfiguration.last.route;
if (lastRoute.onExit != null && navigatorKey.currentContext != null) {
walker.buildState(_configuration, _configuration.);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unrelated?

…-if-hierarchical-logging-enabled-is-true

# Conflicts:
#	packages/go_router/CHANGELOG.md
…-if-hierarchical-logging-enabled-is-true

# Conflicts:
#	packages/go_router/CHANGELOG.md
@ValentinVignal
Copy link
Contributor Author

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

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chunhtai chunhtai requested a review from hangyujin April 18, 2024 21:41
Copy link
Contributor

@hangyujin hangyujin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hangyujin
Copy link
Contributor

Hi @ValentinVignal, can you bump version?

…-if-hierarchical-logging-enabled-is-true

# Conflicts:
#	packages/go_router/CHANGELOG.md
#	packages/go_router/pubspec.yaml
@chunhtai
Copy link
Contributor

@ValentinVignal looks like the ci is still not happy

…-if-hierarchical-logging-enabled-is-true

# Conflicts:
#	packages/go_router/CHANGELOG.md
@ValentinVignal
Copy link
Contributor Author

@chunhtai It should be good now

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 29, 2024
@auto-submit auto-submit bot merged commit 9aa04eb into flutter:main Apr 29, 2024
78 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 30, 2024
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Apr 30, 2024
flutter/packages@87a7c51...cc47b06

2024-04-30 joonas.kerttula@codemate.com [google_maps_flutter_web] Add marker clustering support (flutter/packages#6187)
2024-04-30 joonas.kerttula@codemate.com [google_maps_flutter_android] Add marker clustering support (flutter/packages#6185)
2024-04-29 32538273+ValentinVignal@users.noreply.github.com [go_router] Don't log if `hierarchicalLoggingEnabled` is `true`  (flutter/packages#6019)
2024-04-29 43054281+camsim99@users.noreply.github.com [file_selector_android] Update `LICENSE` file to include newly added licensed code (flutter/packages#6626)
2024-04-29 43054281+camsim99@users.noreply.github.com [file_selector_android] Modifies `getDirectoryPath`, `openFile`, `openFiles` to return file/directory paths instead of URIs (flutter/packages#6438)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
TecHaxter pushed a commit to TecHaxter/flutter_packages that referenced this pull request May 22, 2024
…ter#6019)

Fixes flutter/flutter#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].*
arc-yong pushed a commit to Arctuition/packages-arc that referenced this pull request Jun 14, 2024
…ter#6019)

Fixes flutter/flutter#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].*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: go_router
Projects
None yet
4 participants