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
diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart
index 6eed33f..4f2b1b2 100644
--- a/packages/flutter_tools/lib/src/commands/create.dart
+++ b/packages/flutter_tools/lib/src/commands/create.dart
@@ -363,8 +363,11 @@
final PubContext pubContext;
switch (template) {
case FlutterProjectType.app:
+ final bool skipWidgetTestsGeneration =
+ sampleCode != null || emptyArgument;
+
generatedFileCount += await generateApp(
- <String>['app', 'app_test_widget'],
+ <String>['app', if (!skipWidgetTestsGeneration) 'app_test_widget'],
relativeDir,
templateContext,
overwrite: overwrite,
@@ -454,9 +457,6 @@
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 @@
mainDartFile.writeAsStringSync(sampleCode);
}
- int _removeTestDir(Directory directory) {
- final Directory testDir = directory.childDirectory('test');
- if (!testDir.existsSync()) {
- return 0;
- }
- final List<FileSystemEntity> 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<String> _getSupportedPlatformsFromTemplateContext(Map<String, Object?> templateContext) {
return <String>[
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 9782b28..5fc257c 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 @@
));
});
+ testUsingContext('does not remove an existing test/ directory when recreating an application project with the --empty flag', () async {
+ await _createProject(
+ projectDir,
+ <String>['--no-pub', '--empty'],
+ <String>[],
+ );
+
+ projectDir.childDirectory('test').childFile('example_test.dart').createSync(recursive: true);
+
+ await _createProject(
+ projectDir,
+ <String>['--no-pub', '--empty'],
+ <String>['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,
+ <String>['--no-pub', '--empty'],
+ <String>[],
+ unexpectedPaths: <String>['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,
+ <String>['--no-pub', '--empty'],
+ <String>[],
+ );
+
+ await _createProject(
+ projectDir,
+ <String>['--no-pub', '--empty'],
+ <String>[],
+ unexpectedPaths: <String>['test'],
+ );
+
+ expect(projectDir.childDirectory('test'), isNot(exists));
+ });
+
testUsingContext('can create a sample-based project', () async {
await _createAndAnalyzeProject(
projectDir,