Update logic for getting Analytics instance from package:unified_analytics (#134756)
Part of: - https://github.com/flutter/flutter/issues/128251 Currently, when we want to use the analytics instance from `package:unified_analytics`, we are just grabbing it from globals. However, with the legacy analytics instance, there are some things we check to return a no-op version of the instance.. for example, if we are running on bots or a non standard branch, we use a no-op instance This PR uses the same previous checks for the new analytics instance
This commit is contained in:
@@ -61,6 +61,7 @@ import 'persistent_tool_state.dart';
|
||||
import 'reporting/crash_reporting.dart';
|
||||
import 'reporting/first_run.dart';
|
||||
import 'reporting/reporting.dart';
|
||||
import 'reporting/unified_analytics.dart';
|
||||
import 'resident_runner.dart';
|
||||
import 'run_hot.dart';
|
||||
import 'runner/local_engine.dart';
|
||||
@@ -88,11 +89,10 @@ Future<T> runInContext<T>(
|
||||
body: runnerWrapper,
|
||||
overrides: overrides,
|
||||
fallbacks: <Type, Generator>{
|
||||
Analytics: () => Analytics(
|
||||
tool: DashTool.flutterTool,
|
||||
flutterChannel: globals.flutterVersion.channel,
|
||||
flutterVersion: globals.flutterVersion.frameworkVersion,
|
||||
dartVersion: globals.flutterVersion.dartSdkVersion,
|
||||
Analytics: () => getAnalytics(
|
||||
runningOnBot: runningOnBot,
|
||||
flutterVersion: globals.flutterVersion,
|
||||
environment: globals.platform.environment,
|
||||
),
|
||||
AndroidBuilder: () => AndroidGradleBuilder(
|
||||
java: globals.java,
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:unified_analytics/unified_analytics.dart';
|
||||
|
||||
import '../version.dart';
|
||||
|
||||
/// This function is called from within the context runner to perform
|
||||
/// checks that are necessary for determining if a no-op version of
|
||||
/// [Analytics] gets returned.
|
||||
///
|
||||
/// When [enableAsserts] is set to `true`, various assert statements
|
||||
/// will be enabled to ensure usage of this class is within GA4 limitations.
|
||||
///
|
||||
/// For testing purposes, pass in a [FakeAnalytics] instance initialized with
|
||||
/// an in-memory [FileSystem] to prevent writing to disk.
|
||||
Analytics getAnalytics({
|
||||
required bool runningOnBot,
|
||||
required FlutterVersion flutterVersion,
|
||||
required Map<String, String> environment,
|
||||
bool enableAsserts = false,
|
||||
FakeAnalytics? analyticsOverride,
|
||||
}) {
|
||||
final String version = flutterVersion.getVersionString(redactUnknownBranches: true);
|
||||
final bool suppressEnvFlag = environment['FLUTTER_SUPPRESS_ANALYTICS']?.toLowerCase() == 'true';
|
||||
|
||||
if (// Ignore local user branches.
|
||||
version.startsWith('[user-branch]') ||
|
||||
// Many CI systems don't do a full git checkout.
|
||||
version.endsWith('/unknown') ||
|
||||
// Ignore bots.
|
||||
runningOnBot ||
|
||||
// Ignore when suppressed by FLUTTER_SUPPRESS_ANALYTICS.
|
||||
suppressEnvFlag) {
|
||||
return NoOpAnalytics();
|
||||
}
|
||||
|
||||
// Providing an override of the [Analytics] instance is preferred when
|
||||
// running tests for this function to prevent writing to the filesystem
|
||||
if (analyticsOverride != null) {
|
||||
return analyticsOverride;
|
||||
}
|
||||
|
||||
return Analytics(
|
||||
tool: DashTool.flutterTool,
|
||||
flutterChannel: flutterVersion.channel,
|
||||
flutterVersion: flutterVersion.frameworkVersion,
|
||||
dartVersion: flutterVersion.dartSdkVersion,
|
||||
enableAsserts: enableAsserts,
|
||||
);
|
||||
}
|
||||
@@ -48,7 +48,7 @@ dependencies:
|
||||
http_multi_server: 3.2.1
|
||||
convert: 3.1.1
|
||||
async: 2.11.0
|
||||
unified_analytics: 4.0.0
|
||||
unified_analytics: 4.0.1
|
||||
|
||||
cli_config: 0.1.1
|
||||
graphs: 2.3.1
|
||||
@@ -112,4 +112,4 @@ dartdoc:
|
||||
# Exclude this package from the hosted API docs.
|
||||
nodoc: true
|
||||
|
||||
# PUBSPEC CHECKSUM: 887b
|
||||
# PUBSPEC CHECKSUM: 0f7c
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/reporting/unified_analytics.dart';
|
||||
import 'package:unified_analytics/src/enums.dart';
|
||||
import 'package:unified_analytics/unified_analytics.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/fakes.dart';
|
||||
|
||||
void main() {
|
||||
const String userBranch = 'abc123';
|
||||
const String homeDirectoryName = 'home';
|
||||
const DashTool tool = DashTool.flutterTool;
|
||||
|
||||
late FileSystem fs;
|
||||
late Directory home;
|
||||
late FakeAnalytics analyticsOverride;
|
||||
|
||||
setUp(() {
|
||||
fs = MemoryFileSystem.test();
|
||||
home = fs.directory(homeDirectoryName);
|
||||
|
||||
// Prepare the tests by "onboarding" the tool into the package
|
||||
// by invoking the [clientShowedMessage] method for the provided
|
||||
// [tool]
|
||||
final FakeAnalytics initialAnalytics = FakeAnalytics(
|
||||
tool: tool,
|
||||
homeDirectory: home,
|
||||
dartVersion: '3.0.0',
|
||||
platform: DevicePlatform.macos,
|
||||
fs: fs,
|
||||
surveyHandler: SurveyHandler(
|
||||
homeDirectory: home,
|
||||
fs: fs,
|
||||
),
|
||||
);
|
||||
initialAnalytics.clientShowedMessage();
|
||||
|
||||
analyticsOverride = FakeAnalytics(
|
||||
tool: tool,
|
||||
homeDirectory: home,
|
||||
dartVersion: '3.0.0',
|
||||
platform: DevicePlatform.macos,
|
||||
fs: fs,
|
||||
surveyHandler: SurveyHandler(
|
||||
homeDirectory: home,
|
||||
fs: fs,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
group('Unit testing getAnalytics', () {
|
||||
testWithoutContext('Successfully creates the instance for standard branch', () {
|
||||
final Analytics analytics = getAnalytics(
|
||||
runningOnBot: false,
|
||||
flutterVersion: FakeFlutterVersion(),
|
||||
environment: const <String, String>{},
|
||||
analyticsOverride: analyticsOverride,
|
||||
);
|
||||
|
||||
expect(analytics.clientId, isNot(NoOpAnalytics.staticClientId),
|
||||
reason: 'The CLIENT ID should be a randomly generated id');
|
||||
expect(analytics, isNot(isA<NoOpAnalytics>()));
|
||||
});
|
||||
|
||||
testWithoutContext('NoOp instance for user branch', () {
|
||||
final Analytics analytics = getAnalytics(
|
||||
runningOnBot: false,
|
||||
flutterVersion: FakeFlutterVersion(
|
||||
branch: userBranch,
|
||||
frameworkRevision: '3.14.0-14.0.pre.370',
|
||||
),
|
||||
environment: const <String, String>{},
|
||||
analyticsOverride: analyticsOverride,
|
||||
);
|
||||
|
||||
expect(
|
||||
analytics.clientId,
|
||||
NoOpAnalytics.staticClientId,
|
||||
reason: 'The client ID should match the NoOp client id',
|
||||
);
|
||||
expect(analytics, isA<NoOpAnalytics>());
|
||||
});
|
||||
|
||||
testWithoutContext('NoOp instance for unknown branch', () {
|
||||
final Analytics analytics = getAnalytics(
|
||||
runningOnBot: false,
|
||||
flutterVersion: FakeFlutterVersion(
|
||||
frameworkRevision: 'unknown',
|
||||
),
|
||||
environment: const <String, String>{},
|
||||
analyticsOverride: analyticsOverride,
|
||||
);
|
||||
|
||||
expect(
|
||||
analytics.clientId,
|
||||
NoOpAnalytics.staticClientId,
|
||||
reason: 'The client ID should match the NoOp client id',
|
||||
);
|
||||
expect(analytics, isA<NoOpAnalytics>());
|
||||
});
|
||||
|
||||
testWithoutContext('NoOp instance when running on bots', () {
|
||||
final Analytics analytics = getAnalytics(
|
||||
runningOnBot: true,
|
||||
flutterVersion: FakeFlutterVersion(),
|
||||
environment: const <String, String>{},
|
||||
analyticsOverride: analyticsOverride,
|
||||
);
|
||||
|
||||
expect(
|
||||
analytics.clientId,
|
||||
NoOpAnalytics.staticClientId,
|
||||
reason: 'The client ID should match the NoOp client id',
|
||||
);
|
||||
expect(analytics, isA<NoOpAnalytics>());
|
||||
});
|
||||
|
||||
testWithoutContext('NoOp instance when suppressing via env variable', () {
|
||||
final Analytics analytics = getAnalytics(
|
||||
runningOnBot: true,
|
||||
flutterVersion: FakeFlutterVersion(),
|
||||
environment: const <String, String>{'FLUTTER_SUPPRESS_ANALYTICS': 'true'},
|
||||
analyticsOverride: analyticsOverride,
|
||||
);
|
||||
|
||||
expect(
|
||||
analytics.clientId,
|
||||
NoOpAnalytics.staticClientId,
|
||||
reason: 'The client ID should match the NoOp client id',
|
||||
);
|
||||
expect(analytics, isA<NoOpAnalytics>());
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -35,6 +35,7 @@ import 'package:flutter_tools/src/reporting/reporting.dart';
|
||||
import 'package:flutter_tools/src/version.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:test/fake.dart';
|
||||
import 'package:unified_analytics/unified_analytics.dart';
|
||||
|
||||
import 'common.dart';
|
||||
import 'fake_http_client.dart';
|
||||
@@ -120,6 +121,7 @@ void testUsingContext(
|
||||
Pub: () => ThrowingPub(), // prevent accidentally using pub.
|
||||
CrashReporter: () => const NoopCrashReporter(),
|
||||
TemplateRenderer: () => const MustacheTemplateRenderer(),
|
||||
Analytics: () => NoOpAnalytics(),
|
||||
},
|
||||
body: () {
|
||||
return runZonedGuarded<Future<dynamic>>(() {
|
||||
|
||||
@@ -426,7 +426,7 @@ class FakeFlutterVersion implements FlutterVersion {
|
||||
|
||||
@override
|
||||
String getVersionString({bool redactUnknownBranches = false}) {
|
||||
return 'v0.0.0';
|
||||
return '${getBranchName(redactUnknownBranches: redactUnknownBranches)}/$frameworkRevision';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user