Catch UnsupportedError thrown when user provides an asset directory path containing invalid characters (#141214)

Fixes https://github.com/flutter/flutter/issues/140092
This commit is contained in:
Andrew Kolos 2024-01-18 00:35:59 -08:00 committed by GitHub
parent e1e470d459
commit 0833929c99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 2 deletions

View file

@ -912,8 +912,16 @@ class ManifestAssetBundle implements AssetBundle {
Package? attributedPackage,
List<String>? flavors,
}) {
final String directoryPath = _fileSystem.path.join(
assetBase, assetUri.toFilePath(windows: _platform.isWindows));
final String directoryPath;
try {
directoryPath = _fileSystem.path
.join(assetBase, assetUri.toFilePath(windows: _platform.isWindows));
} on UnsupportedError catch (e) {
throwToolExit(
'Unable to search for asset files in directory path "${assetUri.path}". '
'Please ensure that this is valid URI that points to a directory '
'that is available on the local file system.\nError details:\n$e');
}
if (!_fileSystem.directory(directoryPath).existsSync()) {
_logger.printError('Error: unable to find directory entry in pubspec.yaml: $directoryPath');

View file

@ -153,6 +153,30 @@ flutter:
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('throws ToolExit when directory entry contains invalid characters', () async {
testFileSystem.file('.packages').createSync();
testFileSystem.file('pubspec.yaml')
..createSync()
..writeAsStringSync(r'''
name: example
flutter:
assets:
- https://mywebsite.com/images/
''');
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
expect(() => bundle.build(packagesPath: '.packages'), throwsToolExit(
message: 'Unable to search for asset files in directory path "https%3A//mywebsite.com/images/". '
'Please ensure that this is valid URI that points to a directory that is '
'available on the local file system.\n'
'Error details:\n'
'Unsupported operation: Illegal character in path: https:',
));
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
ProcessManager: () => FakeProcessManager.any(),
Platform: () => FakePlatform(operatingSystem: 'windows'),
});
testUsingContext('handle removal of wildcard directories', () async {
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
final File pubspec = globals.fs.file('pubspec.yaml')