diff --git a/packages/shorebird_tests/test/android_test.dart b/packages/shorebird_tests/test/android_test.dart index 41d2a651d23e4..b24cce0ee9bf6 100644 --- a/packages/shorebird_tests/test/android_test.dart +++ b/packages/shorebird_tests/test/android_test.dart @@ -45,7 +45,7 @@ void main() { testWithShorebirdProject( 'correctly changes the app id', (projectDirectory) async { - projectDirectory.addAndroidFlavors(); + await projectDirectory.addProjectFlavors(); projectDirectory.addShorebirdFlavors(); await projectDirectory.runFlutterBuildApk(flavor: 'internal'); @@ -64,7 +64,7 @@ void main() { 'correctly changes the app id and adds the public key', (projectDirectory) async { const base64PublicKey = 'public_123'; - projectDirectory.addAndroidFlavors(); + await projectDirectory.addProjectFlavors(); projectDirectory.addShorebirdFlavors(); await projectDirectory.runFlutterBuildApk( diff --git a/packages/shorebird_tests/test/ios_test.dart b/packages/shorebird_tests/test/ios_test.dart index 519623c87ede2..3fe6e1652c3e5 100644 --- a/packages/shorebird_tests/test/ios_test.dart +++ b/packages/shorebird_tests/test/ios_test.dart @@ -10,7 +10,7 @@ void main() { await projectDirectory.runFlutterBuildIos(); expect(projectDirectory.iosArchiveFile().existsSync(), isTrue); - expect(projectDirectory.getGeneratedIoShorebirdYaml(), completes); + expect(projectDirectory.getGeneratedIosShorebirdYaml(), completes); }); group('when passing the public key through the environment variable', () { @@ -27,14 +27,13 @@ void main() { ); final generatedYaml = - await projectDirectory.getGeneratedIoShorebirdYaml(); + await projectDirectory.getGeneratedIosShorebirdYaml(); expect( generatedYaml.keys, containsAll(originalYaml.keys), ); - print(generatedYaml); expect( generatedYaml['patch_public_key'], equals(base64PublicKey), @@ -43,7 +42,49 @@ void main() { ); }); - // TODO(erickzanardo): Add tests for flavors. + group('when building with a flavor', () { + testWithShorebirdProject( + 'correctly changes the app id', + (projectDirectory) async { + await projectDirectory.addProjectFlavors(); + projectDirectory.addShorebirdFlavors(); + + await projectDirectory.runFlutterBuildIos(flavor: 'internal'); + + final generatedYaml = + await projectDirectory.getGeneratedIosShorebirdYaml(); + + expect(generatedYaml['app_id'], equals('internal_123')); + }, + ); + + group('when public key passed through environment variable', () { + testWithShorebirdProject( + 'correctly changes the app id and adds the public key', + (projectDirectory) async { + const base64PublicKey = 'public_123'; + await projectDirectory.addProjectFlavors(); + projectDirectory.addShorebirdFlavors(); + + await projectDirectory.runFlutterBuildIos( + flavor: 'internal', + environment: { + 'SHOREBIRD_PUBLIC_KEY': base64PublicKey, + }, + ); + + final generatedYaml = + await projectDirectory.getGeneratedIosShorebirdYaml(); + + expect(generatedYaml['app_id'], equals('internal_123')); + expect( + generatedYaml['patch_public_key'], + equals(base64PublicKey), + ); + }, + ); + }); + }); }, testOn: 'mac-os', ); diff --git a/packages/shorebird_tests/test/shorebird_tests.dart b/packages/shorebird_tests/test/shorebird_tests.dart index fbc26661fa7cb..847b319e6e6df 100644 --- a/packages/shorebird_tests/test/shorebird_tests.dart +++ b/packages/shorebird_tests/test/shorebird_tests.dart @@ -27,7 +27,7 @@ Future _runFlutterCommand( List arguments, { required Directory workingDirectory, Map? environment, -}) async { +}) { return Process.run( _flutterBinaryFile.absolute.path, arguments, @@ -110,42 +110,59 @@ extension ShorebirdProjectDirectoryOnDirectory on Directory { path.join(this.path, 'android', 'app', 'build.gradle'), ); - void addAndroidFlavors() { - // TODO(erickzanardo): Maybe in the future make this more dynamic - // and allow the user to pass the flavors, but it is good for now. - const flavors = ''' - flavorDimensions "track" - productFlavors { - playStore { - dimension "track" - applicationIdSuffix ".ps" - } - internal { - dimension "track" - applicationIdSuffix ".internal" - } - global { - dimension "track" - applicationIdSuffix ".global" - } - } -'''; + Future addPubDependency(String name, {bool dev = false}) { + return _runFlutterCommand( + ['pub', 'add', if (dev) '--dev', name], + workingDirectory: this, + ); + } - final currentGradleContent = appGradleFile.readAsStringSync(); - appGradleFile.writeAsStringSync( - ''' -${currentGradleContent.replaceFirst( - ' buildTypes {', - ' $flavors\n buildTypes {', - )} -''', + Future addProjectFlavors() async { + await addPubDependency('flutter_flavorizr', dev: true); + + await File( + path.join( + this.path, + 'flavorizr.yaml', + ), + ).writeAsString(''' +flavors: + playStore: + app: + name: "App" + + android: + applicationId: "com.example.shorebird_test" + ios: + bundleId: "com.example.shorebird_test" + internal: + app: + name: "App (Internal)" + + android: + applicationId: "com.example.shorebird_test.internal" + ios: + bundleId: "com.example.shorebird_test.internal" + global: + app: + name: "App (Global)" + + android: + applicationId: "com.example.shorebird_test.global" + ios: + bundleId: "com.example.shorebird_test.global" +'''); + + await _runFlutterCommand( + ['pub', 'run', 'flutter_flavorizr'], + workingDirectory: this, ); } void addShorebirdFlavors() { const flavors = ''' flavors: - global: global_123 + global: global_123 internal: internal_123 playStore: playStore_123 '''; @@ -179,11 +196,12 @@ $flavors Future runFlutterBuildIos({ Map? environment, + String? flavor, }) async { final result = await _runFlutterCommand( // The projects used to test are generated on spot, to make it simpler we don't // configure any apple accounts on it, so we skip code signing here. - ['build', 'ipa', '--no-codesign'], + ['build', 'ipa', '--no-codesign', if (flavor != null) '--flavor=$flavor'], workingDirectory: this, environment: environment, ); @@ -233,7 +251,7 @@ $flavors return loadYaml(yamlString) as YamlMap; } - Future getGeneratedIoShorebirdYaml() async { + Future getGeneratedIosShorebirdYaml() async { final yamlString = File( path.join( iosArchiveFile().path,