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
This commit is contained in:
Victor Eronmosele
2024-05-22 18:37:05 +01:00
committed by GitHub
parent 6332ff6690
commit 4c929f0f44
2 changed files with 50 additions and 18 deletions

View File

@@ -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(
<String>['app', 'app_test_widget'],
<String>['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<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)

View File

@@ -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,
<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,