Skip to content

Patch tests for unified_analytics 5.8.8+2 fixes #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/unified_analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ permissions: read-all

on:
pull_request:
branches: [ main ]
paths:
- '.github/workflows/unified_analytics.yml'
- 'pkgs/unified_analytics/**'
Expand Down
24 changes: 15 additions & 9 deletions pkgs/unified_analytics/lib/src/log_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:convert';

import 'package:clock/clock.dart';
import 'package:file/file.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;

import 'constants.dart';
Expand Down Expand Up @@ -161,16 +162,21 @@ class LogFileStats {
class LogHandler {
final FileSystem fs;
final Directory homeDirectory;
final File logFile;
File _logFile;
final ErrorHandler _errorHandler;

File get logFile => _logFile;

@visibleForTesting
set logFile(File value) => _logFile = value;

/// A log handler constructor that will delegate saving
/// logs and retrieving stats from the persisted log.
LogHandler({
required this.fs,
required this.homeDirectory,
required ErrorHandler errorHandler,
}) : logFile = fs.file(p.join(
}) : _logFile = fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kLogFileName,
Expand All @@ -186,7 +192,7 @@ class LogHandler {
// Parse each line of the log file through [LogItem],
// some returned records may be null if malformed, they will be
// removed later through `whereType<LogItem>`
final records = logFile
final records = _logFile
.readAsLinesSync()
.map((String e) {
try {
Expand Down Expand Up @@ -283,26 +289,26 @@ class LogHandler {
/// or less than [kLogFileLength] records.
void save({required Map<String, Object?> data}) {
try {
final stat = logFile.statSync();
final stat = _logFile.statSync();
List<String> records;
if (stat.size > kMaxLogFileSize) {
logFile.deleteSync();
logFile.createSync();
_logFile.deleteSync();
_logFile.createSync();
records = [];
} else {
records = logFile.readAsLinesSync();
records = _logFile.readAsLinesSync();
}
final content = '${jsonEncode(data)}\n';

// When the record count is less than the max, add as normal;
// else drop the oldest records until equal to max
if (records.length < kLogFileLength) {
logFile.writeAsStringSync(content, mode: FileMode.writeOnlyAppend);
_logFile.writeAsStringSync(content, mode: FileMode.writeOnlyAppend);
} else {
records.add(content);
records = records.skip(records.length - kLogFileLength).toList();

logFile.writeAsStringSync(records.join('\n'));
_logFile.writeAsStringSync(records.join('\n'));
}
} on FileSystemException {
// Logging isn't important enough to warrant raising a
Expand Down
32 changes: 22 additions & 10 deletions pkgs/unified_analytics/test/log_handler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:test/test.dart';

import 'package:unified_analytics/src/constants.dart';
import 'package:unified_analytics/src/enums.dart';
import 'package:unified_analytics/src/error_handler.dart';
import 'package:unified_analytics/src/log_handler.dart';
import 'package:unified_analytics/src/utils.dart';
import 'package:unified_analytics/unified_analytics.dart';
Expand Down Expand Up @@ -207,19 +208,20 @@ void main() {
test(
'Catches and discards any FileSystemException raised from attempting '
'to write to the log file', () async {
final logFilePath = 'log.txt';
final fs = MemoryFileSystem.test(opHandle: (context, operation) {
if (context == logFilePath && operation == FileSystemOp.write) {
if (context.endsWith(kLogFileName) && operation == FileSystemOp.write) {
throw FileSystemException(
'writeFrom failed',
logFilePath,
context,
const OSError('No space left on device', 28),
);
}
});
final logFile = fs.file(logFilePath);
logFile.createSync();
final logHandler = LogHandler(logFile: logFile);
final logHandler = LogHandler(
fs: fs,
homeDirectory: fs.currentDirectory,
errorHandler: ErrorHandler(sendFunction: (_) {}),
);

logHandler.save(data: {});
});
Expand All @@ -228,7 +230,13 @@ void main() {
var deletedLargeLogFile = false;
var wroteDataToLogFile = false;
const data = <String, Object?>{};
final logFile = _FakeFile('log.txt')

final logHandler = LogHandler(
fs: fs,
homeDirectory: fs.currentDirectory,
errorHandler: ErrorHandler(sendFunction: (_) {}),
);
logHandler.logFile = _FakeFile('log.txt')
.._deleteSyncImpl = (() => deletedLargeLogFile = true)
.._createSyncImpl = () {}
.._statSyncImpl = (() => _FakeFileStat(kMaxLogFileSize + 1))
Expand All @@ -237,7 +245,6 @@ void main() {
expect(mode, FileMode.writeOnlyAppend);
wroteDataToLogFile = true;
};
final logHandler = LogHandler(logFile: logFile);

logHandler.save(data: data);
expect(deletedLargeLogFile, isTrue);
Expand All @@ -247,7 +254,7 @@ void main() {
test('does not delete log file if smaller than kMaxLogFileSize', () async {
var wroteDataToLogFile = false;
const data = <String, Object?>{};
final logFile = _FakeFile('log.txt')
final fakeLogFile = _FakeFile('log.txt')
.._deleteSyncImpl =
(() => fail('called logFile.deleteSync() when file was less than '
'kMaxLogFileSize'))
Expand All @@ -259,7 +266,12 @@ void main() {
expect(mode, FileMode.writeOnlyAppend);
wroteDataToLogFile = true;
};
final logHandler = LogHandler(logFile: logFile);
final logHandler = LogHandler(
fs: fs,
homeDirectory: fs.currentDirectory,
errorHandler: ErrorHandler(sendFunction: (_) {}),
);
logHandler.logFile = fakeLogFile;

logHandler.save(data: data);
expect(wroteDataToLogFile, isTrue);
Expand Down