mirror of
https://github.com/flutter/packages.git
synced 2025-08-15 02:48:37 +08:00

Since our existing `gradlew lint` check doesn't catch all warnings (notably, deprecation warnings, but also others like raw values), this: - Configures our plugin example apps to enable `-Xlint:all -Werror` for the javac for the plugin project, so that we also get javac lint coverage in CI. This is done in the example app so that it won't affect plugin clients. - Adds a check to `lint-android` that the example is configured this way, so that we don't forget to set it up when adding new plugins (or re-generating plugin examples from template). Where it was trivial for me to just fix existing violations, I did so in this PR. For the rest that had violations, I commented out the enabling of the flags, with a TODO. Normally we would do this kind of thing with a `script/configs` file to opt packages out of a new check, but since this is part of an existing check rather than a whole new command, doing it that way would disable `gradlew lint` for those packages as well. Making a whole new command seemed more complex for the long term, so this seemed like the best short term option. We should try to remove as many of these opt-outs as possible ASAP. Part of https://github.com/flutter/flutter/issues/91868
273 lines
8.2 KiB
Dart
273 lines
8.2 KiB
Dart
// Copyright 2013 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:io' as io;
|
|
|
|
import 'package:args/command_runner.dart';
|
|
import 'package:file/file.dart';
|
|
import 'package:file/memory.dart';
|
|
import 'package:flutter_plugin_tools/src/common/core.dart';
|
|
import 'package:flutter_plugin_tools/src/common/plugin_utils.dart';
|
|
import 'package:flutter_plugin_tools/src/lint_android_command.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
import 'mocks.dart';
|
|
import 'util.dart';
|
|
|
|
void main() {
|
|
group('LintAndroidCommand', () {
|
|
FileSystem fileSystem;
|
|
late Directory packagesDir;
|
|
late CommandRunner<void> runner;
|
|
late MockPlatform mockPlatform;
|
|
late RecordingProcessRunner processRunner;
|
|
|
|
setUp(() {
|
|
fileSystem = MemoryFileSystem();
|
|
packagesDir = createPackagesDirectory(fileSystem: fileSystem);
|
|
mockPlatform = MockPlatform();
|
|
processRunner = RecordingProcessRunner();
|
|
final LintAndroidCommand command = LintAndroidCommand(
|
|
packagesDir,
|
|
processRunner: processRunner,
|
|
platform: mockPlatform,
|
|
);
|
|
|
|
runner = CommandRunner<void>(
|
|
'lint_android_test', 'Test for $LintAndroidCommand');
|
|
runner.addCommand(command);
|
|
});
|
|
|
|
void writeFakeBuildGradle(RepositoryPackage example, String pluginName,
|
|
{bool warningsConfigured = true}) {
|
|
final String warningConfig = '''
|
|
gradle.projectsEvaluated {
|
|
project(":$pluginName") {
|
|
tasks.withType(JavaCompile) {
|
|
options.compilerArgs << "-Xlint:all" << "-Werror"
|
|
}
|
|
}
|
|
}
|
|
''';
|
|
example
|
|
.platformDirectory(FlutterPlatform.android)
|
|
.childFile('build.gradle')
|
|
.writeAsStringSync('''
|
|
buildscript {
|
|
repositories {
|
|
google()
|
|
mavenCentral()
|
|
}
|
|
dependencies {
|
|
classpath 'com.android.tools.build:gradle:8.0.1'
|
|
}
|
|
}
|
|
allprojects {
|
|
repositories {
|
|
google()
|
|
mavenCentral()
|
|
}
|
|
}
|
|
${warningsConfigured ? warningConfig : ''}
|
|
''');
|
|
}
|
|
|
|
test('runs gradle lint', () async {
|
|
final RepositoryPackage plugin =
|
|
createFakePlugin('plugin1', packagesDir, extraFiles: <String>[
|
|
'example/android/gradlew',
|
|
], platformSupport: <String, PlatformDetails>{
|
|
platformAndroid: const PlatformDetails(PlatformSupport.inline)
|
|
});
|
|
writeFakeBuildGradle(plugin.getExamples().first, 'plugin1');
|
|
|
|
final Directory androidDir =
|
|
plugin.getExamples().first.platformDirectory(FlutterPlatform.android);
|
|
|
|
final List<String> output =
|
|
await runCapturingPrint(runner, <String>['lint-android']);
|
|
|
|
expect(
|
|
processRunner.recordedCalls,
|
|
orderedEquals(<ProcessCall>[
|
|
ProcessCall(
|
|
androidDir.childFile('gradlew').path,
|
|
const <String>['plugin1:lintDebug'],
|
|
androidDir.path,
|
|
),
|
|
]),
|
|
);
|
|
|
|
expect(
|
|
output,
|
|
containsAllInOrder(<Matcher>[
|
|
contains('Running for plugin1'),
|
|
contains('No issues found!'),
|
|
]));
|
|
});
|
|
|
|
test('runs on all examples', () async {
|
|
final List<String> examples = <String>['example1', 'example2'];
|
|
final RepositoryPackage plugin = createFakePlugin('plugin1', packagesDir,
|
|
examples: examples,
|
|
extraFiles: <String>[
|
|
'example/example1/android/gradlew',
|
|
'example/example2/android/gradlew',
|
|
],
|
|
platformSupport: <String, PlatformDetails>{
|
|
platformAndroid: const PlatformDetails(PlatformSupport.inline)
|
|
});
|
|
for (final RepositoryPackage example in plugin.getExamples()) {
|
|
writeFakeBuildGradle(example, 'plugin1');
|
|
}
|
|
|
|
final Iterable<Directory> exampleAndroidDirs = plugin.getExamples().map(
|
|
(RepositoryPackage example) =>
|
|
example.platformDirectory(FlutterPlatform.android));
|
|
|
|
final List<String> output =
|
|
await runCapturingPrint(runner, <String>['lint-android']);
|
|
|
|
expect(
|
|
processRunner.recordedCalls,
|
|
orderedEquals(<ProcessCall>[
|
|
for (final Directory directory in exampleAndroidDirs)
|
|
ProcessCall(
|
|
directory.childFile('gradlew').path,
|
|
const <String>['plugin1:lintDebug'],
|
|
directory.path,
|
|
),
|
|
]),
|
|
);
|
|
|
|
expect(
|
|
output,
|
|
containsAllInOrder(<Matcher>[
|
|
contains('Running for plugin1'),
|
|
contains('No issues found!'),
|
|
]));
|
|
});
|
|
|
|
test('fails if gradlew is missing', () async {
|
|
createFakePlugin('plugin1', packagesDir,
|
|
platformSupport: <String, PlatformDetails>{
|
|
platformAndroid: const PlatformDetails(PlatformSupport.inline)
|
|
});
|
|
|
|
Error? commandError;
|
|
final List<String> output = await runCapturingPrint(
|
|
runner, <String>['lint-android'], errorHandler: (Error e) {
|
|
commandError = e;
|
|
});
|
|
|
|
expect(commandError, isA<ToolExit>());
|
|
expect(
|
|
output,
|
|
containsAllInOrder(
|
|
<Matcher>[
|
|
contains('Build examples before linting'),
|
|
],
|
|
));
|
|
});
|
|
|
|
test('fails if linting finds issues', () async {
|
|
final RepositoryPackage plugin =
|
|
createFakePlugin('plugin1', packagesDir, extraFiles: <String>[
|
|
'example/android/gradlew',
|
|
], platformSupport: <String, PlatformDetails>{
|
|
platformAndroid: const PlatformDetails(PlatformSupport.inline)
|
|
});
|
|
writeFakeBuildGradle(plugin.getExamples().first, 'plugin1');
|
|
|
|
final String gradlewPath = plugin
|
|
.getExamples()
|
|
.first
|
|
.platformDirectory(FlutterPlatform.android)
|
|
.childFile('gradlew')
|
|
.path;
|
|
processRunner.mockProcessesForExecutable[gradlewPath] = <io.Process>[
|
|
MockProcess(exitCode: 1),
|
|
];
|
|
|
|
Error? commandError;
|
|
final List<String> output = await runCapturingPrint(
|
|
runner, <String>['lint-android'], errorHandler: (Error e) {
|
|
commandError = e;
|
|
});
|
|
|
|
expect(commandError, isA<ToolExit>());
|
|
expect(
|
|
output,
|
|
containsAllInOrder(
|
|
<Matcher>[
|
|
contains('The following packages had errors:'),
|
|
],
|
|
));
|
|
});
|
|
|
|
test('fails if javac lint-warnings-as-errors is missing', () async {
|
|
final RepositoryPackage plugin =
|
|
createFakePlugin('plugin1', packagesDir, extraFiles: <String>[
|
|
'example/android/gradlew',
|
|
], platformSupport: <String, PlatformDetails>{
|
|
platformAndroid: const PlatformDetails(PlatformSupport.inline)
|
|
});
|
|
writeFakeBuildGradle(plugin.getExamples().first, 'plugin1',
|
|
warningsConfigured: false);
|
|
|
|
Error? commandError;
|
|
final List<String> output = await runCapturingPrint(
|
|
runner, <String>['lint-android'], errorHandler: (Error e) {
|
|
commandError = e;
|
|
});
|
|
|
|
expect(commandError, isA<ToolExit>());
|
|
expect(
|
|
output,
|
|
containsAllInOrder(
|
|
<Matcher>[
|
|
contains('The example example is not configured to treat javac '
|
|
'lints and warnings as errors.'),
|
|
contains('The following packages had errors:'),
|
|
],
|
|
));
|
|
});
|
|
|
|
test('skips non-Android plugins', () async {
|
|
createFakePlugin('plugin1', packagesDir);
|
|
|
|
final List<String> output =
|
|
await runCapturingPrint(runner, <String>['lint-android']);
|
|
|
|
expect(
|
|
output,
|
|
containsAllInOrder(
|
|
<Matcher>[
|
|
contains(
|
|
'SKIPPING: Plugin does not have an Android implementation.')
|
|
],
|
|
));
|
|
});
|
|
|
|
test('skips non-inline plugins', () async {
|
|
createFakePlugin('plugin1', packagesDir,
|
|
platformSupport: <String, PlatformDetails>{
|
|
platformAndroid: const PlatformDetails(PlatformSupport.federated)
|
|
});
|
|
|
|
final List<String> output =
|
|
await runCapturingPrint(runner, <String>['lint-android']);
|
|
|
|
expect(
|
|
output,
|
|
containsAllInOrder(
|
|
<Matcher>[
|
|
contains(
|
|
'SKIPPING: Plugin does not have an Android implementation.')
|
|
],
|
|
));
|
|
});
|
|
});
|
|
}
|