mirror of
https://github.com/flutter/packages.git
synced 2025-06-01 06:18:12 +08:00
[flutter_plugin_tools] Add a command to lint Android code (#4206)
Adds a new `lint-android` command to run `gradlew lint` on Android plugins. Also standardizes the names of the Cirrus tasks that run all the build and platform-specific (i.e., not Dart unit test) tests for each platform, as they were getting unnecessarily long and complex in some cases. Fixes https://github.com/flutter/flutter/issues/87071
This commit is contained in:
158
script/tool/test/lint_android_command_test.dart
Normal file
158
script/tool/test/lint_android_command_test.dart
Normal file
@ -0,0 +1,158 @@
|
||||
// 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(style: FileSystemStyle.posix);
|
||||
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);
|
||||
});
|
||||
|
||||
test('runs gradle lint', () async {
|
||||
final Directory pluginDir =
|
||||
createFakePlugin('plugin1', packagesDir, extraFiles: <String>[
|
||||
'example/android/gradlew',
|
||||
], platformSupport: <String, PlatformSupport>{
|
||||
kPlatformAndroid: PlatformSupport.inline
|
||||
});
|
||||
|
||||
final Directory androidDir =
|
||||
pluginDir.childDirectory('example').childDirectory('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('fails if gradlew is missing', () async {
|
||||
createFakePlugin('plugin1', packagesDir,
|
||||
platformSupport: <String, PlatformSupport>{
|
||||
kPlatformAndroid: 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 example before linting'),
|
||||
],
|
||||
));
|
||||
});
|
||||
|
||||
test('fails if linting finds issues', () async {
|
||||
createFakePlugin('plugin1', packagesDir,
|
||||
platformSupport: <String, PlatformSupport>{
|
||||
kPlatformAndroid: PlatformSupport.inline
|
||||
});
|
||||
|
||||
processRunner.mockProcessesForExecutable['gradlew'] = <io.Process>[
|
||||
MockProcess.failing(),
|
||||
];
|
||||
|
||||
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 example before linting'),
|
||||
],
|
||||
));
|
||||
});
|
||||
|
||||
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 implemenatation.')
|
||||
],
|
||||
));
|
||||
});
|
||||
|
||||
test('skips non-inline plugins', () async {
|
||||
createFakePlugin('plugin1', packagesDir,
|
||||
platformSupport: <String, PlatformSupport>{
|
||||
kPlatformAndroid: PlatformSupport.federated
|
||||
});
|
||||
|
||||
final List<String> output =
|
||||
await runCapturingPrint(runner, <String>['lint-android']);
|
||||
|
||||
expect(
|
||||
output,
|
||||
containsAllInOrder(
|
||||
<Matcher>[
|
||||
contains(
|
||||
'SKIPPING: Plugin does not have an Android implemenatation.')
|
||||
],
|
||||
));
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user