Skip to content

Commit

Permalink
refactor log_handler_test.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkolos committed Jun 5, 2024
1 parent e5d4c8b commit edded56
Showing 1 changed file with 47 additions and 35 deletions.
82 changes: 47 additions & 35 deletions pkgs/unified_analytics/test/log_handler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,20 @@ import 'package:unified_analytics/src/utils.dart';
import 'package:unified_analytics/unified_analytics.dart';

void main() {
late FakeAnalytics analytics;
late Directory homeDirectory;
late MemoryFileSystem fs;
late File logFile;

final testEvent = Event.hotReloadTime(timeMs: 10);

setUp(() {
fs = MemoryFileSystem.test(style: FileSystemStyle.posix);
homeDirectory = fs.directory('home');
logFile = fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kLogFileName,
));

// Create the initialization analytics instance to onboard the tool
final initializationAnalytics = Analytics.fake(
tool: DashTool.flutterTool,
homeDirectory: homeDirectory,
dartVersion: 'dartVersion',
fs: fs,
platform: DevicePlatform.macos,
);
initializationAnalytics.clientShowedMessage();

// This instance is free to send events since the instance above
// has confirmed that the client has shown the message
analytics = Analytics.fake(
tool: DashTool.flutterTool,
homeDirectory: homeDirectory,
dartVersion: 'dartVersion',
fs: fs,
platform: DevicePlatform.macos,
);
});

test('Ensure that log file is created', () {
final (fs, logFile, analytics) = _setUpFakeAnalytics();
expect(logFile.existsSync(), true);
});

test('LogFileStats is null before events are sent', () {
final (fs, logFile, analytics) = _setUpFakeAnalytics();
expect(analytics.logFileStats(), isNull);
});

test('LogFileStats returns valid response after sent events', () async {
final (fs, logFile, analytics) = _setUpFakeAnalytics();
final countOfEventsToSend = 10;

for (var i = 0; i < countOfEventsToSend; i++) {
Expand All @@ -71,6 +39,8 @@ void main() {
});

test('The only record in the log file is malformed', () async {
final (fs, logFile, analytics) = _setUpFakeAnalytics();

// Write invalid json for the only log record
logFile.writeAsStringSync('{{\n');

Expand All @@ -93,6 +63,8 @@ void main() {
});

test('The first record is malformed, but rest are valid', () async {
final (fs, logFile, analytics) = _setUpFakeAnalytics();

// Write invalid json for the only log record
logFile.writeAsStringSync('{{\n');

Expand All @@ -109,6 +81,8 @@ void main() {
});

test('Several records are malformed', () async {
final (fs, logFile, analytics) = _setUpFakeAnalytics();

final countOfMalformedRecords = 4;
for (var i = 0; i < countOfMalformedRecords; i++) {
final currentContents = logFile.readAsStringSync();
Expand Down Expand Up @@ -136,6 +110,8 @@ void main() {
});

test('Valid json but invalid keys', () {
final (fs, logFile, analytics) = _setUpFakeAnalytics();

// The second line here is missing the "events" top level
// key which should cause an error for that record only
//
Expand All @@ -157,6 +133,8 @@ void main() {
});

test('Malformed record gets phased out after several events', () async {
final (fs, logFile, analytics) = _setUpFakeAnalytics();

// Write invalid json for the only log record
logFile.writeAsStringSync('{{\n');

Expand Down Expand Up @@ -192,6 +170,8 @@ void main() {
});

test('Catching cast errors for each log record silently', () async {
final (fs, logFile, analytics) = _setUpFakeAnalytics();

// Write a json array to the log file which will cause
// a cast error when parsing each line
logFile.writeAsStringSync('[{}, 1, 2, 3]\n');
Expand Down Expand Up @@ -269,3 +249,35 @@ void main() {
expect(newString, testString);
});
}

(FileSystem fs, File logFile, FakeAnalytics analytics) _setUpFakeAnalytics() {
final fs = MemoryFileSystem.test(style: FileSystemStyle.posix);
final homeDirectory = fs.directory('home');
final logFile = fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kLogFileName,
));

// Create the initialization analytics instance to onboard the tool
final initializationAnalytics = Analytics.fake(
tool: DashTool.flutterTool,
homeDirectory: homeDirectory,
dartVersion: 'dartVersion',
fs: fs,
platform: DevicePlatform.macos,
);
initializationAnalytics.clientShowedMessage();

// This instance is free to send events since the instance above
// has confirmed that the client has shown the message
final analytics = Analytics.fake(
tool: DashTool.flutterTool,
homeDirectory: homeDirectory,
dartVersion: 'dartVersion',
fs: fs,
platform: DevicePlatform.macos,
);

return (fs, logFile, analytics);
}

0 comments on commit edded56

Please sign in to comment.