Skip to content

Commit

Permalink
Merge pull request #2265 from leancodepl/fix-checking-java-version
Browse files Browse the repository at this point in the history
Fix checking java version
  • Loading branch information
pdenert authored Jul 15, 2024
2 parents f104929 + 92aa7bb commit 600d614
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/patrol_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0.1

- Fallback to read `java` version from `JAVA_HOME` when `flutter doctor` doesn't print any.

## 3.0.0

- **Breaking:** Use `java` version from `flutter doctor`
Expand Down
35 changes: 26 additions & 9 deletions packages/patrol_cli/lib/src/android/android_test_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AndroidTestBackend {
final FileSystem _fs;
final DisposeScope _disposeScope;
final Logger _logger;
late final String javaPath;
late final String? javaPath;

Future<void> build(AndroidAppOptions options) async {
await loadJavaPathFromFlutterDoctor(options.flutter.command.executable);
Expand All @@ -56,9 +56,11 @@ class AndroidTestBackend {
options.toGradleAssembleInvocation(isWindows: _platform.isWindows),
runInShell: true,
workingDirectory: _fs.currentDirectory.childDirectory('android').path,
environment: {
'JAVA_HOME': javaPath,
},
environment: javaPath != null
? {
'JAVA_HOME': javaPath!,
}
: {},
)
..disposedBy(scope);
process.listenStdOut((l) => _logger.detail('\t: $l')).disposedBy(scope);
Expand All @@ -80,9 +82,11 @@ class AndroidTestBackend {
options.toGradleAssembleTestInvocation(isWindows: _platform.isWindows),
runInShell: true,
workingDirectory: _fs.currentDirectory.childDirectory('android').path,
environment: {
'JAVA_HOME': javaPath,
},
environment: javaPath != null
? {
'JAVA_HOME': javaPath!,
}
: {},
)
..disposedBy(scope);
process.listenStdOut((l) => _logger.detail('\t: $l')).disposedBy(scope);
Expand All @@ -102,6 +106,9 @@ class AndroidTestBackend {
});
}

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

Expand Down Expand Up @@ -130,10 +137,16 @@ class AndroidTestBackend {
javaCompleterPath.complete(path);
}
},
onDone: () {
if (!javaCompleterPath.isCompleted) {
javaCompleterPath.complete(null);
}
},
onError: (error) => javaCompleterPath.complete(null),
).disposedBy(scope);
});

javaPath = await javaCompleterPath.future ?? '';
javaPath = await javaCompleterPath.future;
}

/// Executes the tests of the given [options] on the given [device].
Expand All @@ -156,7 +169,11 @@ class AndroidTestBackend {
runInShell: true,
environment: {
'ANDROID_SERIAL': device.id,
'JAVA_HOME': javaPath,
...javaPath != null
? {
'JAVA_HOME': javaPath!,
}
: {},
},
workingDirectory: _fs.currentDirectory.childDirectory('android').path,
)
Expand Down
2 changes: 1 addition & 1 deletion packages/patrol_cli/lib/src/base/constants.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/// Version of Patrol CLI. Must be kept in sync with pubspec.yaml.
/// If you update this, make sure that compatibility-table.mdx is updated (if needed)
const version = '3.0.0';
const version = '3.0.1';
26 changes: 24 additions & 2 deletions packages/patrol_cli/lib/src/compatibility_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,43 @@ Future<void> _checkJavaVersion(
final javaCompleterVersion = Completer<Version?>();

await disposeScope.run((scope) async {
final process = await processManager.start(
final processFlutter = await processManager.start(
[flutterExecutable, 'doctor', '--verbose'],
workingDirectory: projectRoot.path,
runInShell: true,
)
..disposedBy(scope);

process.listenStdOut(
processFlutter.listenStdOut(
(line) async {
if (line.contains('• Java version') &&
javaCompleterVersion.isCompleted == false) {
final versionString = line.split(' ').last.replaceAll(')', '');
javaCompleterVersion.complete(Version.parse(versionString));
}
},
onDone: () async {
if (!javaCompleterVersion.isCompleted) {
final processJava = await processManager.start(
['javac', '--version'],
workingDirectory: projectRoot.path,
runInShell: true,
)
..disposedBy(scope);

processJava.listenStdOut(
(line) async {
if (line.startsWith('javac')) {
javaCompleterVersion
.complete(Version.parse(line.split(' ').last));
}
},
onDone: () => javaCompleterVersion.complete(null),
onError: (error) => javaCompleterVersion.complete(null),
).disposedBy(scope);
}
},
onError: (error) => javaCompleterVersion.complete(null),
).disposedBy(scope);
});

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

0 comments on commit 600d614

Please sign in to comment.