#[derive(Debug, Default)]
pub struct CargoCommand {
features: Option<Vec<String>>,
all_features: bool,
no_default_features: bool,
target: Option<String>,
}
impl CargoCommand {
pub fn features(mut self, features: Option<Vec<String>>) -> Self {
self.features = features;
self
}
pub fn all_features(mut self, value: bool) -> Self {
self.all_features = value;
self
}
pub fn no_default_features(mut self, value: bool) -> Self {
self.no_default_features = value;
self
}
pub fn target(mut self, target: Option<impl ToString>) -> Self {
self.target = target.map(|t| t.to_string());
self
}
pub fn into_args(self) -> Vec<String> {
let mut args = Vec::<String>::with_capacity(8);
args.extend_from_slice(&["cargo".to_string(), "check".to_string()]);
if let Some(features) = self.features {
let features = features.join(",");
args.extend_from_slice(&["--features".to_string(), features]);
}
if self.all_features {
args.push("--all-features".to_string());
}
if self.no_default_features {
args.push("--no-default-features".to_string());
}
if let Some(target) = self.target {
args.push("--target".to_string());
args.push(target);
}
args
}
}
#[cfg(test)]
mod tests {
use crate::external_command::cargo_command::CargoCommand;
#[test]
fn set_features_none() {
let cargo_command = CargoCommand::default();
let cargo_command = cargo_command.features(None);
assert_eq!(
cargo_command.into_args().join(" "),
"cargo check".to_string()
);
}
#[test]
fn set_features_one() {
let cargo_command = CargoCommand::default();
let cargo_command = cargo_command.features(Some(vec!["pika".to_string()]));
assert_eq!(
cargo_command.into_args().join(" "),
"cargo check --features pika".to_string()
);
}
#[test]
fn set_features_two() {
let cargo_command = CargoCommand::default();
let cargo_command =
cargo_command.features(Some(vec!["chu".to_string(), "chris".to_string()]));
assert_eq!(
cargo_command.into_args().join(" "),
"cargo check --features chu,chris".to_string()
);
}
#[test]
fn set_no_default_features() {
let cargo_command = CargoCommand::default();
let cargo_command = cargo_command.no_default_features(true);
assert_eq!(
cargo_command.into_args().join(" "),
"cargo check --no-default-features".to_string()
);
}
#[test]
fn set_all_features() {
let cargo_command = CargoCommand::default();
let cargo_command = cargo_command.all_features(true);
assert_eq!(
cargo_command.into_args().join(" "),
"cargo check --all-features".to_string()
);
}
#[test]
fn set_target_none() {
let cargo_command = CargoCommand::default();
let cargo_command = cargo_command.target(None::<String>);
assert_eq!(
cargo_command.into_args().join(" "),
"cargo check".to_string()
);
}
#[test]
fn set_target_some() {
let cargo_command = CargoCommand::default();
let cargo_command = cargo_command.target(Some("some"));
assert_eq!(
cargo_command.into_args().join(" "),
"cargo check --target some".to_string()
);
}
#[test]
fn combination_of_everything() {
let cargo_command = CargoCommand::default();
let cargo_command = cargo_command
.features(Some(vec!["pika".to_string(), "chu".to_string()]))
.all_features(true)
.no_default_features(true)
.target(Some("pickme"));
let cmd = cargo_command.into_args().join(" ");
assert!(cmd.contains("--all-features"));
assert!(cmd.contains("--features pika,chu"));
assert!(cmd.contains("--no-default-features"));
assert!(cmd.contains("--target pickme"));
}
}