//! The server root: one directory, named once. //! //! `--root` selects the directory and `config.json` lives inside it, so the root //! *is* wherever the config was found; the `root` field in the file is a record //! of that, not a second knob. Re-joining a relative field onto the config's own //! directory is what made `--root runtime` become `runtime/runtime/…`: `init` //! created and migrated one tree, and every command after it ran against a //! second one with its own empty database. use std::path::{Path, PathBuf}; use std::process::Command; use aur_build::config::Config; /// The exact shape of the bug: a relative `--root`, saved verbatim, read back by /// the next command. Relative to the crate root here, which is the cwd cargo /// gives a test — the same situation as running `--root runtime` from a directory /// that is not `/`. #[test] fn a_relative_root_is_absolutized_once_and_never_doubled() { let rel = PathBuf::from("target").join("config-root-test").join("runtime"); let _ = std::fs::remove_dir_all(&rel); let cfg = Config { root: rel.clone(), ..Config::default() }; cfg.save().unwrap(); let reloaded = Config::load(&rel).unwrap(); assert!( reloaded.root.is_absolute(), "a saved root must come back absolute, not relative: {}", reloaded.root.display() ); assert_eq!(reloaded.root, std::path::absolute(&rel).unwrap()); assert!( !rel.join("runtime").exists(), "the root must never be joined onto itself: {}", rel.join("runtime").display() ); // Everything derived hangs off the one root, so the database cannot end up in // a second tree while the config sits in the first. assert!(reloaded.db_path().starts_with(&reloaded.root)); assert!(reloaded.repo_arch_dir().starts_with(&reloaded.root)); std::fs::remove_dir_all(&rel).unwrap(); } /// The legacy shape on disk: a config written by an `init` that saved /// `--root runtime` verbatim. Loading it must not resurrect the doubled tree. #[test] fn a_stale_relative_root_in_the_file_is_ignored() { let tmp = tempfile::tempdir().unwrap(); std::fs::write( tmp.path().join("config.json"), "{\n \"root\": \"runtime\"\n}\n", ) .unwrap(); let cfg = Config::load(tmp.path()).unwrap(); assert_eq!(cfg.root, std::path::absolute(tmp.path()).unwrap()); assert!(!cfg.root.join("runtime").exists()); } #[test] fn a_root_with_no_config_is_an_error_not_a_fresh_server() { let tmp = tempfile::tempdir().unwrap(); let err = Config::load(tmp.path()).unwrap_err(); assert!( err.to_string().contains("run `aur-build init`"), "the caller must be able to branch on \"there is no config yet\": {err}" ); } /// The CLI half of the same rule. The old fallback invented a default config for /// *any* command, so a typo'd `--root` quietly produced an empty server whose /// commands reported success against a database with nothing in it. #[test] fn the_cli_refuses_a_root_that_was_never_initialized() { let tmp = tempfile::tempdir().unwrap(); let out = Command::new(env!("CARGO_BIN_EXE_aur-build")) .args(["--root", &tmp.path().display().to_string(), "packages"]) .output() .unwrap(); assert!( !out.status.success(), "a root with no config must fail, not answer from an empty server" ); assert!( out.stdout.is_empty(), "nothing may be printed as though the server existed: {}", String::from_utf8_lossy(&out.stdout) ); let err = String::from_utf8_lossy(&out.stderr); assert!(err.contains("init"), "{err}"); } /// …and `init` is still allowed to run where there is no config yet, which is the /// only reason the fallback existed. #[test] fn init_still_runs_where_there_is_no_config_yet() { let tmp = tempfile::tempdir().unwrap(); let root = tmp.path().join("runtime"); let out = Command::new(env!("CARGO_BIN_EXE_aur-build")) .args(["--root", &root.display().to_string(), "init", "--repo", "aur"]) .output() .unwrap(); assert!( out.status.success(), "{}", String::from_utf8_lossy(&out.stderr) ); assert!(root.join("config.json").exists()); assert!(root.join("aur-build.db").exists()); // And the command after it agrees about where the tree is. let cfg = Config::load(&root).unwrap(); assert_eq!(cfg.root, std::path::absolute(&root).unwrap()); assert!(!root.join("runtime").exists(), "no doubled tree"); } /// A root that exists but holds nothing but a config still resolves to itself, /// whichever way it was spelled. #[test] fn an_absolute_root_is_used_verbatim() { let tmp = tempfile::tempdir().unwrap(); let dir: &Path = tmp.path(); std::fs::write( dir.join("config.json"), format!("{{\n \"root\": \"{}\"\n}}\n", dir.display()), ) .unwrap(); assert_eq!(Config::load(dir).unwrap().root, dir); }