|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io' as io; |
| 6 | + |
| 7 | +import 'package:file/file.dart'; |
| 8 | +import 'package:process/process.dart'; |
| 9 | +import 'package:yaml/yaml.dart'; |
| 10 | + |
| 11 | +import '../../src/common.dart'; |
| 12 | +import '../test_utils.dart'; |
| 13 | +import '../transition_test_utils.dart'; |
| 14 | +import 'native_assets_test_utils.dart'; |
| 15 | + |
| 16 | +/// Regression test as part of https://github.com/flutter/flutter/pull/150742. |
| 17 | +/// |
| 18 | +/// Previously, creating a new (blank, i.e. from `flutter create`) Flutter |
| 19 | +/// project, adding `native_assets_cli`, and adding an otherwise valid build hook |
| 20 | +/// (`/hook/build.dart`) would fail to build due to the accompanying shell script |
| 21 | +/// (at least on macOS) assuming the glob would find at least one output. |
| 22 | +/// |
| 23 | +/// This test verifies that a blank Flutter project with native_assets_cli can |
| 24 | +/// build, and does so across all of the host platform and target platform |
| 25 | +/// combinations that could trigger this error. |
| 26 | +/// |
| 27 | +/// The version of `native_assets_cli` is derived from the template used by |
| 28 | +/// `flutter create --type=pacakges_ffi`. See |
| 29 | +/// [_getPackageFfiTemplatePubspecVersion]. |
| 30 | +void main() { |
| 31 | + if (!platform.isMacOS && !platform.isLinux && !platform.isWindows) { |
| 32 | + // TODO(dacoharkes): Implement Fuchsia. https://github.com/flutter/flutter/issues/129757 |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + const ProcessManager processManager = LocalProcessManager(); |
| 38 | + final String constraint = _getPackageFfiTemplatePubspecVersion(); |
| 39 | + |
| 40 | + setUpAll(() { |
| 41 | + processManager.runSync(<String>[ |
| 42 | + flutterBin, |
| 43 | + 'config', |
| 44 | + '--enable-native-assets', |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + // Test building a host, iOS, and APK (Android) target where possible. |
| 49 | + for (final String buildCommand in <String>[ |
| 50 | + // Current (Host) OS. |
| 51 | + platform.operatingSystem, |
| 52 | + |
| 53 | + // On macOS, also test iOS. |
| 54 | + if (platform.isMacOS) 'ios', |
| 55 | + |
| 56 | + // On every host platform, test Android. |
| 57 | + 'apk', |
| 58 | + ]) { |
| 59 | + _testBuildCommand( |
| 60 | + buildCommand: buildCommand, |
| 61 | + processManager: processManager, |
| 62 | + nativeAssetsCliVersionConstraint: constraint, |
| 63 | + codeSign: buildCommand != 'ios', |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void _testBuildCommand({ |
| 69 | + required String buildCommand, |
| 70 | + required String nativeAssetsCliVersionConstraint, |
| 71 | + required ProcessManager processManager, |
| 72 | + required bool codeSign, |
| 73 | +}) { |
| 74 | + testWithoutContext( |
| 75 | + 'flutter build "$buildCommand" succeeds without libraries', |
| 76 | + () async { |
| 77 | + await inTempDir((Directory tempDirectory) async { |
| 78 | + const String packageName = 'uses_package_native_assets_cli'; |
| 79 | + |
| 80 | + // Create a new (plain Dart SDK) project. |
| 81 | + await expectLater( |
| 82 | + processManager.run( |
| 83 | + <String>[ |
| 84 | + flutterBin, |
| 85 | + 'create', |
| 86 | + '--no-pub', |
| 87 | + packageName, |
| 88 | + ], |
| 89 | + workingDirectory: tempDirectory.path, |
| 90 | + ), |
| 91 | + completion(const ProcessResultMatcher()), |
| 92 | + ); |
| 93 | + |
| 94 | + final Directory packageDirectory = tempDirectory.childDirectory( |
| 95 | + packageName, |
| 96 | + ); |
| 97 | + |
| 98 | + // Add native_assets_cli and resolve implicitly (pub add does pub get). |
| 99 | + // See https://dart.dev/tools/pub/cmd/pub-add#version-constraint. |
| 100 | + await expectLater( |
| 101 | + processManager.run( |
| 102 | + <String>[ |
| 103 | + flutterBin, |
| 104 | + 'packages', |
| 105 | + 'add', |
| 106 | + 'native_assets_cli:$nativeAssetsCliVersionConstraint', |
| 107 | + ], |
| 108 | + workingDirectory: packageDirectory.path, |
| 109 | + ), |
| 110 | + completion(const ProcessResultMatcher()), |
| 111 | + ); |
| 112 | + |
| 113 | + // Add a build hook that does nothing to the package. |
| 114 | + packageDirectory.childDirectory('hook').childFile('build.dart') |
| 115 | + ..createSync(recursive: true) |
| 116 | + ..writeAsStringSync(''' |
| 117 | +import 'package:native_assets_cli/native_assets_cli.dart'; |
| 118 | +
|
| 119 | +void main(List<String> args) async { |
| 120 | + await build(args, (config, output) async {}); |
| 121 | +} |
| 122 | +'''); |
| 123 | + |
| 124 | + // Try building. |
| 125 | + await expectLater( |
| 126 | + processManager.run( |
| 127 | + <String>[ |
| 128 | + flutterBin, |
| 129 | + 'build', |
| 130 | + buildCommand, |
| 131 | + '--debug', |
| 132 | + if (!codeSign) '--no-codesign', |
| 133 | + ], |
| 134 | + workingDirectory: packageDirectory.path, |
| 135 | + ), |
| 136 | + completion(const ProcessResultMatcher()), |
| 137 | + ); |
| 138 | + }); |
| 139 | + }, |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +/// Reads `templates/package_ffi/pubspec.yaml.tmpl` to use the package version. |
| 144 | +/// |
| 145 | +/// For example, if the template would output: |
| 146 | +/// ```yaml |
| 147 | +/// dependencies: |
| 148 | +/// native_assets_cli: ^0.8.0 |
| 149 | +/// ``` |
| 150 | +/// |
| 151 | +/// ... then this function would return `'^0.8.0'`. |
| 152 | +String _getPackageFfiTemplatePubspecVersion() { |
| 153 | + final String path = Context().join( |
| 154 | + getFlutterRoot(), |
| 155 | + 'packages', |
| 156 | + 'flutter_tools', |
| 157 | + 'templates', |
| 158 | + 'package_ffi', |
| 159 | + 'pubspec.yaml.tmpl', |
| 160 | + ); |
| 161 | + final YamlDocument yaml = loadYamlDocument( |
| 162 | + io.File(path).readAsStringSync(), |
| 163 | + sourceUrl: Uri.parse(path), |
| 164 | + ); |
| 165 | + final YamlMap rootNode = yaml.contents as YamlMap; |
| 166 | + final YamlMap dependencies = rootNode.nodes['dependencies']! as YamlMap; |
| 167 | + final String version = dependencies['native_assets_cli']! as String; |
| 168 | + return version; |
| 169 | +} |
0 commit comments