From ca2638b7eacb5642b9e80dad2659fc99968a01da Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Fri, 26 Jul 2024 13:39:45 -0700 Subject: [PATCH] test: add a test for shorebird.yaml compiling that uses disk Previous tests only worked in memory, this actually tests that we change the file on disk correctly. --- .github/workflows/shorebird_ci.yml | 1 + packages/flutter_tools/lib/src/ios/mac.dart | 2 +- .../lib/src/shorebird/shorebird_yaml.dart | 4 +-- .../shorebird/shorebird_yaml_test.dart | 25 +++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/.github/workflows/shorebird_ci.yml b/.github/workflows/shorebird_ci.yml index 0c191d76218ee..bfc10a055a118 100644 --- a/.github/workflows/shorebird_ci.yml +++ b/.github/workflows/shorebird_ci.yml @@ -48,6 +48,7 @@ jobs: if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' # TODO(eseidel): We can't run all flutter_tools tests until we make # our changes not throw exceptions on missing shorebird.yaml. + # https://github.com/shorebirdtech/shorebird/issues/2392 run: ../../bin/flutter test test/general.shard/shorebird working-directory: packages/flutter_tools diff --git a/packages/flutter_tools/lib/src/ios/mac.dart b/packages/flutter_tools/lib/src/ios/mac.dart index a9e7a1047c64c..d0a008289f472 100644 --- a/packages/flutter_tools/lib/src/ios/mac.dart +++ b/packages/flutter_tools/lib/src/ios/mac.dart @@ -519,7 +519,7 @@ Future buildXcodeProject({ } try { - updateShorebirdYaml(buildInfo, app.shorebirdYamlPath); + updateShorebirdYaml(buildInfo, app.shorebirdYamlPath, environment: globals.platform.environment); } on Exception catch (error) { globals.printError('[shorebird] failed to generate shorebird configuration.\n$error'); return XcodeBuildResult(success: false); diff --git a/packages/flutter_tools/lib/src/shorebird/shorebird_yaml.dart b/packages/flutter_tools/lib/src/shorebird/shorebird_yaml.dart index 6dcd3eb2e4aab..ba103d0109bf4 100644 --- a/packages/flutter_tools/lib/src/shorebird/shorebird_yaml.dart +++ b/packages/flutter_tools/lib/src/shorebird/shorebird_yaml.dart @@ -9,14 +9,14 @@ import '../base/file_system.dart'; import '../build_info.dart'; import '../globals.dart' as globals; -void updateShorebirdYaml(BuildInfo buildInfo, String shorebirdYamlPath) { +void updateShorebirdYaml(BuildInfo buildInfo, String shorebirdYamlPath, {required Map environment}) { final File shorebirdYaml = globals.fs.file(shorebirdYamlPath); if (!shorebirdYaml.existsSync()) { throw Exception('shorebird.yaml not found at $shorebirdYamlPath'); } final YamlDocument input = loadYamlDocument(shorebirdYaml.readAsStringSync()); final YamlMap yamlMap = input.contents as YamlMap; - final Map compiled = compileShorebirdYaml(yamlMap, flavor: buildInfo.flavor, environment: globals.platform.environment); + final Map compiled = compileShorebirdYaml(yamlMap, flavor: buildInfo.flavor, environment: environment); // Currently we write out over the same yaml file, we should fix this to // write to a new .json file instead and avoid naming confusion between the // input and compiled files. diff --git a/packages/flutter_tools/test/general.shard/shorebird/shorebird_yaml_test.dart b/packages/flutter_tools/test/general.shard/shorebird/shorebird_yaml_test.dart index 231cd2ddb14b5..68a373a3e8548 100644 --- a/packages/flutter_tools/test/general.shard/shorebird/shorebird_yaml_test.dart +++ b/packages/flutter_tools/test/general.shard/shorebird/shorebird_yaml_test.dart @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:io'; + +import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/shorebird/shorebird_yaml.dart'; import 'package:test/test.dart'; import 'package:yaml/yaml.dart'; @@ -69,5 +72,27 @@ base_url: https://example.com 'patch_public_key': '4-a', }); }); + test('edit in place', () { + const String yamlContents = ''' +app_id: 1-a +auto_update: false +flavors: + foo: 2-a + bar: 3-a +base_url: https://example.com +'''; + // Make a temporary file to test editing in place. + final Directory tempDir = Directory.systemTemp.createTempSync('shorebird_yaml_test.'); + final File tempFile = File('${tempDir.path}/shorebird.yaml'); + tempFile.writeAsStringSync(yamlContents); + updateShorebirdYaml(const BuildInfo(BuildMode.release, 'foo', treeShakeIcons: false), tempFile.path, environment: {'SHOREBIRD_PUBLIC_KEY': '4-a'}); + final String updatedContents = tempFile.readAsStringSync(); + // Order is not guaranteed, so parse as YAML to compare. + final YamlDocument updated = loadYamlDocument(updatedContents); + final YamlMap yamlMap = updated.contents as YamlMap; + expect(yamlMap['app_id'], '2-a'); + expect(yamlMap['auto_update'], false); + expect(yamlMap['base_url'], 'https://example.com'); + }); }); }