#![allow(clippy::missing_panics_doc)]
use std::path::Path;
use kawauso_process::Invocation;
use kawauso_process::invocation::Argument;
fn variables(invocation: &Invocation) -> Vec<String> {
invocation
.environment()
.iter()
.map(|variable| format!("{}={}", variable.name(), variable.value()))
.collect()
}
#[test]
fn arg_with_a_metacharacter_keeps_the_argument() {
let invocation = Invocation::new("ls").arg("*.rs");
assert_eq!(invocation.arguments()[0].get(), "*.rs");
}
#[test]
fn arg_with_a_space_keeps_one_argument() {
let invocation = Invocation::new("git").arg("--message=two words");
assert_eq!(invocation.arguments().len(), 1);
}
#[test]
fn arg_with_two_arguments_keeps_the_order_of_the_calls() {
let invocation = Invocation::new("git").arg("status").arg("--short");
let arguments: Vec<_> = invocation.arguments().iter().map(Argument::get).collect();
assert_eq!(arguments, vec!["status", "--short"]);
}
#[test]
fn args_with_a_collection_appends_every_argument() {
let invocation = Invocation::new("cargo")
.arg("build")
.args(["--release", "--locked"]);
assert_eq!(invocation.arguments().len(), 3);
}
#[test]
fn display_with_a_program_that_holds_a_space_marks_it() {
let invocation = Invocation::new("/opt/my tool/bin/tool");
assert_eq!(invocation.to_string(), "\"/opt/my tool/bin/tool\"");
}
#[test]
fn display_with_a_variable_shows_it_in_front_of_the_program() {
let invocation = Invocation::new("cargo")
.arg("build")
.env("RUSTUP_TOOLCHAIN", "nightly");
assert_eq!(
invocation.to_string(),
"RUSTUP_TOOLCHAIN=nightly cargo build"
);
}
#[test]
fn display_with_a_variable_whose_value_holds_a_space_marks_the_value() {
let invocation = Invocation::new("cargo")
.arg("build")
.env("RUSTFLAGS", "-D warnings");
assert_eq!(
invocation.to_string(),
"RUSTFLAGS=\"-D warnings\" cargo build"
);
}
#[test]
fn display_with_a_variable_whose_value_is_empty_marks_the_value() {
let invocation = Invocation::new("cargo").arg("build").env("NO_COLOR", "");
assert_eq!(invocation.to_string(), "NO_COLOR=\"\" cargo build");
}
#[test]
fn display_with_an_argument_that_holds_a_space_marks_it() {
let invocation = Invocation::new("sh").arg("-c").arg("echo hello world");
assert_eq!(invocation.to_string(), "sh -c \"echo hello world\"");
}
#[test]
fn display_with_arguments_names_the_program_and_every_argument() {
let invocation = Invocation::new("git").arg("status").arg("--short");
assert_eq!(invocation.to_string(), "git status --short");
}
#[test]
fn display_with_two_variables_keeps_the_order_of_the_calls() {
let invocation = Invocation::new("cargo")
.env("RUSTUP_TOOLCHAIN", "nightly")
.env("CARGO_TERM_COLOR", "always");
assert_eq!(
invocation.to_string(),
"RUSTUP_TOOLCHAIN=nightly CARGO_TERM_COLOR=always cargo"
);
}
#[test]
fn env_with_a_variable_keeps_it() {
let invocation = Invocation::new("cargo").env("RUSTUP_TOOLCHAIN", "nightly");
assert_eq!(variables(&invocation), vec!["RUSTUP_TOOLCHAIN=nightly"]);
}
#[test]
fn env_with_the_name_of_an_earlier_variable_keeps_the_place() {
let invocation = Invocation::new("cargo")
.env("RUSTUP_TOOLCHAIN", "stable")
.env("CARGO_TERM_COLOR", "always")
.env("RUSTUP_TOOLCHAIN", "nightly");
let names: Vec<_> = invocation
.environment()
.iter()
.map(|variable| variable.name().get())
.collect();
assert_eq!(names, vec!["RUSTUP_TOOLCHAIN", "CARGO_TERM_COLOR"]);
}
#[test]
fn env_with_the_name_of_an_earlier_variable_replaces_the_value() {
let invocation = Invocation::new("cargo")
.env("RUSTUP_TOOLCHAIN", "stable")
.env("RUSTUP_TOOLCHAIN", "nightly");
assert_eq!(variables(&invocation), vec!["RUSTUP_TOOLCHAIN=nightly"]);
}
#[test]
fn env_with_two_variables_keeps_the_order_of_the_calls() {
let invocation = Invocation::new("cargo")
.env("RUSTUP_TOOLCHAIN", "nightly")
.env("CARGO_TERM_COLOR", "always");
assert_eq!(
variables(&invocation),
vec!["RUSTUP_TOOLCHAIN=nightly", "CARGO_TERM_COLOR=always"]
);
}
#[test]
fn envs_with_a_collection_sets_every_variable() {
let invocation = Invocation::new("cargo")
.env("RUSTUP_TOOLCHAIN", "nightly")
.envs([("CARGO_TERM_COLOR", "always"), ("NO_COLOR", "")]);
assert_eq!(invocation.environment().len(), 3);
}
#[test]
fn envs_with_the_name_of_an_earlier_variable_replaces_the_value() {
let invocation = Invocation::new("cargo").envs([
("RUSTUP_TOOLCHAIN", "stable"),
("RUSTUP_TOOLCHAIN", "nightly"),
]);
assert_eq!(variables(&invocation), vec!["RUSTUP_TOOLCHAIN=nightly"]);
}
#[test]
fn in_directory_with_a_directory_keeps_it() {
let invocation = Invocation::new("cargo").in_directory("crates/example");
assert_eq!(
invocation
.working_directory()
.map(|directory| directory.get()),
Some(Path::new("crates/example"))
);
}
#[test]
fn new_with_a_program_keeps_the_program() {
let invocation = Invocation::new("/usr/bin/git");
assert_eq!(invocation.program().get(), Path::new("/usr/bin/git"));
}
#[test]
fn new_without_a_directory_reports_no_directory() {
let invocation = Invocation::new("git").arg("status");
assert!(invocation.working_directory().is_none());
}