diff --git a/Cargo.lock b/Cargo.lock index 4a689d9..4f5f5b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,7 @@ dependencies = [ "clap", "futures", "num_cpus", + "pretty_assertions", "rand", "toml", "walkdir", diff --git a/src/alejandra_cli/Cargo.toml b/src/alejandra_cli/Cargo.toml index e683fd6..df93e8d 100644 --- a/src/alejandra_cli/Cargo.toml +++ b/src/alejandra_cli/Cargo.toml @@ -30,3 +30,6 @@ license = "Unlicense" name = "alejandra_cli" repository = "https://github.com/kamadorueda/alejandra" version = "3.1.0" + +[dev-dependencies] +pretty_assertions = "1.3.0" diff --git a/src/alejandra_cli/src/cli.rs b/src/alejandra_cli/src/cli.rs index 040a0a0..dee76ac 100644 --- a/src/alejandra_cli/src/cli.rs +++ b/src/alejandra_cli/src/cli.rs @@ -1,3 +1,4 @@ +use std::env; use std::fs::read_to_string; use std::io::Read; @@ -145,8 +146,15 @@ pub fn main() -> ! { let include: Vec<&str> = args.include.iter().map(String::as_str).collect::>(); - let threads = - args.threads.map_or_else(num_cpus::get_physical, Into::::into); + // Try CLI value, then env var, then fall back to number of physical CPUs. + let threads: usize = if let Some(cli_threads) = args.threads { + cli_threads.into() // convert u8 to usize + } else { + env::var("ALEJANDRA_THREADS") + .ok() + .and_then(|v| v.parse::().ok()) + .unwrap_or_else(num_cpus::get_physical) + }; let verbosity = match args.quiet { 0 => Verbosity::Everything, diff --git a/src/alejandra_cli/tests/cli.rs b/src/alejandra_cli/tests/cli.rs index 9de6513..3af89c8 100644 --- a/src/alejandra_cli/tests/cli.rs +++ b/src/alejandra_cli/tests/cli.rs @@ -4,6 +4,8 @@ use std::path::PathBuf; use std::process::Command; use std::process::Stdio; +use pretty_assertions::assert_eq; + #[derive(Debug)] struct TestCase { args: &'static [&'static str], @@ -126,6 +128,7 @@ fn cases() { output_got.push_str(&format!("args: {:?}\n", case.args)); let mut child = Command::new("cargo") + .env("ALEJANDRA_THREADS", "4") .args(["run", "--quiet", "--"]) .args(case.args) .stdin(Stdio::piped())