Skip to content

Commit 574d82b

Browse files
Jonah Williamsmingwandroid
authored andcommitted
[flutter_tools] remove globals from base/android (flutter#60480)
Remove unnecessary use of globals from base tests, and an android test. This changes the test to avoid bouncing through the global getters, which can lead to incorrectly cached zone values. Switches the memory filesystem implementation to the test implementation
1 parent e47f381 commit 574d82b

File tree

4 files changed

+134
-120
lines changed

4 files changed

+134
-120
lines changed

packages/flutter_tools/test/general.shard/android/android_sdk_test.dart

Lines changed: 73 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
import 'package:file/memory.dart';
66
import 'package:flutter_tools/src/android/android_sdk.dart';
7+
import 'package:flutter_tools/src/base/config.dart';
78
import 'package:flutter_tools/src/base/file_system.dart';
89
import 'package:flutter_tools/src/base/io.dart' show ProcessResult;
10+
import 'package:flutter_tools/src/base/logger.dart';
911
import 'package:flutter_tools/src/base/platform.dart';
10-
import 'package:flutter_tools/src/globals.dart' as globals;
12+
import 'package:meta/meta.dart';
1113
import 'package:mockito/mockito.dart';
1214
import 'package:process/process.dart';
1315

@@ -18,12 +20,18 @@ import '../../src/mocks.dart';
1820
class MockProcessManager extends Mock implements ProcessManager {}
1921

2022
void main() {
21-
MemoryFileSystem fs;
23+
MemoryFileSystem fileSystem;
2224
MockProcessManager processManager;
25+
Config config;
2326

2427
setUp(() {
25-
fs = MemoryFileSystem();
28+
fileSystem = MemoryFileSystem.test();
2629
processManager = MockProcessManager();
30+
config = Config.test(
31+
'test',
32+
directory: fileSystem.currentDirectory,
33+
logger: BufferLogger.test(),
34+
);
2735
});
2836

2937
group('android_sdk AndroidSdk', () {
@@ -38,144 +46,154 @@ void main() {
3846

3947
testUsingContext('parse sdk', () {
4048
sdkDir = MockAndroidSdk.createSdkDirectory();
41-
globals.config.setValue('android-sdk', sdkDir.path);
49+
config.setValue('android-sdk', sdkDir.path);
4250

4351
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
4452
expect(sdk.latestVersion, isNotNull);
4553
expect(sdk.latestVersion.sdkLevel, 23);
4654
}, overrides: <Type, Generator>{
47-
FileSystem: () => fs,
55+
FileSystem: () => fileSystem,
4856
ProcessManager: () => FakeProcessManager.any(),
57+
Config: () => config,
4958
});
5059

5160
testUsingContext('parse sdk N', () {
5261
sdkDir = MockAndroidSdk.createSdkDirectory(withAndroidN: true);
53-
globals.config.setValue('android-sdk', sdkDir.path);
62+
config.setValue('android-sdk', sdkDir.path);
5463

5564
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
5665
expect(sdk.latestVersion, isNotNull);
5766
expect(sdk.latestVersion.sdkLevel, 24);
5867
}, overrides: <Type, Generator>{
59-
FileSystem: () => fs,
68+
FileSystem: () => fileSystem,
6069
ProcessManager: () => FakeProcessManager.any(),
70+
Config: () => config,
6171
});
6272

6373
testUsingContext('returns sdkmanager path under cmdline tools on Linux/macOS', () {
6474
sdkDir = MockAndroidSdk.createSdkDirectory();
65-
globals.config.setValue('android-sdk', sdkDir.path);
75+
config.setValue('android-sdk', sdkDir.path);
6676

6777
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
68-
globals.fs.file(
69-
globals.fs.path.join(sdk.directory, 'cmdline-tools', 'latest', 'bin', 'sdkmanager')
78+
fileSystem.file(
79+
fileSystem.path.join(sdk.directory, 'cmdline-tools', 'latest', 'bin', 'sdkmanager')
7080
).createSync(recursive: true);
7181

72-
expect(sdk.sdkManagerPath, globals.fs.path.join(sdk.directory, 'cmdline-tools', 'latest', 'bin', 'sdkmanager'));
82+
expect(sdk.sdkManagerPath, fileSystem.path.join(sdk.directory, 'cmdline-tools', 'latest', 'bin', 'sdkmanager'));
7383
}, overrides: <Type, Generator>{
74-
FileSystem: () => fs,
84+
FileSystem: () => fileSystem,
7585
ProcessManager: () => FakeProcessManager.any(),
7686
Platform: () => FakePlatform(operatingSystem: 'linux'),
87+
Config: () => config,
7788
});
7889

7990
testUsingContext('returns sdkmanager.bat path under cmdline tools for windows', () {
8091
sdkDir = MockAndroidSdk.createSdkDirectory();
81-
globals.config.setValue('android-sdk', sdkDir.path);
92+
config.setValue('android-sdk', sdkDir.path);
8293

8394
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
84-
globals.fs.file(
85-
globals.fs.path.join(sdk.directory, 'cmdline-tools', 'latest', 'bin', 'sdkmanager.bat')
95+
fileSystem.file(
96+
fileSystem.path.join(sdk.directory, 'cmdline-tools', 'latest', 'bin', 'sdkmanager.bat')
8697
).createSync(recursive: true);
8798

8899
expect(sdk.sdkManagerPath,
89-
globals.fs.path.join(sdk.directory, 'cmdline-tools', 'latest', 'bin', 'sdkmanager.bat'));
100+
fileSystem.path.join(sdk.directory, 'cmdline-tools', 'latest', 'bin', 'sdkmanager.bat'));
90101
}, overrides: <Type, Generator>{
91-
FileSystem: () => fs,
102+
FileSystem: () => fileSystem,
92103
ProcessManager: () => FakeProcessManager.any(),
93104
Platform: () => FakePlatform(operatingSystem: 'windows'),
105+
Config: () => config,
94106
});
95107

96108
testUsingContext('returns sdkmanager path under tools if cmdline doesnt exist', () {
97109
sdkDir = MockAndroidSdk.createSdkDirectory();
98-
globals.config.setValue('android-sdk', sdkDir.path);
110+
config.setValue('android-sdk', sdkDir.path);
99111

100112
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
101113

102-
expect(sdk.sdkManagerPath, globals.fs.path.join(sdk.directory, 'tools', 'bin', 'sdkmanager'));
114+
expect(sdk.sdkManagerPath, fileSystem.path.join(sdk.directory, 'tools', 'bin', 'sdkmanager'));
103115
}, overrides: <Type, Generator>{
104-
FileSystem: () => fs,
116+
FileSystem: () => fileSystem,
105117
ProcessManager: () => FakeProcessManager.any(),
118+
Config: () => config,
106119
});
107120

108121
testUsingContext('returns sdkmanager version', () {
109122
sdkDir = MockAndroidSdk.createSdkDirectory();
110-
globals.config.setValue('android-sdk', sdkDir.path);
123+
config.setValue('android-sdk', sdkDir.path);
111124

112125
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
113-
when(globals.processManager.canRun(sdk.sdkManagerPath)).thenReturn(true);
114-
when(globals.processManager.runSync(<String>[sdk.sdkManagerPath, '--version'],
126+
when(processManager.canRun(sdk.sdkManagerPath)).thenReturn(true);
127+
when(processManager.runSync(<String>[sdk.sdkManagerPath, '--version'],
115128
environment: argThat(isNotNull, named: 'environment')))
116129
.thenReturn(ProcessResult(1, 0, '26.1.1\n', ''));
117-
if (globals.platform.isMacOS) {
118-
when(globals.processManager.runSync(
119-
<String>['/usr/libexec/java_home', '-v', '1.8'],
120-
workingDirectory: anyNamed('workingDirectory'),
121-
environment: anyNamed('environment'),
122-
)).thenReturn(ProcessResult(0, 0, '', ''));
123-
}
130+
when(processManager.runSync(
131+
<String>['/usr/libexec/java_home', '-v', '1.8'],
132+
workingDirectory: anyNamed('workingDirectory'),
133+
environment: anyNamed('environment'),
134+
)).thenReturn(ProcessResult(0, 0, '', ''));
135+
124136
expect(sdk.sdkManagerVersion, '26.1.1');
125137
}, overrides: <Type, Generator>{
126-
FileSystem: () => fs,
138+
FileSystem: () => fileSystem,
127139
ProcessManager: () => processManager,
140+
Config: () => config,
128141
});
129142

130143
testUsingContext('returns validate sdk is well formed', () {
131-
sdkDir = MockBrokenAndroidSdk.createSdkDirectory();
132-
globals.config.setValue('android-sdk', sdkDir.path);
144+
sdkDir = MockBrokenAndroidSdk.createSdkDirectory(
145+
fileSystem: fileSystem,
146+
);
147+
config.setValue('android-sdk', sdkDir.path);
133148

134149
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
135-
when(globals.processManager.canRun(sdk.adbPath)).thenReturn(true);
150+
when(processManager.canRun(sdk.adbPath)).thenReturn(true);
136151

137152
final List<String> validationIssues = sdk.validateSdkWellFormed();
138153
expect(validationIssues.first, 'No valid Android SDK platforms found in'
139154
' /.tmp_rand0/flutter_mock_android_sdk.rand0/platforms. Candidates were:\n'
140155
' - android-22\n'
141156
' - android-23');
142157
}, overrides: <Type, Generator>{
143-
FileSystem: () => fs,
158+
FileSystem: () => fileSystem,
144159
ProcessManager: () => processManager,
160+
Config: () => config,
161+
Platform: () => FakePlatform(operatingSystem: 'linux'),
145162
});
146163

147164
testUsingContext('does not throw on sdkmanager version check failure', () {
148165
sdkDir = MockAndroidSdk.createSdkDirectory();
149-
globals.config.setValue('android-sdk', sdkDir.path);
166+
config.setValue('android-sdk', sdkDir.path);
150167

151168
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
152-
when(globals.processManager.canRun(sdk.sdkManagerPath)).thenReturn(true);
153-
when(globals.processManager.runSync(<String>[sdk.sdkManagerPath, '--version'],
169+
when(processManager.canRun(sdk.sdkManagerPath)).thenReturn(true);
170+
when(processManager.runSync(<String>[sdk.sdkManagerPath, '--version'],
154171
environment: argThat(isNotNull, named: 'environment')))
155172
.thenReturn(ProcessResult(1, 1, '26.1.1\n', 'Mystery error'));
156-
if (globals.platform.isMacOS) {
157-
when(globals.processManager.runSync(
158-
<String>['/usr/libexec/java_home', '-v', '1.8'],
159-
workingDirectory: anyNamed('workingDirectory'),
160-
environment: anyNamed('environment'),
161-
)).thenReturn(ProcessResult(0, 0, '', ''));
162-
}
173+
when(processManager.runSync(
174+
<String>['/usr/libexec/java_home', '-v', '1.8'],
175+
workingDirectory: anyNamed('workingDirectory'),
176+
environment: anyNamed('environment'),
177+
)).thenReturn(ProcessResult(0, 0, '', ''));
178+
163179
expect(sdk.sdkManagerVersion, isNull);
164180
}, overrides: <Type, Generator>{
165-
FileSystem: () => fs,
181+
FileSystem: () => fileSystem,
166182
ProcessManager: () => processManager,
183+
Config: () => config,
167184
});
168185

169186
testUsingContext('throws on sdkmanager version check if sdkmanager not found', () {
170187
sdkDir = MockAndroidSdk.createSdkDirectory(withSdkManager: false);
171-
globals.config.setValue('android-sdk', sdkDir.path);
188+
config.setValue('android-sdk', sdkDir.path);
172189

173190
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
174-
when(globals.processManager.canRun(sdk.sdkManagerPath)).thenReturn(false);
191+
when(processManager.canRun(sdk.sdkManagerPath)).thenReturn(false);
175192
expect(() => sdk.sdkManagerVersion, throwsToolExit());
176193
}, overrides: <Type, Generator>{
177-
FileSystem: () => fs,
194+
FileSystem: () => fileSystem,
178195
ProcessManager: () => processManager,
196+
Config: () => config,
179197
});
180198
});
181199
}
@@ -185,15 +203,15 @@ class MockBrokenAndroidSdk extends Mock implements AndroidSdk {
185203
static Directory createSdkDirectory({
186204
bool withAndroidN = false,
187205
bool withSdkManager = true,
206+
@required FileSystem fileSystem,
188207
}) {
189-
final Directory dir = globals.fs.systemTempDirectory.createTempSync('flutter_mock_android_sdk.');
190-
final String exe = globals.platform.isWindows ? '.exe' : '';
208+
final Directory dir = fileSystem.systemTempDirectory.createTempSync('flutter_mock_android_sdk.');
191209
_createSdkFile(dir, 'licenses/dummy');
192-
_createSdkFile(dir, 'platform-tools/adb$exe');
210+
_createSdkFile(dir, 'platform-tools/adb');
193211

194-
_createSdkFile(dir, 'build-tools/sda/aapt$exe');
195-
_createSdkFile(dir, 'build-tools/af/aapt$exe');
196-
_createSdkFile(dir, 'build-tools/ljkasd/aapt$exe');
212+
_createSdkFile(dir, 'build-tools/sda/aapt');
213+
_createSdkFile(dir, 'build-tools/af/aapt');
214+
_createSdkFile(dir, 'build-tools/ljkasd/aapt');
197215

198216
_createSdkFile(dir, 'platforms/android-22/android.jar');
199217
_createSdkFile(dir, 'platforms/android-23/android.jar');

0 commit comments

Comments
 (0)