From 4c929f0f447c0d56d056d903bd463a37022e74c4 Mon Sep 17 00:00:00 2001 From: Victor Eronmosele Date: Wed, 22 May 2024 18:37:05 +0100 Subject: [PATCH] Prevent test folder deletion on running `flutter create --empty` on an existing app project (#147160) This PR modifies the `flutter create --empty` command to not delete the `test/` folder when run on an existing app project. Before: ```bash flutter create my_app --empty mkdir my_app/test if test -d my_app/test; then echo "test exists"; else echo "test does not exist"; fi # test exists flutter create my_app --empty if test -d my_app/test; then echo "test exists"; else echo "test does not exist"; fi # test does not exist ``` After: ```bash flutter create my_app --empty mkdir my_app/test if test -d my_app/test; then echo "test exists"; else echo "test does not exist"; fi # test exists flutter create my_app --empty if test -d my_app/test; then echo "test exists"; else echo "test does not exist"; fi # test exists ``` Fixes https://github.com/flutter/flutter/issues/134928 --- .../lib/src/commands/create.dart | 22 ++------- .../commands.shard/permeable/create_test.dart | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 6eed33fb657c..4f2b1b2cb6a5 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 9782b28d9c6f..5fc257ce710e 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,