use std::path::PathBuf;
use assert_cmd::Command;
use insta::assert_snapshot;
use predicates::prelude::*;
use pretty_assertions::assert_eq;
use tempfile::TempDir;
const BAD_PATH: &str = "libtlafmt/tests/corpus/differential_equations.tla";
const GOOD_PATH: &str = "libtlafmt/tests/snapshots/format__corpus@differential_equations.tla.snap";
fn cmd() -> Command {
Command::cargo_bin("tlafmt").unwrap()
}
fn dir() -> TempDir {
tempfile::Builder::new()
.prefix(".tlafmt-test")
.tempdir_in("./")
.expect("cannot create tempdir for test state")
}
fn format(path: &str) -> String {
String::from_utf8(
cmd()
.arg(path)
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
}
#[test]
fn test_help_text() {
let stdout = String::from_utf8(
cmd()
.arg("--help")
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap();
assert_snapshot!(stdout);
}
#[test]
fn test_check_mode() {
assert_snapshot!(String::from_utf8(
cmd()
.arg("--check")
.arg(BAD_PATH)
.assert()
.failure()
.stdout(predicate::eq(""))
.code(predicate::eq(3))
.get_output()
.stderr
.clone(),
)
.unwrap());
cmd()
.arg("--check")
.arg(GOOD_PATH)
.assert()
.success()
.stdout(predicate::eq(""))
.stderr(predicate::eq(""))
.code(predicate::eq(0));
}
#[test]
fn test_in_place() {
let wd = dir();
let mut file = PathBuf::from(wd.path());
file.push("test.rs");
std::fs::copy(BAD_PATH, &file).expect("cannot copy file for test");
cmd()
.arg("--in-place")
.arg(file.to_str().unwrap())
.assert()
.success()
.stdout(predicate::eq(""))
.stderr(predicate::eq(""))
.code(predicate::eq(0));
let control = format(BAD_PATH);
let got = std::fs::read_to_string(file).unwrap();
assert_eq!(control, got);
}
#[test]
fn test_in_place_conflicts_check() {
let unformatted = std::fs::read_to_string(BAD_PATH).unwrap();
cmd()
.arg("--in-place")
.arg("--check")
.arg(BAD_PATH)
.write_stdin(unformatted)
.assert()
.failure()
.stdout(predicate::eq(""))
.stderr(predicate::eq(
"\
error: the argument '--in-place' cannot be used with '--check'
Usage: tlafmt --in-place <FILE>
For more information, try '--help'.
",
))
.code(predicate::eq(2));
}
#[test]
fn test_from_stdin() {
let unformatted = std::fs::read_to_string(BAD_PATH).unwrap();
let control = format(BAD_PATH);
cmd()
.arg("--stdin")
.write_stdin(unformatted.clone())
.assert()
.success()
.stdout(predicate::eq(control.clone()))
.stderr(predicate::eq(""))
.code(predicate::eq(0));
cmd() .arg("--stdin")
.arg("--check")
.write_stdin(unformatted)
.assert()
.failure()
.stdout(predicate::eq(""))
.code(predicate::eq(3));
cmd() .arg("--stdin")
.arg("--check")
.write_stdin(control)
.assert()
.success()
.stdout(predicate::eq(""))
.stderr(predicate::eq(""))
.code(predicate::eq(0));
}
#[test]
fn test_from_stdin_conflicts_path() {
let unformatted = std::fs::read_to_string(BAD_PATH).unwrap();
cmd()
.arg("--stdin")
.arg(BAD_PATH)
.write_stdin(unformatted)
.assert()
.failure()
.stdout(predicate::eq(""))
.stderr(predicate::eq(
"\
error: the argument '--stdin' cannot be used with '[FILE]'
Usage: tlafmt --stdin [FILE]
For more information, try '--help'.
",
))
.code(predicate::eq(2));
}
#[test]
fn test_from_stdin_conflicts_in_place() {
let unformatted = std::fs::read_to_string(BAD_PATH).unwrap();
cmd()
.arg("--stdin")
.arg("--in-place")
.write_stdin(unformatted)
.assert()
.failure()
.stdout(predicate::eq(""))
.stderr(predicate::eq(
"\
error: the argument '--stdin' cannot be used with '--in-place'
Usage: tlafmt --stdin [FILE]
For more information, try '--help'.
",
))
.code(predicate::eq(2));
}