Skip to content

Commit 70b1fee

Browse files
Erengunwolfenrain
andauthored
fix: reorder Flutter SDK check and pubGet execution in PackagesGetCommand (#1285)
* fix: reorder Flutter SDK check and pubGet execution in PackagesGetCommand * fix: fix formatting on lib/src/commands/packages/commands/get.dart * test: add test for handling Flutter not installed scenario in packages get command * Update test/src/commands/packages/commands/get_test.dart Co-authored-by: Jochum van der Ploeg <[email protected]> * Update lib/src/commands/packages/commands/get.dart * fix: improve error message for missing Flutter SDK * fix: enhance error message for missing Flutter SDK with troubleshooting link --------- Co-authored-by: Jochum van der Ploeg <[email protected]>
1 parent 31aba9f commit 70b1fee

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

lib/src/commands/packages/commands/get.dart

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,27 @@ class PackagesGetCommand extends Command<int> {
5050
final target = _argResults.rest.length == 1 ? _argResults.rest[0] : '.';
5151
final targetPath = path.normalize(Directory(target).absolute.path);
5252
final isFlutterInstalled = await Flutter.installed(logger: _logger);
53-
if (isFlutterInstalled) {
54-
try {
55-
await Flutter.pubGet(
56-
cwd: targetPath,
57-
recursive: recursive,
58-
ignore: ignore,
59-
logger: _logger,
60-
);
61-
} on PubspecNotFound catch (_) {
62-
_logger.err('Could not find a pubspec.yaml in $targetPath');
63-
return ExitCode.noInput.code;
64-
} on Exception catch (error) {
65-
_logger.err('$error');
66-
return ExitCode.unavailable.code;
67-
}
53+
if (!isFlutterInstalled) {
54+
_logger.err(
55+
'Could not find Flutter SDK. '
56+
'Please ensure it is installed and added to your PATH. '
57+
'For troubleshooting, see https://docs.flutter.dev/install/troubleshoot',
58+
);
59+
return ExitCode.unavailable.code;
60+
}
61+
try {
62+
await Flutter.pubGet(
63+
cwd: targetPath,
64+
recursive: recursive,
65+
ignore: ignore,
66+
logger: _logger,
67+
);
68+
} on PubspecNotFound catch (_) {
69+
_logger.err('Could not find a pubspec.yaml in $targetPath');
70+
return ExitCode.noInput.code;
71+
} on Exception catch (error) {
72+
_logger.err('$error');
73+
return ExitCode.unavailable.code;
6874
}
6975
return ExitCode.success.code;
7076
}

test/src/commands/packages/commands/get_test.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:mocktail/mocktail.dart';
77
import 'package:path/path.dart' as path;
88
import 'package:test/test.dart';
99
import 'package:universal_io/io.dart';
10+
import 'package:very_good_cli/src/cli/cli.dart';
1011

1112
import '../../../../helpers/helpers.dart';
1213

@@ -99,6 +100,58 @@ void main() {
99100
}),
100101
);
101102

103+
test(
104+
'returns exit code unavailable when Flutter is not installed',
105+
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
106+
final tempDirectory = Directory.systemTemp.createTempSync();
107+
addTearDown(() => tempDirectory.deleteSync(recursive: true));
108+
109+
File(path.join(tempDirectory.path, 'pubspec.yaml')).writeAsStringSync(
110+
'''
111+
name: example
112+
version: 0.1.0
113+
114+
environment:
115+
sdk: ^3.8.0
116+
''',
117+
);
118+
119+
// Mock flutter process to simulate flutter not being installed
120+
Future<ProcessResult> mockProcess(
121+
String executable,
122+
List<String> arguments, {
123+
String? workingDirectory,
124+
bool runInShell = false,
125+
}) async {
126+
if (executable == 'flutter') {
127+
throw Exception('flutter not installed');
128+
}
129+
return ProcessResult(42, 0, '', '');
130+
}
131+
132+
late int result;
133+
await ProcessOverrides.runZoned(
134+
() async {
135+
result = await commandRunner.run([
136+
'packages',
137+
'get',
138+
tempDirectory.path,
139+
]);
140+
},
141+
runProcess: mockProcess,
142+
);
143+
144+
expect(result, equals(ExitCode.unavailable.code));
145+
verify(() {
146+
logger.err(
147+
'Could not find Flutter SDK. '
148+
'Please ensure it is installed and added to your PATH. '
149+
'For troubleshooting, see https://docs.flutter.dev/install/troubleshoot',
150+
);
151+
}).called(1);
152+
}),
153+
);
154+
102155
test(
103156
'ignores .fvm directory',
104157
withRunner((commandRunner, logger, pubUpdater, printLogs) async {

0 commit comments

Comments
 (0)