flutter/packages/flutter_tools/test/commands.shard/permeable/build_aar_test.dart
auto-submit[bot] 4372bfbc6c
Reverts "Add workspace (#169451)" (#169468)
<!-- start_original_pr_link -->
Reverts: flutter/flutter#169451
<!-- end_original_pr_link -->
<!-- start_initiating_author -->
Initiated by: matanlurey
<!-- end_initiating_author -->
<!-- start_revert_reason -->
Reason for reverting: Broke a number of post-submit tests
(ios_app_extension, packages_autoroller).
<!-- end_revert_reason -->
<!-- start_original_pr_author -->
Original PR Author: mosuem
<!-- end_original_pr_author -->

<!-- start_reviewers -->
Reviewed By: {matanlurey}
<!-- end_reviewers -->

<!-- start_revert_body -->
This change reverts the following previous change:
Reland after #169357.

Switch Flutter to use pub workspaces as a preparation to unpin selected
packages.

Assumptions:

1. No packages in this repository are published to pub.dev --> We can
use `any` dependencies in most local pubspecs, as the global constraint
defines the version. An exception are the packages used outside of this
repo with an `sdk` dependency, namely `flutter_localizations`,
`flutter_test`, and `flutter`.
2. The "universes" `{flutter_tools}` and `{flutter,
flutter_localizations, flutter_goldens}` can use different packages
versions, as they are not resolved together. --> We do not need to
upgrade them in sync, we can first do one "universe", then the other.

Based on these assumptions, we use
https://github.com/mosuem/pubspec_merger.dart to merge all packages in
the `flutter` universe into a top-level pub workspace.

The `flutter` and `flutter_tools` workspaces being separate also ensures
that changes to `flutter` will not inadvertently break `flutter_tools`,
with not-so-nice consequences for our users which would be unable to run
`flutter upgrade`.

There is a third "top-level" pubspec besides `./pubspec.yaml` and
`packages/flutter_tools/pubspec.yaml`, namely
`packages/flutter_tools/.../widget_preview_scaffold/pubspec.yaml`. This
is an artifact due to it living under `flutter_tools`, so it can't be
part of the `./pubspec.yaml` workspace. Moving it would be a larger
change, and out of the scope of this PR.

This required a rewrite of the update-packages tool, but the main
functionality stays the same, as well as the argument names, to ensure a
seamless transition.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

<!-- end_revert_body -->

Co-authored-by: auto-submit[bot] <flutter-engprod-team@google.com>
2025-05-26 14:07:27 +00:00

579 lines
19 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:args/command_runner.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/android_builder.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/android/android_studio.dart';
import 'package:flutter_tools/src/android/java.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/build_aar.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/features.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/project.dart';
import 'package:test/fake.dart';
import 'package:unified_analytics/testing.dart';
import 'package:unified_analytics/unified_analytics.dart';
import '../../src/android_common.dart';
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/fake_process_manager.dart';
import '../../src/fakes.dart' hide FakeFlutterProjectFactory;
import '../../src/test_flutter_command_runner.dart';
void main() {
Cache.disableLocking();
/// Runs the equivalent of `flutter build aar`.
///
/// If [arguments] are provided, they are appended to the end, i.e.:
/// ```sh
/// flutter build aar [arguments]
/// ```
///
/// If [androidSdk] is provided, it is used, otherwise defaults to [FakeAndroidSdk].
Future<BuildAarCommand> runBuildAar(
String target, {
AndroidSdk? androidSdk = const _FakeAndroidSdk(),
List<String>? arguments,
}) async {
final BuildAarCommand command = BuildAarCommand(
androidSdk: androidSdk,
fileSystem: globals.fs,
logger: BufferLogger.test(),
verboseHelp: false,
);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['aar', ...?arguments, target]);
return command;
}
group('Usage', () {
late Directory tempDir;
late FakeAnalytics analytics;
setUp(() {
analytics = getInitializedFakeAnalyticsInstance(
fs: MemoryFileSystem.test(),
fakeFlutterVersion: FakeFlutterVersion(),
);
tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.');
});
tearDown(() {
tryToDelete(tempDir);
});
testUsingContext(
'indicate that project is a module',
() async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
await runBuildAar(projectPath, arguments: <String>['--no-pub']);
expect(
analytics.sentEvents,
contains(
Event.commandUsageValues(
workflow: 'aar',
buildAarProjectType: 'module',
buildAarTargetPlatform: 'android-arm,android-arm64,android-x64',
commandHasTerminal: false,
),
),
);
},
overrides: <Type, Generator>{
AndroidBuilder: () => _CapturingFakeAndroidBuilder(),
Analytics: () => analytics,
},
);
testUsingContext(
'indicate the target platform',
() async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
await runBuildAar(
projectPath,
arguments: <String>['--no-pub', '--target-platform=android-arm'],
);
expect(
analytics.sentEvents,
contains(
Event.commandUsageValues(
workflow: 'aar',
buildAarProjectType: 'module',
buildAarTargetPlatform: 'android-arm',
commandHasTerminal: false,
),
),
);
},
overrides: <Type, Generator>{
AndroidBuilder: () => _CapturingFakeAndroidBuilder(),
Analytics: () => analytics,
},
);
// Regression test for https://github.com/flutter/flutter/issues/162649.
testUsingContext(
'triggers builds even with --pub',
() async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
await runBuildAar(
projectPath,
arguments: <String>['--target-platform=android-arm'],
// If we use --no-pub, it bypasses validation that occurs only on a
// build with --pub, which as a consequence means that we aren't
// testing every code branch.
);
},
overrides: <Type, Generator>{
AndroidBuilder: () => _CapturingFakeAndroidBuilder(),
Analytics: () => analytics,
Pub: FakePub.new,
},
);
testUsingContext(
'logs success',
() async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
await runBuildAar(
projectPath,
arguments: <String>['--no-pub', '--target-platform=android-arm'],
);
final Iterable<Event> successEvent = analytics.sentEvents.where(
(Event e) =>
e.eventName == DashEvent.flutterCommandResult &&
e.eventData['commandPath'] == 'create' &&
e.eventData['result'] == 'success',
);
expect(successEvent, isNotEmpty, reason: 'Tool should send create success event');
},
overrides: <Type, Generator>{
AndroidBuilder: () => _CapturingFakeAndroidBuilder(),
Analytics: () => analytics,
},
);
});
group('flag parsing', () {
late Directory tempDir;
late _CapturingFakeAndroidBuilder fakeAndroidBuilder;
setUp(() {
fakeAndroidBuilder = _CapturingFakeAndroidBuilder();
tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_build_aar_test.');
});
tearDown(() {
tryToDelete(tempDir);
});
testUsingContext('defaults', () async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
await runBuildAar(projectPath, arguments: <String>['--no-pub']);
expect(
fakeAndroidBuilder.capturedBuildAarCalls,
hasLength(1),
reason: 'A single call to buildAar was expected.',
);
final Invocation buildAarCall = fakeAndroidBuilder.capturedBuildAarCalls.single;
expect(buildAarCall.namedArguments[#buildNumber], '1.0');
final List<BuildMode> buildModes = <BuildMode>[];
for (final AndroidBuildInfo androidBuildInfo
in buildAarCall.namedArguments[#androidBuildInfo] as Set<AndroidBuildInfo>) {
final BuildInfo buildInfo = androidBuildInfo.buildInfo;
buildModes.add(buildInfo.mode);
if (buildInfo.mode.isPrecompiled) {
expect(buildInfo.treeShakeIcons, isTrue);
expect(buildInfo.trackWidgetCreation, isTrue);
} else {
expect(buildInfo.treeShakeIcons, isFalse);
expect(buildInfo.trackWidgetCreation, isTrue);
}
expect(buildInfo.flavor, isNull);
expect(buildInfo.splitDebugInfoPath, isNull);
expect(buildInfo.dartObfuscation, isFalse);
expect(androidBuildInfo.targetArchs, <AndroidArch>[
AndroidArch.armeabi_v7a,
AndroidArch.arm64_v8a,
AndroidArch.x86_64,
]);
}
expect(buildModes, hasLength(3));
expect(
buildModes,
containsAll(<BuildMode>[BuildMode.debug, BuildMode.profile, BuildMode.release]),
);
}, overrides: <Type, Generator>{AndroidBuilder: () => fakeAndroidBuilder});
testUsingContext('parses flags', () async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
await runBuildAar(
projectPath,
arguments: <String>[
'--no-pub',
'--no-debug',
'--no-profile',
'--target-platform',
'android-x86',
'--tree-shake-icons',
'--flavor',
'free',
'--build-number',
'200',
'--split-debug-info',
'/project-name/v1.2.3/',
'--obfuscate',
'--dart-define=foo=bar',
],
);
expect(
fakeAndroidBuilder.capturedBuildAarCalls,
hasLength(1),
reason: 'A single call to buildAar was expected.',
);
final Invocation buildAarCall = fakeAndroidBuilder.capturedBuildAarCalls.single;
expect(buildAarCall.namedArguments[#buildNumber], '200');
final AndroidBuildInfo androidBuildInfo =
(buildAarCall.namedArguments[#androidBuildInfo] as Set<AndroidBuildInfo>).single;
expect(androidBuildInfo.targetArchs, <AndroidArch>[AndroidArch.x86]);
final BuildInfo buildInfo = androidBuildInfo.buildInfo;
expect(buildInfo.mode, BuildMode.release);
expect(buildInfo.treeShakeIcons, isTrue);
expect(buildInfo.flavor, 'free');
expect(buildInfo.splitDebugInfoPath, '/project-name/v1.2.3/');
expect(buildInfo.dartObfuscation, isTrue);
expect(buildInfo.dartDefines.contains('foo=bar'), isTrue);
}, overrides: <Type, Generator>{AndroidBuilder: () => fakeAndroidBuilder});
});
group('Gradle', () {
late Directory tempDir;
late String gradlew;
late FakeProcessManager processManager;
late String flutterRoot;
late FakeAnalytics fakeAnalytics;
setUp(() {
tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.');
fakeAnalytics = getInitializedFakeAnalyticsInstance(
fs: MemoryFileSystem.test(),
fakeFlutterVersion: FakeFlutterVersion(),
);
gradlew = globals.fs.path.join(
tempDir.path,
'flutter_project',
'.android',
globals.platform.isWindows ? 'gradlew.bat' : 'gradlew',
);
processManager = FakeProcessManager.empty();
flutterRoot = getFlutterRoot();
});
tearDown(() {
tryToDelete(tempDir);
});
group('AndroidSdk', () {
testUsingContext(
'throws throwsToolExit if AndroidSdk is null',
() async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
await expectLater(
() async {
await runBuildAar(projectPath, androidSdk: null, arguments: <String>['--no-pub']);
},
throwsToolExit(
message: 'No Android SDK found. Try setting the ANDROID_HOME environment variable',
),
);
},
overrides: <Type, Generator>{
FlutterProjectFactory: () => FakeFlutterProjectFactory(tempDir),
ProcessManager: () => FakeProcessManager.any(),
},
);
});
group('throws ToolExit', () {
testUsingContext('main.dart not found', () async {
await expectLater(() async {
await runBuildAar(
'missing_project',
arguments: <String>[
'--no-pub',
globals.fs.path.join('missing_project', 'lib', 'main.dart'),
],
);
}, throwsToolExit(message: 'main.dart does not exist'));
});
testUsingContext('flutter project not valid', () async {
await expectLater(() async {
await runBuildAar(tempDir.path, arguments: <String>['--no-pub']);
}, throwsToolExit(message: 'is not a valid flutter project'));
});
});
testUsingContext(
'support ExtraDartFlagOptions',
() async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
processManager.addCommand(
FakeCommand(
command: <String>[
gradlew,
'-I=${globals.fs.path.join(flutterRoot, 'packages', 'flutter_tools', 'gradle', 'aar_init_script.gradle')}',
'-Pflutter-root=$flutterRoot',
'-Poutput-dir=${globals.fs.path.join(tempDir.path, 'flutter_project', 'build', 'host')}',
'-Pis-plugin=false',
'-PbuildNumber=1.0',
'-q',
'-Ptarget=${globals.fs.path.join('lib', 'main.dart')}',
'-Pdart-defines=RkxVVFRFUl9WRVJTSU9OPTAuMC4w,RkxVVFRFUl9DSEFOTkVMPW1hc3Rlcg==,RkxVVFRFUl9HSVRfVVJMPWh0dHBzOi8vZ2l0aHViLmNvbS9mbHV0dGVyL2ZsdXR0ZXIuZ2l0,RkxVVFRFUl9GUkFNRVdPUktfUkVWSVNJT049MTExMTE=,RkxVVFRFUl9FTkdJTkVfUkVWSVNJT049YWJjZGU=,RkxVVFRFUl9EQVJUX1ZFUlNJT049MTI=',
'-Pdart-obfuscation=false',
'-Pextra-front-end-options=foo,bar',
'-Ptrack-widget-creation=true',
'-Ptree-shake-icons=true',
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'assembleAarRelease',
],
exitCode: 1,
),
);
await expectLater(
() => runBuildAar(
projectPath,
arguments: <String>[
'--no-pub',
'--no-debug',
'--no-profile',
'--extra-front-end-options=foo',
'--extra-front-end-options=bar',
],
),
throwsToolExit(message: 'Gradle task assembleAarRelease failed with exit code 1'),
);
expect(processManager, hasNoRemainingExpectations);
},
overrides: <Type, Generator>{
FlutterProjectFactory: () => FakeFlutterProjectFactory(tempDir),
Java: () => null,
ProcessManager: () => processManager,
FeatureFlags: () => TestFeatureFlags(isIOSEnabled: false),
AndroidStudio: () => _FakeAndroidStudio(),
},
);
group('Impeller AndroidManifest.xml setting', () {
// Adds a key-value `<meta-data>` pair to the `<application>` tag in the
// corresponding `AndroidManifest.xml` file, right before the closing
// `</application>` tag.
void writeManifestMetadata({
required String projectPath,
required String name,
required String value,
}) {
final String manifestPath = globals.fs.path.join(
projectPath,
'.android',
'app',
'src',
'main',
'AndroidManifest.xml',
);
// It would be unnecessarily complicated to parse this XML file and
// insert the key-value pair, so we just insert it right before the
// closing </application> tag.
final String oldManifest = globals.fs.file(manifestPath).readAsStringSync();
final String newManifest = oldManifest.replaceFirst(
'</application>',
' <meta-data\n'
' android:name="$name"\n'
' android:value="$value" />\n'
' </application>',
);
globals.fs.file(manifestPath).writeAsStringSync(newManifest);
}
testUsingContext(
'a default AAR build reports Impeller as enabled',
() async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
await runBuildAar(projectPath, arguments: <String>['--no-pub']);
expect(
fakeAnalytics.sentEvents,
contains(
Event.flutterBuildInfo(label: 'manifest-aar-impeller-enabled', buildType: 'android'),
),
);
},
overrides: <Type, Generator>{
Analytics: () => fakeAnalytics,
AndroidBuilder: () => _CapturingFakeAndroidBuilder(),
FlutterProjectFactory: () => FakeFlutterProjectFactory(tempDir),
},
);
testUsingContext(
'EnableImpeller="true" reports an enabled event',
() async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
final FlutterProject project = FlutterProject.fromDirectory(
globals.fs.directory(projectPath),
);
await project.android.ensureReadyForPlatformSpecificTooling();
writeManifestMetadata(
projectPath: projectPath,
name: 'io.flutter.embedding.android.EnableImpeller',
value: 'true',
);
await runBuildAar(projectPath, arguments: <String>['--no-pub']);
expect(
fakeAnalytics.sentEvents,
contains(
Event.flutterBuildInfo(label: 'manifest-aar-impeller-enabled', buildType: 'android'),
),
);
},
overrides: <Type, Generator>{
Analytics: () => fakeAnalytics,
AndroidBuilder: () => _CapturingFakeAndroidBuilder(),
FlutterProjectFactory: () => FakeFlutterProjectFactory(tempDir),
},
);
testUsingContext(
'EnableImpeller="false" reports an disabled event',
() async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=module'],
);
final FlutterProject project = FlutterProject.fromDirectory(
globals.fs.directory(projectPath),
);
await project.android.ensureReadyForPlatformSpecificTooling();
writeManifestMetadata(
projectPath: projectPath,
name: 'io.flutter.embedding.android.EnableImpeller',
value: 'false',
);
await runBuildAar(projectPath, arguments: <String>['--no-pub']);
expect(
fakeAnalytics.sentEvents,
contains(
Event.flutterBuildInfo(label: 'manifest-aar-impeller-disabled', buildType: 'android'),
),
);
},
overrides: <Type, Generator>{
Analytics: () => fakeAnalytics,
AndroidBuilder: () => _CapturingFakeAndroidBuilder(),
FlutterProjectFactory: () => FakeFlutterProjectFactory(tempDir),
},
);
});
});
}
/// A fake implementation of [AndroidBuilder] that allows [buildAar] calls.
///
/// Calls to [buildAar] are stored as [capturedBuildAarCalls], other calls are rejected.
final class _CapturingFakeAndroidBuilder extends Fake implements AndroidBuilder {
final List<Invocation> capturedBuildAarCalls = <Invocation>[];
@override
Object? noSuchMethod(Invocation invocation) {
if (invocation.memberName != #buildAar) {
return super.noSuchMethod(invocation);
}
capturedBuildAarCalls.add(invocation);
return Future<void>.value();
}
}
final class _FakeAndroidSdk with Fake implements AndroidSdk {
const _FakeAndroidSdk();
}
final class _FakeAndroidStudio extends Fake implements AndroidStudio {
@override
String get javaPath => 'java';
}
class FakePub extends Fake implements Pub {
@override
Future<void> get({
PubContext? context,
required FlutterProject project,
bool upgrade = false,
bool offline = false,
bool generateSyntheticPackage = false,
String? flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
PubOutputMode outputMode = PubOutputMode.all,
}) async {}
}