Files
packages/script/tool/test/common/gradle_test.dart
stuartmorgan 74cf0287f9 [flutter_plugin_tools] Improve process mocking (#4254)
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.
2021-08-24 11:42:21 -07:00

180 lines
5.4 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:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_plugin_tools/src/common/gradle.dart';
import 'package:test/test.dart';
import '../mocks.dart';
import '../util.dart';
void main() {
late FileSystem fileSystem;
late RecordingProcessRunner processRunner;
setUp(() {
fileSystem = MemoryFileSystem();
processRunner = RecordingProcessRunner();
});
group('isConfigured', () {
test('reports true when configured on Windows', () async {
final Directory plugin = createFakePlugin(
'plugin', fileSystem.directory('/'),
extraFiles: <String>['android/gradlew.bat']);
final GradleProject project = GradleProject(
plugin,
processRunner: processRunner,
platform: MockPlatform(isWindows: true),
);
expect(project.isConfigured(), true);
});
test('reports true when configured on non-Windows', () async {
final Directory plugin = createFakePlugin(
'plugin', fileSystem.directory('/'),
extraFiles: <String>['android/gradlew']);
final GradleProject project = GradleProject(
plugin,
processRunner: processRunner,
platform: MockPlatform(isMacOS: true),
);
expect(project.isConfigured(), true);
});
test('reports false when not configured on Windows', () async {
final Directory plugin = createFakePlugin(
'plugin', fileSystem.directory('/'),
extraFiles: <String>['android/foo']);
final GradleProject project = GradleProject(
plugin,
processRunner: processRunner,
platform: MockPlatform(isWindows: true),
);
expect(project.isConfigured(), false);
});
test('reports true when configured on non-Windows', () async {
final Directory plugin = createFakePlugin(
'plugin', fileSystem.directory('/'),
extraFiles: <String>['android/foo']);
final GradleProject project = GradleProject(
plugin,
processRunner: processRunner,
platform: MockPlatform(isMacOS: true),
);
expect(project.isConfigured(), false);
});
});
group('runXcodeBuild', () {
test('runs without arguments', () async {
final Directory plugin = createFakePlugin(
'plugin', fileSystem.directory('/'),
extraFiles: <String>['android/gradlew']);
final GradleProject project = GradleProject(
plugin,
processRunner: processRunner,
platform: MockPlatform(isMacOS: true),
);
final int exitCode = await project.runCommand('foo');
expect(exitCode, 0);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall(
plugin.childDirectory('android').childFile('gradlew').path,
const <String>[
'foo',
],
plugin.childDirectory('android').path),
]));
});
test('runs with arguments', () async {
final Directory plugin = createFakePlugin(
'plugin', fileSystem.directory('/'),
extraFiles: <String>['android/gradlew']);
final GradleProject project = GradleProject(
plugin,
processRunner: processRunner,
platform: MockPlatform(isMacOS: true),
);
final int exitCode = await project.runCommand(
'foo',
arguments: <String>['--bar', '--baz'],
);
expect(exitCode, 0);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall(
plugin.childDirectory('android').childFile('gradlew').path,
const <String>[
'foo',
'--bar',
'--baz',
],
plugin.childDirectory('android').path),
]));
});
test('runs with the correct wrapper on Windows', () async {
final Directory plugin = createFakePlugin(
'plugin', fileSystem.directory('/'),
extraFiles: <String>['android/gradlew.bat']);
final GradleProject project = GradleProject(
plugin,
processRunner: processRunner,
platform: MockPlatform(isWindows: true),
);
final int exitCode = await project.runCommand('foo');
expect(exitCode, 0);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall(
plugin.childDirectory('android').childFile('gradlew.bat').path,
const <String>[
'foo',
],
plugin.childDirectory('android').path),
]));
});
test('returns error codes', () async {
final Directory plugin = createFakePlugin(
'plugin', fileSystem.directory('/'),
extraFiles: <String>['android/gradlew.bat']);
final GradleProject project = GradleProject(
plugin,
processRunner: processRunner,
platform: MockPlatform(isWindows: true),
);
processRunner.mockProcessesForExecutable[project.gradleWrapper.path] =
<io.Process>[
MockProcess(exitCode: 1),
];
final int exitCode = await project.runCommand('foo');
expect(exitCode, 1);
});
});
}