error handling when path to dir provided instead of file (#109796)

This commit is contained in:
Elias Yishak
2022-09-14 14:13:13 -07:00
committed by GitHub
parent 435c000b0a
commit 0c829caad8
3 changed files with 78 additions and 14 deletions

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/cache.dart';
@@ -26,12 +27,12 @@ void main() {
throw Exception('dummy');
};
await expectLater(() => createTestCommandRunner(ScreenshotCommand())
await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
.run(<String>['screenshot', '--type=skia', '--observatory-url=http://localhost:8181']),
throwsA(isException.having((Exception exception) => exception.toString(), 'message', contains('dummy'))),
);
await expectLater(() => createTestCommandRunner(ScreenshotCommand())
await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
.run(<String>['screenshot', '--type=rasterizer', '--observatory-url=http://localhost:8181']),
throwsA(isException.having((Exception exception) => exception.toString(), 'message', contains('dummy'))),
);
@@ -39,29 +40,70 @@ void main() {
testUsingContext('rasterizer and skia screenshots require observatory uri', () async {
await expectLater(() => createTestCommandRunner(ScreenshotCommand())
await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
.run(<String>['screenshot', '--type=skia']),
throwsToolExit(message: 'Observatory URI must be specified for screenshot type skia')
);
await expectLater(() => createTestCommandRunner(ScreenshotCommand())
await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
.run(<String>['screenshot', '--type=rasterizer',]),
throwsToolExit(message: 'Observatory URI must be specified for screenshot type rasterizer'),
);
});
testUsingContext('device screenshots require device', () async {
await expectLater(() => createTestCommandRunner(ScreenshotCommand())
await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
.run(<String>['screenshot']),
throwsToolExit(message: 'Must have a connected device for screenshot type device'),
);
});
testUsingContext('device screenshots cannot provided Observatory', () async {
await expectLater(() => createTestCommandRunner(ScreenshotCommand())
await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
.run(<String>['screenshot', '--observatory-url=http://localhost:8181']),
throwsToolExit(message: 'Observatory URI cannot be provided for screenshot type device'),
);
});
});
group('Screenshot file validation', () {
testWithoutContext('successful in pwd', () async {
final MemoryFileSystem fs = MemoryFileSystem.test();
fs.file('test.png').createSync();
fs.directory('sub_dir').createSync();
fs.file('sub_dir/test.png').createSync();
expect(() => ScreenshotCommand.checkOutput(fs.file('test.png'), fs),
returnsNormally);
expect(
() => ScreenshotCommand.checkOutput(fs.file('sub_dir/test.png'), fs),
returnsNormally);
});
testWithoutContext('failed in pwd', () async {
final MemoryFileSystem fs = MemoryFileSystem.test();
fs.directory('sub_dir').createSync();
expect(
() => ScreenshotCommand.checkOutput(fs.file('test.png'), fs),
throwsToolExit(
message: 'File was not created, ensure path is valid'));
expect(
() => ScreenshotCommand.checkOutput(fs.file('../'), fs),
throwsToolExit(
message: 'File was not created, ensure path is valid'));
expect(
() => ScreenshotCommand.checkOutput(fs.file('.'), fs),
throwsToolExit(
message: 'File was not created, ensure path is valid'));
expect(
() => ScreenshotCommand.checkOutput(fs.file('/'), fs),
throwsToolExit(
message: 'File was not created, ensure path is valid'));
expect(
() => ScreenshotCommand.checkOutput(fs.file('sub_dir/test.png'), fs),
throwsToolExit(
message: 'File was not created, ensure path is valid'));
});
});
}