A Dart package that provides an abstract interface for managing system process execution. This package allows both direct execution of system commands and the injection of custom implementations, making it ideal for extensibility and unit testing.
- Abstract interface for executing system processes.
- Supports both
runandstartmethods for process execution. - Provides a singleton-like pattern for dependency injection.
- Default implementation delegates to Dart's
dart:ioProcessclass. - Easily mockable for testing environments.
Add the following to your pubspec.yaml:
dependencies:
process: <latest_version>Then run:
dart pub getimport 'process/process.dart';void main() async {
final result = await Process.instance.run(
'echo',
['Hello, World!'],
runInShell: true,
);
print('Exit code: ${result.exitCode}');
print('Stdout: ${result.stdout}');
}class MockProcess extends Process {
@override
Future<io.Process> start(
String executable,
List<String> arguments, {
String? workingDirectory,
bool runInShell = false,
}) async {
// Return a fake process or mock behavior
throw UnimplementedError();
}
@override
Future<io.ProcessResult> run(
String executable,
List<String> arguments, {
bool runInShell = false,
}) async {
return io.ProcessResult(0, 0, 'Mocked output', '');
}
}
void main() async {
Process.instance = MockProcess();
final result = await Process.instance.run('any', []);
print('Mocked stdout: ${result.stdout}');
}Runs a process and waits for its result.
Starts a process asynchronously and returns the Process instance.
You can easily inject a mock Process implementation for unit testing your logic without executing real commands on the host machine.
Process(abstract interface)_ProcessImpl(default implementation usingdart:io)
Contributions are welcome! Please feel free to submit a pull request or open an issue.