From 3fe4ca0a99bd6da70fa7df99eabeafca8402ac23 Mon Sep 17 00:00:00 2001 From: Polina Cherkasova Date: Thu, 13 Jul 2023 14:19:02 -0700 Subject: [PATCH] Enable turn on/off tracking for leak types for a test. (#92) --- doc/TROUBLESHOOT.md | 2 +- pkgs/leak_tracker/CHANGELOG.md | 5 ++++ .../src/leak_tracking/leak_tracker_model.dart | 30 +++++++++++++++---- pkgs/leak_tracker/pubspec.yaml | 2 +- pkgs/leak_tracker_flutter_test/pubspec.yaml | 2 +- .../test/end_to_end_test.dart | 2 +- .../test/test_infra/helpers.dart | 6 ++-- pkgs/leak_tracker_testing/pubspec.yaml | 2 +- 8 files changed, 37 insertions(+), 14 deletions(-) diff --git a/doc/TROUBLESHOOT.md b/doc/TROUBLESHOOT.md index 4ca2365b..a942a0d7 100644 --- a/doc/TROUBLESHOOT.md +++ b/doc/TROUBLESHOOT.md @@ -51,7 +51,7 @@ Or, you can temporarily set global flag, to make all tests collecting debug info ``` setUpAll(() { - collectDebugInformationForLeaks = true; + LeakTrackerGlobalFlags.collectDebugInformationForLeaks = true; }); ``` diff --git a/pkgs/leak_tracker/CHANGELOG.md b/pkgs/leak_tracker/CHANGELOG.md index eae6017a..9a9e1092 100644 --- a/pkgs/leak_tracker/CHANGELOG.md +++ b/pkgs/leak_tracker/CHANGELOG.md @@ -1,3 +1,8 @@ +# 8.0.0 + +* Enable turn on/off tracking for leak types. +* Put all global flags into one class. + # 7.0.8 * Disconnect from service after obtaining retaining paths. diff --git a/pkgs/leak_tracker/lib/src/leak_tracking/leak_tracker_model.dart b/pkgs/leak_tracker/lib/src/leak_tracking/leak_tracker_model.dart index 61564a50..0ca71e0f 100644 --- a/pkgs/leak_tracker/lib/src/leak_tracking/leak_tracker_model.dart +++ b/pkgs/leak_tracker/lib/src/leak_tracking/leak_tracker_model.dart @@ -4,8 +4,16 @@ import '../shared/shared_model.dart'; -/// If true, the leak tracker will collect debug information for leaks. -bool collectDebugInformationForLeaks = false; +// ignore: avoid_classes_with_only_static_members, as it is ok for enum-like classes. +/// Global settings for leak tracker. +class LeakTrackerGlobalSettings { + /// If true, the leak tracker will collect debug information for leaks. + static bool collectDebugInformationForLeaks = false; + + /// If true, a warning will be printed when leak tracking is + /// requested for a non-supported platform. + static bool warnForNonSupportedPlatforms = true; +} /// Handler to collect leak summary. typedef LeakSummaryCallback = void Function(LeakSummary); @@ -115,6 +123,8 @@ class LeakTrackingConfiguration { /// /// Customized configuration is needed only for test debugging, /// not for regular test runs. +// TODO(polina-c): update helpers to respect allow lists defined in this class +// https://github.com/flutter/devtools/issues/5606 class LeakTrackingTestConfig { /// Creates a new instance of [LeakTrackingTestConfig]. const LeakTrackingTestConfig({ @@ -123,6 +133,8 @@ class LeakTrackingTestConfig { this.failTestOnLeaks = true, this.notGCedAllowList = const {}, this.notDisposedAllowList = const {}, + this.allowAllNotDisposed = false, + this.allowAllNotGCed = false, }); /// Creates a new instance of [LeakTrackingTestConfig] for debugging leaks. @@ -139,6 +151,8 @@ class LeakTrackingTestConfig { this.failTestOnLeaks = true, this.notGCedAllowList = const {}, this.notDisposedAllowList = const {}, + this.allowAllNotDisposed = false, + this.allowAllNotGCed = false, }); /// Creates a new instance of [LeakTrackingTestConfig] to collect retaining path. @@ -153,12 +167,10 @@ class LeakTrackingTestConfig { this.failTestOnLeaks = true, this.notGCedAllowList = const {}, this.notDisposedAllowList = const {}, + this.allowAllNotDisposed = false, + this.allowAllNotGCed = false, }); - /// If true, a warning will be printed when leak tracking is - /// requested for a non-supported platform. - static bool warnForNonSupportedPlatforms = true; - /// When to collect stack trace information. /// /// Knowing call stack may help to troubleshoot memory leaks. @@ -192,4 +204,10 @@ class LeakTrackingTestConfig { /// /// If number of instances is [null], any number of instances is allowed. final Map notDisposedAllowList; + + /// If true, all notDisposed leaks will be allowed. + final bool allowAllNotDisposed; + + /// If true, all notGCed leaks will be allowed. + final bool allowAllNotGCed; } diff --git a/pkgs/leak_tracker/pubspec.yaml b/pkgs/leak_tracker/pubspec.yaml index bf1f0165..8663aa8e 100644 --- a/pkgs/leak_tracker/pubspec.yaml +++ b/pkgs/leak_tracker/pubspec.yaml @@ -1,5 +1,5 @@ name: leak_tracker -version: 7.0.8 +version: 8.0.0 description: A framework for memory leak tracking for Dart and Flutter applications. repository: https://github.com/dart-lang/leak_tracker/tree/main/pkgs/leak_tracker diff --git a/pkgs/leak_tracker_flutter_test/pubspec.yaml b/pkgs/leak_tracker_flutter_test/pubspec.yaml index 277a3fff..1d6acd8b 100644 --- a/pkgs/leak_tracker_flutter_test/pubspec.yaml +++ b/pkgs/leak_tracker_flutter_test/pubspec.yaml @@ -13,7 +13,7 @@ dev_dependencies: sdk: flutter integration_test: sdk: flutter - leak_tracker: ^7.0.4 + leak_tracker: # Version is not specified here, because it is overridden below. leak_tracker_testing: path: ../leak_tracker_testing layerlens: diff --git a/pkgs/leak_tracker_flutter_test/test/end_to_end_test.dart b/pkgs/leak_tracker_flutter_test/test/end_to_end_test.dart index 855fa623..fd6346c3 100644 --- a/pkgs/leak_tracker_flutter_test/test/end_to_end_test.dart +++ b/pkgs/leak_tracker_flutter_test/test/end_to_end_test.dart @@ -49,7 +49,7 @@ void main() { late Leaks leaks; setUp( - () => collectDebugInformationForLeaks = true, + () => LeakTrackerGlobalSettings.collectDebugInformationForLeaks = true, ); testWidgetsWithLeakTracking( diff --git a/pkgs/leak_tracker_flutter_test/test/test_infra/helpers.dart b/pkgs/leak_tracker_flutter_test/test/test_infra/helpers.dart index 6905daef..8dfdd68e 100644 --- a/pkgs/leak_tracker_flutter_test/test/test_infra/helpers.dart +++ b/pkgs/leak_tracker_flutter_test/test/test_infra/helpers.dart @@ -35,7 +35,7 @@ void testWidgetsWithLeakTracking( LeakTrackingTestConfig? leakTrackingTestConfig, }) { final config = leakTrackingTestConfig ?? - (collectDebugInformationForLeaks + (LeakTrackerGlobalSettings.collectDebugInformationForLeaks ? LeakTrackingTestConfig.debug() : const LeakTrackingTestConfig()); @@ -83,11 +83,11 @@ Future withFlutterLeakTracking( // Leak tracker does not work for web platform. if (kIsWeb) { final bool shouldPrintWarning = !_webWarningPrinted && - LeakTrackingTestConfig.warnForNonSupportedPlatforms; + LeakTrackerGlobalSettings.warnForNonSupportedPlatforms; if (shouldPrintWarning) { _webWarningPrinted = true; debugPrint( - 'Leak tracking is not supported on web platform.\nTo turn off this message, set `LeakTrackingTestConfig.warnForNonSupportedPlatforms` to false.', + 'Leak tracking is not supported on web platform.\nTo turn off this message, set `LeakTrackerGlobalFlags.warnForNonSupportedPlatforms` to false.', ); } await callback(); diff --git a/pkgs/leak_tracker_testing/pubspec.yaml b/pkgs/leak_tracker_testing/pubspec.yaml index beb55602..82b0cd7f 100644 --- a/pkgs/leak_tracker_testing/pubspec.yaml +++ b/pkgs/leak_tracker_testing/pubspec.yaml @@ -7,7 +7,7 @@ environment: sdk: '>=3.0.0 <4.0.0' dependencies: - leak_tracker: ^7.0.4 + leak_tracker: # Version is not specified here, because it is overridden below. test: ^1.16.0 dev_dependencies: