From 1a2da0d76d96b823298462876acdfe990255fdcc Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Fri, 24 May 2024 13:34:10 -0300 Subject: [PATCH 1/4] feat: adding better flavor tests --- .github/workflows/shorebird_ci.yml | 2 +- .../shorebird_tests/test/android_test.dart | 4 +- packages/shorebird_tests/test/ios_test.dart | 52 +++++++++++- .../shorebird_tests/test/shorebird_tests.dart | 82 +++++++++++-------- 4 files changed, 102 insertions(+), 38 deletions(-) diff --git a/.github/workflows/shorebird_ci.yml b/.github/workflows/shorebird_ci.yml index ea4f27f0361bd..079ff0151a160 100644 --- a/.github/workflows/shorebird_ci.yml +++ b/.github/workflows/shorebird_ci.yml @@ -57,5 +57,5 @@ jobs: java-version: "11" - name: 🐦 Run Shorebird Tests - run: dart test + run: dart test --concurrency=1 working-directory: packages/shorebird_tests 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..127e7f57c5bcc 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,7 +27,7 @@ void main() { ); final generatedYaml = - await projectDirectory.getGeneratedIoShorebirdYaml(); + await projectDirectory.getGeneratedIosShorebirdYaml(); expect( generatedYaml.keys, @@ -43,7 +43,53 @@ 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( + //flavor: 'internal', + ); + + 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( + //flavor: 'internal', + ); + + 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, From fe2655ed276337e732ce3fd016bd69931ecb18b7 Mon Sep 17 00:00:00 2001 From: Erick Date: Mon, 27 May 2024 15:04:19 -0300 Subject: [PATCH 2/4] Reverting concurrency change. --- .github/workflows/shorebird_ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/shorebird_ci.yml b/.github/workflows/shorebird_ci.yml index 079ff0151a160..ea4f27f0361bd 100644 --- a/.github/workflows/shorebird_ci.yml +++ b/.github/workflows/shorebird_ci.yml @@ -57,5 +57,5 @@ jobs: java-version: "11" - name: 🐦 Run Shorebird Tests - run: dart test --concurrency=1 + run: dart test working-directory: packages/shorebird_tests From c533a060ec32fc6c35470c0f00486aad462b86bd Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Mon, 27 May 2024 15:47:00 -0300 Subject: [PATCH 3/4] removing commented code --- packages/shorebird_tests/test/ios_test.dart | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/shorebird_tests/test/ios_test.dart b/packages/shorebird_tests/test/ios_test.dart index 127e7f57c5bcc..8e444bbedaf9f 100644 --- a/packages/shorebird_tests/test/ios_test.dart +++ b/packages/shorebird_tests/test/ios_test.dart @@ -53,9 +53,7 @@ void main() { await projectDirectory.runFlutterBuildIos(flavor: 'internal'); final generatedYaml = - await projectDirectory.getGeneratedIosShorebirdYaml( - //flavor: 'internal', - ); + await projectDirectory.getGeneratedIosShorebirdYaml(); expect(generatedYaml['app_id'], equals('internal_123')); }, @@ -77,9 +75,7 @@ void main() { ); final generatedYaml = - await projectDirectory.getGeneratedIosShorebirdYaml( - //flavor: 'internal', - ); + await projectDirectory.getGeneratedIosShorebirdYaml(); expect(generatedYaml['app_id'], equals('internal_123')); expect( From 5bd6dd085a0071ba3a98fe06c6bf5e0a9907d9d3 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Mon, 27 May 2024 15:48:37 -0300 Subject: [PATCH 4/4] removing print statement --- packages/shorebird_tests/test/ios_test.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/shorebird_tests/test/ios_test.dart b/packages/shorebird_tests/test/ios_test.dart index 8e444bbedaf9f..3fe6e1652c3e5 100644 --- a/packages/shorebird_tests/test/ios_test.dart +++ b/packages/shorebird_tests/test/ios_test.dart @@ -34,7 +34,6 @@ void main() { containsAll(originalYaml.keys), ); - print(generatedYaml); expect( generatedYaml['patch_public_key'], equals(base64PublicKey),