Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ class FirebaseDartConfigurationWrite {
}

fileConfigurationLines.removeRange(startIndex, endIndex + 1);
while (startIndex < fileConfigurationLines.length &&
fileConfigurationLines[startIndex].trim().isEmpty) {
fileConfigurationLines.removeAt(startIndex);
}

// Insert the new platform configuration
fileConfigurationLines.insertAll(
startIndex - 1,
startIndex,
_buildFirebaseOptions(
options,
platform.toLowerCase(),
Expand Down Expand Up @@ -177,14 +181,16 @@ class FirebaseDartConfigurationWrite {
}
}

return formatList(fileConfigurationLines).join('\n');
final result = formatList(fileConfigurationLines).join('\n');
return result.endsWith('\n') ? result : '$result\n';
}

String _buildConfigurationFile() {
_stringBuffer.clear();
_writeHeader();
_writeClass();
return formatList(_stringBuffer.toString().split('\n')).join('\n');
final result = formatList(_stringBuffer.toString().split('\n')).join('\n');
return result.endsWith('\n') ? result : '$result\n';
}

// ensure only one empty line between each static property
Expand All @@ -208,7 +214,6 @@ class FirebaseDartConfigurationWrite {
.where((entry) => entry.value != null)
.map((entry) => " ${entry.key}: '${entry.value}',"),
' );', // FirebaseOptions
'',
];
}

Expand Down Expand Up @@ -281,14 +286,8 @@ class FirebaseDartConfigurationWrite {
}

void _writeClass() {
_stringBuffer.writeAll(
<String>[
'class DefaultFirebaseOptions {',
' static FirebaseOptions get currentPlatform {',
'',
],
'\n',
);
_stringBuffer.writeln('class DefaultFirebaseOptions {');
_stringBuffer.writeln(' static FirebaseOptions get currentPlatform {');
_writeCurrentPlatformWeb();
_stringBuffer.writeln(' switch (defaultTargetPlatform) {');
_writeCurrentPlatformSwitchAndroid();
Expand Down Expand Up @@ -321,6 +320,7 @@ class FirebaseDartConfigurationWrite {
_buildFirebaseOptions(options, platform),
'\n',
);
_stringBuffer.writeln();
}

void _writeThrowUnsupportedForPlatform(String platform, String indentation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,44 @@ import 'package:test/test.dart';

void main() {
group('FirebaseDartConfigurationWrite', () {
// Regression test for
// https://github.com/invertase/flutterfire_cli/issues/422
test(
'replaces placeholder firebase_options.dart with generated output',
() {
void testWithWriter({
required String testName,
String? initialContent,
FirebaseOptions? webOptions,
FirebaseOptions? androidOptions,
required void Function(String content) verify,
}) {
test(testName, () {
final tempDir = Directory.systemTemp.createTempSync();
addTearDown(() => tempDir.deleteSync(recursive: true));

final filePath = p.join(tempDir.path, 'lib', 'firebase_options.dart');
File(filePath)
..createSync(recursive: true)
..writeAsStringSync('''
final file = File(filePath)..createSync(recursive: true);
if (initialContent != null) {
file.writeAsStringSync(initialContent);
}

final writer = FirebaseDartConfigurationWrite(
configurationFilePath: filePath,
flutterAppPath: tempDir.path,
firebaseProjectId: 'test-project-id',
webOptions: webOptions,
androidOptions: androidOptions,
);

writer.write();

final updatedContent = file.readAsStringSync();
verify(updatedContent);
});
}

// Regression test for
// https://github.com/invertase/flutterfire_cli/issues/422
testWithWriter(
testName:
'replaces placeholder firebase_options.dart with generated output',
initialContent: '''
// File normally generated by FlutterFire CLI. This is a stand-in.
// See README.md for details.
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
Expand All @@ -32,31 +58,65 @@ class DefaultFirebaseOptions {
);
}
}
''');

final writer = FirebaseDartConfigurationWrite(
configurationFilePath: filePath,
flutterAppPath: tempDir.path,
firebaseProjectId: 'test-project-id',
webOptions: const FirebaseOptions(
optionsSourceContent: '{}',
optionsSourceFileName: 'firebase-config.json',
apiKey: 'api-key',
appId: 'app-id',
messagingSenderId: 'sender-id',
projectId: 'test-project-id',
measurementId: 'measurement-id',
),
);

writer.write();

final updatedContent = File(filePath).readAsStringSync();
''',
webOptions: const FirebaseOptions(
optionsSourceContent: '{}',
optionsSourceFileName: 'firebase-config.json',
apiKey: 'api-key',
appId: 'app-id',
messagingSenderId: 'sender-id',
projectId: 'test-project-id',
measurementId: 'measurement-id',
),
verify: (updatedContent) {
expect(updatedContent, contains('if (kIsWeb) {'));
expect(updatedContent, contains('return web;'));
expect(updatedContent, contains('static const FirebaseOptions web'));
expect(updatedContent, isNot(contains('UnimplementedError')));
},
);

testWithWriter(
testName: 'generates correct output for JUST web (golden test)',
webOptions: const FirebaseOptions(
optionsSourceContent: '{}',
optionsSourceFileName: 'firebase-config.json',
apiKey: 'api-key',
appId: 'app-id',
messagingSenderId: 'sender-id',
projectId: 'test-project-id',
measurementId: 'measurement-id',
),
verify: (updatedContent) {
final goldenFile = File('test/goldens/firebase_options_just_web.dart_');
final expectedContent = goldenFile.readAsStringSync();
expect(updatedContent, equals(expectedContent));
},
);

testWithWriter(
testName:
'cleans up trailing empty lines when updating existing configuration',
initialContent: '''
class DefaultFirebaseOptions {
static const FirebaseOptions web = FirebaseOptions(
apiKey: 'old-api-key',
);

}
''',
webOptions: const FirebaseOptions(
optionsSourceContent: '{}',
optionsSourceFileName: 'firebase-config.json',
apiKey: 'new-api-key',
appId: 'app-id',
messagingSenderId: 'sender-id',
projectId: 'test-project-id',
),
verify: (updatedContent) {
expect(updatedContent, contains(');\n}'));
expect(updatedContent, isNot(contains(');\n\n}')));
},
);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// File generated by FlutterFire CLI.
// ignore_for_file: type=lint
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;

/// Default [FirebaseOptions] for use with your Firebase apps.
///
/// Example:
/// ```dart
/// import 'firebase_options.dart';
/// // ...
/// await Firebase.initializeApp(
/// options: DefaultFirebaseOptions.currentPlatform,
/// );
/// ```
class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
return web;
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for android - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.iOS:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for ios - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.macOS:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for macos - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.windows:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for windows - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.linux:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for linux - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
default:
throw UnsupportedError(
'DefaultFirebaseOptions are not supported for this platform.',
);
}
}

static const FirebaseOptions web = FirebaseOptions(
apiKey: 'api-key',
appId: 'app-id',
messagingSenderId: 'sender-id',
projectId: 'test-project-id',
measurementId: 'measurement-id',
);
}
Loading