@@ -7,18 +7,44 @@ import 'package:test/test.dart';
77
88void main () {
99 group ('FirebaseDartConfigurationWrite' , () {
10- // Regression test for
11- // https://github.com/invertase/flutterfire_cli/issues/422
12- test (
13- 'replaces placeholder firebase_options.dart with generated output' ,
14- () {
10+ void testWithWriter ({
11+ required String testName,
12+ String ? initialContent,
13+ FirebaseOptions ? webOptions,
14+ FirebaseOptions ? androidOptions,
15+ required void Function (String content) verify,
16+ }) {
17+ test (testName, () {
1518 final tempDir = Directory .systemTemp.createTempSync ();
1619 addTearDown (() => tempDir.deleteSync (recursive: true ));
1720
1821 final filePath = p.join (tempDir.path, 'lib' , 'firebase_options.dart' );
19- File (filePath)
20- ..createSync (recursive: true )
21- ..writeAsStringSync ('''
22+ final file = File (filePath)..createSync (recursive: true );
23+ if (initialContent != null ) {
24+ file.writeAsStringSync (initialContent);
25+ }
26+
27+ final writer = FirebaseDartConfigurationWrite (
28+ configurationFilePath: filePath,
29+ flutterAppPath: tempDir.path,
30+ firebaseProjectId: 'test-project-id' ,
31+ webOptions: webOptions,
32+ androidOptions: androidOptions,
33+ );
34+
35+ writer.write ();
36+
37+ final updatedContent = file.readAsStringSync ();
38+ verify (updatedContent);
39+ });
40+ }
41+
42+ // Regression test for
43+ // https://github.com/invertase/flutterfire_cli/issues/422
44+ testWithWriter (
45+ testName:
46+ 'replaces placeholder firebase_options.dart with generated output' ,
47+ initialContent: '''
2248// File normally generated by FlutterFire CLI. This is a stand-in.
2349// See README.md for details.
2450import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
@@ -32,31 +58,65 @@ class DefaultFirebaseOptions {
3258 );
3359 }
3460}
35- ''' );
36-
37- final writer = FirebaseDartConfigurationWrite (
38- configurationFilePath: filePath,
39- flutterAppPath: tempDir.path,
40- firebaseProjectId: 'test-project-id' ,
41- webOptions: const FirebaseOptions (
42- optionsSourceContent: '{}' ,
43- optionsSourceFileName: 'firebase-config.json' ,
44- apiKey: 'api-key' ,
45- appId: 'app-id' ,
46- messagingSenderId: 'sender-id' ,
47- projectId: 'test-project-id' ,
48- measurementId: 'measurement-id' ,
49- ),
50- );
51-
52- writer.write ();
53-
54- final updatedContent = File (filePath).readAsStringSync ();
61+ ''' ,
62+ webOptions: const FirebaseOptions (
63+ optionsSourceContent: '{}' ,
64+ optionsSourceFileName: 'firebase-config.json' ,
65+ apiKey: 'api-key' ,
66+ appId: 'app-id' ,
67+ messagingSenderId: 'sender-id' ,
68+ projectId: 'test-project-id' ,
69+ measurementId: 'measurement-id' ,
70+ ),
71+ verify: (updatedContent) {
5572 expect (updatedContent, contains ('if (kIsWeb) {' ));
5673 expect (updatedContent, contains ('return web;' ));
5774 expect (updatedContent, contains ('static const FirebaseOptions web' ));
5875 expect (updatedContent, isNot (contains ('UnimplementedError' )));
5976 },
6077 );
78+
79+ testWithWriter (
80+ testName: 'generates correct output for JUST web (golden test)' ,
81+ webOptions: const FirebaseOptions (
82+ optionsSourceContent: '{}' ,
83+ optionsSourceFileName: 'firebase-config.json' ,
84+ apiKey: 'api-key' ,
85+ appId: 'app-id' ,
86+ messagingSenderId: 'sender-id' ,
87+ projectId: 'test-project-id' ,
88+ measurementId: 'measurement-id' ,
89+ ),
90+ verify: (updatedContent) {
91+ final goldenFile = File ('test/goldens/firebase_options_just_web.dart_' );
92+ final expectedContent = goldenFile.readAsStringSync ();
93+ expect (updatedContent, equals (expectedContent));
94+ },
95+ );
96+
97+ testWithWriter (
98+ testName:
99+ 'cleans up trailing empty lines when updating existing configuration' ,
100+ initialContent: '''
101+ class DefaultFirebaseOptions {
102+ static const FirebaseOptions web = FirebaseOptions(
103+ apiKey: 'old-api-key',
104+ );
105+
106+ }
107+ ''' ,
108+ webOptions: const FirebaseOptions (
109+ optionsSourceContent: '{}' ,
110+ optionsSourceFileName: 'firebase-config.json' ,
111+ apiKey: 'new-api-key' ,
112+ appId: 'app-id' ,
113+ messagingSenderId: 'sender-id' ,
114+ projectId: 'test-project-id' ,
115+ ),
116+ verify: (updatedContent) {
117+ expect (updatedContent, contains (');\n }' ));
118+ expect (updatedContent, isNot (contains (');\n\n }' )));
119+ },
120+ );
61121 });
62122}
0 commit comments