Skip to content

Commit

Permalink
test: add cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamadorueda committed Dec 9, 2024
1 parent cdc093a commit 6db8876
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
/**/target/
/.direnv
/.idea/
/front/build
/front/worktree
/tarpaulin*
37 changes: 18 additions & 19 deletions src/alejandra/tests/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;

use alejandra::config::Config;
use alejandra::config::Indentation;
Expand All @@ -11,28 +12,28 @@ fn cases() {

let configs = HashMap::from([
("default", Config::default()),
("indentation-tabs", Config {
indentation: Indentation::Tabs,
..Default::default()
}),
("indentation-tabs", Config { indentation: Indentation::Tabs }),
]);

let cases_path = PathBuf::new().join("tests").join("cases");

for (config_name, config) in configs {
let cases: Vec<String> =
std::fs::read_dir(format!("tests/cases/{}", config_name))
.unwrap()
.map(|entry| entry.unwrap().file_name().into_string().unwrap())
.collect();
let config_cases_path = cases_path.join(config_name);

let cases: Vec<String> = std::fs::read_dir(&config_cases_path)
.unwrap()
.map(|entry| entry.unwrap().file_name().into_string().unwrap())
.collect();

for case in cases {
let path_in =
format!("tests/cases/{}/{}/in.nix", config_name, case);
let case_path = config_cases_path.join(&case);

let path_in = case_path.join("in.nix");
let content_in = std::fs::read_to_string(&path_in).unwrap();

let path_out =
format!("tests/cases/{}/{}/out.nix", config_name, case);
let path_out = case_path.join("out.nix");
let content_got = alejandra::format::in_memory(
path_in.clone(),
path_in.to_str().unwrap().to_owned(),
content_in.clone(),
config,
)
Expand All @@ -45,14 +46,12 @@ fn cases() {
.unwrap();
}

let content_out =
std::fs::read_to_string(path_out.clone()).unwrap();
let content_out = std::fs::read_to_string(&path_out).unwrap();

assert_eq!(
content_out, content_got,
"Test case `{}/{}` failed; see \
`src/alejandra/tests/cases/{}/{}/`",
config_name, case, config_name, case,
"Test case `{:?}` failed",
case_path
);
}
}
Expand Down
167 changes: 167 additions & 0 deletions src/alejandra_cli/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
use std::fmt::Write as _;
use std::io::Write as _;
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;

#[derive(Debug)]
struct TestCase {
args: &'static [&'static str],
stdin: Option<&'static str>,
}

const CASES: &[TestCase] = &[
TestCase { args: &["--help"], stdin: None },
TestCase { args: &["--version"], stdin: None },
TestCase { args: &[], stdin: None },
TestCase { args: &["--quiet"], stdin: Some("[]") },
TestCase { args: &["--quiet", "--quiet"], stdin: Some("[]") },
TestCase { args: &["--check", "--quiet"], stdin: Some("[]\n") },
TestCase { args: &["--check", "--quiet"], stdin: Some("[\t]") },
TestCase { args: &["--check", "--quiet", "--quiet"], stdin: Some("[]\n") },
TestCase { args: &["--check", "--quiet", "--quiet"], stdin: Some("[\t]") },
TestCase { args: &["--quiet"], stdin: Some("[") },
TestCase { args: &["--quiet", "--quiet"], stdin: Some("[") },
TestCase { args: &[".", "--exclude", ".", "--quiet"], stdin: None },
TestCase {
args: &["--exclude", ".", "--quiet", "--quiet", "--", "."],
stdin: None,
},
TestCase {
args: &["--check", "tests/inputs/changed.nix", "--quiet"],
stdin: None,
},
TestCase {
args: &[
"-c",
"tests/inputs/changed.nix",
"-q",
"-e",
"tests/changed.nix",
],
stdin: None,
},
TestCase {
args: &["--check", "tests/inputs/changed.nix", "-qq"],
stdin: None,
},
TestCase {
args: &["--check", "tests/inputs/unchanged.nix", "-q"],
stdin: None,
},
TestCase {
args: &["--check", "tests/inputs/unchanged.nix", "-qq"],
stdin: None,
},
TestCase {
args: &["--check", "tests/inputs/error.nix", "-q"],
stdin: None,
},
TestCase {
args: &["--check", "tests/inputs/error.nix", "-qq"],
stdin: None,
},
TestCase {
args: &[
"--check",
"tests/inputs/unchanged.nix",
"--experimental-config",
"../../alejandra.toml",
"--threads",
"1",
],
stdin: None,
},
TestCase {
args: &[
"--check",
"tests/inputs/unchanged.nix",
"--experimental-config",
"tests/configs/empty_config.toml",
"-t",
"1",
],
stdin: None,
},
TestCase {
args: &[
"--check",
"tests/inputs/unchanged.nix",
"--experimental-config",
"tests/configs/wrong_key.toml",
],
stdin: None,
},
];

#[test]
fn cases() {
let should_update = std::env::var("UPDATE").is_ok();

let output_path = PathBuf::new().join("tests").join("output.txt");

let mut output_got = String::new();

for case in CASES {
output_got.push_str("===\n");
output_got.push_str(&format!("args: {:?}\n", case.args));

let mut child = Command::new("cargo")
.args(["run", "--quiet", "--"])
.args(case.args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("command failed");

if let Some(stdin) = case.stdin {
output_got.push_str(&format!("stdin: {:?}\n", stdin));

child
.stdin
.take()
.unwrap()
.write_all(stdin.as_bytes())
.expect("unable to write to child stdin");
}

let output = child.wait_with_output().expect("child command failed");

let stdout = String::from_utf8(output.stdout).expect("invalid utf-8");
if !stdout.is_empty() {
output_got
.push_str(&format!("stdout:\n{}\n", indent_and_clean(&stdout)));
}

let stderr = String::from_utf8(output.stderr).expect("invalid utf-8");
if !stderr.is_empty() {
output_got
.push_str(&format!("stderr:\n{}\n", indent_and_clean(&stderr)));
}

output_got
.push_str(&format!("exit code: {:?}\n", output.status.code()));
}

if should_update {
std::fs::File::create(&output_path)
.unwrap()
.write_all(output_got.as_bytes())
.unwrap();
}

let output_expected = std::fs::read_to_string(&output_path).unwrap();

assert_eq!(output_expected, output_got);
}

fn indent_and_clean(data: &str) -> String {
data.lines().filter(|line| !line.starts_with(['👏', '🤟', '⭐'])).fold(
String::new(),
|mut output, line| {
let _ = writeln!(output, " {}", line);
output
},
)
}
Empty file.
1 change: 1 addition & 0 deletions src/alejandra_cli/tests/configs/wrong_key.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdf = "asdf"
1 change: 1 addition & 0 deletions src/alejandra_cli/tests/inputs/changed.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions src/alejandra_cli/tests/inputs/error.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[
1 change: 1 addition & 0 deletions src/alejandra_cli/tests/inputs/unchanged.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading

0 comments on commit 6db8876

Please sign in to comment.