|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
| 5 | +import 'dart:convert'; |
| 6 | + |
5 | 7 | import 'package:file/file.dart'; |
6 | 8 | import 'package:file/memory.dart'; |
7 | 9 | import 'package:path/path.dart' as p; |
| 10 | +import 'package:test/fake.dart'; |
8 | 11 | import 'package:test/test.dart'; |
9 | 12 |
|
10 | 13 | import 'package:unified_analytics/src/constants.dart'; |
@@ -212,6 +215,47 @@ void main() { |
212 | 215 | logHandler.save(data: {}); |
213 | 216 | }); |
214 | 217 |
|
| 218 | + test('deletes log file larger than kMaxLogFileSize', () async { |
| 219 | + var deletedLargeLogFile = false; |
| 220 | + var wroteDataToLogFile = false; |
| 221 | + const data = <String, Object?>{}; |
| 222 | + final logFile = _FakeFile('log.txt') |
| 223 | + .._deleteSyncImpl = (() => deletedLargeLogFile = true) |
| 224 | + .._createSyncImpl = () {} |
| 225 | + .._statSyncImpl = (() => _FakeFileStat(kMaxLogFileSize + 1)) |
| 226 | + .._writeAsStringSync = (contents, {mode = FileMode.append}) { |
| 227 | + expect(contents.trim(), data.toString()); |
| 228 | + expect(mode, FileMode.writeOnlyAppend); |
| 229 | + wroteDataToLogFile = true; |
| 230 | + }; |
| 231 | + final logHandler = LogHandler(logFile: logFile); |
| 232 | + |
| 233 | + logHandler.save(data: data); |
| 234 | + expect(deletedLargeLogFile, isTrue); |
| 235 | + expect(wroteDataToLogFile, isTrue); |
| 236 | + }); |
| 237 | + |
| 238 | + test('does not delete log file if smaller than kMaxLogFileSize', () async { |
| 239 | + var wroteDataToLogFile = false; |
| 240 | + const data = <String, Object?>{}; |
| 241 | + final logFile = _FakeFile('log.txt') |
| 242 | + .._deleteSyncImpl = |
| 243 | + (() => fail('called logFile.deleteSync() when file was less than ' |
| 244 | + 'kMaxLogFileSize')) |
| 245 | + .._createSyncImpl = () {} |
| 246 | + .._readAsLinesSyncImpl = (() => ['three', 'previous', 'lines']) |
| 247 | + .._statSyncImpl = (() => _FakeFileStat(kMaxLogFileSize - 1)) |
| 248 | + .._writeAsStringSync = (contents, {mode = FileMode.append}) { |
| 249 | + expect(contents.trim(), data.toString()); |
| 250 | + expect(mode, FileMode.writeOnlyAppend); |
| 251 | + wroteDataToLogFile = true; |
| 252 | + }; |
| 253 | + final logHandler = LogHandler(logFile: logFile); |
| 254 | + |
| 255 | + logHandler.save(data: data); |
| 256 | + expect(wroteDataToLogFile, isTrue); |
| 257 | + }); |
| 258 | + |
215 | 259 | test('Catching cast errors for each log record silently', () async { |
216 | 260 | // Write a json array to the log file which will cause |
217 | 261 | // a cast error when parsing each line |
@@ -290,3 +334,51 @@ void main() { |
290 | 334 | expect(newString, testString); |
291 | 335 | }); |
292 | 336 | } |
| 337 | + |
| 338 | +class _FakeFileStat extends Fake implements FileStat { |
| 339 | + _FakeFileStat(this.size); |
| 340 | + |
| 341 | + @override |
| 342 | + final int size; |
| 343 | +} |
| 344 | + |
| 345 | +class _FakeFile extends Fake implements File { |
| 346 | + _FakeFile(this.path); |
| 347 | + |
| 348 | + List<String> Function()? _readAsLinesSyncImpl; |
| 349 | + |
| 350 | + @override |
| 351 | + List<String> readAsLinesSync({Encoding encoding = utf8}) => |
| 352 | + _readAsLinesSyncImpl!(); |
| 353 | + |
| 354 | + @override |
| 355 | + final String path; |
| 356 | + |
| 357 | + FileStat Function()? _statSyncImpl; |
| 358 | + |
| 359 | + @override |
| 360 | + FileStat statSync() => _statSyncImpl!(); |
| 361 | + |
| 362 | + void Function()? _deleteSyncImpl; |
| 363 | + |
| 364 | + @override |
| 365 | + void deleteSync({bool recursive = false}) => _deleteSyncImpl!(); |
| 366 | + |
| 367 | + void Function()? _createSyncImpl; |
| 368 | + |
| 369 | + @override |
| 370 | + void createSync({bool recursive = false, bool exclusive = false}) { |
| 371 | + return _createSyncImpl!(); |
| 372 | + } |
| 373 | + |
| 374 | + void Function(String contents, {FileMode mode})? _writeAsStringSync; |
| 375 | + |
| 376 | + @override |
| 377 | + void writeAsStringSync( |
| 378 | + String contents, { |
| 379 | + FileMode mode = FileMode.write, |
| 380 | + Encoding encoding = utf8, |
| 381 | + bool flush = false, |
| 382 | + }) => |
| 383 | + _writeAsStringSync!(contents, mode: mode); |
| 384 | +} |
0 commit comments