Skip to content

Commit

Permalink
test: add unit tests for updated progress logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jtdLab authored and felangel committed Jul 2, 2024
1 parent 454ec72 commit 565dacc
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 21 deletions.
1 change: 0 additions & 1 deletion packages/mason_logger/lib/src/progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ class Progress {

/// Update the progress message.
void update(String update) {
if (_timer != null) _write(_clearPrevMessage);
_message = update;
_onTick(_timer);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mason_logger/test/src/mason_logger_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void main() {

when(() => stdout.supportsAnsiEscapes).thenReturn(true);
when(() => stdout.hasTerminal).thenReturn(true);
when(() => stdout.terminalColumns).thenReturn(80);
});

group('theme', () {
Expand Down Expand Up @@ -622,7 +623,6 @@ void main() {
group('.progress', () {
test('writes lines to stdout', () async {
when(() => stdout.hasTerminal).thenReturn(true);
when(() => stdout.terminalColumns).thenReturn(16);
await IOOverrides.runZoned(
() async {
const time = '(0.Xs)';
Expand Down
172 changes: 153 additions & 19 deletions packages/mason_logger/test/src/progress_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void main() {
stdout = _MockStdout();
when(() => stdout.supportsAnsiEscapes).thenReturn(true);
when(() => stdout.hasTerminal).thenReturn(true);
when(() => stdout.terminalColumns).thenReturn(1);
});

test('writes ms when elapsed time is less than 0.1s', () async {
Expand Down Expand Up @@ -202,10 +203,11 @@ void main() {
});

group('.complete', () {
test('writes lines to stdout', () async {
test('writes lines shorter than terminal columns to stdout', () async {
await _runZoned(
() async {
const message = 'test message';
when(() => stdout.terminalColumns).thenReturn(32);
const message = 'short message';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
progress.complete();
Expand All @@ -215,18 +217,51 @@ void main() {
any(
that: matches(
RegExp(
r'\[2K\u000D\[92m⠙\[0m test message... \[90m\(8\dms\)\[0m',
r'\[92m⠙\[0m short message... \[90m\(\d{2}ms\)\[0m',
),
),
),
);
},
).called(1);
verify(
() {
stdout.write(
'''\r✓ short message (0.1s)\n''',
);
},
).called(1);
},
stdout: () => stdout,
zoneValues: {AnsiCode: true},
);
});

test('writes lines longer than terminal columns to stdout', () async {
await _runZoned(
() async {
when(() => stdout.terminalColumns).thenReturn(32);
const message = 'looooooooooooooooonger message';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
progress.complete();
verify(
() {
stdout.write(
any(
that: matches(
RegExp(
r'\[92m⠙\[0m looooooooooooooooonger message... \[90m\(\d{2}ms\)\[0m',
),
),
),
);
},
).called(1);
verify(
() {
stdout.write(
'''[2K\r[92m✓[0m test message [90m(0.1s)[0m\n''',
'''[2K[1A[2K\r[92m✓[0m looooooooooooooooonger message [90m(0.1s)[0m\n''',
);
},
).called(1);
Expand All @@ -252,43 +287,86 @@ void main() {
});

group('.update', () {
test('writes lines to stdout', () async {
test('writes lines shorter than terminal columns to stdout', () async {
await _runZoned(
() async {
const message = 'message';
const update = 'update';
when(() => stdout.terminalColumns).thenReturn(32);
const message = 'short message';
const update = 'short update';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
progress.update(update);
await Future<void>.delayed(const Duration(milliseconds: 100));

verify(
() {
stdout.write(
any(
that: matches(
RegExp(
r'\[2K\u000D\[92m⠙\[0m message... \[90m\(8\dms\)\[0m',
r'\[92m⠙\[0m short message... \[90m\(\d{2}ms\)\[0m',
),
),
),
);
},
).called(1);
verify(
() {
stdout.write(
'''\r⠹ short update... (0.1s)''',
);
},
).called(1);
verify(
() {
stdout.write(
'''\r⠸ short update... (0.2s)''',
);
},
).called(1);
},
stdout: () => stdout,
zoneValues: {AnsiCode: true},
);
});

test('writes lines longer than terminal columns to stdout', () async {
await _runZoned(
() async {
when(() => stdout.terminalColumns).thenReturn(32);
const message = 'looooooooooooooooonger message';
const update = 'looooooooooooooooonger update';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
progress.update(update);
await Future<void>.delayed(const Duration(milliseconds: 100));
verify(
() {
stdout.write(
any(
that: matches(
RegExp(
r'\[2K\u000D\[92m\[0m update... \[90m\(0\.1s\)\[0m',
r'\[92m\[0m looooooooooooooooonger message... \[90m\(\d{2}ms\)\[0m',
),
),
),
);
},
).called(1);
verify(
() {
stdout.write(
'''\r⠹ looooooooooooooooonger update... (0.1s)''',
);
},
).called(1);
verify(
() {
stdout.write(
'''\r⠸ looooooooooooooooonger update... (0.2s)''',
);
},
).called(1);
},
stdout: () => stdout,
zoneValues: {AnsiCode: true},
Expand All @@ -313,33 +391,65 @@ void main() {
});

group('.fail', () {
test('writes lines to stdout', () async {
test('writes lines shorter than terminal columns to stdout', () async {
await _runZoned(
() async {
const time = '(0.1s)';
const message = 'test message';
when(() => stdout.terminalColumns).thenReturn(32);
const message = 'short message';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
progress.fail();

verify(
() {
stdout.write(
any(
that: matches(
RegExp(
r'\[2K\u000D\[92m⠙\[0m test message... \[90m\(8\dms\)\[0m',
r'\[92m⠙\[0m short message... \[90m\(\d{2}ms\)\[0m',
),
),
),
);
},
).called(1);
verify(
() {
stdout.write(
'''\r✗ short message (0.1s)\n''',
);
},
).called(1);
},
stdout: () => stdout,
zoneValues: {AnsiCode: true},
);
});

test('writes lines longer than terminal columns to stdout', () async {
await _runZoned(
() async {
when(() => stdout.terminalColumns).thenReturn(32);
const message = 'looooooooooooooooonger message';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
progress.fail();
verify(
() {
stdout.write(
'''\u000D✗ $message $time\n''',
any(
that: matches(
RegExp(
r'\[92m⠙\[0m looooooooooooooooonger message... \[90m\(\d{2}ms\)\[0m',
),
),
),
);
},
).called(1);
verify(
() {
stdout.write(
'''\r✗ looooooooooooooooonger message (0.1s)\n''',
);
},
).called(1);
Expand All @@ -365,9 +475,10 @@ void main() {
});

group('.cancel', () {
test('writes lines to stdout', () async {
test('writes lines shorter than terminal columns to stdout', () async {
await _runZoned(
() async {
when(() => stdout.terminalColumns).thenReturn(32);
const message = 'test message';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
Expand All @@ -378,27 +489,50 @@ void main() {
any(
that: matches(
RegExp(
r'\[2K\u000D\[92m⠙\[0m test message... \[90m\(8\dms\)\[0m',
r'\[92m⠙\[0m test message... \[90m\(\d{2}ms\)\[0m',
),
),
),
);
},
).called(1);
verify(
() {
stdout.write('\r');
},
).called(1);
},
stdout: () => stdout,
zoneValues: {AnsiCode: true},
);
});

test('writes lines longer than terminal columns to stdout', () async {
await _runZoned(
() async {
when(() => stdout.terminalColumns).thenReturn(32);
const message = 'looooooooooooooooonger message';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
progress.cancel();
verify(
() {
stdout.write(
any(
that: matches(
RegExp(
r'\[2K\u000D',
r'\[92m⠙\[0m looooooooooooooooonger message... \[90m\(\d{2}ms\)\[0m',
),
),
),
);
},
).called(1);
verify(
() {
stdout.write('\r');
},
).called(1);
},
stdout: () => stdout,
zoneValues: {AnsiCode: true},
Expand Down

0 comments on commit 565dacc

Please sign in to comment.