mirror of
https://github.com/flutter/packages.git
synced 2025-05-30 13:07:27 +08:00

The mock process runner used in most of the tests had poor handling of stdout/stderr: - By default it would return the `List<int>` output from the mock process, which was never correct since the parent process runner interface always sets encodings, thus the `dynamic` should always be of type `String` - The process for setting output on a `MockProcess` was awkward, since it was based on a `Stream<Lint<int>>`, even though in every case what we actually want to do is just set the full output to a string. - A hack was at some point added (presumably due to the above issues) to bypass that flow at the process runner level, and instead return a `String` set there. That meant there were two ways of setting output (one of which that only worked for one of the ways of running processes) - That hack wasn't updated when the ability to return multiple mock processes instead of a single global mock process was added, so the API was even more confusing, and there was no way to set different output for different processes. This changes the test APIs so that: - `MockProcess` takes stdout and stderr as strings, and internally manages converting them to a `Stream<List<int>>`. - `RecordingProcessRunner` correctly decodes and returns the output streams when constructing a process result. It also removes the resultStdout and resultStderr hacks, as well as the legacy `processToReturn` API, and converts all uses to the new structure, which is both simpler to use, and clearly associates output with specific processes.
159 lines
4.6 KiB
Dart
159 lines
4.6 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(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(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('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.')
|
|
],
|
|
));
|
|
});
|
|
});
|
|
}
|