Skip to content

Commit

Permalink
Merge pull request #2426 from leancodepl/small-fixes-in-patrol-log
Browse files Browse the repository at this point in the history
Small fixes in patrol log
  • Loading branch information
pdenert authored Nov 22, 2024
2 parents f8bef0b + 5bf814a commit 7ab26fb
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 21 deletions.
6 changes: 6 additions & 0 deletions packages/patrol_log/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.2.0

- Fix report path when path contain spaces.
- Fix path to the test file on the failed test list in summary.
- Add `ConfigEntry`.

## 0.1.0

- Add option to not clear test steps.
Expand Down
22 changes: 10 additions & 12 deletions packages/patrol_log/lib/src/ansi_codes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ class AnsiCodes {
/// `[1m` - Set text to bold.
static const String bold = '$escape[1m';

/// `[30m` - Set text color to gray.
static const String gray = '$escape[30m';
static String color(String color) => '$escape[${color}m';

/// `[38;5;87m` - Set text color to light blue. Used for native actions.
static const String lightBlue = '$escape[38;5;87m';

static String custom(String color) => '$escape[${color}m';

static String get green => custom('32');
static String get yellow => custom('33');
static String get blue => custom('34');
static String get magenta => custom('35');
static String get cyan => custom('36');
static String gray = color('30');
static String red = color('31');
static String green = color('32');
static String yellow = color('33');
static String blue = color('34');
static String lightBlue = color('38;5;87');
static String magenta = color('35');
static String cyan = color('36');
static String orange = color('38;5;208');
}
30 changes: 30 additions & 0 deletions packages/patrol_log/lib/src/entries/config_entry.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
part of 'entry.dart';

@JsonSerializable()
class ConfigEntry extends Entry {
ConfigEntry({
required this.config,
DateTime? timestamp,
}) : super(
timestamp: timestamp ?? DateTime.now(),
type: EntryType.config,
);

@override
factory ConfigEntry.fromJson(Map<String, dynamic> json) =>
_$ConfigEntryFromJson(json);

final Map<String, dynamic> config;

@override
Map<String, dynamic> toJson() => _$ConfigEntryToJson(this);

@override
String pretty() => config.toString();

@override
String toString() => 'ConfigEntry(${toJson()})';

@override
List<Object?> get props => [config, timestamp, type];
}
8 changes: 7 additions & 1 deletion packages/patrol_log/lib/src/entries/entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ part 'error_entry.dart';
part 'log_entry.dart';
part 'step_entry.dart';
part 'test_entry.dart';
part 'warning_entry.dart';
part 'config_entry.dart';

part 'entry.g.dart';

