Files
packages/script/tool/test/mocks.dart
stuartmorgan 77460f03f2 [flutter_plugin_tools] Make unit tests pass on Windows (#4149)
The purpose of this PR is to make running all unit tests on Windows pass (vs failing a large portion of the tests as currently happens). This does not mean that the commands actually work when run on Windows, or that Windows support is tested, only that it's possible to actually run the tests themselves. This is prep for actually supporting parts of the tool on Windows in future PRs.

Major changes:
- Make the tests significantly more hermetic:
  - Make almost all tools take a `Platform` constructor argument that can be used to inject a mock platform to control what OS the command acts like it is running on under test.
  - Add a path `Context` object to the base command, whose style matches the `Platform`, and use that almost everywhere instead of the top-level `path` functions.
  - In cases where Posix behavior is always required (such as parsing `git` output), explicitly use the `posix` context object for `path` functions.
- Start laying the groundwork for actual Windows support:
  - Replace all uses of `flutter` as a command with a getter that returns `flutter` or `flutter.bat` as appropriate.
  - For user messages that include relative paths, use a helper that always uses Posix-style relative paths for consistent output.

This bumps the version since quite a few changes have built up, and having a cut point before starting to make more changes to the commands to support Windows seems like a good idea.

Part of https://github.com/flutter/flutter/issues/86113
2021-07-09 19:38:13 -04:00

76 lines
1.7 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:async';
import 'dart:io' as io;
import 'package:file/file.dart';
import 'package:mockito/mockito.dart';
import 'package:platform/platform.dart';
class MockPlatform extends Mock implements Platform {
MockPlatform({
this.isLinux = false,
this.isMacOS = false,
this.isWindows = false,
});
@override
bool isLinux;
@override
bool isMacOS;
@override
bool isWindows;
@override
Uri get script => isWindows
? Uri.file(r'C:\foo\bar', windows: true)
: Uri.file('/foo/bar', windows: false);
}
class MockProcess extends Mock implements io.Process {
MockProcess();
/// A mock process that terminates with exitCode 0.
MockProcess.succeeding() {
exitCodeCompleter.complete(0);
}
/// A mock process that terminates with exitCode 1.
MockProcess.failing() {
exitCodeCompleter.complete(1);
}
final Completer<int> exitCodeCompleter = Completer<int>();
final StreamController<List<int>> stdoutController =
StreamController<List<int>>();
final StreamController<List<int>> stderrController =
StreamController<List<int>>();
final MockIOSink stdinMock = MockIOSink();
@override
int get pid => 99;
@override
Future<int> get exitCode => exitCodeCompleter.future;
@override
Stream<List<int>> get stdout => stdoutController.stream;
@override
Stream<List<int>> get stderr => stderrController.stream;
@override
IOSink get stdin => stdinMock;
}
class MockIOSink extends Mock implements IOSink {
List<String> lines = <String>[];
@override
void writeln([Object? obj = '']) => lines.add(obj.toString());
}