use std::fs;
use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::tempdir;
fn run_in(dir: &std::path::Path, args: &[&str]) -> assert_cmd::assert::Assert {
Command::cargo_bin("pgsafe")
.unwrap()
.current_dir(dir)
.args(args)
.assert()
}
#[test]
fn since_lints_only_files_after_the_cutoff() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("0001_legacy.sql"), "DROP TABLE a;\n").unwrap();
fs::write(dir.path().join("0002_cut.sql"), "DROP TABLE b;\n").unwrap();
fs::write(
dir.path().join("0003_new.sql"),
"CREATE INDEX i ON t (x);\n",
)
.unwrap();
run_in(
dir.path(),
&[
"--since",
"0002_cut.sql",
"0001_legacy.sql",
"0002_cut.sql",
"0003_new.sql",
],
)
.stdout(
predicate::str::contains("drop-table")
.not()
.and(predicate::str::contains("add-index-non-concurrent")),
);
}
#[test]
fn config_since_is_honored_and_cli_overrides_it() {
let dir = tempdir().unwrap();
fs::write(
dir.path().join(".pgsafe.toml"),
"since = \"0002_cut.sql\"\n",
)
.unwrap();
fs::write(dir.path().join("0001_legacy.sql"), "DROP TABLE a;\n").unwrap();
fs::write(dir.path().join("0002_cut.sql"), "DROP TABLE b;\n").unwrap();
fs::write(dir.path().join("0003_new.sql"), "DROP TABLE c;\n").unwrap();
run_in(
dir.path(),
&["0001_legacy.sql", "0002_cut.sql", "0003_new.sql"],
)
.stdout(
predicate::str::contains("0003_new.sql")
.and(predicate::str::contains("0001_legacy.sql").not()),
);
run_in(
dir.path(),
&[
"--since",
"0001_legacy.sql",
"0001_legacy.sql",
"0002_cut.sql",
"0003_new.sql",
],
)
.stdout(
predicate::str::contains("0002_cut.sql")
.and(predicate::str::contains("0003_new.sql"))
.and(predicate::str::contains("0001_legacy.sql").not()),
);
}
#[test]
fn since_and_git_diff_are_mutually_exclusive() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("m.sql"), "DROP TABLE a;\n").unwrap();
run_in(
dir.path(),
&["--since", "0001.sql", "--git-diff", "HEAD", "m.sql"],
)
.failure()
.code(2); }
#[test]
fn since_rejects_stdin() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("m.sql"), "DROP TABLE a;\n").unwrap();
run_in(dir.path(), &["--since", "0001.sql", "m.sql", "-"])
.failure()
.code(2)
.stderr(predicate::str::contains("stdin"));
fs::write(dir.path().join(".pgsafe.toml"), "since = \"0001.sql\"\n").unwrap();
run_in(dir.path(), &["m.sql", "-"])
.failure()
.code(2)
.stderr(predicate::str::contains("since"));
}