Skip to content

Commit

Permalink
fix(mason_logger): link should depend on ansi escape support
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Jan 13, 2025
1 parent 4de5083 commit 3696692
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/mason_logger/lib/src/link.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:io/ansi.dart';

/// Wraps [uri] with an escape sequence so it's recognized as a hyperlink.
/// An optional message can be used in place of the [uri].
/// If no [message] is provided, the text content will be the full [uri].
Expand All @@ -10,6 +12,10 @@
/// print(richLink); // Equivalent to `[The Dart Website](https://dart.dev)` in markdown
/// ```
String link({required Uri uri, String? message}) {
if (!ansiOutputEnabled) {
return message != null ? '[$message]($uri)' : uri.toString();
}

const leading = '\x1B]8;;';
const trailing = '\x1B\\';

Expand Down
7 changes: 7 additions & 0 deletions packages/mason_logger/test/ci.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ Future<void> main() async {
progress.update('Almost done');
await Future<void>.delayed(const Duration(seconds: 1));
progress.complete('Done');

final masonGitHub = link(
uri: Uri.parse('https://github.com/felangel/mason'),
message: 'GitHub',
);

logger.info('Learn more at $masonGitHub');
}
6 changes: 5 additions & 1 deletion packages/mason_logger/test/fixtures/ci.txt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
⠋ Calculating...⠙ This is taking longer than expected... (1.0s)⠹ Almost done... (2.0s)✓ Done (3.0s)
⠋ Calculating...
⠙ This is taking longer than expected... (1.0s)
⠹ Almost done... (2.0s)
✓ Done (3.0s)
Learn more at [GitHub](https://github.com/felangel/mason)
43 changes: 41 additions & 2 deletions packages/mason_logger/test/src/link_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import 'dart:io';

import 'package:mason_logger/mason_logger.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';

class _MockStdout extends Mock implements Stdout {}

void main() {
group('link', () {
late Stdout stdout;
late Stdout stderr;

setUp(() {
stdout = _MockStdout();
stderr = _MockStdout();
when(() => stdout.supportsAnsiEscapes).thenReturn(true);
when(() => stderr.supportsAnsiEscapes).thenReturn(true);
});

R runWithOverrides<R>(R Function() body) {
return IOOverrides.runZoned(
body,
stdout: () => stdout,
stderr: () => stderr,
);
}

final uri = Uri.parse('https://github.com/felangel/mason/issues/');
const lead = '\x1B]8;;';
const trail = '\x1B\\';
Expand All @@ -11,7 +34,7 @@ void main() {
'builds output with correct encodings: ' r'\x1B]8;;' ' and ' r'\x1B\\',
() {
const message = 'message';
final output = link(message: message, uri: uri);
final output = runWithOverrides(() => link(message: message, uri: uri));
final matcher = stringContainsInOrder(
[lead, '$uri', trail, message, lead, trail],
);
Expand All @@ -21,12 +44,28 @@ void main() {
);

test('builds String with Uri when message is null: ', () {
final output = link(uri: uri);
final output = runWithOverrides(() => link(uri: uri));
final matcher = stringContainsInOrder(
[lead, '$uri', trail, '$uri', lead, trail],
);

expect(output, matcher);
});

test('builds output when ansi escapes are not supported', () {
when(() => stdout.supportsAnsiEscapes).thenReturn(false);
when(() => stderr.supportsAnsiEscapes).thenReturn(false);
final output = runWithOverrides(() => link(uri: uri));
final matcher = stringContainsInOrder(['$uri']);
expect(output, matcher);
});

test('builds output with message when ansi escapes are not supported', () {
when(() => stdout.supportsAnsiEscapes).thenReturn(false);
when(() => stderr.supportsAnsiEscapes).thenReturn(false);
const message = 'message';
final output = runWithOverrides(() => link(uri: uri, message: message));
expect(output, equals('[$message]($uri)'));
});
});
}

0 comments on commit 3696692

Please sign in to comment.