Reduce the chances of bots reporting analytics (#13915)
* First version * Prevent modification of .flutter during analytics test * Pass in directory and override analyzer warning due to conditional import * Review comments
This commit is contained in:
parent
ad898ad03f
commit
e25d881046
@ -27,6 +27,12 @@ bool get isRunningOnBot {
|
||||
// https://www.appveyor.com/docs/environment-variables/
|
||||
platform.environment.containsKey('APPVEYOR') ||
|
||||
|
||||
// https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
|
||||
(platform.environment.containsKey('AWS_REGION') && platform.environment.containsKey('CODEBUILD_INITIATOR')) ||
|
||||
|
||||
// https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables
|
||||
platform.environment.containsKey('JENKINS_URL') ||
|
||||
|
||||
// Properties on Flutter's Chrome Infra bots.
|
||||
platform.environment['CHROME_HEADLESS'] == '1' ||
|
||||
platform.environment.containsKey('BUILDBOT_BUILDERNAME');
|
||||
|
@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
|
||||
import 'package:usage/usage_io.dart';
|
||||
|
||||
import 'base/context.dart';
|
||||
import 'base/file_system.dart';
|
||||
import 'base/os.dart';
|
||||
import 'base/platform.dart';
|
||||
import 'base/utils.dart';
|
||||
@ -19,11 +20,16 @@ const String _kFlutterUA = 'UA-67589403-6';
|
||||
Usage get flutterUsage => Usage.instance;
|
||||
|
||||
class Usage {
|
||||
/// Create a new Usage instance; [versionOverride] is used for testing.
|
||||
Usage({ String settingsName: 'flutter', String versionOverride }) {
|
||||
/// Create a new Usage instance; [versionOverride] and [configDirOverride] are
|
||||
/// used for testing.
|
||||
Usage({ String settingsName: 'flutter', String versionOverride, String configDirOverride}) {
|
||||
final FlutterVersion flutterVersion = FlutterVersion.instance;
|
||||
final String version = versionOverride ?? flutterVersion.getVersionString(whitelistBranchName: true);
|
||||
_analytics = new AnalyticsIO(_kFlutterUA, settingsName, version);
|
||||
_analytics = new AnalyticsIO(_kFlutterUA, settingsName, version,
|
||||
// Analyzer doesn't recognize that [Directory] objects match up due to a
|
||||
// conditional import.
|
||||
// ignore: argument_type_not_assignable
|
||||
documentDirectory: configDirOverride != null ? fs.directory(configDirOverride) : null);
|
||||
|
||||
// Report a more detailed OS version string than package:usage does by default.
|
||||
_analytics.setSessionValue('cd1', os.name);
|
||||
@ -33,19 +39,13 @@ class Usage {
|
||||
if (platform.environment.containsKey('FLUTTER_HOST')) {
|
||||
_analytics.setSessionValue('aiid', platform.environment['FLUTTER_HOST']);
|
||||
}
|
||||
|
||||
bool runningOnCI = false;
|
||||
_analytics.analyticsOpt = AnalyticsOpt.optOut;
|
||||
|
||||
// Many CI systems don't do a full git checkout.
|
||||
if (version.endsWith('/unknown'))
|
||||
runningOnCI = true;
|
||||
|
||||
// Check for common CI systems.
|
||||
if (isRunningOnBot)
|
||||
runningOnCI = true;
|
||||
|
||||
// If we think we're running on a CI system, default to not sending analytics.
|
||||
_analytics.analyticsOpt = runningOnCI ? AnalyticsOpt.optIn : AnalyticsOpt.optOut;
|
||||
if (version.endsWith('/unknown') || isRunningOnBot) {
|
||||
// If we think we're running on a CI system, suppress sending analytics.
|
||||
suppressAnalytics = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns [Usage] active in the current app context.
|
||||
|
@ -55,7 +55,7 @@ void main() {
|
||||
expect(count, 0);
|
||||
}, overrides: <Type, Generator>{
|
||||
FlutterVersion: () => new FlutterVersion(const Clock()),
|
||||
Usage: () => new Usage(),
|
||||
Usage: () => new Usage(configDirOverride: temp.path),
|
||||
});
|
||||
|
||||
// Ensure we don't send for the 'flutter config' command.
|
||||
@ -74,7 +74,7 @@ void main() {
|
||||
expect(count, 0);
|
||||
}, overrides: <Type, Generator>{
|
||||
FlutterVersion: () => new FlutterVersion(const Clock()),
|
||||
Usage: () => new Usage(),
|
||||
Usage: () => new Usage(configDirOverride: temp.path),
|
||||
});
|
||||
});
|
||||
|
||||
@ -134,6 +134,11 @@ void main() {
|
||||
});
|
||||
|
||||
group('analytics bots', () {
|
||||
Directory temp;
|
||||
setUp(() {
|
||||
temp = fs.systemTempDirectory.createTempSync('flutter_tools');
|
||||
});
|
||||
|
||||
testUsingContext('don\'t send on bots', () async {
|
||||
int count = 0;
|
||||
flutterUsage.onSend.listen((Map<String, dynamic> data) => count++);
|
||||
@ -144,6 +149,22 @@ void main() {
|
||||
Usage: () => new Usage(
|
||||
settingsName: 'flutter_bot_test',
|
||||
versionOverride: 'dev/unknown',
|
||||
configDirOverride: temp.path,
|
||||
),
|
||||
});
|
||||
|
||||
testUsingContext('don\'t send on bots even when opted in', () async {
|
||||
int count = 0;
|
||||
flutterUsage.onSend.listen((Map<String, dynamic> data) => count++);
|
||||
flutterUsage.enabled = true;
|
||||
|
||||
await createTestCommandRunner().run(<String>['--version']);
|
||||
expect(count, 0);
|
||||
}, overrides: <Type, Generator>{
|
||||
Usage: () => new Usage(
|
||||
settingsName: 'flutter_bot_test',
|
||||
versionOverride: 'dev/unknown',
|
||||
configDirOverride: temp.path,
|
||||
),
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user