Skip to content

Commit 31c1292

Browse files
authored
Make native asset integration test more robust, thereby allowing smooth auto-update of packages via flutter update-packages (#158170)
Currently the bot that runs `flutter update-packages` makes PRs that fail due to native asset integration tests failing. The root cause is due to incompatible versions on `package:logging`. The bot tries to upgrade `package:logging` from `1.2.0` to `1.3.0`. Here's what seems to happen: * `flutter update-packages` will update `dev/integration_tests/link_hook/pubspec.yaml` with `package:logging` to `1.3.0` (as it does with all other `pubspec.yaml` files in the flutter repository) * `flutter create --template=package_ffi` will generate a template with `package:logging` `^1.2.0` * The test in question * creates ffi template (which will use `^1.2.0`) * make it depend on `dev/integration_tests/link_hook` (which uses `=1.3.0`) * changes logging dependency from the template from `^1.2.0` to `=1.2.0` IMHO * `flutter update-packages` is doing what it's supposed to * `flutter create --template=package_ffi` can generate templates with versions it determines (maybe there are use cases where we want to generate templates with older versions) * The problematic part is the test: * it makes the generated template depend on `link_hook` and * changes template generated pubspec to use pinned dependencies This PR makes the test package (created via template) use the pinned package versions from `dev/integration_tests/link_hook` (for dependencies that are common among the two). All other dependencies that the template has on top of `dev/integration_tests/link_hook` it can pin as it does currently. This will give us deterministic CI behavior (as we use flutter pined packages and remaining deps being pinned via template) It avoids changing the `flutter update-packages` and `flutter create --template=package_ffi` (as their behavior seems reasonable) Should fix flutter/flutter#158135
1 parent ecc2437 commit 31c1292

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

packages/flutter_tools/test/integration.shard/isolated/native_assets_test_utils.dart

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:convert';
56
import 'dart:io';
67

78
import 'package:file/file.dart';
89
import 'package:file_testing/file_testing.dart';
10+
import 'package:yaml/yaml.dart';
911

1012
import '../../src/common.dart';
1113
import '../test_utils.dart' show ProcessResultMatcher, fileSystem;
@@ -65,18 +67,30 @@ Future<void> addLinkHookDependency(String packageName, Directory packageDirector
6567
.childDirectory('link_hook');
6668
expect(linkHookDirectory, exists);
6769

68-
final File pubspecFile = packageDirectory.childFile('pubspec.yaml');
69-
final String pubspecOld =
70-
(await pubspecFile.readAsString()).replaceAll('\r\n', '\n');
71-
final String pubspecNew = pubspecOld.replaceFirst('''
72-
dependencies:
73-
''', '''
74-
dependencies:
75-
link_hook:
76-
path: ${linkHookDirectory.path}
77-
''');
78-
expect(pubspecNew, isNot(pubspecOld));
79-
await pubspecFile.writeAsString(pubspecNew);
70+
final File linkHookPubspecFile = linkHookDirectory.childFile('pubspec.yaml');
71+
final File thisPubspecFile = packageDirectory.childFile('pubspec.yaml');
72+
73+
final Map<String, Object?> linkHookPubspec = _pubspecAsMutableJson(linkHookPubspecFile.readAsStringSync());
74+
final Map<String, Object?> allLinkHookDeps = linkHookPubspec['dependencies']! as Map<String, Object?>;
75+
76+
final Map<String, Object?> thisPubspec = _pubspecAsMutableJson(thisPubspecFile.readAsStringSync());
77+
78+
final Map<String, Object?> thisDependencies = thisPubspec['dependencies']! as Map<String, Object?>;
79+
final Map<String, Object?> thisDevDependencies = thisPubspec['dev_dependencies']! as Map<String, Object?>;
80+
81+
// Flutter CI uses pinned dependencies for all packages (including
82+
// dev/integration_tests/link_hook) for deterministic testing on CI.
83+
//
84+
// The ffi template that was generated with `flutter create` does not use
85+
// pinned dependencies.
86+
//
87+
// We ensure that the test package we generate here will have versions
88+
// compatible with the one from flutter CIs pinned dependencies.
89+
_updateDependencies(thisDependencies, allLinkHookDeps);
90+
_updateDependencies(thisDevDependencies, allLinkHookDeps);
91+
thisDependencies['link_hook'] = <String, Object?>{ 'path' : linkHookDirectory.path };
92+
93+
await thisPubspecFile.writeAsString(json.encode(thisPubspec));
8094

8195
final File dartFile =
8296
packageDirectory.childDirectory('lib').childFile('$packageName.dart');
@@ -103,6 +117,16 @@ import '${packageName}_bindings_generated.dart' as bindings;
103117
await dartFile.writeAsString(dartFileNew2);
104118
}
105119

120+
Map<String, Object?> _pubspecAsMutableJson(String pubspecContent) {
121+
return json.decode(json.encode(loadYaml(pubspecContent))) as Map<String, Object?>;
122+
}
123+
124+
void _updateDependencies(Map<String, Object?> to, Map<String, Object?> from) {
125+
for (final String packageName in to.keys) {
126+
to[packageName] = from[packageName] ?? to[packageName];
127+
}
128+
}
129+
106130
/// Adds a native library to be built by the builder and dynamically link it to
107131
/// the main library.
108132
Future<void> addDynamicallyLinkedNativeLibrary(String packageName, Directory packageDirectory) async {

0 commit comments

Comments
 (0)