flutter/packages/flutter_tools/test/integration.shard/analyze_suggestions_integration_test.dart
Reid Baker 2383400fe0
Add Java-Gradle-AGP validation to flutter analyze (#123916)
https://github.com/flutter/flutter/issues/123917

Doc covering a broad set of issues related to android studio updating. 

https://docs.google.com/document/d/1hTXkjbUrBnXgu8NQsth1c3aEqo77rWoEj8CcsQ39wwQ/edit?pli=1#

Specifically this pr: 
- Adds new functions to find a projects AGP, Gradle and java versions,
and tests.
- Adds new functions that take versions and parse if the versions are
compatible with each other, and tests.
- Adds validator for `flutter analyze --suggestions` that evaluates the
java/gradle/agp versions and checks if they are compatible, and
integration test.
- Updates the version of gradle used by
dev/integration_tests/flutter_gallery/ to the minimum supported by java
18 so that the integration tests pass (It is unknown why the java
version is 18.9 instead of 11)
- Moves `isWithinVersionRange` to version.dart, and tests. 
- Adds FakeAndroidStudio to fakes to be used in multiple tests but does
not remove existing copies.

Metrics will be included as part of the definition of done for this bug
but not as part of this cl. It is already too big.

Known work still left in this pr: 
* Understand why analyze integration tests are failing. 


Example output if Java and gradle are not compatible: 
```
┌───────────────────────────────────────────────────────────────────┐
│ General Info                                                      │
│ [✓] App Name: espresso_example                                    │
│ [✓] Supported Platforms: android                                  │
│ [✓] Is Flutter Package: yes                                       │
│ [✓] Uses Material Design: yes                                     │
│ [✓] Is Plugin: no                                                 │
│ [✗] Java/Gradle/Android Gradle Plugin:                            │
│                                                                   │
│ Incompatible Java/Gradle versions.                                │
│                                                                   │
│ Java Version: 17.0.6, Gradle Version: 7.0.2                       │
│                                                                   │
│ See the link below for more information.                          │
│ https://docs.gradle.org/current/userguide/compatibility.html#java │
│                                                                   │
└───────────────────────────────────────────────────────────────────┘
```
Example output if Gradle and AGP are not compatible
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ General Info                                                                │
│ [✓] App Name: espresso_example                                              │
│ [✓] Supported Platforms: android                                            │
│ [✓] Is Flutter Package: yes                                                 │
│ [✓] Uses Material Design: yes                                               │
│ [✓] Is Plugin: no                                                           │
│ [✗] Java/Gradle/Android Gradle Plugin: Incompatible Gradle/AGP versions.    │
│                                                                             │
│ Gradle Version: 7.0.2, AGP Version: 7.4.2                                   │
│                                                                             │
│ Update gradle to at least "7.5".                                            │
│ See the link below for more information:                                    │
│ https://developer.android.com/studio/releases/gradle-plugin#updating-gradle │
│                                                                             │
│ Incompatible Java/Gradle versions.                                          │
│                                                                             │
│ Java Version: 17.0.6, Gradle Version: 7.0.2                                 │
│                                                                             │
│ See the link below for more information:                                    │
│ https://docs.gradle.org/current/userguide/compatibility.html#java           │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```
Example output if Java/Gradle/Agp are not compatible. 
```

┌─────────────────────────────────────────────────────────────────────────────┐
│ General Info                                                                │
│ [✓] App Name: espresso_example                                              │
│ [✓] Supported Platforms: android                                            │
│ [✓] Is Flutter Package: yes                                                 │
│ [✓] Uses Material Design: yes                                               │
│ [✓] Is Plugin: no                                                           │
│ [✗] Java/Gradle/Android Gradle Plugin: Incompatible Gradle/AGP versions.    │
│                                                                             │
│ Gradle Version: 7.0.2, AGP Version: 7.4.2                                   │
│                                                                             │
│ Update gradle to at least "7.5".                                            │
│ See the link below for more information:                                    │
│ https://developer.android.com/studio/releases/gradle-plugin#updating-gradle │
│                                                                             │
│ Incompatible Java/Gradle versions.                                          │
│                                                                             │
│ Java Version: 17.0.6, Gradle Version: 7.0.2                                 │
│                                                                             │
│ See the link below for more information:                                    │
│ https://docs.gradle.org/current/userguide/compatibility.html#java           │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

Commit messages
- Add function to gradle_utils.dart that gets the gradle version from
wrapper or system and add a test for each situation
- Add method to get agp version, add method to validate agp against
gradle version, update documentation, add tests for agp validation.
- Update dart doc for validateGradleAndAgp to describe where the info
came from and corner case behavior, create function to validate java and
gradle and hardcode return to false
- Fill out and test java gradle compatibility function in gradle_utils
- Hook up java gradle evaluateion to hasValidJavaGradleAgpVersions with
hardcoded java version
- Add java --version output parsing and tests
- Add getJavaBinary test
- Update comment in android_sdk for mac behavior with java_home -v

## 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].
- [ ] All existing and new tests are passing.
2023-04-05 14:36:05 -04:00

172 lines
8.3 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 'dart:convert';
import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/commands/analyze.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/project_validator.dart';
import '../src/common.dart';
import '../src/context.dart';
import '../src/test_flutter_command_runner.dart';
import 'test_utils.dart';
void main() {
late FileSystem fileSystem;
group('analyze --suggestions command integration', () {
setUp(() {
fileSystem = globals.localFileSystem;
});
testUsingContext('General Info Project Validator', () async {
final BufferLogger loggerTest = BufferLogger.test();
final AnalyzeCommand command = AnalyzeCommand(
artifacts: globals.artifacts!,
fileSystem: fileSystem,
logger: loggerTest,
platform: globals.platform,
terminal: globals.terminal,
processManager: globals.processManager,
allProjectValidators: <ProjectValidator>[GeneralInfoProjectValidator()],
suppressAnalytics: true,
);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>[
'analyze',
'--no-pub',
'--no-current-package',
'--suggestions',
'../../dev/integration_tests/flutter_gallery',
]);
const String expected = '\n'
'┌───────────────────────────────────────────────────────────────────┐\n'
'│ General Info │\n'
'│ [✓] App Name: flutter_gallery │\n'
'│ [✓] Supported Platforms: android, ios, web, macos, linux, windows │\n'
'│ [✓] Is Flutter Package: yes │\n'
'│ [✓] Uses Material Design: yes │\n'
'│ [✓] Is Plugin: no │\n'
'│ [✓] Java/Gradle/Android Gradle Plugin: ${AndroidProject.validJavaGradleAgpString}\n'
'└───────────────────────────────────────────────────────────────────┘\n';
expect(loggerTest.statusText, contains(expected));
});
testUsingContext('PubDependenciesProjectValidator success ', () async {
final BufferLogger loggerTest = BufferLogger.test();
final AnalyzeCommand command = AnalyzeCommand(
artifacts: globals.artifacts!,
fileSystem: fileSystem,
logger: loggerTest,
platform: globals.platform,
terminal: globals.terminal,
processManager: globals.processManager,
allProjectValidators: <ProjectValidator>[
PubDependenciesProjectValidator(globals.processManager),
],
suppressAnalytics: true,
);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>[
'analyze',
'--no-pub',
'--no-current-package',
'--suggestions',
'../../dev/integration_tests/flutter_gallery',
]);
const String expected = '\n'
'┌────────────────────────────────────────────────────────────────────────────────────┐\n'
'│ Pub dependencies │\n'
'│ [✓] Dart dependencies: All pub dependencies are hosted on https://pub.dartlang.org │\n'
'└────────────────────────────────────────────────────────────────────────────────────┘\n';
expect(loggerTest.statusText, contains(expected));
});
});
group('analyze --suggestions --machine command integration', () {
late Directory tempDir;
late Platform platform;
setUpAll(() async {
platform = const LocalPlatform();
tempDir = createResolvedTempDirectorySync('run_test.');
await globals.processManager.run(<String>['flutter', 'create', 'test_project'], workingDirectory: tempDir.path);
});
tearDown(() async {
tryToDelete(tempDir);
});
testUsingContext('analyze --suggestions --machine produces expected values', () async {
final ProcessResult result = await globals.processManager.run(<String>['flutter', 'analyze', '--suggestions', '--machine'], workingDirectory: tempDir.childDirectory('test_project').path);
expect(result.stdout is String, true);
expect((result.stdout as String).startsWith('{\n'), true);
expect(result.stdout, isNot(contains(',\n}'))); // No trailing commas allowed in JSON
expect((result.stdout as String).endsWith('}\n'), true);
final Map<String, dynamic> decoded = jsonDecode(result.stdout as String) as Map<String, dynamic>;
expect(decoded.containsKey('FlutterProject.android.exists'), true);
expect(decoded.containsKey('FlutterProject.ios.exists'), true);
expect(decoded.containsKey('FlutterProject.web.exists'), true);
expect(decoded.containsKey('FlutterProject.macos.exists'), true);
expect(decoded.containsKey('FlutterProject.linux.exists'), true);
expect(decoded.containsKey('FlutterProject.windows.exists'), true);
expect(decoded.containsKey('FlutterProject.fuchsia.exists'), true);
expect(decoded.containsKey('FlutterProject.android.isKotlin'), true);
expect(decoded.containsKey('FlutterProject.ios.isSwift'), true);
expect(decoded.containsKey('FlutterProject.isModule'), true);
expect(decoded.containsKey('FlutterProject.isPlugin'), true);
expect(decoded.containsKey('FlutterProject.manifest.appname'), true);
expect(decoded.containsKey('FlutterVersion.frameworkRevision'), true);
expect(decoded.containsKey('FlutterProject.directory'), true);
expect(decoded.containsKey('FlutterProject.metadataFile'), true);
expect(decoded.containsKey('Platform.operatingSystem'), true);
expect(decoded.containsKey('Platform.isAndroid'), true);
expect(decoded.containsKey('Platform.isIOS'), true);
expect(decoded.containsKey('Platform.isWindows'), true);
expect(decoded.containsKey('Platform.isMacOS'), true);
expect(decoded.containsKey('Platform.isFuchsia'), true);
expect(decoded.containsKey('Platform.pathSeparator'), true);
expect(decoded.containsKey('Cache.flutterRoot'), true);
expect(decoded['FlutterProject.android.exists'], true);
expect(decoded['FlutterProject.ios.exists'], true);
expect(decoded['FlutterProject.web.exists'], true);
expect(decoded['FlutterProject.macos.exists'], true);
expect(decoded['FlutterProject.linux.exists'], true);
expect(decoded['FlutterProject.windows.exists'], true);
expect(decoded['FlutterProject.fuchsia.exists'], false);
expect(decoded['FlutterProject.android.isKotlin'], true);
expect(decoded['FlutterProject.ios.isSwift'], true);
expect(decoded['FlutterProject.isModule'], false);
expect(decoded['FlutterProject.isPlugin'], false);
expect(decoded['FlutterProject.manifest.appname'], 'test_project');
expect(decoded['FlutterVersion.frameworkRevision'], '');
expect(decoded['Platform.isAndroid'], false);
expect(decoded['Platform.isIOS'], false);
expect(decoded['Platform.isWindows'], platform.isWindows);
expect(decoded['Platform.isMacOS'], platform.isMacOS);
expect(decoded['Platform.isFuchsia'], platform.isFuchsia);
expect(decoded['Platform.pathSeparator'], platform.pathSeparator);
}, overrides: <Type, Generator>{});
});
}