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

This commit is contained in:
Zachary Anderson
2020-03-10 15:21:02 -07:00
committed by GitHub
parent 3d0082d054
commit 78b45fb1b2
2 changed files with 33 additions and 4 deletions

View File

@@ -239,13 +239,21 @@ class CreateCommand extends FlutterCommand {
}
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.
Future<String> _fetchSamplesIndexFromServer() async {
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
@@ -259,8 +267,7 @@ class CreateCommand extends FlutterCommand {
final String samplesJson = await _fetchSamplesIndexFromServer();
if (samplesJson == null) {
throwToolExit('Unable to download samples', exitCode: 2);
}
else {
} else {
outputFile.writeAsStringSync(samplesJson);
globals.printStatus('Wrote samples JSON to "$outputFilePath"');
}

View File

@@ -1283,6 +1283,28 @@ void main() {
HttpClientFactory: () =>
() => 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 {
final String outputFile = globals.fs.path.join(tempDir.path, 'flutter_samples.json');
final CreateCommand command = CreateCommand();