Skip to content

Commit

Permalink
Prevent test folder deletion on running flutter create --empty on a…
Browse files Browse the repository at this point in the history
…n 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 flutter/flutter#134928
  • Loading branch information
victoreronmosele authored May 22, 2024
1 parent 6332ff6 commit 4c929f0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
22 changes: 4 additions & 18 deletions packages/flutter_tools/lib/src/commands/create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4c929f0

Please sign in to comment.