Unverified Commit 78b45fb1 authored by Zachary Anderson's avatar Zachary Anderson Committed by GitHub

[flutter_tools] Throw a tool exit when samples fetch fails (#52355)

parent 3d0082d0
...@@ -239,13 +239,21 @@ class CreateCommand extends FlutterCommand { ...@@ -239,13 +239,21 @@ class CreateCommand extends FlutterCommand {
} }
final Uri snippetsUri = Uri.https(_snippetsHost, 'snippets/$sampleId.dart'); final Uri snippetsUri = Uri.https(_snippetsHost, 'snippets/$sampleId.dart');
return utf8.decode(await _net.fetchUrl(snippetsUri)); final List<int> data = await _net.fetchUrl(snippetsUri);
if (data == null || data.isEmpty) {
return null;
}
return utf8.decode(data);
} }
/// Fetches the samples index file from the Flutter docs website. /// Fetches the samples index file from the Flutter docs website.
Future<String> _fetchSamplesIndexFromServer() async { Future<String> _fetchSamplesIndexFromServer() async {
final Uri snippetsUri = Uri.https(_snippetsHost, 'snippets/index.json'); final Uri snippetsUri = Uri.https(_snippetsHost, 'snippets/index.json');
return utf8.decode(await _net.fetchUrl(snippetsUri, maxAttempts: 2)); final List<int> data = await _net.fetchUrl(snippetsUri, maxAttempts: 2);
if (data == null || data.isEmpty) {
return null;
}
return utf8.decode(data);
} }
/// Fetches the samples index file from the server and writes it to /// Fetches the samples index file from the server and writes it to
...@@ -259,8 +267,7 @@ class CreateCommand extends FlutterCommand { ...@@ -259,8 +267,7 @@ class CreateCommand extends FlutterCommand {
final String samplesJson = await _fetchSamplesIndexFromServer(); final String samplesJson = await _fetchSamplesIndexFromServer();
if (samplesJson == null) { if (samplesJson == null) {
throwToolExit('Unable to download samples', exitCode: 2); throwToolExit('Unable to download samples', exitCode: 2);
} } else {
else {
outputFile.writeAsStringSync(samplesJson); outputFile.writeAsStringSync(samplesJson);
globals.printStatus('Wrote samples JSON to "$outputFilePath"'); globals.printStatus('Wrote samples JSON to "$outputFilePath"');
} }
......
...@@ -1283,6 +1283,28 @@ void main() { ...@@ -1283,6 +1283,28 @@ void main() {
HttpClientFactory: () => HttpClientFactory: () =>
() => MockHttpClient(200, result: samplesIndexJson), () => MockHttpClient(200, result: samplesIndexJson),
}); });
testUsingContext('Throws tool exit on empty samples index', () async {
final String outputFile = globals.fs.path.join(tempDir.path, 'flutter_samples.json');
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
final List<String> args = <String>[
'create',
'--list-samples',
outputFile,
];
await expectLater(
runner.run(args),
throwsToolExit(
exitCode: 2,
message: 'Unable to download samples',
));
}, overrides: <Type, Generator>{
HttpClientFactory: () =>
() => MockHttpClient(200, result: ''),
});
testUsingContext('provides an error to the user if samples json download fails', () async { testUsingContext('provides an error to the user if samples json download fails', () async {
final String outputFile = globals.fs.path.join(tempDir.path, 'flutter_samples.json'); final String outputFile = globals.fs.path.join(tempDir.path, 'flutter_samples.json');
final CreateCommand command = CreateCommand(); final CreateCommand command = CreateCommand();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment