blob: 155883e52f32e044506f350c64ef7f9bcf5ede2a [file] [log] [blame]
// Copyright 2024, the Dart project authors. Please see the AUTHORS file
// for details. 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';
import 'package:deps/command.dart';
import 'package:test/test.dart';
/// Overrides [Command.run] for a function [body] to return a fixed [result].
///
/// Checks that [Command.executable] matches the [expected] executable.
///
/// Returns the return value of the function [body].
R mockedRun<R>(ProcessResult result, String expected,
List<String>? expectedArguments, R Function() body) {
return overrideRun((executable, arguments, {runInShell = false}) async {
expect(executable, expected);
if (expectedArguments != null) {
expect(arguments, expectedArguments);
}
return result;
}, body);
}
void main() {
test('echo Hello World!', () async {
final result =
await Command('echo', ['Hello', 'World!'], runInShell: true).run();
expect(result.stdout, 'Hello World!\n');
expect(result.exitCode, 0);
expect(result.stderr, '');
});
test('mocked run', () async {
final expectedResult = ProcessResult(123, 567, 'Override!', 'Error!');
final result = await mockedRun(expectedResult, 'echo', ['Hello', 'World!'],
() => Command('echo', ['Hello', 'World!'], runInShell: true).run());
expect(result, expectedResult);
});
test('mocked run wrong executable', () {
final expectedResult = ProcessResult(123, 567, null, null);
final future = mockedRun(
expectedResult, 'right', null, () => Command('wrong', []).run());
expect(future, throwsA(isA<TestFailure>()));
});
test('mocked run wrong arguments', () {
final expectedResult = ProcessResult(123, 567, null, null);
final future = mockedRun(
expectedResult, 'right', ['wrong'], () => Command('right', []).run());
expect(future, throwsA(isA<TestFailure>()));
});
}