Clean up startProgress logic. (#19695) (#20009)

Disallow calling stop() or cancel() multiple times. This means that
when you use startProgress you have to more carefully think about what
exactly is going on.

Properly cancel startProgress in non-ANSI situations, so that
back-to-back startProgress calls all render to the console.
This commit is contained in:
Ian Hickson
2018-07-30 16:58:07 -07:00
committed by GitHub
parent ee396272d3
commit acf4b6c1aa
15 changed files with 261 additions and 243 deletions

View File

@@ -4,6 +4,7 @@
import 'dart:async';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:test/test.dart';
@@ -23,9 +24,9 @@ void main() {
verboseLogger.printError('Helpless!');
expect(mockLogger.statusText, matches(r'^\[ (?: {0,2}\+[0-9]{1,3} ms| )\] Hey Hey Hey Hey\n'
r'\[ (?: {0,2}\+[0-9]{1,3} ms| )\] Oooh, I do I do I do\n$'));
r'\[ (?: {0,2}\+[0-9]{1,3} ms| )\] Oooh, I do I do I do\n$'));
expect(mockLogger.traceText, '');
expect(mockLogger.errorText, matches(r'^\[ (?: {0,2}\+[0-9]{1,3} ms| )\] Helpless!\n$'));
expect(mockLogger.errorText, matches( r'^\[ (?: {0,2}\+[0-9]{1,3} ms| )\] Helpless!\n$'));
});
});
@@ -40,23 +41,27 @@ void main() {
mockStdio = new MockStdio();
ansiSpinner = new AnsiSpinner();
called = 0;
ansiStatus = new AnsiStatus('Hello world', true, () => called++, 20);
ansiStatus = new AnsiStatus(
message: 'Hello world',
expectSlowOperation: true,
padding: 20,
onFinish: () => called++,
);
});
List<String> outputLines() => mockStdio.writtenToStdout.join('').split('\n');
Future<void> doWhile(bool doThis()) async {
return Future.doWhile(() async {
// Future.doWhile() isn't enough by itself, because the VM never gets
// around to scheduling the other tasks for some reason.
await new Future<void>.delayed(const Duration(milliseconds: 0));
return doThis();
Future<void> doWhileAsync(bool doThis()) async {
return Future.doWhile(() {
// We want to let other tasks run at the same time, so we schedule these
// using a timer rather than a microtask.
return Future<bool>.delayed(Duration.zero, doThis);
});
}
testUsingContext('AnsiSpinner works', () async {
ansiSpinner.start();
await doWhile(() => ansiSpinner.ticks < 10);
await doWhileAsync(() => ansiSpinner.ticks < 10);
List<String> lines = outputLines();
expect(lines[0], startsWith(' \b-\b\\\b|\b/\b-\b\\\b|\b/'));
expect(lines[0].endsWith('\n'), isFalse);
@@ -66,44 +71,37 @@ void main() {
expect(lines[0], endsWith('\b \b'));
expect(lines.length, equals(1));
// Verify that stopping multiple times doesn't clear multiple times.
ansiSpinner.stop();
lines = outputLines();
expect(lines[0].endsWith('\b \b '), isFalse);
expect(lines.length, equals(1));
ansiSpinner.cancel();
lines = outputLines();
expect(lines[0].endsWith('\b \b '), isFalse);
expect(lines.length, equals(1));
// Verify that stopping or canceling multiple times throws.
expect(() { ansiSpinner.stop(); }, throwsA(const isInstanceOf<AssertionError>()));
expect(() { ansiSpinner.cancel(); }, throwsA(const isInstanceOf<AssertionError>()));
}, overrides: <Type, Generator>{Stdio: () => mockStdio});
testUsingContext('AnsiStatus works when cancelled', () async {
ansiStatus.start();
await doWhile(() => ansiStatus.ticks < 10);
await doWhileAsync(() => ansiStatus.ticks < 10);
List<String> lines = outputLines();
expect(lines[0], startsWith('Hello world \b-\b\\\b|\b/\b-\b\\\b|\b/\b-'));
expect(lines[0].endsWith('\n'), isFalse);
expect(lines.length, equals(1));
expect(lines[0].endsWith('\n'), isFalse);
// Verify a cancel does _not_ print the time and prints a newline.
ansiStatus.cancel();
lines = outputLines();
final List<Match> matches = secondDigits.allMatches(lines[0]).toList();
expect(matches, isEmpty);
expect(lines[0], endsWith('\b \b'));
expect(lines.length, equals(2));
expect(called, equals(1));
ansiStatus.cancel();
lines = outputLines();
expect(lines[0].endsWith('\b \b\b \b'), isFalse);
expect(lines.length, equals(2));
expect(called, equals(1));
ansiStatus.stop();
lines = outputLines();
expect(lines[0].endsWith('\b \b\b \b'), isFalse);
expect(lines.length, equals(2));
expect(called, equals(1));
expect(lines[1], equals(''));
// Verify that stopping or canceling multiple times throws.
expect(() { ansiStatus.cancel(); }, throwsA(const isInstanceOf<AssertionError>()));
expect(() { ansiStatus.stop(); }, throwsA(const isInstanceOf<AssertionError>()));
}, overrides: <Type, Generator>{Stdio: () => mockStdio});
testUsingContext('AnsiStatus works when stopped', () async {
ansiStatus.start();
await doWhile(() => ansiStatus.ticks < 10);
await doWhileAsync(() => ansiStatus.ticks < 10);
List<String> lines = outputLines();
expect(lines[0], startsWith('Hello world \b-\b\\\b|\b/\b-\b\\\b|\b/\b-'));
expect(lines.length, equals(1));
@@ -111,55 +109,55 @@ void main() {
// Verify a stop prints the time.
ansiStatus.stop();
lines = outputLines();
List<Match> matches = secondDigits.allMatches(lines[0]).toList();
final List<Match> matches = secondDigits.allMatches(lines[0]).toList();
expect(matches, isNotNull);
expect(matches, hasLength(1));
Match match = matches.first;
final Match match = matches.first;
expect(lines[0], endsWith(match.group(0)));
final String initialTime = match.group(0);
expect(called, equals(1));
expect(lines.length, equals(2));
expect(lines[1], equals(''));
// Verify stopping more than once generates no additional output.
ansiStatus.stop();
lines = outputLines();
matches = secondDigits.allMatches(lines[0]).toList();
expect(matches, hasLength(1));
match = matches.first;
expect(lines[0], endsWith(initialTime));
expect(called, equals(1));
expect(lines.length, equals(2));
expect(lines[1], equals(''));
// Verify that stopping or canceling multiple times throws.
expect(() { ansiStatus.stop(); }, throwsA(const isInstanceOf<AssertionError>()));
expect(() { ansiStatus.cancel(); }, throwsA(const isInstanceOf<AssertionError>()));
}, overrides: <Type, Generator>{Stdio: () => mockStdio});
testUsingContext('AnsiStatus works when cancelled', () async {
ansiStatus.start();
await doWhile(() => ansiStatus.ticks < 10);
List<String> lines = outputLines();
expect(lines[0], startsWith('Hello world \b-\b\\\b|\b/\b-\b\\\b|\b/\b-'));
expect(lines.length, equals(1));
testUsingContext('sequential startProgress calls with StdoutLogger', () async {
context[Logger].startProgress('AAA')..stop();
context[Logger].startProgress('BBB')..stop();
expect(outputLines(), <String>[
'AAA',
'BBB',
'',
]);
}, overrides: <Type, Generator>{
Stdio: () => mockStdio,
Logger: () => new StdoutLogger(),
});
// Verify a cancel does _not_ print the time and prints a newline.
ansiStatus.cancel();
lines = outputLines();
List<Match> matches = secondDigits.allMatches(lines[0]).toList();
expect(matches, isEmpty);
expect(lines[0], endsWith('\b \b'));
expect(called, equals(1));
// TODO(jcollins-g): Consider having status objects print the newline
// when canceled, or never printing a newline at all.
expect(lines.length, equals(2));
testUsingContext('sequential startProgress calls with VerboseLogger and StdoutLogger', () async {
context[Logger].startProgress('AAA')..stop();
context[Logger].startProgress('BBB')..stop();
expect(outputLines(), <Matcher>[
matches(r'^\[ (?: {0,2}\+[0-9]{1,3} ms| )\] AAA$'),
matches(r'^\[ (?: {0,2}\+[0-9]{1,3} ms| )\] AAA \(completed\)$'),
matches(r'^\[ (?: {0,2}\+[0-9]{1,3} ms| )\] BBB$'),
matches(r'^\[ (?: {0,2}\+[0-9]{1,3} ms| )\] BBB \(completed\)$'),
matches(r'^$'),
]);
}, overrides: <Type, Generator>{
Stdio: () => mockStdio,
Logger: () => new VerboseLogger(new StdoutLogger()),
});
// Verifying calling stop after cancel doesn't print anything weird.
ansiStatus.stop();
lines = outputLines();
matches = secondDigits.allMatches(lines[0]).toList();
expect(matches, isEmpty);
expect(lines[0], endsWith('\b \b'));
expect(called, equals(1));
expect(lines[0], isNot(endsWith('\b \b\b \b')));
expect(lines.length, equals(2));
}, overrides: <Type, Generator>{Stdio: () => mockStdio});
testUsingContext('sequential startProgress calls with BufferLogger', () async {
context[Logger].startProgress('AAA')..stop();
context[Logger].startProgress('BBB')..stop();
final BufferLogger logger = context[Logger];
expect(logger.statusText, 'AAA\nBBB\n');
}, overrides: <Type, Generator>{
Logger: () => new BufferLogger(),
});
});
}