use throwsA (#82238)

This commit is contained in:
Alexandre Ardhuin
2021-05-11 19:19:01 +02:00
committed by GitHub
parent 7b9a175557
commit df3662b15e
22 changed files with 311 additions and 297 deletions

View File

@@ -819,12 +819,10 @@ class LoggerInterrupted implements Exception {
Future<void> expectLoggerInterruptEndsTask(Future<void> task, StreamLogger logger) async {
logger.interrupt(); // an exception during the task should cause it to fail...
try {
await task;
expect(false, isTrue); // (shouldn't reach here)
} on ToolExit catch (error) {
expect(error.exitCode, 2); // ...with exit code 2.
}
await expectLater(
() => task,
throwsA(isA<ToolExit>().having((ToolExit error) => error.exitCode, 'exitCode', 2)),
);
}
VMServiceConnector getFakeVmServiceFactory({

View File

@@ -23,12 +23,10 @@ void main() {
testUsingContext('fail with a bad device id', () async {
final LogsCommand command = LogsCommand();
try {
await createTestCommandRunner(command).run(<String>['-d', 'abc123', 'logs']);
fail('Expect exception');
} on ToolExit catch (e) {
expect(e.exitCode ?? 1, 1);
}
await expectLater(
() => createTestCommandRunner(command).run(<String>['-d', 'abc123', 'logs']),
throwsA(isA<ToolExit>().having((ToolExit error) => error.exitCode, 'exitCode', anyOf(isNull, 1))),
);
});
});
}

View File

@@ -51,12 +51,10 @@ void main() {
testUsingContext('fails when target not found', () async {
final RunCommand command = RunCommand();
try {
await createTestCommandRunner(command).run(<String>['run', '-t', 'abc123', '--no-pub']);
fail('Expect exception');
} on ToolExit catch (e) {
expect(e.exitCode ?? 1, 1);
}
expect(
() => createTestCommandRunner(command).run(<String>['run', '-t', 'abc123', '--no-pub']),
throwsA(isA<ToolExit>().having((ToolExit error) => error.exitCode, 'exitCode', anyOf(isNull, 1))),
);
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
@@ -69,18 +67,20 @@ void main() {
fileSystem.file('.packages').createSync();
final RunCommand command = RunCommand();
try {
await createTestCommandRunner(command).run(<String>[
await expectLater(
() => createTestCommandRunner(command).run(<String>[
'run',
'--use-application-binary=app/bar/faz',
'--fast-start',
'--no-pub',
'--show-test-device',
]);
fail('Expect exception');
} on Exception catch (e) {
expect(e.toString(), isNot(contains('--fast-start is not supported with --use-application-binary')));
}
]),
throwsA(isA<Exception>().having(
(Exception exception) => exception.toString(),
'toString',
isNot(contains('--fast-start is not supported with --use-application-binary')),
)),
);
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
@@ -97,15 +97,13 @@ void main() {
..createSync(recursive: true);
final RunCommand command = RunCommand();
try {
await createTestCommandRunner(command).run(<String>[
await expectLater(
() => createTestCommandRunner(command).run(<String>[
'run',
'--no-pub',
]);
fail('Expect exception');
} on Exception catch (e) {
expect(e, isA<ToolExit>());
}
]),
throwsA(isA<ToolExit>()),
);
final BufferLogger bufferLogger = globals.logger as BufferLogger;
expect(
bufferLogger.statusText,
@@ -124,16 +122,17 @@ void main() {
.createSync(recursive: true);
final RunCommand command = RunCommand();
try {
await createTestCommandRunner(command).run(<String>[
await expectLater(
() => createTestCommandRunner(command).run(<String>[
'run',
'--no-pub',
]);
fail('Expect exception');
} on Exception catch (e) {
expect(e, isA<ToolExit>());
expect(e.toString(), contains('No pubspec.yaml file found'));
}
]),
throwsA(isA<ToolExit>().having(
(ToolExit error) => error.toString(),
'toString()',
contains('No pubspec.yaml file found'),
)),
);
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
@@ -180,16 +179,14 @@ void main() {
(Invocation invocation) => Future<List<Device>>.value(noDevices)
);
try {
await createTestCommandRunner(command).run(<String>[
await expectLater(
() => createTestCommandRunner(command).run(<String>[
'run',
'--no-pub',
'--no-hot',
]);
fail('Expect exception');
} on ToolExit catch (e) {
expect(e.message, null);
}
]),
throwsA(isA<ToolExit>().having((ToolExit error) => error.message, 'message', isNull)),
);
expect(
testLogger.statusText,
@@ -256,16 +253,14 @@ void main() {
(Invocation invocation) => Future<List<Device>>.value(<Device>[]),
);
try {
await createTestCommandRunner(command).run(<String>[
await expectLater(
() => createTestCommandRunner(command).run(<String>[
'run',
'--no-pub',
'--no-hot',
]);
fail('Expect exception');
} on ToolExit catch (e) {
expect(e.message, null);
}
]),
throwsA(isA<ToolExit>().having((ToolExit error) => error.message, 'message', isNull)),
);
expect(
testLogger.statusText,

View File

@@ -61,15 +61,16 @@ void main() {
final ShellCompletionCommand command = ShellCompletionCommand();
const String outputFile = 'bash-setup.sh';
globals.fs.file(outputFile).createSync();
try {
await createTestCommandRunner(command).run(
await expectLater(
() => createTestCommandRunner(command).run(
<String>['bash-completion', outputFile],
);
fail('Expect ToolExit exception');
} on ToolExit catch (error) {
expect(error.exitCode ?? 1, 1);
expect(error.message, contains('Use --overwrite'));
}
),
throwsA(
isA<ToolExit>()
.having((ToolExit error) => error.exitCode, 'exitCode', anyOf(isNull, 1))
.having((ToolExit error) => error.message, 'message', contains('Use --overwrite')),
),
);
expect(globals.fs.isFileSync(outputFile), isTrue);
expect(globals.fs.file(outputFile).readAsStringSync(), isEmpty);
}, overrides: <Type, Generator>{

View File

@@ -161,13 +161,18 @@ void main() {
double: () => context.get<int>()! * 1.0,
},
);
try {
await value;
fail('ContextDependencyCycleException expected but not thrown.');
} on ContextDependencyCycleException catch (e) {
expect(e.cycle, <Type>[String, double, int]);
expect(e.toString(), 'Dependency cycle detected: String -> double -> int');
}
expect(
() => value,
throwsA(
isA<ContextDependencyCycleException>()
.having((ContextDependencyCycleException error) => error.cycle, 'cycle', <Type>[String, double, int])
.having(
(ContextDependencyCycleException error) => error.toString(),
'toString()',
'Dependency cycle detected: String -> double -> int',
),
),
);
});
});

View File

@@ -211,13 +211,14 @@ void main() {
exitCode: 1,
stderr: stderr,
));
try {
processUtils.runSync(<String>['kaboom'], throwOnError: true);
fail('ProcessException expected.');
} on ProcessException catch (e) {
expect(e, isA<ProcessException>());
expect(e.message.contains(stderr), false);
}
expect(
() => processUtils.runSync(<String>['kaboom'], throwOnError: true),
throwsA(isA<ProcessException>().having(
(ProcessException error) => error.message,
'message',
isNot(contains(stderr)),
)),
);
});
testWithoutContext('throws with stderr in exception on failure with verboseExceptions', () async {

View File

@@ -406,12 +406,10 @@ void main() {
botDetector: const BotDetectorAlwaysNo(),
processManager: processManager,
);
try {
await pub.get(context: PubContext.flutterTests);
throw AssertionError('pubGet did not fail');
} on ToolExit catch (error) {
expect(error.message, 'pub get failed (66; err3)');
}
await expectLater(
() => pub.get(context: PubContext.flutterTests),
throwsA(isA<ToolExit>().having((ToolExit error) => error.message, 'message', 'pub get failed (66; err3)')),
);
expect(logger.statusText,
'Running "flutter pub get" in /...\n'
'out1\n'

View File

@@ -174,13 +174,14 @@ void main() {
}
Future<void> failToEvaluateExpression(FlutterTestDriver flutter) async {
ObjRef res;
try {
res = await flutter.evaluateInFrame('"test"');
} on RPCError catch (e) {
expect(e.message, contains('Expression evaluation is not supported for this configuration'));
}
expect(res, null);
await expectLater(
() => flutter.evaluateInFrame('"test"'),
throwsA(isA<RPCError>().having(
(RPCError error) => error.message,
'message',
contains('Expression evaluation is not supported for this configuration'),
)),
);
}
Future<void> checkStaticScope(FlutterTestDriver flutter) async {