diff --git a/packages/flutter_tools/lib/src/template.dart b/packages/flutter_tools/lib/src/template.dart index 943730c2dd..73701e71b7 100644 --- a/packages/flutter_tools/lib/src/template.dart +++ b/packages/flutter_tools/lib/src/template.dart @@ -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 _templateFilePaths; + /// Render the template into [directory]. + /// + /// May throw a [ToolExit] if the directory is not writable. int render( Directory destination, Map 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 diff --git a/packages/flutter_tools/test/template_test.dart b/packages/flutter_tools/test/template_test.dart new file mode 100644 index 0000000000..1cca5580dc --- /dev/null +++ b/packages/flutter_tools/test/template_test.dart @@ -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, {}), + throwsA(isInstanceOf())); + })); +} + +class MockDirectory extends Mock implements Directory {}