Expand Down Expand Up @@ -39,14 +41,18 @@ enum EntryType {
error,
step,
test,
log;
log,
warning,
config;

static EntryType byName(String name) {
return switch (name) {
'error' => EntryType.error,
'step' => EntryType.step,
'test' => EntryType.test,
'log' => EntryType.log,
'warning' => EntryType.warning,
'config' => EntryType.config,
_ => throw ArgumentError('Unknown EntryType: $name')
};
}
Expand Down
30 changes: 30 additions & 0 deletions packages/patrol_log/lib/src/entries/entry.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/patrol_log/lib/src/entries/error_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ErrorEntry extends Entry {

@override
String pretty() {
return message;
return '${AnsiCodes.red}$message${AnsiCodes.reset}';
}

@override
Expand Down
32 changes: 32 additions & 0 deletions packages/patrol_log/lib/src/entries/warning_entry.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
part of 'entry.dart';

@JsonSerializable()
class WarningEntry extends Entry {
WarningEntry({
required this.message,
DateTime? timestamp,
}) : super(
timestamp: timestamp ?? DateTime.now(),
type: EntryType.warning,
);

@override
factory WarningEntry.fromJson(Map<String, dynamic> json) =>
_$WarningEntryFromJson(json);

final String message;

@override
Map<String, dynamic> toJson() => _$WarningEntryToJson(this);

@override
String pretty() {
return '${AnsiCodes.yellow}$message${AnsiCodes.reset}';
}

@override
String toString() => 'WarningEntry(${toJson()})';

@override
List<Object?> get props => [message, timestamp, type];
}
32 changes: 28 additions & 4 deletions packages/patrol_log/lib/src/patrol_log_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class PatrolLogReader {
StreamController<Entry>.broadcast();
late final StreamSubscription<Entry> _streamSubscription;

final Map<String, dynamic> _config = {};

void listen() {
// Listen to the entry stream and pretty print the patrol logs.
readEntries();
Expand Down Expand Up @@ -93,11 +95,10 @@ class PatrolLogReader {

/// Take line containg PATROL_LOG tag, parse it to [Entry] and add to stream.
void _parsePatrolLog(String line) {
final regExp = RegExp(r'PATROL_LOG \{(.*?)\}');
final regExp = RegExp('PATROL_LOG (.*)');
final match = regExp.firstMatch(line);
if (match != null) {
final matchedText = match.group(1)!;
final json = '{$matchedText}';
final json = match.group(1)!;
final entry = parseEntry(json);

if (entry case TestEntry _) {
Expand Down Expand Up @@ -146,6 +147,8 @@ class PatrolLogReader {
EntryType.test => TestEntry.fromJson(json),
EntryType.log => LogEntry.fromJson(json),
EntryType.error => ErrorEntry.fromJson(json),
EntryType.warning => WarningEntry.fromJson(json),
EntryType.config => ConfigEntry.fromJson(json),
};
}

Expand Down Expand Up @@ -205,12 +208,33 @@ class PatrolLogReader {
log(entry.pretty());

case ErrorEntry():
case WarningEntry():
log(entry.pretty());
case ConfigEntry():
_readConfig(entry);
}
},
);
}

/// Read the config passed by [PatrolLogWriter].
void _readConfig(ConfigEntry entry) {
if (_config.isNotEmpty) {
return;
}

_config.addAll(entry.config);

if (_config['printLogs'] == false) {
final warningEntry = WarningEntry(
message: 'Printing flutter steps is disabled in the config. '
'To enable it, set `PatrolTesterConfig(printLogs: true)`.',
);

log(warningEntry.pretty());
}
}

/// Returns a summary of the test results. That contains:
///
/// - Total number of tests
Expand All @@ -226,7 +250,7 @@ class PatrolLogReader {
'${Emojis.failure} Failed: $failedTestsCount\n'
'${failedTestsCount > 0 ? '$failedTestsList\n' : ''}'
'${Emojis.skip} Skipped: $skippedTests\n'
'${Emojis.report} Report: $reportPath\n'
'${Emojis.report} Report: ${reportPath.replaceAll(' ', '%20')}\n'
'${Emojis.duration} Duration: ${_stopwatch.elapsed.inSeconds}s\n';

/// Closes the stream subscription and the stream controller.
Expand Down
6 changes: 5 additions & 1 deletion packages/patrol_log/lib/src/patrol_log_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import 'dart:convert';
import 'package:patrol_log/patrol_log.dart';

class PatrolLogWriter {
PatrolLogWriter() : _controller = StreamController<Entry>.broadcast() {
PatrolLogWriter({Map<String, dynamic> config = const {}})
: _controller = StreamController<Entry>.broadcast() {
write();

/// Pass config to the PatrolLogReader
log(ConfigEntry(config: config));
}

final StreamController<Entry> _controller;
Expand Down
4 changes: 3 additions & 1 deletion packages/patrol_log/lib/src/patrol_single_test_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class PatrolSingleTestEntry {
closingTestEntry?.status ?? openingTestEntry.status;

/// The name of the test with the path.
String get nameWithPath => openingTestEntry.nameWithPath;
///
/// If test is not closed yet, the path is unknown, so return only test name.
String get nameWithPath => closingTestEntry?.nameWithPath ?? name;

/// The execution time of the test.
Duration get executionTime {
Expand Down
2 changes: 1 addition & 1 deletion packages/patrol_log/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: patrol_log
description: >
Log package for Patrol, a powerful Flutter-native UI testing framework.
version: 0.1.0
version: 0.2.0
homepage: https://patrol.leancode.co
repository: https://github.com/leancodepl/patrol/tree/master/packages/patrol_log
issue_tracker: https://github.com/leancodepl/patrol/issues
Expand Down

0 comments on commit 7ab26fb

Please sign in to comment.