Catch filesystem exception from flutter create (#38101)

This commit is contained in:
Jonah Williams
2019-08-12 09:51:03 -07:00
committed by GitHub
parent b8484e348e
commit 4b73746aff
2 changed files with 41 additions and 1 deletions

View File

@@ -4,6 +4,7 @@
import 'package:mustache/mustache.dart' as mustache;
import 'base/common.dart';
import 'base/file_system.dart';
import 'cache.dart';
import 'globals.dart';
@@ -62,13 +63,22 @@ class Template {
Map<String /* relative */, String /* absolute source */> _templateFilePaths;
/// Render the template into [directory].
///
/// May throw a [ToolExit] if the directory is not writable.
int render(
Directory destination,
Map<String, dynamic> context, {
bool overwriteExisting = true,
bool printStatusWhenWriting = true,
}) {
destination.createSync(recursive: true);
try {
destination.createSync(recursive: true);
} on FileSystemException catch (err) {
printError(err.toString());
throwToolExit('Failed to flutter create at ${destination.path}.');
return 0;
}
int fileCount = 0;
/// Returns the resolved destination path corresponding to the specified

View File

@@ -0,0 +1,30 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/template.dart';
import 'package:mockito/mockito.dart';
import 'src/common.dart';
import 'src/testbed.dart';
void main() {
Testbed testbed;
setUp(() {
testbed = Testbed();
});
test('Template.render throws ToolExit when FileSystem exception is raised', () => testbed.run(() {
final Template template = Template(fs.directory('examples'), fs.currentDirectory);
final MockDirectory mockDirectory = MockDirectory();
when(mockDirectory.createSync(recursive: true)).thenThrow(const FileSystemException());
expect(() => template.render(mockDirectory, <String, Object>{}),
throwsA(isInstanceOf<ToolExit>()));
}));
}
class MockDirectory extends Mock implements Directory {}