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

Fixes https://github.com/flutter/flutter/issues/135461
diff --git a/packages/flutter_tools/lib/src/ios/native_assets.dart b/packages/flutter_tools/lib/src/ios/native_assets.dart
index 3e2d595..21ddb2c 100644
--- a/packages/flutter_tools/lib/src/ios/native_assets.dart
+++ b/packages/flutter_tools/lib/src/ios/native_assets.dart
@@ -23,7 +23,7 @@
   required Uri projectUri,
   required FileSystem fileSystem,
 }) async {
-  if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) {
+  if (!await nativeBuildRequired(buildRunner)) {
     return null;
   }
 
@@ -72,7 +72,7 @@
   required Uri yamlParentDirectory,
   required FileSystem fileSystem,
 }) async {
-  if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) {
+  if (!await nativeBuildRequired(buildRunner)) {
     await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory, fileSystem);
     return <Uri>[];
   }
diff --git a/packages/flutter_tools/lib/src/linux/native_assets.dart b/packages/flutter_tools/lib/src/linux/native_assets.dart
index a9dcb33..6c52a33 100644
--- a/packages/flutter_tools/lib/src/linux/native_assets.dart
+++ b/packages/flutter_tools/lib/src/linux/native_assets.dart
@@ -23,7 +23,7 @@
   bool flutterTester = false,
   required FileSystem fileSystem,
 }) async {
-  if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) {
+  if (!await nativeBuildRequired(buildRunner)) {
     return null;
   }
 
@@ -90,7 +90,7 @@
     // CMake requires the folder to exist to do copying.
     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);
     return (nativeAssetsYaml, <Uri>[]);
   }
diff --git a/packages/flutter_tools/lib/src/macos/native_assets.dart b/packages/flutter_tools/lib/src/macos/native_assets.dart
index c62c28f..1bdf27d 100644
--- a/packages/flutter_tools/lib/src/macos/native_assets.dart
+++ b/packages/flutter_tools/lib/src/macos/native_assets.dart
@@ -23,7 +23,7 @@
   bool flutterTester = false,
   required FileSystem fileSystem,
 }) async {
-  if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) {
+  if (!await nativeBuildRequired(buildRunner)) {
     return null;
   }
 
@@ -77,7 +77,7 @@
 }) async {
   const OS targetOs = OS.macOS;
   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);
     return (nativeAssetsYaml, <Uri>[]);
   }
diff --git a/packages/flutter_tools/lib/src/native_assets.dart b/packages/flutter_tools/lib/src/native_assets.dart
index b269fd8..caeffa3 100644
--- a/packages/flutter_tools/lib/src/native_assets.dart
+++ b/packages/flutter_tools/lib/src/native_assets.dart
@@ -214,7 +214,7 @@
 ///
 /// Native asset builds cannot be run without a package config. If there is
 /// 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();
   if (!packageConfigExists) {
     globals.logger.printTrace('No package config found. Skipping native assets compilation.');
@@ -222,24 +222,23 @@
   return !packageConfigExists;
 }
 
-/// Checks that if native assets is disabled, none of the dependencies declare
-/// native assets.
-///
-/// 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) {
+Future<bool> nativeBuildRequired(NativeAssetsBuildRunner buildRunner) async {
+  if (await _hasNoPackageConfig(buildRunner)) {
     return false;
   }
   final List<Package> packagesWithNativeAssets = await buildRunner.packagesWithNativeAssets();
   if (packagesWithNativeAssets.isEmpty) {
-    return true;
+    return false;
   }
-  final String packageNames = packagesWithNativeAssets.map((Package p) => p.name).join(' ');
-  throwToolExit(
-    'Package(s) $packageNames require the native assets feature to be enabled. '
-    'Enable using `flutter config --enable-native-assets`.',
-  );
+
+  if (!featureFlags.isNativeAssetsEnabled) {
+    final String packageNames = packagesWithNativeAssets.map((Package p) => p.name).join(' ');
+    throwToolExit(
+      'Package(s) $packageNames require the native assets feature to be enabled. '
+      'Enable using `flutter config --enable-native-assets`.',
+    );
+  }
+  return true;
 }
 
 /// Ensures that either this project has no native assets, or that native assets
