diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 6eed33fb65..4f2b1b2cb6 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -363,8 +363,11 @@ class CreateCommand extends CreateBase { final PubContext pubContext; switch (template) { case FlutterProjectType.app: + final bool skipWidgetTestsGeneration = + sampleCode != null || emptyArgument; + generatedFileCount += await generateApp( - ['app', 'app_test_widget'], + ['app', if (!skipWidgetTestsGeneration) 'app_test_widget'], relativeDir, templateContext, overwrite: overwrite, @@ -454,9 +457,6 @@ class CreateCommand extends CreateBase { if (sampleCode != null) { _applySample(relativeDir, sampleCode); } - if (sampleCode != null || emptyArgument) { - generatedFileCount += _removeTestDir(relativeDir); - } globals.printStatus('Wrote $generatedFileCount files.'); globals.printStatus('\nAll done!'); final String application = @@ -779,20 +779,6 @@ Your $application code is in $relativeAppMain. mainDartFile.writeAsStringSync(sampleCode); } - int _removeTestDir(Directory directory) { - final Directory testDir = directory.childDirectory('test'); - if (!testDir.existsSync()) { - return 0; - } - final List files = testDir.listSync(recursive: true); - try { - testDir.deleteSync(recursive: true); - } on FileSystemException catch (exception) { - throwToolExit('Failed to delete test directory: $exception'); - } - return -files.length; - } - List _getSupportedPlatformsFromTemplateContext(Map templateContext) { return [ for (final String platform in kAllCreatePlatforms) diff --git a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart index 9782b28d9c..5fc257ce71 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart @@ -2109,6 +2109,52 @@ void main() { )); }); + testUsingContext('does not remove an existing test/ directory when recreating an application project with the --empty flag', () async { + await _createProject( + projectDir, + ['--no-pub', '--empty'], + [], + ); + + projectDir.childDirectory('test').childFile('example_test.dart').createSync(recursive: true); + + await _createProject( + projectDir, + ['--no-pub', '--empty'], + ['test/example_test.dart'], + ); + + expect(projectDir.childDirectory('test').childFile('example_test.dart'), exists); + }); + + testUsingContext('does not create a test/ directory when creating a new application project with the --empty flag', () async { + await _createProject( + projectDir, + ['--no-pub', '--empty'], + [], + unexpectedPaths: ['test'], + ); + + expect(projectDir.childDirectory('test'), isNot(exists)); + }); + + testUsingContext("does not create a test/ directory, if it doesn't already exist, when recreating an application project with the --empty flag", () async { + await _createProject( + projectDir, + ['--no-pub', '--empty'], + [], + ); + + await _createProject( + projectDir, + ['--no-pub', '--empty'], + [], + unexpectedPaths: ['test'], + ); + + expect(projectDir.childDirectory('test'), isNot(exists)); + }); + testUsingContext('can create a sample-based project', () async { await _createAndAnalyzeProject( projectDir,