Skip to content

Commit

Permalink
fix: firebase CLI can sometimes append time out object on successful …
Browse files Browse the repository at this point in the history
…JSON response
  • Loading branch information
russellwheatley committed Apr 18, 2024
1 parent c4c11b3 commit 1c61491
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/flutterfire_cli/lib/src/common/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ const noPathVariableFound = r'There is no $PATH variable in your environment. '
const validationCheck =
'This should be validated before any configuration is written to the project.';

/// Text appended to firebase tools CLI output even when successful. See:
/// https://github.com/invertase/flutterfire_cli/issues/262
/// https://github.com/invertase/flutterfire_cli/issues/282
const appendedErrorText = '''
}{
"status": "error",
"error": "Timed out."
}''';

/// A base class for all FlutterFire CLI exceptions.
abstract class FlutterFireException implements Exception {}

Expand Down
7 changes: 7 additions & 0 deletions packages/flutterfire_cli/lib/src/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,10 @@ void validateAndroidPackageName(String appId) {
);
}
}

String firebaseCLIJsonParse(String output) {
return output.replaceFirst(
appendedErrorText,
'}',
);
}
2 changes: 1 addition & 1 deletion packages/flutterfire_cli/lib/src/firebase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Future<Map<String, dynamic>> runFirebaseCommand(
runInShell: true,
);

final jsonString = process.stdout.toString();
final jsonString = firebaseCLIJsonParse(process.stdout.toString());

Map<String, dynamic> commandResult;

Expand Down
94 changes: 94 additions & 0 deletions packages/flutterfire_cli/test/unit_tests.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:flutterfire_cli/src/common/strings.dart';
import 'package:flutterfire_cli/src/common/utils.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -128,4 +130,96 @@ void main() {
);
});
});

group(
'Firebase CLI JSON response parser function `firebaseCLIJsonParse()`',
() {
test('Valid JSON response from Firebase CLI', () {
const jsonData = '''
{
"status": "success",
"result": [
{
"projectId": "project-id",
"projectNumber": "2380",
"displayName": "Display",
"name": "projects/project-id",
"resources": {
"hostingSite": "project-id",
"storageBucket": "project-id.appspot.com",
"locationId": "europe-west"
},
"state": "ACTIVE",
"etag": "1_c74d64e0-ba66-42e6-88se-303ds3222dsds"
},
{
"projectId": "rtc-test-94090",
"projectNumber": "978336444",
"displayName": "rtc-test",
"name": "projects/rtc-test-9848",
"resources": {
"hostingSite": "rtc-test-93d9839"
},
"state": "ACTIVE",
"etag": "1_81c10fa0-f712-4cbf-b0fd-e35e0403094"
}
]
}
''';

final jsonString = firebaseCLIJsonParse(jsonData);
// This should succeed in parsing JSON
final result = Map<String, dynamic>.from(
const JsonDecoder().convert(jsonString) as Map,
);

expect(result['status'], 'success');
});

test('Invalid JSON response from Firebase CLI', () {
const jsonData = '''
{
"status": "success",
"result": [
{
"projectId": "project-id",
"projectNumber": "2380",
"displayName": "Display",
"name": "projects/project-id",
"resources": {
"hostingSite": "project-id",
"storageBucket": "project-id.appspot.com",
"locationId": "europe-west"
},
"state": "ACTIVE",
"etag": "1_c74d64e0-ba66-42e6-88se-303ds3222dsds"
},
{
"projectId": "rtc-test-94090",
"projectNumber": "978336444",
"displayName": "rtc-test",
"name": "projects/rtc-test-9848",
"resources": {
"hostingSite": "rtc-test-93d9839"
},
"state": "ACTIVE",
"etag": "1_81c10fa0-f712-4cbf-b0fd-e35e0403094"
}
]
}{
"status": "error",
"error": "Timed out."
}
''';

final jsonString = firebaseCLIJsonParse(jsonData);
// This should succeed in parsing JSON
final result = Map<String, dynamic>.from(
const JsonDecoder().convert(jsonString) as Map,
);

expect(result['status'], 'success');
});
},
);
}

0 comments on commit 1c61491

Please sign in to comment.