Skip to content

Commit 4e3be0b

Browse files
authored
Fix the --empty flag to not try working with non-app templates (#141632)
## Description This adds a check to make sure that the `--empty` flag isn't applied to non-app templates. ## Related Issues - Fixes flutter/flutter#141592 ## Tests - Added a test.
1 parent 9bbff1b commit 4e3be0b

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

packages/flutter_tools/lib/src/commands/create.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,19 @@ class CreateCommand extends CreateBase {
208208
String? sampleCode;
209209
final String? sampleArgument = stringArg('sample');
210210
final bool emptyArgument = boolArg('empty');
211+
final FlutterProjectType template = _getProjectType(projectDir);
211212
if (sampleArgument != null) {
212-
final String? templateArgument = stringArg('template');
213-
if (templateArgument != null && FlutterProjectType.fromCliName(templateArgument) != FlutterProjectType.app) {
213+
if (template != FlutterProjectType.app) {
214214
throwToolExit('Cannot specify --sample with a project type other than '
215215
'"${FlutterProjectType.app.cliName}"');
216216
}
217217
// Fetch the sample from the server.
218218
sampleCode = await _fetchSampleFromServer(sampleArgument);
219219
}
220+
if (emptyArgument && template != FlutterProjectType.app) {
221+
throwToolExit('The --empty flag is only supported for the app template.');
222+
}
220223

221-
final FlutterProjectType template = _getProjectType(projectDir);
222224
final bool generateModule = template == FlutterProjectType.module;
223225
final bool generateMethodChannelsPlugin = template == FlutterProjectType.plugin;
224226
final bool generateFfiPackage = template == FlutterProjectType.packageFfi;
@@ -764,8 +766,15 @@ Your $application code is in $relativeAppMain.
764766

765767
int _removeTestDir(Directory directory) {
766768
final Directory testDir = directory.childDirectory('test');
769+
if (!testDir.existsSync()) {
770+
return 0;
771+
}
767772
final List<FileSystemEntity> files = testDir.listSync(recursive: true);
768-
testDir.deleteSync(recursive: true);
773+
try {
774+
testDir.deleteSync(recursive: true);
775+
} on FileSystemException catch (exception) {
776+
throwToolExit('Failed to delete test directory: $exception');
777+
}
769778
return -files.length;
770779
}
771780

packages/flutter_tools/test/commands.shard/permeable/create_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,26 @@ void main() {
20382038
isNot(contains('Getting Started')));
20392039
});
20402040

2041+
2042+
testUsingContext("can't create an empty non-application project", () async {
2043+
final String outputDir = globals.fs.path.join(tempDir.path, 'test_project');
2044+
final CreateCommand command = CreateCommand();
2045+
final CommandRunner<void> runner = createTestCommandRunner(command);
2046+
final List<String> args = <String>[
2047+
'create',
2048+
'--no-pub',
2049+
'--empty',
2050+
'--template=plugin',
2051+
outputDir,
2052+
];
2053+
2054+
await expectLater(
2055+
runner.run(args),
2056+
throwsToolExit(
2057+
message: 'The --empty flag is only supported for the app template.',
2058+
));
2059+
});
2060+
20412061
testUsingContext('can create a sample-based project', () async {
20422062
await _createAndAnalyzeProject(
20432063
projectDir,

0 commit comments

Comments
 (0)