Skip to content

Commit 600d614

Browse files
authored
Merge pull request #2265 from leancodepl/fix-checking-java-version
Fix checking java version
2 parents f104929 + 92aa7bb commit 600d614

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

packages/patrol_cli/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.0.1
2+
3+
- Fallback to read `java` version from `JAVA_HOME` when `flutter doctor` doesn't print any.
4+
15
## 3.0.0
26

37
- **Breaking:** Use `java` version from `flutter doctor`

packages/patrol_cli/lib/src/android/android_test_backend.dart

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AndroidTestBackend {
3838
final FileSystem _fs;
3939
final DisposeScope _disposeScope;
4040
final Logger _logger;
41-
late final String javaPath;
41+
late final String? javaPath;
4242

4343
Future<void> build(AndroidAppOptions options) async {
4444
await loadJavaPathFromFlutterDoctor(options.flutter.command.executable);
@@ -56,9 +56,11 @@ class AndroidTestBackend {
5656
options.toGradleAssembleInvocation(isWindows: _platform.isWindows),
5757
runInShell: true,
5858
workingDirectory: _fs.currentDirectory.childDirectory('android').path,
59-
environment: {
60-
'JAVA_HOME': javaPath,
61-
},
59+
environment: javaPath != null
60+
? {
61+
'JAVA_HOME': javaPath!,
62+
}
63+
: {},
6264
)
6365
..disposedBy(scope);
6466
process.listenStdOut((l) => _logger.detail('\t: $l')).disposedBy(scope);
@@ -80,9 +82,11 @@ class AndroidTestBackend {
8082
options.toGradleAssembleTestInvocation(isWindows: _platform.isWindows),
8183
runInShell: true,
8284
workingDirectory: _fs.currentDirectory.childDirectory('android').path,
83-
environment: {
84-
'JAVA_HOME': javaPath,
85-
},
85+
environment: javaPath != null
86+
? {
87+
'JAVA_HOME': javaPath!,
88+
}
89+
: {},
8690
)
8791
..disposedBy(scope);
8892
process.listenStdOut((l) => _logger.detail('\t: $l')).disposedBy(scope);
@@ -102,6 +106,9 @@ class AndroidTestBackend {
102106
});
103107
}
104108

109+
/// Load the Java path from the output of `flutter doctor`.
110+
/// If this will be null, then the Java path will not be set and patrol
111+
/// tries to use the Java path from the PATH environment variable.
105112
Future<void> loadJavaPathFromFlutterDoctor(String commandExecutable) async {
106113
final javaCompleterPath = Completer<String?>();
107114

@@ -130,10 +137,16 @@ class AndroidTestBackend {
130137
javaCompleterPath.complete(path);
131138
}
132139
},
140+
onDone: () {
141+
if (!javaCompleterPath.isCompleted) {
142+
javaCompleterPath.complete(null);
143+
}
144+
},
145+
onError: (error) => javaCompleterPath.complete(null),
133146
).disposedBy(scope);
134147
});
135148

136-
javaPath = await javaCompleterPath.future ?? '';
149+
javaPath = await javaCompleterPath.future;
137150
}
138151

139152
/// Executes the tests of the given [options] on the given [device].
@@ -156,7 +169,11 @@ class AndroidTestBackend {
156169
runInShell: true,
157170
environment: {
158171
'ANDROID_SERIAL': device.id,
159-
'JAVA_HOME': javaPath,
172+
...javaPath != null
173+
? {
174+
'JAVA_HOME': javaPath!,
175+
}
176+
: {},
160177
},
161178
workingDirectory: _fs.currentDirectory.childDirectory('android').path,
162179
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/// Version of Patrol CLI. Must be kept in sync with pubspec.yaml.
22
/// If you update this, make sure that compatibility-table.mdx is updated (if needed)
3-
const version = '3.0.0';
3+
const version = '3.0.1';

packages/patrol_cli/lib/src/compatibility_checker.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,43 @@ Future<void> _checkJavaVersion(
103103
final javaCompleterVersion = Completer<Version?>();
104104

105105
await disposeScope.run((scope) async {
106-
final process = await processManager.start(
106+
final processFlutter = await processManager.start(
107107
[flutterExecutable, 'doctor', '--verbose'],
108108
workingDirectory: projectRoot.path,
109109
runInShell: true,
110110
)
111111
..disposedBy(scope);
112112

113-
process.listenStdOut(
113+
processFlutter.listenStdOut(
114114
(line) async {
115115
if (line.contains('• Java version') &&
116116
javaCompleterVersion.isCompleted == false) {
117117
final versionString = line.split(' ').last.replaceAll(')', '');
118118
javaCompleterVersion.complete(Version.parse(versionString));
119119
}
120120
},
121+
onDone: () async {
122+
if (!javaCompleterVersion.isCompleted) {
123+
final processJava = await processManager.start(
124+
['javac', '--version'],
125+
workingDirectory: projectRoot.path,
126+
runInShell: true,
127+
)
128+
..disposedBy(scope);
129+
130+
processJava.listenStdOut(
131+
(line) async {
132+
if (line.startsWith('javac')) {
133+
javaCompleterVersion
134+
.complete(Version.parse(line.split(' ').last));
135+
}
136+
},
137+
onDone: () => javaCompleterVersion.complete(null),
138+
onError: (error) => javaCompleterVersion.complete(null),
139+
).disposedBy(scope);
140+
}
141+
},
142+
onError: (error) => javaCompleterVersion.complete(null),
121143
).disposedBy(scope);
122144
});
123145

packages/patrol_cli/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: patrol_cli
22
description: >
33
Command-line tool for Patrol, a powerful Flutter-native UI testing framework.
4-
version: 3.0.0 # Must be kept in sync with constants.dart
4+
version: 3.0.1 # Must be kept in sync with constants.dart
55
homepage: https://patrol.leancode.co
66
repository: https://github.com/leancodepl/patrol/tree/master/packages/patrol_cli
77
issue_tracker: https://github.com/leancodepl/patrol/issues?q=is%3Aopen+is%3Aissue+label%3A%22package%3A+patrol_cli%22

0 commit comments

Comments
 (0)