Unverified Commit 851497ff authored by Christopher Fujino's avatar Christopher Fujino Committed by GitHub

[flutter_tools] fix tests with no native assets running native asset build (#135474)

Fixes https://github.com/flutter/flutter/issues/135461
parent 67d4a831
...@@ -23,7 +23,7 @@ Future<Uri?> dryRunNativeAssetsIOS({ ...@@ -23,7 +23,7 @@ Future<Uri?> dryRunNativeAssetsIOS({
required Uri projectUri, required Uri projectUri,
required FileSystem fileSystem, required FileSystem fileSystem,
}) async { }) async {
if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) { if (!await nativeBuildRequired(buildRunner)) {
return null; return null;
} }
...@@ -72,7 +72,7 @@ Future<List<Uri>> buildNativeAssetsIOS({ ...@@ -72,7 +72,7 @@ Future<List<Uri>> buildNativeAssetsIOS({
required Uri yamlParentDirectory, required Uri yamlParentDirectory,
required FileSystem fileSystem, required FileSystem fileSystem,
}) async { }) async {
if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) { if (!await nativeBuildRequired(buildRunner)) {
await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory, fileSystem); await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory, fileSystem);
return <Uri>[]; return <Uri>[];
} }
......
...@@ -23,7 +23,7 @@ Future<Uri?> dryRunNativeAssetsLinux({ ...@@ -23,7 +23,7 @@ Future<Uri?> dryRunNativeAssetsLinux({
bool flutterTester = false, bool flutterTester = false,
required FileSystem fileSystem, required FileSystem fileSystem,
}) async { }) async {
if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) { if (!await nativeBuildRequired(buildRunner)) {
return null; return null;
} }
...@@ -90,7 +90,7 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsLinux({ ...@@ -90,7 +90,7 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsLinux({
// CMake requires the folder to exist to do copying. // CMake requires the folder to exist to do copying.
await buildDir.create(recursive: true); await buildDir.create(recursive: true);
} }
if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) { if (!await nativeBuildRequired(buildRunner)) {
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory ?? buildUri_, fileSystem); final Uri nativeAssetsYaml = await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory ?? buildUri_, fileSystem);
return (nativeAssetsYaml, <Uri>[]); return (nativeAssetsYaml, <Uri>[]);
} }
......
...@@ -23,7 +23,7 @@ Future<Uri?> dryRunNativeAssetsMacOS({ ...@@ -23,7 +23,7 @@ Future<Uri?> dryRunNativeAssetsMacOS({
bool flutterTester = false, bool flutterTester = false,
required FileSystem fileSystem, required FileSystem fileSystem,
}) async { }) async {
if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) { if (!await nativeBuildRequired(buildRunner)) {
return null; return null;
} }
...@@ -77,7 +77,7 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsMacOS({ ...@@ -77,7 +77,7 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsMacOS({
}) async { }) async {
const OS targetOs = OS.macOS; const OS targetOs = OS.macOS;
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOs); final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOs);
if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) { if (!await nativeBuildRequired(buildRunner)) {
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory ?? buildUri_, fileSystem); final Uri nativeAssetsYaml = await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory ?? buildUri_, fileSystem);
return (nativeAssetsYaml, <Uri>[]); return (nativeAssetsYaml, <Uri>[]);
} }
......
...@@ -214,7 +214,7 @@ BuildMode nativeAssetsBuildMode(build_info.BuildMode buildMode) { ...@@ -214,7 +214,7 @@ BuildMode nativeAssetsBuildMode(build_info.BuildMode buildMode) {
/// ///
/// Native asset builds cannot be run without a package config. If there is /// Native asset builds cannot be run without a package config. If there is
/// no package config, leave a logging trace about that. /// no package config, leave a logging trace about that.
Future<bool> hasNoPackageConfig(NativeAssetsBuildRunner buildRunner) async { Future<bool> _hasNoPackageConfig(NativeAssetsBuildRunner buildRunner) async {
final bool packageConfigExists = await buildRunner.hasPackageConfig(); final bool packageConfigExists = await buildRunner.hasPackageConfig();
if (!packageConfigExists) { if (!packageConfigExists) {
globals.logger.printTrace('No package config found. Skipping native assets compilation.'); globals.logger.printTrace('No package config found. Skipping native assets compilation.');
...@@ -222,24 +222,23 @@ Future<bool> hasNoPackageConfig(NativeAssetsBuildRunner buildRunner) async { ...@@ -222,24 +222,23 @@ Future<bool> hasNoPackageConfig(NativeAssetsBuildRunner buildRunner) async {
return !packageConfigExists; return !packageConfigExists;
} }
/// Checks that if native assets is disabled, none of the dependencies declare Future<bool> nativeBuildRequired(NativeAssetsBuildRunner buildRunner) async {
/// native assets. if (await _hasNoPackageConfig(buildRunner)) {
///
/// If any of the dependencies have native assets, but native assets are
/// disabled, exits the tool.
Future<bool> isDisabledAndNoNativeAssets(NativeAssetsBuildRunner buildRunner) async {
if (featureFlags.isNativeAssetsEnabled) {
return false; return false;
} }
final List<Package> packagesWithNativeAssets = await buildRunner.packagesWithNativeAssets(); final List<Package> packagesWithNativeAssets = await buildRunner.packagesWithNativeAssets();
if (packagesWithNativeAssets.isEmpty) { if (packagesWithNativeAssets.isEmpty) {
return true; return false;
} }
if (!featureFlags.isNativeAssetsEnabled) {
final String packageNames = packagesWithNativeAssets.map((Package p) => p.name).join(' '); final String packageNames = packagesWithNativeAssets.map((Package p) => p.name).join(' ');
throwToolExit( throwToolExit(
'Package(s) $packageNames require the native assets feature to be enabled. ' 'Package(s) $packageNames require the native assets feature to be enabled. '
'Enable using `flutter config --enable-native-assets`.', 'Enable using `flutter config --enable-native-assets`.',
); );
}
return true;
} }
/// Ensures that either this project has no native assets, or that native assets /// Ensures that either this project has no native assets, or that native assets
...@@ -252,7 +251,7 @@ Future<void> ensureNoNativeAssetsOrOsIsSupported( ...@@ -252,7 +251,7 @@ Future<void> ensureNoNativeAssetsOrOsIsSupported(
FileSystem fileSystem, FileSystem fileSystem,
NativeAssetsBuildRunner buildRunner, NativeAssetsBuildRunner buildRunner,
) async { ) async {
if (await hasNoPackageConfig(buildRunner)) { if (await _hasNoPackageConfig(buildRunner)) {
return; return;
} }
final List<Package> packagesWithNativeAssets = await buildRunner.packagesWithNativeAssets(); final List<Package> packagesWithNativeAssets = await buildRunner.packagesWithNativeAssets();
...@@ -345,12 +344,7 @@ Future<Uri?> dryRunNativeAssets({ ...@@ -345,12 +344,7 @@ Future<Uri?> dryRunNativeAssets({
buildRunner: buildRunner, buildRunner: buildRunner,
); );
} else { } else {
await ensureNoNativeAssetsOrOsIsSupported( await nativeBuildRequired(buildRunner);
projectUri,
const LocalPlatform().operatingSystem,
fileSystem,
buildRunner,
);
nativeAssetsYaml = null; nativeAssetsYaml = null;
} }
case build_info.TargetPlatform.linux_arm64: case build_info.TargetPlatform.linux_arm64:
...@@ -389,7 +383,7 @@ Future<Uri?> dryRunNativeAssetsMultipeOSes({ ...@@ -389,7 +383,7 @@ Future<Uri?> dryRunNativeAssetsMultipeOSes({
required FileSystem fileSystem, required FileSystem fileSystem,
required Iterable<build_info.TargetPlatform> targetPlatforms, required Iterable<build_info.TargetPlatform> targetPlatforms,
}) async { }) async {
if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) { if (await nativeBuildRequired(buildRunner)) {
return null; return null;
} }
......
...@@ -14,6 +14,7 @@ import 'package:flutter_tools/src/build_system/targets/native_assets.dart'; ...@@ -14,6 +14,7 @@ import 'package:flutter_tools/src/build_system/targets/native_assets.dart';
import 'package:flutter_tools/src/features.dart'; import 'package:flutter_tools/src/features.dart';
import 'package:flutter_tools/src/native_assets.dart'; import 'package:flutter_tools/src/native_assets.dart';
import 'package:native_assets_cli/native_assets_cli.dart' as native_assets_cli; import 'package:native_assets_cli/native_assets_cli.dart' as native_assets_cli;
import 'package:package_config/package_config.dart' show Package;
import '../../../src/common.dart'; import '../../../src/common.dart';
import '../../../src/context.dart'; import '../../../src/context.dart';
...@@ -104,6 +105,7 @@ void main() { ...@@ -104,6 +105,7 @@ void main() {
await createPackageConfig(iosEnvironment); await createPackageConfig(iosEnvironment);
final NativeAssetsBuildRunner buildRunner = FakeNativeAssetsBuildRunner( final NativeAssetsBuildRunner buildRunner = FakeNativeAssetsBuildRunner(
packagesWithNativeAssetsResult: <Package>[Package('foo', iosEnvironment.buildDir.uri)],
buildResult: FakeNativeAssetsBuilderResult(assets: <native_assets_cli.Asset>[ buildResult: FakeNativeAssetsBuilderResult(assets: <native_assets_cli.Asset>[
native_assets_cli.Asset( native_assets_cli.Asset(
id: 'package:foo/foo.dart', id: 'package:foo/foo.dart',
......
...@@ -6,6 +6,7 @@ import 'package:file/file.dart'; ...@@ -6,6 +6,7 @@ import 'package:file/file.dart';
import 'package:file/memory.dart'; import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart'; import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/artifacts.dart'; import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/base/platform.dart';
...@@ -86,6 +87,24 @@ void main() { ...@@ -86,6 +87,24 @@ void main() {
); );
}); });
testUsingContext('does not throw if clang not present but no native assets present', overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(isNativeAssetsEnabled: true),
ProcessManager: () => FakeProcessManager.empty(),
}, () async {
final File packageConfig = environment.projectDir.childFile('.dart_tool/package_config.json');
await packageConfig.create(recursive: true);
await buildNativeAssetsLinux(
projectUri: projectUri,
buildMode: BuildMode.debug,
fileSystem: fileSystem,
buildRunner: _BuildRunnerWithoutClang(),
);
expect(
(globals.logger as BufferLogger).traceText,
isNot(contains('Building native assets for ')),
);
});
testUsingContext('dry run for multiple OSes with no package config', overrides: <Type, Generator>{ testUsingContext('dry run for multiple OSes with no package config', overrides: <Type, Generator>{
ProcessManager: () => FakeProcessManager.empty(), ProcessManager: () => FakeProcessManager.empty(),
}, () async { }, () async {
...@@ -372,3 +391,8 @@ void main() { ...@@ -372,3 +391,8 @@ void main() {
expect(result.cc, Uri.file('/some/path/to/clang')); expect(result.cc, Uri.file('/some/path/to/clang'));
}); });
} }
class _BuildRunnerWithoutClang extends FakeNativeAssetsBuildRunner {
@override
Future<CCompilerConfig> get cCompilerConfig async => throwToolExit('Failed to find clang++ on the PATH.');
}
...@@ -2420,7 +2420,7 @@ flutter: ...@@ -2420,7 +2420,7 @@ flutter:
expect(buildRunner.buildInvocations, 0); expect(buildRunner.buildInvocations, 0);
expect(buildRunner.dryRunInvocations, 1); expect(buildRunner.dryRunInvocations, 1);
expect(buildRunner.hasPackageConfigInvocations, 1); expect(buildRunner.hasPackageConfigInvocations, 1);
expect(buildRunner.packagesWithNativeAssetsInvocations, 0); expect(buildRunner.packagesWithNativeAssetsInvocations, 1);
}), }),
overrides: <Type, Generator>{ overrides: <Type, Generator>{
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment