-
Notifications
You must be signed in to change notification settings - Fork 62
delete old log file if it exceeds kMaxLogFileSize of 25MiB #277
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ description: >- | |
to Google Analytics. | ||
# When updating this, keep the version consistent with the changelog and the | ||
# value in lib/src/constants.dart. | ||
version: 6.1.1 | ||
version: 6.1.2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As an FYI, for the publishing automation we have set up for this repo, you generally either want to have the pubspec version end in Docs for this are at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ahh, I missed the last step (link in the comment to publish). Thanks. |
||
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics | ||
|
||
environment: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,12 @@ | |
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
|
||
import 'package:file/file.dart'; | ||
import 'package:file/memory.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:test/fake.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'package:unified_analytics/src/constants.dart'; | ||
|
@@ -212,6 +215,47 @@ void main() { | |
logHandler.save(data: {}); | ||
}); | ||
|
||
test('deletes log file larger than kMaxLogFileSize', () async { | ||
var deletedLargeLogFile = false; | ||
var wroteDataToLogFile = false; | ||
const data = <String, Object?>{}; | ||
final logFile = _FakeFile('log.txt') | ||
.._deleteSyncImpl = (() => deletedLargeLogFile = true) | ||
.._createSyncImpl = () {} | ||
.._statSyncImpl = (() => _FakeFileStat(kMaxLogFileSize + 1)) | ||
.._writeAsStringSync = (contents, {mode = FileMode.append}) { | ||
expect(contents.trim(), data.toString()); | ||
expect(mode, FileMode.writeOnlyAppend); | ||
wroteDataToLogFile = true; | ||
}; | ||
final logHandler = LogHandler(logFile: logFile); | ||
|
||
logHandler.save(data: data); | ||
expect(deletedLargeLogFile, isTrue); | ||
expect(wroteDataToLogFile, isTrue); | ||
}); | ||
|
||
test('does not delete log file if smaller than kMaxLogFileSize', () async { | ||
var wroteDataToLogFile = false; | ||
const data = <String, Object?>{}; | ||
final logFile = _FakeFile('log.txt') | ||
.._deleteSyncImpl = | ||
(() => fail('called logFile.deleteSync() when file was less than ' | ||
'kMaxLogFileSize')) | ||
.._createSyncImpl = () {} | ||
.._readAsLinesSyncImpl = (() => ['three', 'previous', 'lines']) | ||
.._statSyncImpl = (() => _FakeFileStat(kMaxLogFileSize - 1)) | ||
.._writeAsStringSync = (contents, {mode = FileMode.append}) { | ||
expect(contents.trim(), data.toString()); | ||
expect(mode, FileMode.writeOnlyAppend); | ||
wroteDataToLogFile = true; | ||
}; | ||
final logHandler = LogHandler(logFile: logFile); | ||
|
||
logHandler.save(data: data); | ||
expect(wroteDataToLogFile, isTrue); | ||
}); | ||
|
||
test('Catching cast errors for each log record silently', () async { | ||
// Write a json array to the log file which will cause | ||
// a cast error when parsing each line | ||
|
@@ -290,3 +334,51 @@ void main() { | |
expect(newString, testString); | ||
}); | ||
} | ||
|
||
class _FakeFileStat extends Fake implements FileStat { | ||
_FakeFileStat(this.size); | ||
|
||
@override | ||
final int size; | ||
} | ||
|
||
class _FakeFile extends Fake implements File { | ||
_FakeFile(this.path); | ||
Comment on lines
+345
to
+346
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why use mutable fields for method implementations over optional constructor parameters? Do you find this more readable? |
||
|
||
List<String> Function()? _readAsLinesSyncImpl; | ||
|
||
@override | ||
List<String> readAsLinesSync({Encoding encoding = utf8}) => | ||
_readAsLinesSyncImpl!(); | ||
|
||
@override | ||
final String path; | ||
|
||
FileStat Function()? _statSyncImpl; | ||
|
||
@override | ||
FileStat statSync() => _statSyncImpl!(); | ||
|
||
void Function()? _deleteSyncImpl; | ||
|
||
@override | ||
void deleteSync({bool recursive = false}) => _deleteSyncImpl!(); | ||
|
||
void Function()? _createSyncImpl; | ||
|
||
@override | ||
void createSync({bool recursive = false, bool exclusive = false}) { | ||
return _createSyncImpl!(); | ||
} | ||
|
||
void Function(String contents, {FileMode mode})? _writeAsStringSync; | ||
|
||
@override | ||
void writeAsStringSync( | ||
String contents, { | ||
FileMode mode = FileMode.write, | ||
Encoding encoding = utf8, | ||
bool flush = false, | ||
}) => | ||
_writeAsStringSync!(contents, mode: mode); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.