@@ -8,12 +8,15 @@ import 'package:flutter_tools/src/base/logger.dart';
88import 'package:flutter_tools/src/base/platform.dart' ;
99import 'package:flutter_tools/src/build_system/build_system.dart' ;
1010import 'package:flutter_tools/src/cache.dart' ;
11- import 'package:flutter_tools/src/commands/build.dart' ;
11+ import 'package:flutter_tools/src/commands/build_aar.dart' ;
12+ import 'package:flutter_tools/src/features.dart' ;
13+ import 'package:flutter_tools/src/project.dart' ;
1214import 'package:unified_analytics/unified_analytics.dart' ;
1315
16+ import '../../src/android_common.dart' ;
1417import '../../src/common.dart' ;
1518import '../../src/context.dart' ;
16- import '../../src/fake_build_command.dart' ;
19+
1720import '../../src/fake_process_manager.dart' ;
1821import '../../src/fakes.dart' ;
1922import '../../src/test_build_system.dart' ;
@@ -26,6 +29,8 @@ void main() {
2629 late Platform platform;
2730 late Cache cache;
2831 late FakeAnalytics fakeAnalytics;
32+ late FakeToolContext toolContext;
33+ late FakeAndroidContext androidContext;
2934
3035 setUpAll (() {
3136 Cache .disableLocking ();
@@ -43,12 +48,19 @@ void main() {
4348 fs: fs,
4449 fakeFlutterVersion: FakeFlutterVersion (),
4550 );
51+ toolContext = FakeToolContext (
52+ cache: cache,
53+ fs: fs,
54+ logger: logger,
55+ platform: platform,
56+ processManager: processManager,
57+ projectFactory: FlutterProjectFactory (fileSystem: fs, logger: logger),
58+ );
59+ androidContext = FakeAndroidContext (androidSdk: FakeAndroidSdk ());
4660 });
4761
48- testUsingContext (
49- 'will not build an AAR for a plugin' ,
50- () async {
51- fs.file ('pubspec.yaml' ).writeAsStringSync ('''
62+ testWithoutContext ('will not build an AAR for a plugin' , () async {
63+ fs.file ('pubspec.yaml' ).writeAsStringSync ('''
5264name: foo_bar
5365
5466flutter:
@@ -58,114 +70,71 @@ flutter:
5870 null
5971''' );
6072
61- final BuildCommand command = createFakeBuildCommand (
62- androidSdk: FakeAndroidSdk (),
63- buildSystem: TestBuildSystem .all (BuildResult (success: true )),
64- fileSystem: fs,
65- logger: logger,
66- osUtils: FakeOperatingSystemUtils (),
67- config: FakeConfig (),
68- platform: FakePlatform (),
69- fileSystemUtils: FakeFileSystemUtils (),
70- terminal: FakeTerminal (),
71- plistParser: FakePlistParser (),
72- processUtils: FakeProcessUtils (),
73- processManager: FakeProcessManager .any (),
74- templateRenderer: FakeTemplateRenderer (),
75- xcode: FakeXcode (),
76- artifacts: FakeArtifacts (),
77- cache: FakeCache (),
78- flutterVersion: FakeFlutterVersion (),
79- featureFlags: TestFeatureFlags (),
80- );
81-
82- expect (
83- createTestCommandRunner (command).run (const < String > ['build' , 'aar' , '--no-pub' ]),
84- throwsToolExit (message: 'AARs can only be built from modules' ),
85- );
86- expect (processManager, hasNoRemainingExpectations);
87- },
88- overrides: < Type , Generator > {
89- Cache : () => cache,
90- FileSystem : () => fs,
91- Platform : () => platform,
92- ProcessManager : () => processManager,
93- },
94- );
95-
96- testUsingContext (
97- 'will build an AAR for a module' ,
98- () async {
99- fs.file ('pubspec.yaml' ).writeAsStringSync ('''
73+ final command = BuildAarCommand (
74+ androidBuilder: FakeAndroidBuilder (),
75+ androidContext: androidContext,
76+ buildSystem: TestBuildSystem .all (BuildResult (success: true )),
77+ toolContext: toolContext,
78+ );
79+
80+ expect (
81+ createTestCommandRunner (command).run (const < String > ['aar' , '--no-pub' ]),
82+ throwsToolExit (message: 'AARs can only be built from modules' ),
83+ );
84+ expect (processManager, hasNoRemainingExpectations);
85+ });
86+
87+ testUsingContext ('will build an AAR for a module' , () async {
88+ fs.file ('pubspec.yaml' ).writeAsStringSync ('''
10089name: foo_bar
10190
10291flutter:
10392 module:
10493 foo: bar
10594''' );
106- final Directory dotAndroidDir = fs.directory ('.android' )..createSync (recursive: true );
107- dotAndroidDir.childFile ('gradlew' ).createSync ();
108-
109- processManager.addCommands (< FakeCommand > [
110- const FakeCommand (command: < String > ['chmod' , '755' , 'flutter/bin/cache/artifacts' ]),
111- const FakeCommand (command: < String > ['which' , 'java' ]),
112- ...< String > ['Debug' , 'Profile' , 'Release' ].map (
113- (String buildMode) => FakeCommand (
114- command: < Pattern > [
115- '/.android/gradlew' ,
116- '-I=/flutter/packages/flutter_tools/gradle/aar_init_script.gradle' ,
117- ...List <RegExp >.filled (4 , RegExp (r'-P[a-zA-Z-]+=.*' )),
118- '-q' ,
119- ...List <RegExp >.filled (7 , RegExp (r'-P[a-zA-Z-]+=.*' )),
120- 'assembleAar$buildMode ' ,
121- ],
122- onRun: (_) => fs.directory ('/build/host/outputs/repo' ).createSync (recursive: true ),
123- ),
124- ),
125- ]);
126-
127- cache.getArtifactDirectory ('gradle_wrapper' ).createSync (recursive: true );
128-
129- final BuildCommand command = createFakeBuildCommand (
130- androidSdk: FakeAndroidSdk (),
131- buildSystem: TestBuildSystem .all (BuildResult (success: true )),
132- fileSystem: fs,
133- logger: logger,
134- osUtils: FakeOperatingSystemUtils (),
135- config: FakeConfig (),
136- platform: FakePlatform (),
137- fileSystemUtils: FakeFileSystemUtils (),
138- terminal: FakeTerminal (),
139- plistParser: FakePlistParser (),
140- processUtils: FakeProcessUtils (),
141- processManager: FakeProcessManager .any (),
142- templateRenderer: FakeTemplateRenderer (),
143- xcode: FakeXcode (),
144- artifacts: FakeArtifacts (),
145- cache: FakeCache (),
146- flutterVersion: FakeFlutterVersion (),
147- featureFlags: TestFeatureFlags (),
148- );
149-
150- await createTestCommandRunner (command).run (const < String > ['build' , 'aar' , '--no-pub' ]);
151- expect (processManager, hasNoRemainingExpectations);
152- expect (
153- fakeAnalytics.sentEvents,
154- contains (
155- Event .commandUsageValues (
156- workflow: 'build/aar' ,
157- commandHasTerminal: false ,
158- buildAarProjectType: 'module' ,
159- buildAarTargetPlatform: 'android-arm,android-arm64,android-x64' ,
160- ),
95+ final Directory dotAndroidDir = fs.directory ('.android' )..createSync (recursive: true );
96+ dotAndroidDir.childFile ('gradlew' ).createSync ();
97+
98+ final command = BuildAarCommand (
99+ androidBuilder: FakeAndroidBuilder (),
100+ androidContext: androidContext,
101+ buildSystem: TestBuildSystem .all (BuildResult (success: true )),
102+ toolContext: toolContext,
103+ );
104+
105+ await createTestCommandRunner (command, fakeAnalytics).run (const < String > ['aar' , '--no-pub' ]);
106+ expect (
107+ fakeAnalytics.sentEvents,
108+ contains (
109+ Event .commandUsageValues (
110+ workflow: 'aar' ,
111+ commandHasTerminal: false ,
112+ buildAarProjectType: 'module' ,
113+ buildAarTargetPlatform: 'android-arm,android-arm64,android-x64' ,
161114 ),
162- );
163- },
164- overrides: < Type , Generator > {
165- FileSystem : () => fs,
166- Platform : () => platform,
167- ProcessManager : () => processManager,
168- Analytics : () => fakeAnalytics,
169- },
170- );
115+ ),
116+ );
117+ }, overrides: < Type , Generator > {FeatureFlags : () => TestFeatureFlags ()});
118+
119+ testWithoutContext ('throws ToolExit if androidSdk is null' , () async {
120+ fs.file ('pubspec.yaml' ).writeAsStringSync ('''
121+ name: foo_bar
122+
123+ flutter:
124+ module:
125+ foo: bar
126+ ''' );
127+
128+ final command = BuildAarCommand (
129+ androidBuilder: FakeAndroidBuilder (),
130+ androidContext: FakeAndroidContext (),
131+ buildSystem: TestBuildSystem .all (BuildResult (success: true )),
132+ toolContext: toolContext,
133+ );
134+
135+ expect (
136+ createTestCommandRunner (command).run (const < String > ['aar' , '--no-pub' ]),
137+ throwsToolExit (message: 'No Android SDK found' ),
138+ );
139+ });
171140}
0 commit comments