Skip to content

Commit

Permalink
[analyzer] fix an exception when run on platforms w/o home dirs
Browse files Browse the repository at this point in the history
Bug: #37308
Change-Id: I2e117a678bfc99dcc3f48d4a58ea5895d0079261
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107580
Commit-Queue: Devon Carew <devoncarew@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
  • Loading branch information
devoncarew authored and commit-bot@chromium.org committed Jun 28, 2019
1 parent 881ca94 commit 983447f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
43 changes: 40 additions & 3 deletions pkg/telemetry/lib/telemetry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:io';

import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'package:usage/src/usage_impl.dart';
import 'package:usage/src/usage_impl_io.dart';
Expand Down Expand Up @@ -52,14 +53,26 @@ String createAnalyticsStatusMessage(
///
/// This analytics instance will share a common enablement state with the rest
/// of the Dart SDK tools.
_TelemetryAnalytics createAnalyticsInstance(
Analytics createAnalyticsInstance(
String trackingId,
String applicationName, {
bool disableForSession: false,
}) {
Directory dir = getDartStorageDirectory();
if (dir == null) {
// Some systems don't support user home directories; for those, fail
// gracefully by returning a disabled analytics object.
return new _DisabledAnalytics(trackingId, applicationName);
}

if (!dir.existsSync()) {
dir.createSync();
try {
dir.createSync();
} catch (e) {
// If we can't create the directory for the analytics settings, fail
// gracefully by returning a disabled analytics object.
return new _DisabledAnalytics(trackingId, applicationName);
}
}

File file = new File(path.join(dir.path, _settingsFileName));
Expand All @@ -71,8 +84,15 @@ _TelemetryAnalytics createAnalyticsInstance(
///
/// Typically, the directory is `~/.dart/` (and the settings file is
/// `analytics.json`).
///
/// This can return null under some conditions, including when the user's home
/// directory does not exist.
@visibleForTesting
Directory getDartStorageDirectory() {
return new Directory(path.join(userHomeDir(), _dartDirectoryName));
Directory homeDirectory = new Directory(userHomeDir());
if (!homeDirectory.existsSync()) return null;

return new Directory(path.join(homeDirectory.path, _dartDirectoryName));
}

/// Return the version of the Dart SDK.
Expand Down Expand Up @@ -111,6 +131,23 @@ class _TelemetryAnalytics extends AnalyticsImpl {
}
}

class _DisabledAnalytics extends AnalyticsMock {
@override
final String trackingId;
@override
final String applicationName;

_DisabledAnalytics(this.trackingId, this.applicationName);

@override
bool get enabled => false;
}

/// Detect whether we're running on a bot or in a continuous testing
/// environment.
///
/// We should periodically keep this code up to date with
/// https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/base/utils.dart#L20.
bool isRunningOnBot() {
final Map<String, String> env = Platform.environment;

Expand Down
1 change: 1 addition & 0 deletions pkg/telemetry/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ environment:

dependencies:
http: ^0.12.0
meta: ^1.0.2
path: ^1.4.0
stack_trace: ^1.7.0
usage: ^3.2.0+1
Expand Down

0 comments on commit 983447f

Please sign in to comment.