@@ -252,7 +251,7 @@
   FileSystem fileSystem,
   NativeAssetsBuildRunner buildRunner,
 ) async {
-  if (await hasNoPackageConfig(buildRunner)) {
+  if (await _hasNoPackageConfig(buildRunner)) {
     return;
   }
   final List<Package> packagesWithNativeAssets = await buildRunner.packagesWithNativeAssets();
@@ -345,12 +344,7 @@
           buildRunner: buildRunner,
         );
       } else {
-        await ensureNoNativeAssetsOrOsIsSupported(
-          projectUri,
-          const LocalPlatform().operatingSystem,
-          fileSystem,
-          buildRunner,
-        );
+        await nativeBuildRequired(buildRunner);
         nativeAssetsYaml = null;
       }
     case build_info.TargetPlatform.linux_arm64:
@@ -389,7 +383,7 @@
   required FileSystem fileSystem,
   required Iterable<build_info.TargetPlatform> targetPlatforms,
 }) async {
-  if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) {
+  if (await nativeBuildRequired(buildRunner)) {
     return null;
   }
 
diff --git a/packages/flutter_tools/test/general.shard/build_system/targets/native_assets_test.dart b/packages/flutter_tools/test/general.shard/build_system/targets/native_assets_test.dart
index 09fe961..36d4628 100644
--- a/packages/flutter_tools/test/general.shard/build_system/targets/native_assets_test.dart
+++ b/packages/flutter_tools/test/general.shard/build_system/targets/native_assets_test.dart
@@ -14,6 +14,7 @@
 import 'package:flutter_tools/src/features.dart';
 import 'package:flutter_tools/src/native_assets.dart';
 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/context.dart';
@@ -104,6 +105,7 @@
       await createPackageConfig(iosEnvironment);
 
       final NativeAssetsBuildRunner buildRunner = FakeNativeAssetsBuildRunner(
+        packagesWithNativeAssetsResult: <Package>[Package('foo', iosEnvironment.buildDir.uri)],
         buildResult: FakeNativeAssetsBuilderResult(assets: <native_assets_cli.Asset>[
           native_assets_cli.Asset(
             id: 'package:foo/foo.dart',
diff --git a/packages/flutter_tools/test/general.shard/linux/native_assets_test.dart b/packages/flutter_tools/test/general.shard/linux/native_assets_test.dart
index 34d2fc5..8979707 100644
--- a/packages/flutter_tools/test/general.shard/linux/native_assets_test.dart
+++ b/packages/flutter_tools/test/general.shard/linux/native_assets_test.dart
@@ -6,6 +6,7 @@
 import 'package:file/memory.dart';
 import 'package:file_testing/file_testing.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/logger.dart';
 import 'package:flutter_tools/src/base/platform.dart';
@@ -86,6 +87,24 @@
     );
   });
 
+  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>{
     ProcessManager: () => FakeProcessManager.empty(),
   }, () async {
@@ -372,3 +391,8 @@
     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.');
+}
diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart
index e24262f..2af0b71 100644
--- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart
+++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart
@@ -2420,7 +2420,7 @@
         expect(buildRunner.buildInvocations, 0);
         expect(buildRunner.dryRunInvocations, 1);
         expect(buildRunner.hasPackageConfigInvocations, 1);
-        expect(buildRunner.packagesWithNativeAssetsInvocations, 0);
+        expect(buildRunner.packagesWithNativeAssetsInvocations, 1);
       }),
       overrides: <Type, Generator>{
         ProcessManager: () => FakeProcessManager.any(),