use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
fn started_by_cargo() -> bool {
if std::env::var_os("CARGO_MANIFEST_DIR").is_none() {
eprintln!("skipped: cargo did not start this test");
return false;
}
true
}
fn workspace() -> PathBuf {
let folder = Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join("..");
nibkeep_node::resolved_path(&folder).expect("the workspace's folder resolves")
}
#[test]
fn cargo_gives_a_ceiling_at_the_workspace_and_no_default_library() {
if !started_by_cargo() {
return;
}
let ceiling = std::env::var_os("NIBKEEP_CEILING").expect("cargo sets NIBKEEP_CEILING");
let ceiling = PathBuf::from(ceiling);
assert!(
ceiling.is_absolute(),
"{} is not a full path",
ceiling.display()
);
assert_eq!(
nibkeep_node::resolved_path(&ceiling).expect("the ceiling resolves"),
workspace()
);
assert_eq!(
std::env::var_os("NIBKEEP_LIBRARY").as_deref(),
Some(std::ffi::OsStr::new(""))
);
}
fn nib(test: &Path, here: &Path, ceiling: Option<&Path>, arguments: &[&str]) -> Output {
let mut command = Command::new(env!("CARGO_BIN_EXE_nib"));
command
.current_dir(here)
.env("NIBKEEP_NO_KEYSTORE", "1")
.env("NIBKEEP_STATE", test.with_extension("state"))
.arg("--passphrase-file")
.arg(test.with_extension("passphrase"))
.args(arguments)
.stdin(Stdio::null());
if let Some(ceiling) = ceiling {
command.env("NIBKEEP_CEILING", ceiling);
}
command.output().expect("runs")
}
#[test]
fn a_build_under_development_finds_a_scratch_library_below_its_ceiling_and_none_above() {
if !started_by_cargo() {
return;
}
let test = nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), "a scratch library");
nibkeep_testkit::write_private(&test.with_extension("passphrase"), "correct horse\n");
let started = nib(&test, &test, None, &["init", "--name", "Ada"]);
assert!(started.status.success(), "{started:?}");
let deeper = test.join("Books").join("Read");
std::fs::create_dir_all(&deeper).expect("makes the folders");
let resolved = nibkeep_node::resolved_path(&test).expect("the test's folder resolves");
if resolved.starts_with(workspace()) {
let status = nib(&test, &deeper, None, &["status"]);
assert!(status.status.success(), "{status:?}");
} else {
eprintln!(
"the target folder is outside the workspace, where cargo's ceiling counts for nothing"
);
}
let ceiling = test.join("Books");
let status = nib(&test, &deeper, Some(&ceiling), &["status"]);
assert!(!status.status.success(), "{status:?}");
assert_eq!(
String::from_utf8(status.stderr).expect("UTF-8").trim(),
"nib: this folder is not in a library; if it is a tree's folder that lost its .nib file, run nib init FOLDER in your library's folder; otherwise make a library with nib init, or give one with --library or NIBKEEP_LIBRARY"
);
}