.. | ||
example | ||
lib | ||
test | ||
pubspec.lock | ||
pubspec.yaml | ||
README.md |
Test Process
A Laravel-compatible process management implementation in pure Dart. This package provides a robust way to execute and manage system processes with features like timeouts, input/output handling, and asynchronous execution.
Features
- 💫 Fluent API for process configuration
- ⏱️ Process timeout support
- 🔄 Asynchronous process execution
- 📥 Input/output handling
- 🌍 Environment variables support
- 📁 Working directory configuration
- 🚦 TTY mode support
- 🤫 Quiet mode for suppressing output
- ⚡ Process pooling capabilities
Installation
Add this to your package's pubspec.yaml
file:
dependencies:
test_process: ^1.0.0
Usage
Basic Command Execution
import 'package:test_process/test_process.dart';
void main() async {
final factory = Factory();
// Simple command execution
final result = await factory.run('echo "Hello World"');
print(result.output());
}
Configuring Process Execution
final result = await factory
.command('ls -la')
.withWorkingDirectory('/tmp')
.withEnvironment({'CUSTOM_VAR': 'value'})
.withTimeout(30)
.run();
print('Exit code: ${result.exitCode}');
print('Output: ${result.output()}');
print('Error output: ${result.errorOutput()}');
Asynchronous Process Execution
final process = await factory
.command('long-running-command')
.withTimeout(60)
.start();
print('Process started with PID: ${process.pid}');
// Wait for completion
final result = await process.wait();
print('Process completed with exit code: ${result.exitCode}');
Process Input/Output
// Provide input to process
final result = await factory
.command('cat')
.withInput('Hello from stdin!')
.run();
// Disable output
await factory
.command('noisy-command')
.withoutOutput()
.run();
Error Handling
try {
await factory.run('nonexistent-command');
} on ProcessFailedException catch (e) {
print('Process failed with exit code: ${e.exitCode}');
print('Error output: ${e.errorOutput}');
} on ProcessTimedOutException catch (e) {
print('Process timed out: ${e.message}');
}
API Reference
Factory
The main entry point for creating and running processes.
run(command)
- Run a command synchronouslycommand(command)
- Begin configuring a commandpath(directory)
- Begin configuring a command with a working directory
PendingProcess
Configures how a process should be run.
withCommand(command)
- Set the command to runwithWorkingDirectory(directory)
- Set the working directorywithTimeout(seconds)
- Set the process timeoutwithIdleTimeout(seconds)
- Set the idle timeoutwithEnvironment(env)
- Set environment variableswithInput(input)
- Provide input to the processwithoutOutput()
- Disable process outputwithTty()
- Enable TTY modeforever()
- Disable timeoutrun()
- Run the processstart()
- Start the process asynchronously
ProcessResult
Represents the result of a completed process.
exitCode
- The process exit codeoutput()
- The process standard outputerrorOutput()
- The process error outputsuccessful()
- Whether the process was successfulfailed()
- Whether the process failed
InvokedProcess
Represents a running process.
pid
- The process IDwrite(input)
- Write to the process stdinkill([signal])
- Send a signal to the processwait()
- Wait for the process to complete
Error Handling
The package provides two main exception types:
ProcessFailedException
- Thrown when a process exits with a non-zero codeProcessTimedOutException
- Thrown when a process exceeds its timeout
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This package is open-sourced software licensed under the MIT license.