Add ability to wrap text messages in a box (#94391)

This commit is contained in:
Emmanuel Garcia
2021-12-15 13:19:11 -08:00
committed by GitHub
parent 139a4d3992
commit 79bc1bfa89
8 changed files with 524 additions and 89 deletions

View File

@@ -598,6 +598,18 @@ class StreamLogger extends Logger {
_log('[stdout] $message');
}
@override
void printBox(
String message, {
String title,
}) {
if (title == null) {
_log('[stdout] $message');
} else {
_log('[stdout] $title: $message');
}
}
@override
void printTrace(String message) {
_log('[verbose] $message');

View File

@@ -172,6 +172,25 @@ void main() {
Logger: () => notifyingLogger,
});
testUsingContext('printBox should log to stdout when logToStdout is enabled', () async {
final StringBuffer buffer = await capturedConsolePrint(() {
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon(
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
logToStdout: true,
);
globals.printBox('This is the box message', title: 'Sample title');
return Future<void>.value();
});
expect(buffer.toString().trim(), contains('Sample title: This is the box message'));
}, overrides: <Type, Generator>{
Logger: () => notifyingLogger,
});
testUsingContext('daemon.shutdown command should stop daemon', () async {
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();

View File

@@ -556,8 +556,12 @@ Command: /home/android/gradlew assembleRelease
expect(
testLogger.statusText,
contains(
'You should change the ownership of the project directory to your user, '
'or move the project to a directory with execute permissions.'
'\n'
'┌─ Flutter Fix ───────────────────────────────────────────────────────────────────────────────────┐\n'
'│ [!] Gradle does not have execution permission. │\n'
'│ You should change the ownership of the project directory to your user, or move the project to a │\n'
'│ directory with execute permissions. │\n'
'└─────────────────────────────────────────────────────────────────────────────────────────────────┘\n'
)
);
});
@@ -582,8 +586,12 @@ Command: /home/android/gradlew assembleRelease
expect(
testLogger.statusText,
contains(
'You should change the ownership of the project directory to your user, '
'or move the project to a directory with execute permissions.'
'\n'
'┌─ Flutter Fix ───────────────────────────────────────────────────────────────────────────────────┐\n'
'│ [!] Gradle does not have execution permission. │\n'
'│ You should change the ownership of the project directory to your user, or move the project to a │\n'
'│ directory with execute permissions. │\n'
'└─────────────────────────────────────────────────────────────────────────────────────────────────┘\n'
)
);
});
@@ -608,11 +616,14 @@ Command: /home/android/gradlew assembleRelease
expect(
testLogger.statusText,
contains(
'Unable to download needed Android SDK components, as the '
'following licenses have not been accepted:\n'
'foo, bar\n\n'
'To resolve this, please run the following command in a Terminal:\n'
'flutter doctor --android-licenses'
'\n'
'┌─ Flutter Fix ─────────────────────────────────────────────────────────────────────────────────┐\n'
'│ [!] Unable to download needed Android SDK components, as the following licenses have not been │\n'
'│ accepted: foo, bar │\n'
'│ │\n'
'│ To resolve this, please run the following command in a Terminal: │\n'
'│ flutter doctor --android-licenses │\n'
'└───────────────────────────────────────────────────────────────────────────────────────────────┘\n'
)
);
});
@@ -687,9 +698,13 @@ assembleFooTest
expect(
testLogger.statusText,
contains(
'The android/app/build.gradle file defines product '
'flavors: flavor1, flavor_2 '
'You must specify a --flavor option to select one of them.'
'\n'
'┌─ Flutter Fix ───────────────────────────────────────────────────────────────────────────────────┐\n'
'│ [!] Gradle project does not define a task suitable for the requested build. │\n'
'│ │\n'
'│ The /android/app/build.gradle file defines product flavors: flavor1, flavor_2. You must specify │\n'
'│ a --flavor option to select one of them. │\n'
'└─────────────────────────────────────────────────────────────────────────────────────────────────┘\n'
)
);
expect(fakeProcessManager.hasRemainingExpectations, isFalse);
@@ -722,15 +737,13 @@ assembleProfile
expect(
testLogger.statusText,
contains(
'Gradle project does not define a task suitable '
'for the requested build.'
)
);
expect(
testLogger.statusText,
contains(
'The android/app/build.gradle file does not define any custom product flavors. '
'You cannot use the --flavor option.'
'\n'
'┌─ Flutter Fix ─────────────────────────────────────────────────────────────────────────────────┐\n'
'│ [!] Gradle project does not define a task suitable for the requested build. │\n'
'│ │\n'
'│ The /android/app/build.gradle file does not define any custom product flavors. You cannot use │\n'
'│ the --flavor option. │\n'
'└───────────────────────────────────────────────────────────────────────────────────────────────┘\n'
)
);
expect(fakeProcessManager.hasRemainingExpectations, isFalse);
@@ -762,16 +775,19 @@ assembleProfile
testLogger.statusText,
contains(
'\n'
'The plugin webview_flutter requires a higher Android SDK version.\n'
'Fix this issue by adding the following to the file /android/app/build.gradle:\n'
'android {\n'
' defaultConfig {\n'
' minSdkVersion 19\n'
' }\n'
'}\n'
'\n'
"Note that your app won't be available to users running Android SDKs below 19.\n"
'Alternatively, try to find a version of this plugin that supports these lower versions of the Android SDK.\n'
'┌─ Flutter Fix ─────────────────────────────────────────────────────────────────────────────────┐\n'
'│ The plugin webview_flutter requires a higher Android SDK version. │\n'
'│ Fix this issue by adding the following to the file /android/app/build.gradle: │\n'
'│ android { │\n'
'│ defaultConfig { │\n'
'│ minSdkVersion 19 │\n'
'│ } │\n'
'│ } │\n'
'│ │\n'
"│ Note that your app won't be available to users running Android SDKs below 19. │\n"
'│ Alternatively, try to find a version of this plugin that supports these lower versions of the │\n'
'│ Android SDK. │\n'
'└───────────────────────────────────────────────────────────────────────────────────────────────┘\n'
)
);
}, overrides: <Type, Generator>{
@@ -802,13 +818,15 @@ assembleProfile
testLogger.statusText,
contains(
'\n'
'This issue appears to be https://github.com/flutter/flutter/issues/58247.\n'
'Fix this issue by adding the following to the file /android/app/build.gradle:\n'
'android {\n'
' lintOptions {\n'
' checkReleaseBuilds false\n'
' }\n'
'}\n'
'┌─ Flutter Fix ─────────────────────────────────────────────────────────────────┐\n'
'│ This issue appears to be https://github.com/flutter/flutter/issues/58247. │\n'
'│ Fix this issue by adding the following to the file /android/app/build.gradle: │\n'
'│ android { │\n'
'│ lintOptions { │\n'
'│ checkReleaseBuilds false │\n'
'│ } │\n'
'│ } │\n'
'└───────────────────────────────────────────────────────────────────────────────┘\n'
)
);
}, overrides: <Type, Generator>{
@@ -842,10 +860,11 @@ Execution failed for task ':app:generateDebugFeatureTransitiveDeps'.
testLogger.statusText,
contains(
'\n'
'You need to update the lockfile, or disable Gradle dependency locking.\n'
'To regenerate the lockfiles run: `./gradlew :generateLockfiles` in /android/build.gradle\n'
'To remove dependency locking, remove the `dependencyLocking` from /android/build.gradle\n'
'\n'
'┌─ Flutter Fix ────────────────────────────────────────────────────────────────────────────┐\n'
'│ You need to update the lockfile, or disable Gradle dependency locking. │\n'
'│ To regenerate the lockfiles run: `./gradlew :generateLockfiles` in /android/build.gradle │\n'
'│ To remove dependency locking, remove the `dependencyLocking` from /android/build.gradle │\n'
'└──────────────────────────────────────────────────────────────────────────────────────────┘\n'
)
);
}, overrides: <Type, Generator>{
@@ -872,9 +891,13 @@ Execution failed for task ':app:generateDebugFeatureTransitiveDeps'.
expect(
testLogger.statusText,
contains(
'[!] Your project requires a newer version of the Kotlin Gradle plugin.\n'
' Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then update /android/build.gradle:\n'
" ext.kotlin_version = '<latest-version>'\n"
'\n'
'┌─ Flutter Fix ────────────────────────────────────────────────────────────────────────────────┐\n'
'│ [!] Your project requires a newer version of the Kotlin Gradle plugin. │\n'
'│ Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then │\n'
'│ update /android/build.gradle: │\n'
"│ ext.kotlin_version = '<latest-version>' │\n"
'└──────────────────────────────────────────────────────────────────────────────────────────────┘\n'
)
);
}, overrides: <Type, Generator>{

View File

@@ -282,6 +282,31 @@ void main() {
mockLogger.errorText,
matches('^$red' r'\[ (?: {0,2}\+[0-9]{1,4} ms| )\] ' '${bold}Helpless!$resetBold$resetColor' r'\n$'));
});
testWithoutContext('printBox', () {
final BufferLogger mockLogger = BufferLogger(
terminal: AnsiTerminal(
stdio: FakeStdio(),
platform: FakePlatform(stdoutSupportsAnsi: true),
),
outputPreferences: OutputPreferences.test(showColor: true),
);
final VerboseLogger verboseLogger = VerboseLogger(
mockLogger, stopwatchFactory: FakeStopwatchFactory(stopwatch: fakeStopWatch),
);
verboseLogger.printBox('This is the box message', title: 'Sample title');
expect(
mockLogger.statusText,
contains('[ ] \x1B[1m\x1B[22m\n'
'\x1B[1m ┌─ Sample title ──────────┐\x1B[22m\n'
'\x1B[1m │ This is the box message │\x1B[22m\n'
'\x1B[1m └─────────────────────────┘\x1B[22m\n'
'\x1B[1m \x1B[22m\n'
),
);
});
});
testWithoutContext('Logger does not throw when stdio write throws synchronously', () async {
@@ -928,6 +953,184 @@ void main() {
expect(lines[0], equals(''));
});
testWithoutContext('Stdout printBox puts content inside a box', () {
final Logger logger = StdoutLogger(
terminal: AnsiTerminal(
stdio: fakeStdio,
platform: FakePlatform(),
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: true),
);
logger.printBox('Hello world', title: 'Test title');
final String stdout = fakeStdio.writtenToStdout.join('');
expect(stdout,
contains(
'\n'
'┌─ Test title ┐\n'
'│ Hello world │\n'
'└─────────────┘\n'
),
);
});
testWithoutContext('Stdout printBox does not require title', () {
final Logger logger = StdoutLogger(
terminal: AnsiTerminal(
stdio: fakeStdio,
platform: FakePlatform(),
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: true),
);
logger.printBox('Hello world');
final String stdout = fakeStdio.writtenToStdout.join('');
expect(stdout,
contains(
'\n'
'┌─────────────┐\n'
'│ Hello world │\n'
'└─────────────┘\n'
),
);
});
testWithoutContext('Stdout printBox handles new lines', () {
final Logger logger = StdoutLogger(
terminal: AnsiTerminal(
stdio: fakeStdio,
platform: FakePlatform(),
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: true),
);
logger.printBox('Hello world\nThis is a new line', title: 'Test title');
final String stdout = fakeStdio.writtenToStdout.join('');
expect(stdout,
contains(
'\n'
'┌─ Test title ───────┐\n'
'│ Hello world │\n'
'│ This is a new line │\n'
'└────────────────────┘\n'
),
);
});
testWithoutContext('Stdout printBox handles content with ANSI escape characters', () {
final Logger logger = StdoutLogger(
terminal: AnsiTerminal(
stdio: fakeStdio,
platform: FakePlatform(),
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: true),
);
const String bold = '\u001B[1m';
const String clear = '\u001B[2J\u001B[H';
logger.printBox('${bold}Hello world$clear', title: 'Test title');
final String stdout = fakeStdio.writtenToStdout.join('');
expect(stdout,
contains(
'\n'
'┌─ Test title ┐\n'
'│ ${bold}Hello world$clear │\n'
'└─────────────┘\n'
),
);
});
testWithoutContext('Stdout printBox handles column limit', () {
const int columnLimit = 14;
final Logger logger = StdoutLogger(
terminal: AnsiTerminal(
stdio: fakeStdio,
platform: FakePlatform(),
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: true, wrapColumn: columnLimit),
);
logger.printBox('This line is longer than $columnLimit characters', title: 'Test');
final String stdout = fakeStdio.writtenToStdout.join('');
final List<String> stdoutLines = stdout.split('\n');
expect(stdoutLines.length, greaterThan(1));
expect(stdoutLines[1].length, equals(columnLimit));
expect(stdout,
contains(
'\n'
'┌─ Test ─────┐\n'
'│ This line │\n'
'│ is longer │\n'
'│ than 14 │\n'
'│ characters │\n'
'└────────────┘\n'
),
);
});
testWithoutContext('Stdout printBox handles column limit and respects new lines', () {
const int columnLimit = 14;
final Logger logger = StdoutLogger(
terminal: AnsiTerminal(
stdio: fakeStdio,
platform: FakePlatform(),
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: true, wrapColumn: columnLimit),
);
logger.printBox('This\nline is longer than\n\n$columnLimit characters', title: 'Test');
final String stdout = fakeStdio.writtenToStdout.join('');
final List<String> stdoutLines = stdout.split('\n');
expect(stdoutLines.length, greaterThan(1));
expect(stdoutLines[1].length, equals(columnLimit));
expect(stdout,
contains(
'\n'
'┌─ Test ─────┐\n'
'│ This │\n'
'│ line is │\n'
'│ longer │\n'
'│ than │\n'
'│ │\n'
'│ 14 │\n'
'│ characters │\n'
'└────────────┘\n'
),
);
});
testWithoutContext('Stdout printBox breaks long words that exceed the column limit', () {
const int columnLimit = 14;
final Logger logger = StdoutLogger(
terminal: AnsiTerminal(
stdio: fakeStdio,
platform: FakePlatform(),
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: true, wrapColumn: columnLimit),
);
logger.printBox('Thiswordislongerthan${columnLimit}characters', title: 'Test');
final String stdout = fakeStdio.writtenToStdout.join('');
final List<String> stdoutLines = stdout.split('\n');
expect(stdoutLines.length, greaterThan(1));
expect(stdoutLines[1].length, equals(columnLimit));
expect(stdout,
contains(
'\n'
'┌─ Test ─────┐\n'
'│ Thiswordis │\n'
'│ longerthan │\n'
'│ 14characte │\n'
'│ rs │\n'
'└────────────┘\n'
),
);
});
testWithoutContext('Stdout startProgress on non-color terminal', () async {
final FakeStopwatch fakeStopwatch = FakeStopwatch();
final Logger logger = StdoutLogger(