system_command_runner 0.1.0
system_command_runner: ^0.1.0 copied to clipboard
A lightweight, testable wrapper around Process.run for executing system commands with cross-platform support and easy mocking.
System Command Runner #
A testable wrapper around Process.run for executing system commands.
Why? #
Process.run is hard to mock. This package provides a simple, injectable wrapper that makes your code testable.
Installation #
dependencies:
system_command_runner: ^0.1.0
Usage #
import 'package:system_command_runner/system_command_runner.dart';
final runner = const SystemCommandRunner();
// Basic command
final result = await runner.run('echo', ['Hello, World!']);
print(result.stdout); // Hello, World!
// With working directory and environment
final result2 = await runner.run(
'git',
['status'],
workingDirectory: '/path/to/repo',
environment: {'GIT_CONFIG': '/custom/config'},
);
Testing #
class MockSystemCommandRunner extends Mock implements SystemCommandRunner {}
// In your test
when(() => mockRunner.run('git', ['status'])).thenAnswer(
(_) async => CommandResult(exitCode: 0, stdout: 'clean', stderr: ''),
);
API #
SystemCommandRunner #
run(String executable, List<String> arguments, {String? workingDirectory, Map<String, String>? environment})
CommandResult #
exitCode- Exit codestdout- Standard outputstderr- Standard error