Skip to content

Commit

Permalink
Fix printing of log messages on test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Oct 2, 2023
1 parent 22500ea commit 3968315
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
24 changes: 16 additions & 8 deletions pkgs/native_assets_builder/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,30 @@ Future<void> copyTestProjects({
}
}

/// Logger that outputs the full trace when a test fails.
final logger = Logger('')
..level = Level.ALL
..onRecord.listen((record) {
printOnFailure(record.message);
});
Logger get logger => _logger ??= () {
addTearDown(() => _logger = null);
return _createTestLogger();
}();

Logger? _logger;

Logger createCapturingLogger(
List<String> capturedMessages, {
Level level = Level.ALL,
}) =>
_createTestLogger(capturedMessages: capturedMessages, level: level);

Logger _createTestLogger({
List<String>? capturedMessages,
Level level = Level.ALL,
}) =>
Logger('')
..level = level
..onRecord.listen((record) {
printOnFailure(record.message);
capturedMessages.add(record.message);
printOnFailure(
'${record.level.name}: ${record.time}: ${record.message}',
);
capturedMessages?.add(record.message);
});

final dartExecutable = File(Platform.resolvedExecutable).uri;
4 changes: 3 additions & 1 deletion pkgs/native_assets_cli/example/native_add_library/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ void main(List<String> args) async {
// `package:native_toolchain_c` will output the dynamic or static libraries it built,
// what files it accessed (for caching the build), etc.
buildOutput: buildOutput,
logger: Logger('')..onRecord.listen((record) => print(record.message)),
logger: Logger('')
..level = Level.ALL
..onRecord.listen((record) => print(record.message)),
);

// Write the output according to the native assets protocol so that Dart or
Expand Down
21 changes: 13 additions & 8 deletions pkgs/native_toolchain_c/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,23 @@ Future<Uri> tempDirForTest({String? prefix, bool keepTemp = false}) async {
return tempUri;
}

/// Logger that outputs the full trace when a test fails.
final logger = Logger('')
..level = Level.ALL
..onRecord.listen((record) {
printOnFailure('${record.level.name}: ${record.time}: ${record.message}');
});
Logger get logger => _logger ??= () {
// We lazily create a new logger for each test so that the messages
// captured by printOnFailure are scoped to the correct test.
addTearDown(() => _logger = null);
return _createTestLogger();
}();

Logger? _logger;

Logger createCapturingLogger(List<String> capturedMessages) =>
_createTestLogger(capturedMessages: capturedMessages);

Logger createCapturingLogger(List<String> capturedMessages) => Logger('')
Logger _createTestLogger({List<String>? capturedMessages}) => Logger('')
..level = Level.ALL
..onRecord.listen((record) {
printOnFailure('${record.level.name}: ${record.time}: ${record.message}');
capturedMessages.add(record.message);
capturedMessages?.add(record.message);
});

/// Test files are run in a variety of ways, find this package root in all.
Expand Down

0 comments on commit 3968315

Please sign in to comment.