Crates.io | mockcmd |
lib.rs | mockcmd |
version | 0.0.1 |
source | src |
created_at | 2025-03-06 12:55:59.885325+00 |
updated_at | 2025-03-06 12:55:59.885325+00 |
description | A mockable std::process::Command |
homepage | https://github.com/vrmiguel/mockcmd |
repository | |
max_upload_size | |
id | 1581085 |
size | 20,708 |
mockcmd
An ergonomic drop-in replacement for std::process::Command
with added mocking capabilities for tests.
⚠️ Warning: This crate is still in early development. The API will change significantly in future releases.
std::process::Command
with identical APIAdd this to your Cargo.toml
:
[dependencies]
mockcmd = "*"
[dev-dependencies]
mockcmd = { version = "*", features = ["test"] }
use mockcmd::{Command, mock, was_command_executed};
// Setup a mock for the "git" command
mock("git")
.with_arg("status")
.with_stdout("On branch main\nNothing to commit")
.register();
// Use the Command just like std::process::Command
let output = Command::new("git").arg("status").output().unwrap();
// The mock is used instead of executing the real command
assert_eq!(String::from_utf8_lossy(&output.stdout), "On branch main\nNothing to commit");
// Verify that the command was executed with the correct arguments
assert!(was_command_executed(&["git", "status"]));
assert!(!was_command_executed(&["git", "push"]));
The library uses conditional compilation to provide different implementations:
#[cfg(feature = "test")]
), commands are intercepted and mocked responses are returnedThis means your production code can use the same Command
interface without any behavior changes or performance impact.
Mocks are defined using a builder pattern, which allows for a fluent API:
use mockcmd::mock;
// Create a simple mock
mock("program")
.with_arg("arg1")
.with_arg("arg2")
.with_stdout("Success output")
.with_stderr("Error message")
.with_status(0) // Exit code
.register();
Migration is as simple as changing your import statement:
- use std::process::Command;
+ use mockcmd::Command;
Your existing code will continue to work exactly as before, but now you can add mocks in your tests.