mod common;
use std::fs;
use std::path::Path;
use std::time::{Duration, SystemTime};
use clap::Parser;
use common::{Test, library};
use nibkeep_cli::{Cli, CliError};
use nibkeep_node::NodeError;
fn write(root: &Path, path: &str, content: &str) {
let path = root.join(path);
fs::create_dir_all(path.parent().expect("a folder")).expect("makes folders");
fs::write(&path, content).expect("writes");
let hour_ago = SystemTime::now() - Duration::from_secs(3_600);
fs::File::options()
.write(true)
.open(&path)
.and_then(|file| file.set_modified(hour_ago))
.expect("ages");
}
fn ids_hidden(output: &str) -> String {
output
.split_inclusive(|c: char| c.is_whitespace() || c == ',')
.map(|token| {
let word = token.trim_end_matches(|c: char| c.is_whitespace() || c == ',');
if word.len() == 8 && word.chars().all(|c| c.is_ascii_hexdigit()) {
format!("<id>{}", &token[word.len()..])
} else {
token.to_owned()
}
})
.collect()
}
fn with_tree(name: &str) -> Test {
let mut test = library(name);
let here = test.here.clone();
write(&here, "notes.md", "# Notes\n");
write(&here, "docs/plan.md", "Plan\n");
write(&here, ".nibignore", "*.log\n");
write(&here, "build.log", "noise\n");
test.nib(&["init", "--yes", "--message", "Start"])
.expect("starts");
test
}
#[test]
fn init_in_a_library_makes_its_folder_a_tree_once_the_person_agrees() {
let mut test = library("tree init asks");
let here = test.here.clone();
write(&here, "notes.md", "# Notes\n");
write(&here, "docs/plan.md", "Plan\n");
assert!(matches!(
test.nib(&["init"]),
Err(CliError::UnconfirmedStart)
));
test.answers = Some(["n"].into());
assert!(matches!(
test.nib(&["init"]),
Err(CliError::UnconfirmedStart)
));
assert_eq!(test.questions.len(), 1);
let question = &test.questions[0];
assert!(question.contains("2 files"), "{question}");
assert!(question.contains("purge"), "{question}");
test.answers = Some(["y"].into());
let started = test.nib(&["init"]).expect("starts");
let name = here
.file_name()
.and_then(|name| name.to_str())
.expect("a name");
assert_eq!(
ids_hidden(&started),
format!(
"Started the tree {name} in {}: 2 files, committed as revision <id>\n",
here.display()
)
);
assert!(matches!(
test.nib(&["init", "--yes"]),
Err(CliError::Node(NodeError::TreeInTree(_)))
));
}
#[test]
fn init_makes_a_library_only_where_none_is_found() {
let mut test = Test::new("tree init outside");
for arguments in [
&["init", "."][..],
&["init", "--message", "Start"],
&["init", "--yes"],
] {
let refused = test.nib(arguments);
assert!(
matches!(refused, Err(CliError::NoLibraryForTree(_))),
"{arguments:?}: {refused:?}"
);
let said = refused.expect_err("refused").to_string();
assert!(said.contains("in your library's folder"), "{said}");
assert!(said.contains("nib init alone"), "{said}");
assert!(said.contains("nib init --join"), "{said}");
}
assert!(nibkeep_node::Library::replica_folder(&test.here.join(".nib")).is_err());
test.answer(&["correct horse", "correct horse"]);
let started = test.nib(&["init"]).expect("makes a library");
assert!(
started.starts_with("Ada Lovelace, on laptop, in "),
"{started}"
);
write(&test.here, "notes.md", "# Notes\n");
test.answer(&["correct horse"]);
let tree = test.nib(&["init", "--yes"]).expect("makes a tree");
assert!(tree.starts_with("Started the tree "), "{tree}");
let mut test = library("tree init folder");
let notes = test.here.join("notes");
write(
¬es, "notes.md", "# Notes
",
);
test.here =
nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), "tree init elsewhere");
let started = test
.nib(&["init", notes.to_str().expect("UTF-8"), "--yes"])
.expect("starts");
assert!(started.contains("1 file"), "{started}");
let parses = |arguments: &[&str]| {
Cli::try_parse_from(std::iter::once("nib").chain(arguments.iter().copied())).is_ok()
};
assert!(parses(&["init", "--name", "Ada", "--passphrase"]));
assert!(parses(&["init", "--passphrase", "--device", "laptop"]));
assert!(parses(&["init", "notes", "--yes", "-m", "Start"]));
assert!(!parses(&["init", "notes", "--passphrase"]));
assert!(!parses(&["init", "--device", "laptop", "--yes"]));
assert!(!parses(&["init", "--name", "Ada", "--message", "Start"]));
assert!(!parses(&["init", "--join", "37-482-177", "notes"]));
}
fn beside(name: &str) -> std::path::PathBuf {
let folder = nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), name);
write(&folder, "notes.md", "# Notes\n");
folder
}
fn named_in(folder: &Path) -> std::path::PathBuf {
let text = fs::read_to_string(folder.join(".nib")).expect("reads the file");
let named = text
.strip_prefix("library: ")
.and_then(|rest| rest.strip_suffix('\n'))
.expect("one line naming the library");
fs::canonicalize(named).expect("a folder")
}
#[test]
fn init_in_the_librarys_folder_makes_any_folder_a_tree_that_names_the_library() {
let mut test = library("tree init beside");
let library = test.here.clone();
let project = beside("tree init beside, project");
let name = project
.file_name()
.and_then(|name| name.to_str())
.expect("a name")
.to_owned();
let started = test
.nib(&[
"init",
project.to_str().expect("UTF-8"),
"--yes",
"-m",
"Start",
])
.expect("starts");
assert_eq!(
ids_hidden(&started),
format!(
"Started the tree {name} in {}: 1 file, committed as revision <id>\nWrote .nib into the tree's folder, which tells nib there which library to use\n",
project.display()
)
);
assert_eq!(
named_in(&project),
fs::canonicalize(&library).expect("a folder")
);
test.here.clone_from(&project);
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
write(&project, "docs/plan.md", "Plan\n");
test.here = project.join("docs");
assert_eq!(
test.nib(&["status"]).expect("status"),
"new docs/plan.md\n"
);
let committed = test.nib(&["commit", "-m", "Plan"]).expect("commits");
assert_eq!(
ids_hidden(&committed),
"Committed 1 change as revision <id>\nnew docs/plan.md\n"
);
let whole = test.nib(&["status", "--library-wide"]).expect("status");
assert!(whole.starts_with("Ada, on laptop, in "), "{whole}");
test.here.clone_from(&library);
let other = beside("tree init beside, other");
let shown = test.json(&["init", other.to_str().expect("UTF-8"), "--yes"]);
assert_eq!(
shown["pointer"],
other.join(".nib").display().to_string(),
"{shown}"
);
write(&library, "inside/notes.md", "# Notes\n");
let shown = test.json(&["init", "inside", "--yes"]);
assert_eq!(shown["pointer"], serde_json::Value::Null, "{shown}");
assert!(!library.join("inside/.nib").exists());
}
#[test]
fn a_nib_file_counts_only_for_a_tree_of_its_library_in_that_folder() {
let mut test = library("tree pointer checked");
let library = test.here.clone();
let project = beside("tree pointer checked, project");
test.nib(&["init", project.to_str().expect("UTF-8"), "--yes"])
.expect("starts");
let copy =
nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), "tree pointer checked, copy");
fs::copy(project.join(".nib"), copy.join(".nib")).expect("copies");
test.here.clone_from(©);
for arguments in [
&["status"][..],
&["status", "--library-wide"],
&["device", "list"],
] {
let refused = test.nib(arguments);
assert!(
matches!(refused, Err(CliError::StrayPointer { .. })),
"{arguments:?}: {refused:?}"
);
}
let bad = [
format!("library: {}\n", copy.display()),
"library: relative\n".to_owned(),
"something else\n".to_owned(),
String::new(),
];
for text in bad {
fs::write(copy.join(".nib"), &text).expect("writes");
let refused = test.nib(&["status"]);
assert!(
matches!(refused, Err(CliError::BadPointer { .. })),
"{text:?}: {refused:?}"
);
}
let inner = copy.join("inner");
fs::create_dir_all(&inner).expect("makes a folder");
fs::write(
inner.join(".nib"),
format!("library: {}\n", library.display()),
)
.expect("writes");
test.here.clone_from(&inner);
test.ceiling = Some(copy.clone());
let refused = test.nib(&["status"]);
assert!(
matches!(refused, Err(CliError::BadPointer { .. })),
"{refused:?}"
);
let said = refused.expect_err("refused").to_string();
assert!(said.contains("NIBKEEP_CEILING"), "{said}");
}
#[test]
fn init_writes_a_lost_nib_file_again() {
let mut test = library("tree pointer lost");
let library = test.here.clone();
let project = beside("tree pointer lost, project");
let folder = project.to_str().expect("UTF-8").to_owned();
test.nib(&["init", &folder, "--yes"]).expect("starts");
let name = project
.file_name()
.and_then(|name| name.to_str())
.expect("a name")
.to_owned();
fs::remove_file(project.join(".nib")).expect("deletes");
test.here.clone_from(&project);
assert!(matches!(test.nib(&["status"]), Err(CliError::NotInLibrary)));
test.here.clone_from(&library);
assert_eq!(
test.nib(&["init", &folder]).expect("writes it again"),
format!(
"Wrote .nib again into {}, the tree {name}'s folder, which tells nib there which library to use\n",
project.display()
)
);
assert_eq!(
named_in(&project),
fs::canonicalize(&library).expect("a folder")
);
fs::write(project.join(".nib"), "library: /nowhere\n").expect("writes");
test.nib(&["init", &folder]).expect("writes it again");
assert_eq!(
named_in(&project),
fs::canonicalize(&library).expect("a folder")
);
test.here.clone_from(&project);
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
fs::remove_file(project.join(".nib")).expect("deletes");
test.here.clone_from(&library);
let shown = test.json(&["init", &folder]);
assert_eq!(shown["revision"], serde_json::Value::Null, "{shown}");
assert_eq!(shown["changes"], serde_json::json!([]), "{shown}");
assert_eq!(
shown["pointer"],
project.join(".nib").display().to_string(),
"{shown}"
);
assert!(matches!(
test.nib(&["init", &folder, "--yes"]),
Err(CliError::Node(NodeError::TreeInTree(_)))
));
}
#[test]
fn init_makes_a_copy_of_a_trees_folder_a_tree_of_its_own() {
let mut test = library("tree pointer copied");
let library = test.here.clone();
let project = beside("tree pointer copied, project");
test.nib(&["init", project.to_str().expect("UTF-8"), "--yes"])
.expect("starts");
let copy = beside("tree pointer copied, copy");
fs::copy(project.join(".nib"), copy.join(".nib")).expect("copies");
let started = test
.nib(&["init", copy.to_str().expect("UTF-8"), "--yes"])
.expect("starts");
assert!(started.starts_with("Started the tree "), "{started}");
assert_eq!(
named_in(©),
fs::canonicalize(&library).expect("a folder")
);
test.here.clone_from(©);
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
let again = beside("tree pointer copied, again");
fs::copy(project.join(".nib"), again.join(".nib")).expect("copies");
test.here.clone_from(&again);
let started = test.nib(&["init", "--yes"]).expect("starts");
assert!(started.starts_with("Started the tree "), "{started}");
}
#[test]
fn a_tree_in_the_librarys_folder_keeps_no_nib_file() {
let mut test = library("tree pointer inside");
let library = test.here.clone();
let copied = library.join("copied");
let file = copied.join(".nib");
write(&library, "copied/notes.md", "# Notes\n");
fs::write(&file, "library: /nowhere\n").expect("writes");
let started = test.nib(&["init", "copied", "--yes"]).expect("starts");
assert!(
started.starts_with("Started the tree copied in "),
"{started}"
);
assert!(
started.ends_with(
"Took away .nib from the tree's folder, where nib finds the library without it\n"
),
"{started}"
);
assert!(!file.exists());
test.here.clone_from(&copied);
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
fs::write(&file, "library: /nowhere\n").expect("writes");
assert!(matches!(
test.nib(&["status"]),
Err(CliError::BadPointer { .. })
));
test.here.clone_from(&library);
assert_eq!(
test.nib(&["init", "copied"]).expect("takes it away"),
format!(
"Took away .nib from {}, the tree copied's folder, where nib finds the library without it\n",
copied.display()
)
);
assert!(!file.exists());
test.here.clone_from(&copied);
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
fs::write(&file, format!("library: {}\n", library.display())).expect("writes");
test.here.clone_from(&library);
let shown = test.json(&["init", "copied"]);
assert_eq!(shown["revision"], serde_json::Value::Null, "{shown}");
assert_eq!(shown["pointer"], serde_json::Value::Null, "{shown}");
assert_eq!(
shown["pointer_taken_away"],
file.display().to_string(),
"{shown}"
);
#[cfg(unix)]
{
let target = beside("tree pointer inside, elsewhere").join("old.nib");
fs::write(&target, "library: /nowhere\n").expect("writes");
std::os::unix::fs::symlink(&target, &file).expect("links");
test.nib(&["init", "copied"]).expect("takes it away");
assert!(fs::symlink_metadata(&file).is_err());
assert!(target.is_file());
}
assert!(matches!(
test.nib(&["init", "copied", "--yes"]),
Err(CliError::Node(NodeError::TreeInTree(_)))
));
write(&library, "kept/notes.md", "# Notes\n");
fs::create_dir(library.join("kept/.nib")).expect("makes a folder");
let shown = test.json(&["init", "kept", "--yes"]);
assert_eq!(
shown["pointer_taken_away"],
serde_json::Value::Null,
"{shown}"
);
assert!(library.join("kept/.nib").is_dir());
test.here = library.join("kept");
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
}
#[test]
fn what_concerns_the_librarys_place_does_not_follow_a_nib_file() {
let mut test = library("tree pointer place");
let project = beside("tree pointer place, project");
test.nib(&["init", project.to_str().expect("UTF-8"), "--yes"])
.expect("starts");
test.here.clone_from(&project);
for arguments in [
&["verify", "--drive-checked"][..],
&["sync", "--at-login", "on"],
] {
let refused = test.nib(arguments);
assert!(
matches!(refused, Err(CliError::ThroughPointer(_))),
"{arguments:?}: {refused:?}"
);
}
}
#[test]
fn init_starts_no_tree_where_its_nib_file_cannot_go() {
let mut test = library("tree pointer in the way");
let library = test.here.clone();
let project = beside("tree pointer in the way, project");
fs::create_dir(project.join(".nib")).expect("makes a folder");
let folder = project.to_str().expect("UTF-8").to_owned();
let refused = test.nib(&["init", &folder, "--yes"]);
assert!(
matches!(refused, Err(CliError::PointerInTheWay(_))),
"{refused:?}"
);
fs::remove_dir(project.join(".nib")).expect("deletes");
test.nib(&["init", &folder, "--yes"]).expect("starts");
let elsewhere = nibkeep_testkit::empty_directory(
env!("CARGO_TARGET_TMPDIR"),
"tree pointer in the way, elsewhere",
);
let other = beside("tree pointer in the way, other");
test.here.clone_from(&elsewhere);
let named = library.to_str().expect("UTF-8");
test.nib(&[
"--library",
named,
"init",
other.to_str().expect("UTF-8"),
"--yes",
])
.expect("starts");
assert_eq!(
named_in(&other),
fs::canonicalize(&library).expect("a folder")
);
test.here.clone_from(&other);
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
}
#[test]
fn status_and_commit_show_and_record_what_changed() {
let mut test = with_tree("tree status and commit");
let here = test.here.clone();
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
write(&here, "notes.md", "# Notes, edited\n");
write(&here, "docs/new.md", "New\n");
fs::remove_file(here.join("docs/plan.md")).expect("deletes");
write(&here, "more.log", "left out\n");
let status = test.nib(&["status"]).expect("status");
assert_eq!(
status,
"deleted docs/plan.md\nnew docs/new.md\nedited notes.md\n"
);
assert_eq!(test.nib(&["st"]).expect("status"), status);
let shown = test.json(&["status"]);
assert_eq!(shown["changes"][0]["path"], "docs/new.md");
assert_eq!(shown["changes"][0]["change"], "new");
assert_eq!(shown["changes"][1]["change"], "deleted");
assert_eq!(shown["root"], here.display().to_string());
let whole = test.nib(&["status", "--library-wide"]).expect("status");
assert!(whole.starts_with("Ada, on laptop, in "), "{whole}");
let whole = test.json(&["status", "--library-wide"]);
assert_eq!(whole["user"]["name"], "Ada");
assert!(whole.get("tree").is_none(), "{whole}");
test.here = here.join("docs");
let committed = test
.nib(&["commit", "new.md", "plan.md", "--message", "Docs"])
.expect("commits");
assert_eq!(
ids_hidden(&committed),
"Committed 2 changes as revision <id>\ndeleted docs/plan.md\nnew docs/new.md\n"
);
test.here = here.clone();
assert_eq!(
test.nib(&["status"]).expect("status"),
"edited notes.md\n"
);
let committed = test.json(&["commit"]);
assert_eq!(committed["parts"], 2);
assert!(committed["revision"].is_string());
assert_eq!(committed["changes"][0]["path"], "notes.md");
assert_eq!(
test.nib(&["commit"]).expect("commits"),
"Nothing to commit\n"
);
write(&here, "notes.md", "# Notes, again\n");
let committed = test.nib(&["ci", "-a", "-m", "Again"]).expect("commits");
assert!(committed.contains("edited notes.md"), "{committed}");
assert!(
Cli::try_parse_from(["nib", "commit", "-a", "notes.md"]).is_err(),
"-a means every change, so takes no paths"
);
let found = test.nib(&["find", "Docs"]).expect("finds");
assert!(found.contains("std.revision"), "{found}");
}
#[test]
fn a_move_moves_the_copy_and_the_file_at_once() {
let mut test = with_tree("tree move");
let here = test.here.clone();
let moved = test
.nib(&["mv", "notes.md", "docs/notes.md"])
.expect("moves");
assert_eq!(
ids_hidden(&moved),
"Moved notes.md to docs/notes.md, as revision <id>\n"
);
assert!(here.join("docs/notes.md").exists());
assert!(!here.join("notes.md").exists());
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
test.nib(&["rename", "docs/notes.md", "notes.md"])
.expect("moves");
assert!(here.join("notes.md").exists());
assert!(matches!(
test.nib(&["mv", "notes.md", ".nib/notes.md"]),
Err(CliError::Node(NodeError::UnsafePath(_)))
));
}
#[test]
fn rm_deletes_copies_and_the_next_commit_records_it() {
let mut test = with_tree("tree rm");
let here = test.here.clone();
let removed = test.nib(&["rm", "notes.md"]).expect("removes");
assert_eq!(
removed,
"removed notes.md\nNothing is recorded: nib commit records it.\n"
);
assert!(!here.join("notes.md").exists());
assert_eq!(
test.nib(&["status"]).expect("status"),
"deleted notes.md\n"
);
let removed = test.json(&["remove", "-r", "docs"]);
assert_eq!(removed["removed"], serde_json::json!(["docs/plan.md"]));
assert!(!here.join("docs").exists());
test.nib(&["restore", "notes.md"]).expect("restores");
write(&here, "notes.md", "# Notes, edited\n");
assert!(matches!(
test.nib(&["rm", "notes.md"]),
Err(CliError::Node(NodeError::EditedCopy(_)))
));
test.nib(&["rm", "--force", "notes.md"]).expect("removes");
assert!(!here.join("notes.md").exists());
let committed = test.nib(&["commit", "-m", "Gone"]).expect("commits");
assert!(committed.contains("deleted notes.md"), "{committed}");
assert!(committed.contains("deleted docs/plan.md"), "{committed}");
}
#[test]
fn outside_a_tree_the_tree_commands_say_so() {
let mut test = library("tree outside");
assert!(matches!(test.nib(&["commit"]), Err(CliError::NotInTree(_))));
let status = test.nib(&["status"]).expect("status");
assert!(status.starts_with("Ada, on laptop, in "), "{status}");
let refused = test.nib(&["log"]);
assert!(
matches!(refused, Err(CliError::NoTreeHere(_))),
"{refused:?}"
);
let said = refused.expect_err("refused").to_string();
assert!(said.contains("--tree"), "{said}");
assert!(said.contains("nib history"), "{said}");
}
#[test]
fn what_nib_lacks_says_what_to_do_instead() {
let mut test = with_tree("tree lacks");
for (command, instead) in [
(&["add", "."][..], "nib commit takes in new files"),
(&["add", "-A"][..], "nib commit takes in new files"),
(&["checkout", "main"][..], "nib clone"),
(&["branch"][..], "no branches"),
(&["tag", "v1"][..], "no branches"),
(&["stash"][..], "no stash"),
(&["reset", "--hard"][..], "nib restore"),
(&["rebase", "main"][..], "no branches"),
(&["fetch"][..], "nib sync"),
] {
let refused = test.nib(command);
let Err(error @ CliError::NoSuchCommand { .. }) = refused else {
panic!("{command:?}: {refused:?}");
};
let said = error.to_string();
assert!(said.contains(instead), "{command:?}: {said}");
assert!(
said.starts_with(&format!("there is no {} command", command[0])),
"{said}"
);
}
}
#[test]
fn files_outside_trees_leave_trees_alone() {
let mut test = with_tree("tree and files");
let here = test.here.clone();
let docs = here.join("docs");
let added = test.json(&["file", "add", docs.to_str().expect("UTF-8")]);
assert_eq!(added["files"].as_array().map(Vec::len), Some(0));
assert_eq!(added["left_out"], 1);
let notes = here.join("notes.md");
let named = test.nib_printing(&["file", "add", notes.to_str().expect("UTF-8")]);
assert!(
named.0.contains("in a tree; nib commit records it"),
"{}",
named.0
);
assert!(matches!(named.1, Err(CliError::FilesFailed(1))));
assert!(matches!(
test.nib(&[
"file",
"checkout",
"plan.md",
here.join("out.md").to_str().expect("UTF-8")
]),
Err(CliError::Node(NodeError::TreeFile(_)))
));
}
fn with_two_commits(name: &str) -> (Test, Vec<String>) {
let mut test = library(name);
let here = test.here.clone();
write(&here, "notes.md", "one\ntwo\n");
write(&here, "old.txt", "Old\n");
let started = test.json(&["init", "--yes", "--message", "Start"]);
write(&here, "notes.md", "one\nTWO\n");
fs::remove_file(here.join("old.txt")).expect("deletes");
let second = test.json(&["commit", "--message", "Second"]);
let revision =
|shown: &serde_json::Value| shown["revision"].as_str().expect("a revision").to_owned();
let revisions = vec![revision(&started), revision(&second)];
(test, revisions)
}
#[test]
fn log_diff_and_blame_read_the_tree_s_history() {
let (mut test, revisions) = with_two_commits("tree history commands");
let log = test.nib(&["log"]).expect("logs");
let lines: Vec<&str> = log.lines().collect();
assert!(
lines[0].starts_with("revision ") && lines[0].ends_with("Ada"),
"{log}"
);
assert_eq!(lines[1], " Second");
assert_eq!(
&lines[2..4],
[" deleted old.txt", " edited notes.md"]
);
assert!(log.contains(" Start"), "{log}");
let notes_log = test.json(&["log", "notes.md"]);
assert_eq!(notes_log.as_array().map(Vec::len), Some(2));
let diff = test
.nib(&["diff", &revisions[0], &revisions[1]])
.expect("diffs");
assert!(diff.contains("deleted old.txt"), "{diff}");
assert!(diff.contains("-two\n+TWO\n"), "{diff}");
let named = test
.nib(&["diff", "--from", &revisions[0], "--to", &revisions[1]])
.expect("diffs");
assert_eq!(named, diff);
write(&test.here.clone(), "notes.md", "one\nTWO\nthree\n");
let diff = test.nib(&["diff"]).expect("diffs the copy");
assert!(diff.starts_with("edited notes.md\n"), "{diff}");
assert!(diff.contains("+three"), "{diff}");
let blame = test.json(&["blame", "notes.md"]);
assert_eq!(blame[0]["text"], "one");
assert_eq!(blame[0]["revision"], revisions[0]);
assert_eq!(blame[1]["revision"], revisions[1]);
assert_eq!(test.json(&["annotate", "notes.md"]), blame);
}
#[test]
fn show_of_a_revision_shows_what_it_changed() {
let (mut test, revisions) = with_two_commits("tree show revision");
let shown = test.nib(&["show", &revisions[1]]).expect("shows");
assert!(shown.contains("Second"), "{shown}");
assert!(shown.contains("\ndeleted old.txt\n"), "{shown}");
assert!(shown.contains("\nedited notes.md\n"), "{shown}");
assert!(shown.contains("-two\n+TWO\n"), "{shown}");
let shown = test.json(&["show", &revisions[1]]);
assert_eq!(shown["diff"][0]["path"], "notes.md");
assert_eq!(shown["diff"][0]["change"], "edited");
assert_eq!(shown["diff"][1]["path"], "old.txt");
let notes = test.json(&["show", "notes.md"]);
assert!(notes.get("diff").is_none(), "{notes}");
}
#[test]
fn restore_and_revert_write_the_copy_and_record_nothing() {
let (mut test, revisions) = with_two_commits("tree going back");
let here = test.here.clone();
let restored = test.nib(&["restore", "old.txt"]).expect("restores");
assert_eq!(
restored,
"restored old.txt\nNothing is recorded: nib commit records what you keep.\n"
);
assert_eq!(
fs::read_to_string(here.join("old.txt")).expect("reads"),
"Old\n"
);
fs::remove_file(here.join("old.txt")).expect("deletes it again");
let reverted = test.nib(&["revert", &revisions[1]]).expect("reverts");
assert!(reverted.contains("written notes.md"), "{reverted}");
assert!(reverted.contains("written old.txt"), "{reverted}");
assert_eq!(
fs::read_to_string(here.join("notes.md")).expect("reads"),
"one\ntwo\n"
);
let status = test.nib(&["status"]).expect("status");
assert_eq!(status, "new old.txt\nedited notes.md\n");
let restored = test
.nib(&["restore", "notes.md", "-r", &revisions[1]])
.expect("restores");
assert_eq!(
restored,
"restored notes.md, your edits kept as notes.md.orig\nNothing is recorded: nib commit records what you keep.\n"
);
assert_eq!(
fs::read_to_string(here.join("notes.md")).expect("reads"),
"one\nTWO\n"
);
assert_eq!(
fs::read_to_string(here.join("notes.md.orig")).expect("reads"),
"one\ntwo\n"
);
let restored = test.json(&["restore", "old.txt", "--at", &revisions[0]]);
assert_eq!(restored["written"], serde_json::json!([]));
fs::remove_file(here.join("notes.md.orig")).expect("deletes");
fs::remove_file(here.join("old.txt")).expect("deletes");
test.nib(&["backout", &revisions[1]]).expect("reverts");
let refused = test.nib(&["revert", "notes.md"]);
assert!(
matches!(refused, Err(CliError::RevertTakesRevision(_))),
"{refused:?}"
);
let said = refused.expect_err("refused").to_string();
assert!(said.contains("nib restore notes.md"), "{said}");
}
#[test]
fn a_tree_is_cloned_into_a_new_folder_at_a_revision() {
let (mut test, revisions) = with_two_commits("tree cloned");
let here = test.here.clone();
let name = here
.file_name()
.and_then(|name| name.to_str())
.expect("a name")
.to_owned();
let picture = here
.parent()
.expect("a parent")
.join("tree cloned, picture");
let _ = fs::remove_dir_all(&picture);
let written = test
.nib(&[
"clone",
&name,
picture.to_str().expect("UTF-8"),
"-r",
&revisions[0],
])
.expect("writes");
assert_eq!(
written,
format!(
"Wrote 2 files into {}, a picture of the past, which is no copy\n",
picture.display()
)
);
assert_eq!(
fs::read_to_string(picture.join("notes.md")).expect("reads"),
"one\ntwo\n"
);
assert!(matches!(
test.nib(&["clone", &name, picture.to_str().expect("UTF-8")]),
Err(CliError::Node(NodeError::TreeHereAlready(_)))
));
let elsewhere =
nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), "tree cloned, elsewhere");
test.here.clone_from(&elsewhere);
let library = here.to_str().expect("UTF-8");
test.nib(&["--library", library, "clone", &name, "--at", &revisions[1]])
.expect("writes");
assert_eq!(
fs::read_to_string(elsewhere.join(&name).join("notes.md")).expect("reads"),
"one\nTWO\n"
);
}
#[test]
fn update_brings_the_copy_to_the_tree_as_it_is() {
let mut test = with_tree("tree update command");
let here = test.here.clone();
assert_eq!(
test.nib(&["update"]).expect("updates"),
"The copy here has the tree as it is\n"
);
test.nib(&["edit", "plan.md", "std.tree-path=docs/later.md"])
.expect("moves it in the tree");
let updated = test.nib(&["up"]).expect("updates");
assert_eq!(updated, "moved docs/later.md, from docs/plan.md\n");
assert!(here.join("docs/later.md").exists());
assert_eq!(
test.nib(&["status"]).expect("status"),
"Nothing changed in the tree's copy here\n"
);
}
#[test]
fn pull_syncs_then_updates_and_push_syncs() {
let mut test = with_tree("tree pull");
let here = test.here.clone();
test.nib(&["edit", "plan.md", "std.tree-path=docs/later.md"])
.expect("moves it in the tree");
let (pulled, result) = test.nib_printing(&["pull", "--wait", "0"]);
assert_eq!(pulled, "moved docs/later.md, from docs/plan.md\n");
assert!(
matches!(result, Err(CliError::Node(NodeError::NoOtherReplicas))),
"{result:?}"
);
assert!(here.join("docs/later.md").exists());
let (pulled, result) = test.nib_printing(&["--json", "pull", "--wait", "0"]);
assert!(result.is_err());
let pulled: serde_json::Value = serde_json::from_str(&pulled).expect("JSON");
assert_eq!(pulled["sync"], serde_json::Value::Null);
assert_eq!(pulled["update"]["written"], serde_json::json!([]));
assert!(matches!(
test.nib(&["push", "--wait", "0"]),
Err(CliError::Node(NodeError::NoOtherReplicas))
));
}
fn git(test: &Test, folder: &Path, arguments: &[&str]) -> String {
let output = test
.git_command()
.arg("-C")
.arg(folder)
.args(arguments)
.output()
.expect("runs Git");
assert!(
output.status.success(),
"git {arguments:?}: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8_lossy(&output.stdout).trim().to_owned()
}
fn with_repository(name: &str) -> Test {
let mut test = with_repository_unstarted(name);
test.nib(&["init", "--yes", "--message", "Start"])
.expect("starts");
test
}
fn with_repository_unstarted(name: &str) -> Test {
let test = library(name);
let here = test.here.clone();
write(&here, "notes.md", "# Notes\n");
let remote =
nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), &format!("{name}, remote"));
git(&test, &remote, &["init", "--quiet", "--bare"]);
let settings = format!(
"{}[url \"{}\"]\n\tinsteadOf = https://example.invalid/proj.git\n\tinsteadOf = https://ada:secret-token@example.invalid/proj.git\n",
fs::read_to_string(test.git_home.join("config")).expect("reads"),
remote.display().to_string().replace('\\', "/")
);
fs::write(test.git_home.join("config"), settings).expect("writes");
let proj = here.join("proj");
fs::create_dir(&proj).expect("makes a folder");
git(&test, &proj, &["init", "--quiet"]);
write(&here, "proj/README.md", "Proj\n");
git(&test, &proj, &["add", "README.md"]);
git(&test, &proj, &["commit", "--quiet", "-m", "First"]);
git(
&test,
&proj,
&[
"remote",
"add",
"origin",
"https://ada:secret-token@example.invalid/proj.git",
],
);
git(
&test,
&proj,
&["push", "--quiet", "--set-upstream", "origin", "main"],
);
test
}
#[test]
fn a_tree_records_its_git_repositories_when_it_starts() {
let mut test = with_repository_unstarted("tree git start");
let started = test
.nib(&["init", "--yes", "--message", "Start"])
.expect("starts");
assert!(
started.contains("new proj, a Git repository"),
"{started}"
);
assert!(!started.contains("README"), "{started}");
let status = test.nib(&["status"]).expect("status");
assert_eq!(status, "Nothing changed in the tree's copy here\n");
let listed = test.json(&["status", "--json"]);
assert_eq!(listed["repositories"], serde_json::json!([]));
assert_eq!(listed["only_here"], serde_json::json!([]));
}
#[test]
fn status_names_the_work_that_only_this_computer_holds() {
let mut test = with_repository("tree git work here");
let proj = test.here.join("proj");
write(&test.here, "proj/more.md", "More\n");
git(&test, &proj, &["add", "more.md"]);
git(&test, &proj, &["commit", "--quiet", "-m", "Not pushed"]);
write(&test.here, "proj/draft.md", "Draft\n");
let status = test.nib(&["status"]).expect("status");
assert!(
status.contains("only here proj: changes not committed, 1 commit not pushed"),
"{status}"
);
}
#[test]
fn a_commit_records_what_changed_of_a_repository_and_pin_pins_it() {
let mut test = with_repository("tree git commit");
let proj = test.here.join("proj");
git(
&test,
&proj,
&["remote", "add", "backup", "git@example.invalid:me/proj.git"],
);
let committed = test.nib(&["commit", "-m", "Backup"]).expect("commits");
assert!(
committed.contains("edited proj, a Git repository's remotes, branch or pinned commit"),
"{committed}"
);
assert!(
committed.starts_with("Committed 1 change as revision "),
"{committed}"
);
let head = git(&test, &proj, &["rev-parse", "HEAD"]);
let pinned = test.nib(&["commit", "--pin", "proj"]).expect("pins");
assert_eq!(
ids_hidden(&pinned),
format!("Pinned proj at {}, as revision <id>\n", &head[..12])
);
let unpinned = test.nib(&["commit", "--unpin", "proj"]).expect("unpins");
assert_eq!(ids_hidden(&unpinned), "Unpinned proj, as revision <id>\n");
assert!(matches!(
test.nib(&["commit", "--pin", "notes.md"]),
Err(CliError::NoGitCommit(_) | CliError::Node(NodeError::NoRepository(_)))
));
}
fn as_never_cloned(test: &Test) {
fs::remove_dir_all(test.here.join("proj")).expect("deletes");
for replica in fs::read_dir(test.here.join(".nib/replicas")).expect("lists") {
let index = replica.expect("reads").path().join("trees.sqlite");
if index.exists() {
fs::remove_file(index).expect("loses the index");
}
}
}
#[test]
fn update_clones_a_repository_this_copy_lacks_once_the_person_agrees() {
let mut test = with_repository("tree git clone");
as_never_cloned(&test);
let listed = test.nib(&["update", "--no-clone"]).expect("updates");
assert!(
listed.contains(
"to clone proj, from https://example.invalid/proj.git (origin), recorded by"
),
"{listed}"
);
assert!(listed.contains("nib update --clone clones it"), "{listed}");
assert!(!test.here.join("proj").exists());
test.answers = Some(["n"].into_iter().collect());
test.nib(&["update"]).expect("updates");
assert!(!test.here.join("proj").exists());
test.answers = Some(["y"].into_iter().collect());
let cloned = test.nib(&["update"]).expect("updates");
assert!(cloned.contains("cloned proj"), "{cloned}");
assert!(
test.questions
.iter()
.any(|question| question.contains("proj from https://example.invalid/proj.git"))
);
let proj = test.here.join("proj");
assert_eq!(
fs::read_to_string(proj.join("README.md")).expect("reads"),
"Proj\n"
);
assert_eq!(
git(&test, &proj, &["config", "--get", "remote.origin.url"]),
"https://example.invalid/proj.git"
);
assert_eq!(git(&test, &proj, &["branch", "--show-current"]), "main");
let again = test.nib(&["update"]).expect("updates");
assert!(!again.contains("proj"), "{again}");
}
#[test]
fn a_branch_that_no_remote_has_is_cloned_as_the_remote_s_own() {
let mut test = with_repository("tree git branch not pushed");
let proj = test.here.join("proj");
git(&test, &proj, &["switch", "--quiet", "--create", "feature"]);
test.nib(&["commit", "-m", "Feature"]).expect("commits");
as_never_cloned(&test);
let cloned = test.nib(&["update", "--clone"]).expect("updates");
assert!(cloned.contains("cloned proj"), "{cloned}");
assert_eq!(git(&test, &proj, &["branch", "--show-current"]), "main");
}
#[test]
fn a_clone_that_fails_leaves_nothing_behind() {
let mut test = with_repository("tree git clone fails");
let proj = test.here.join("proj");
write(&test.here, "proj/local.md", "Only here\n");
git(&test, &proj, &["add", "local.md"]);
git(&test, &proj, &["commit", "--quiet", "-m", "Not pushed"]);
test.nib(&["commit", "--pin", "proj"]).expect("pins");
as_never_cloned(&test);
let failed = test.nib(&["update", "--clone"]).expect("updates");
assert!(failed.contains("could not clone proj"), "{failed}");
assert!(!proj.exists());
}
#[test]
fn a_tree_keeps_its_env_as_a_secret_file_read_with_the_secrets_alone() {
let mut test = library("tree secrets");
let here = test.here.clone();
write(&here, "notes.md", "# Notes\n");
write(&here, ".env", "TOKEN=s3ntinel\n");
test.passphrases = ["secrets passphrase", "secrets passphrase"].into();
let started = test.nib(&["init", "--yes"]).expect("starts");
assert!(started.contains("secret .env"), "{started}");
assert!(started.contains("2 files"), "{started}");
write(&here, ".env", "TOKEN=again\n");
let status = test.nib(&["status"]).expect("status");
assert!(status.contains("edited .env"), "{status}");
assert!(status.contains("secret .env"), "{status}");
assert!(matches!(
test.nib(&["commit"]),
Err(CliError::Node(NodeError::SecretsNeeded(_)))
));
let left = test
.nib(&["commit", "--leave-secrets"])
.expect("commits the rest");
assert!(left.contains("left .env"), "{left}");
test.passphrases = ["secrets passphrase"].into();
let committed = test.nib(&["commit"]).expect("commits");
assert!(committed.contains("secret .env"), "{committed}");
write(&here, ".env", "TOKEN=third\n");
let diff = test.nib(&["diff"]).expect("diffs");
assert!(diff.contains("edited .env"), "{diff}");
assert!(!diff.contains("TOKEN"), "{diff}");
test.passphrases = ["secrets passphrase"].into();
let shown = test.nib(&["diff", "--secrets"]).expect("diffs");
assert!(shown.contains("+TOKEN=third"), "{shown}");
assert!(matches!(
test.nib(&["blame", ".env"]),
Err(CliError::Node(NodeError::SecretLines(_)))
));
write(&here, "public.pem", "public\n");
test.nib(&["commit", "--ordinary", "public.pem", "--leave-secrets"])
.expect("commits");
let status = test.nib(&["status"]).expect("status");
assert!(!status.contains("public.pem"), "{status}");
let listed = test.nib(&["status", "--secrets"]).expect("lists");
assert!(listed.contains("secret .env"), "{listed}");
assert!(
listed.contains("ordinary public.pem, which looks secret: nib commit --secret public.pem"),
"{listed}"
);
let listed = test.json(&["status", "--secrets"]);
assert_eq!(listed["secret"][0], ".env");
assert_eq!(listed["looking_secret"][0], "public.pem");
}
#[test]
fn update_asks_before_it_moves_a_secret_file_where_another_device_moved_it() {
let mut test = library("tree secret moved");
let here = test.here.clone();
write(&here, "notes.md", "# Notes\n");
write(&here, ".env", "TOKEN=s3ntinel\n");
test.passphrases = ["secrets passphrase", "secrets passphrase"].into();
test.nib(&["init", "--yes"]).expect("starts");
test.nib(&["edit", ".env", "std.tree-path=public/env.txt"])
.expect("moves it, as another device may");
let held = test.nib(&["update"]).expect("updates");
assert!(held.contains("held .env -> public/env.txt"), "{held}");
assert!(here.join(".env").exists());
test.answers = Some(["y"].into());
test.passphrases = ["secrets passphrase"].into();
let moved = test.nib(&["update"]).expect("updates");
assert_eq!(test.questions.len(), 1, "{:?}", test.questions);
assert!(
moved.contains("moved public/env.txt, from .env"),
"{moved}"
);
assert!(here.join("public/env.txt").exists());
}