Files

360 lines
14 KiB
Rust

//! `worklane init` and the shared-board decision table (c6a61b4).
//!
//! A project is on one of two layouts: the local `.worklane` directory every project
//! always had, or the shared one where the project carries a committed `.worklane.toml`,
//! the board lives in a machine-level boards directory, and `.worklane` is a symlink
//! into it. The machine config decides the default, and the pointer decides where the
//! project already is. The tests below walk the whole table: one per row, the three
//! symlink states, and the standalone path.
//!
//! Every test runs on an isolated machine: `HOME` and the XDG variables point into the
//! temp directory, so the suite can never read or write a real config or boards
//! directory, and a `find_worklane_dir` walk from the project can never climb into one.
use assert_cmd::Command;
use assert_fs::TempDir;
use assert_fs::fixture::{PathChild, PathCreateDir};
use std::fs;
use std::path::{Path, PathBuf};
const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane");
/// A project with no pointer and no board, ready for `init` to decide on.
fn project(temp: &TempDir) -> PathBuf {
temp.child("project").create_dir_all().unwrap();
temp.child("project").path().to_path_buf()
}
fn run(project: &Path, machine: &TempDir, args: &[&str]) -> std::process::Output {
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(project)
.env("HOME", machine.path())
.env("XDG_CONFIG_HOME", machine.path().join(".config"))
.env("XDG_DATA_HOME", machine.path().join(".local/share"))
.args(args);
cmd.assert().get_output().clone()
}
fn stdout(out: &std::process::Output) -> String {
String::from_utf8_lossy(&out.stdout).to_string()
}
fn stderr(out: &std::process::Output) -> String {
String::from_utf8_lossy(&out.stderr).to_string()
}
/// The boards directory of a machine, under its data dir.
fn boards_dir(machine: &TempDir) -> PathBuf {
machine.path().join(".local/share/worklane/boards")
}
/// The board a pointer names when the machine holds it.
fn board_path(machine: &TempDir, uuid: &str, slug: &str) -> PathBuf {
boards_dir(machine).join(format!("{uuid}_{slug}"))
}
/// The machine config, without which a no-pointer project stays on the local layout.
///
/// Carries only the boards repo url; the boards directory defaults under the machine's
/// data dir, which is where these tests put it.
fn with_boards_config(machine: &TempDir) {
let config = machine.path().join(".config/worklane/config.toml");
fs::create_dir_all(config.parent().unwrap()).unwrap();
fs::write(config, "[boards]\nurl = \"git@example:worklane/boards\"\n").unwrap();
}
/// The committed pointer naming a board by uuid. This is the only thing that puts a
/// project on the shared layout.
fn write_pointer(project: &Path, uuid: &str) {
fs::write(project.join(".worklane.toml"), format!("[worklane]\nboard = \"{uuid}\"\n")).unwrap();
}
/// A board directory in the machine's boards repo, named `<uuid>_<slug>`.
fn with_board(machine: &TempDir, uuid: &str, slug: &str) -> PathBuf {
let board = board_path(machine, uuid, slug);
fs::create_dir_all(&board).unwrap();
board
}
/// The relative spelling init gives a `.worklane` symlink, used to build the states a
/// project can already be in.
fn link_target(project: &Path, board: &Path) -> String {
let base = project;
let common = base
.components()
.zip(board.components())
.take_while(|(a, b)| a == b)
.count();
let mut up = String::new();
for _ in common..base.components().count() {
up.push_str("../");
}
let mut rest = String::new();
for component in board.components().skip(common) {
rest.push_str(&component.as_os_str().to_string_lossy());
rest.push('/');
}
format!("{up}{rest}").trim_end_matches('/').to_string()
}
fn symlink_to(project: &Path, target: &str) {
std::os::unix::fs::symlink(target, project.join(".worklane")).unwrap();
}
fn boards_of(machine: &TempDir) -> Vec<PathBuf> {
fs::read_dir(boards_dir(machine))
.unwrap()
.map(|entry| entry.unwrap().path())
.collect()
}
/// The standalone path, and the reason it has to stay: every project written before the
/// shared layout existed, and every project whose machine has no boards config, must
/// still get exactly the board `init` always made.
#[test]
fn no_pointer_no_config_and_no_boards_repo_makes_a_local_board() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
let out = run(&project, &machine, &["init"]);
assert!(out.status.success(), "{}", stderr(&out));
assert_eq!(
stdout(&out),
format!("Initialized worklane in: {}\n", project.join(".worklane").display())
);
assert!(
fs::symlink_metadata(project.join(".worklane")).unwrap().is_dir(),
".worklane must be a real directory"
);
assert!(!project.join(".worklane.toml").exists(), "no pointer on a local board");
for dir in [
"backlog",
"committed",
"reviewing",
"done",
"discarded",
"campaigns",
] {
assert!(project.join(".worklane").join(dir).is_dir(), "local board missing {dir}");
}
}
/// The pointer is committed and carries only the board by uuid, nowhere for a home
/// directory or a server address to hide.
#[test]
fn a_shared_init_commits_a_pointer_carrying_only_the_uuid() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
with_boards_config(&machine);
let out = run(&project, &machine, &["init"]);
assert!(out.status.success(), "{}", stderr(&out));
let output = stdout(&out);
assert!(output.contains("Initialized shared worklane in: "), "{output}");
assert!(output.contains("Board pointer: "), "{output}");
assert!(
output.contains(&format!("Linked {} to it", project.join(".worklane").display())),
"{output}"
);
let board = output
.lines()
.next()
.unwrap()
.trim_start_matches("Initialized shared worklane in: ")
.to_string();
let uuid = Path::new(&board)
.file_name()
.unwrap()
.to_str()
.unwrap()
.split('_')
.next()
.unwrap();
let pointer = fs::read_to_string(project.join(".worklane.toml")).unwrap();
assert_eq!(pointer, format!("[worklane]\nboard = \"{uuid}\"\n"));
assert!(
!pointer.contains(machine.path().to_str().unwrap()),
"no home path in the pointer"
);
assert!(!pointer.contains("git@"), "no server address in the pointer");
}
/// A pointer whose board is present is the state a fresh clone is in: `.worklane` is
/// missing and nothing else is. init links it and creates nothing.
#[test]
fn pointer_with_the_board_present_symlinks_it_and_creates_nothing() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
with_boards_config(&machine);
write_pointer(&project, "12345678-1234-1234-1234-1234567890ab");
let board = with_board(&machine, "12345678-1234-1234-1234-1234567890ab", "project");
let before = fs::read_to_string(project.join(".worklane.toml")).unwrap();
let out = run(&project, &machine, &["init"]);
assert!(out.status.success(), "{}", stderr(&out));
assert!(
stdout(&out).contains(&format!("Linked {} to it", project.join(".worklane").display()))
);
assert_eq!(
fs::canonicalize(project.join(".worklane")).unwrap(),
fs::canonicalize(&board).unwrap()
);
assert_eq!(fs::read_to_string(project.join(".worklane.toml")).unwrap(), before);
assert_eq!(boards_of(&machine).len(), 1, "no second board was created");
}
/// A pointer whose board is absent is an unsynced machine, and creating a second board
/// there is the worst outcome in the design. init refuses, and `--create` is the only
/// way to make one.
#[test]
fn pointer_with_the_board_absent_refuses_and_create_is_the_only_way() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
with_boards_config(&machine);
write_pointer(&project, "abcd5678-abcd-4bcd-abcd-abcdabcdabcd");
with_board(&machine, "12345678-1234-1234-1234-1234567890ab", "other");
let out = run(&project, &machine, &["init"]);
assert!(!out.status.success());
let err = stderr(&out);
assert!(
err.contains("holds no board for abcd5678-abcd-4bcd-abcd-abcdabcdabcd."),
"{err}"
);
assert!(err.contains("`worklane init --create`"), "{err}");
assert!(!project.join(".worklane").exists(), "nothing was linked");
assert_eq!(boards_of(&machine).len(), 1, "no board was created");
let out = run(&project, &machine, &["init", "--create"]);
assert!(out.status.success(), "{}", stderr(&out));
assert_eq!(
fs::canonicalize(project.join(".worklane")).unwrap(),
fs::canonicalize(board_path(&machine, "abcd5678-abcd-4bcd-abcd-abcdabcdabcd", "project"))
.unwrap()
);
}
/// The boards directory itself is gone: that is an uncloned boards repo, not a missing
/// board, and the two must be told apart.
#[test]
fn boards_repo_missing_is_said_as_such() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
with_boards_config(&machine);
write_pointer(&project, "12345678-1234-1234-1234-1234567890ab");
let out = run(&project, &machine, &["init"]);
assert!(!out.status.success());
let err = stderr(&out);
assert!(err.contains("does not exist"), "{err}");
assert!(err.contains("boards repo that is not cloned here"), "{err}");
}
/// The correct symlink in place is the layout already there: init reports no change and
/// writes nothing.
#[test]
fn correct_symlink_already_in_place_reports_no_change() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
with_boards_config(&machine);
let board = with_board(&machine, "12345678-1234-1234-1234-1234567890ab", "project");
write_pointer(&project, "12345678-1234-1234-1234-1234567890ab");
symlink_to(&project, &link_target(&project, &board));
let before = fs::read_to_string(project.join(".worklane.toml")).unwrap();
let out = run(&project, &machine, &["init"]);
assert!(out.status.success(), "{}", stderr(&out));
assert!(stdout(&out).starts_with("worklane already initialized in: "));
assert_eq!(fs::read_to_string(project.join(".worklane.toml")).unwrap(), before);
assert_eq!(boards_of(&machine).len(), 1, "nothing was written");
}
/// A symlink pointing at a different board is a project on the wrong board, and changing
/// what a project is on without asking is how two machines disagree about the work.
#[test]
fn symlink_to_a_different_board_refuses_rather_than_repointing() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
with_boards_config(&machine);
with_board(&machine, "12345678-1234-1234-1234-1234567890ab", "project");
let other = with_board(&machine, "99999999-9999-4999-8999-999999999999", "other");
write_pointer(&project, "12345678-1234-1234-1234-1234567890ab");
let other_target = link_target(&project, &other);
symlink_to(&project, &other_target);
let out = run(&project, &machine, &["init"]);
assert!(!out.status.success());
let err = stderr(&out);
assert!(err.contains("already links to"), "{err}");
assert!(err.contains("not to the board"), "{err}");
assert_eq!(
fs::read_link(project.join(".worklane")).unwrap(),
PathBuf::from(&other_target)
);
}
/// A symlink that resolves to nothing is neither the layout init places nor a board: it
/// is not replaced without asking.
#[test]
fn symlink_that_dangles_is_not_replaced() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
with_boards_config(&machine);
with_board(&machine, "12345678-1234-1234-1234-1234567890ab", "project");
write_pointer(&project, "12345678-1234-1234-1234-1234567890ab");
symlink_to(&project, "nowhere-anyone-knows");
let out = run(&project, &machine, &["init"]);
assert!(!out.status.success());
let err = stderr(&out);
assert!(err.contains("nowhere-anyone-knows, which does not exist"), "{err}");
assert_eq!(
fs::read_link(project.join(".worklane")).unwrap(),
PathBuf::from("nowhere-anyone-knows")
);
}
/// A real `.worklane` directory under a pointer is a board from before the shared
/// layout, and it is never walked over; the refusal names the adopt path.
#[test]
fn real_worklane_directory_under_a_pointer_refuses_and_names_the_adopt_path() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
with_boards_config(&machine);
write_pointer(&project, "12345678-1234-1234-1234-1234567890ab");
with_board(&machine, "12345678-1234-1234-1234-1234567890ab", "project");
fs::create_dir_all(project.join(".worklane").join("backlog")).unwrap();
fs::write(project.join(".worklane").join("backlog").join("b.md"), "# anything\n").unwrap();
let out = run(&project, &machine, &["init"]);
assert!(!out.status.success());
let err = stderr(&out);
assert!(err.contains("is a real directory"), "{err}");
assert!(err.contains("adopt"), "{err}");
assert!(
project.join(".worklane").join("backlog").join("b.md").is_file(),
"the pre-existing board was left intact"
);
}
/// The slug, and only the slug, may change: resolution is a uuid prefix match, so
/// renaming a board breaks no committed pointer.
#[test]
fn renaming_a_board_slug_breaks_no_pointer() {
let machine = TempDir::new().unwrap();
let project = project(&machine);
with_boards_config(&machine);
write_pointer(&project, "12345678-1234-1234-1234-1234567890ab");
let renamed = with_board(&machine, "12345678-1234-1234-1234-1234567890ab", "new-name");
let out = run(&project, &machine, &["init"]);
assert!(out.status.success(), "{}", stderr(&out));
assert_eq!(
fs::canonicalize(project.join(".worklane")).unwrap(),
fs::canonicalize(&renamed).unwrap()
);
}