Unverified Commit 51b23bc9 authored by Elias Yishak's avatar Elias Yishak Committed by GitHub

Update runner to handle logic for both analytics packages (#124606)

Update runner to handle logic for both analytics packages
parent 2e4d976b
......@@ -22,6 +22,7 @@ import 'src/context_runner.dart';
import 'src/doctor.dart';
import 'src/globals.dart' as globals;
import 'src/reporting/crash_reporting.dart';
import 'src/reporting/first_run.dart';
import 'src/reporting/reporting.dart';
import 'src/runner/flutter_command.dart';
import 'src/runner/flutter_command_runner.dart';
......@@ -62,19 +63,6 @@ Future<int> run(
StackTrace? firstStackTrace;
return runZoned<Future<int>>(() async {
try {
// Ensure that the consent message has been displayed
if (globals.analytics.shouldShowMessage) {
globals.logger.printStatus(globals.analytics.getConsentMessage);
// Invoking this will onboard the flutter tool onto
// the package on the developer's machine and will
// allow for events to be sent to Google Analytics
// on subsequent runs of the flutter tool (ie. no events
// will be sent on the first run to allow developers to
// opt out of collection)
globals.analytics.clientShowedMessage();
}
// Disable analytics if user passes in the `--disable-telemetry` option
// `flutter --disable-telemetry`
//
......@@ -100,6 +88,14 @@ Future<int> run(
await globals.analytics.setTelemetry(value);
}
// If the user has opted out of legacy analytics, we will continue
// to opt them out of unified analytics and inform them
if (!globals.flutterUsage.enabled && globals.analytics.telemetryEnabled) {
await globals.analytics.setTelemetry(false);
globals.logger.printStatus(
'Please note that analytics reporting was already disabled, and will continue to be disabled.\n');
}
await runner.run(args);
// Triggering [runZoned]'s error callback does not necessarily mean that
......@@ -275,9 +271,44 @@ Future<File> _createLocalCrashReport(CrashDetails details) async {
}
Future<int> _exit(int code, {required ShutdownHooks shutdownHooks}) async {
// Prints the welcome message if needed.
// Need to get the boolean returned from `messenger.shouldDisplayLicenseTerms()`
// before invoking the print welcome method because the print welcome method
// will set `messenger.shouldDisplayLicenseTerms()` to false
final FirstRunMessenger messenger =
FirstRunMessenger(persistentToolState: globals.persistentToolState!);
final bool legacyAnalyticsMessageShown =
messenger.shouldDisplayLicenseTerms();
// Prints the welcome message if needed for legacy analytics.
globals.flutterUsage.printWelcome();
// Ensure that the consent message has been displayed for unified analytics
if (globals.analytics.shouldShowMessage) {
globals.logger.printStatus(globals.analytics.getConsentMessage);
// Because the legacy analytics may have also sent a message,
// the conditional below will print additional messaging informing
// users that the two consent messages they are receiving is not a
// bug
if (legacyAnalyticsMessageShown) {
globals.logger
.printStatus('You have received two consent messages because '
'the flutter tool is migrating to a new analytics system. '
'Disabling analytics collection will disable both the legacy '
'and new analytics collection systems. '
'You can disable analytics reporting by running either `flutter --disable-telemetry` '
'or `flutter config --no-analytics\n');
}
// Invoking this will onboard the flutter tool onto
// the package on the developer's machine and will
// allow for events to be sent to Google Analytics
// on subsequent runs of the flutter tool (ie. no events
// will be sent on the first run to allow developers to
// opt out of collection)
globals.analytics.clientShowedMessage();
}
// Send any last analytics calls that are in progress without overly delaying
// the tool's exit (we wait a maximum of 250ms).
if (globals.flutterUsage.enabled) {
......
......@@ -316,28 +316,65 @@ void main() {
});
});
testUsingContext('runner disable telemetry with flag', () async {
io.setExitFunctionForTests((int exitCode) {});
expect(globals.analytics.telemetryEnabled, true);
expect(globals.analytics.shouldShowMessage, true);
await runner.run(
<String>['--disable-telemetry'],
() => <FlutterCommand>[],
// This flutterVersion disables crash reporting.
flutterVersion: '[user-branch]/',
shutdownHooks: ShutdownHooks(),
);
group('unified_analytics', () {
final Usage legacyAnalytics = TestUsage();
setUp(() {
legacyAnalytics.enabled = false;
});
expect(globals.analytics.telemetryEnabled, false);
},
overrides: <Type, Generator>{
Analytics: () => FakeAnalytics(),
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
},
);
testUsingContext(
'runner disable telemetry with flag',
() async {
io.setExitFunctionForTests((int exitCode) {});
expect(globals.analytics.telemetryEnabled, true);
expect(globals.analytics.shouldShowMessage, true);
await runner.run(
<String>['--disable-telemetry'],
() => <FlutterCommand>[],
// This flutterVersion disables crash reporting.
flutterVersion: '[user-branch]/',
shutdownHooks: ShutdownHooks(),
);
expect(globals.analytics.telemetryEnabled, false);
},
overrides: <Type, Generator>{
Analytics: () => FakeAnalytics(),
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
},
);
testUsingContext(
'legacy analytics disabled will disable new analytics',
() async {
io.setExitFunctionForTests((int exitCode) {});
await runner.run(
<String>[],
() => <FlutterCommand>[],
// This flutterVersion disables crash reporting.
flutterVersion: '[user-branch]/',
shutdownHooks: ShutdownHooks(),
);
expect(globals.flutterUsage.enabled, false);
expect(globals.analytics.telemetryEnabled, false);
expect(testLogger.statusText.contains(
'Please note that analytics '
'reporting was already disabled'), true);
},
overrides: <Type, Generator>{
Analytics: () => FakeAnalytics(),
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
Usage: () => legacyAnalytics,
},
);
});
}
class CrashingFlutterCommand extends FlutterCommand {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment