From 26311f8b521b11df3537c8ad241378ffca225d51 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 21 Nov 2026 15:42:45 -0700 Subject: [PATCH] test(store): where each system keeps the books, and that no install folder holds them (P15 C1) Co-Authored-By: Claude Fable 5.2 --- crates/ledger-store/src/root.rs | 20 +++-- crates/ledger-store/tests/data_locations.rs | 84 +++++++++++++++++++++ 2 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 crates/ledger-store/tests/data_locations.rs diff --git a/crates/ledger-store/src/root.rs b/crates/ledger-store/src/root.rs index 0353b74c..8c0bc6cb 100644 --- a/crates/ledger-store/src/root.rs +++ b/crates/ledger-store/src/root.rs @@ -357,12 +357,20 @@ pub fn platform_data_dir(home: &Path) -> PathBuf { { return local; } - if cfg!(target_os = "macos") { - home.join("Library").join("Application Support") - } else if cfg!(windows) { - home.join("AppData").join("Local") - } else { - home.join(".local").join("share") + data_dir_for(std::env::consts::OS, home) +} + +/// The data folder under `home` on the system called `os` (a +/// `std::env::consts::OS` name), as each system's conventions put an +/// installed app's own data (P15 C1, WB-pkg-8): `Library/Application +/// Support` on macOS, `AppData\Local` (not the roaming folder: the books +/// are big, hold locks and belong to this machine) on Windows, the XDG +/// default `.local/share` anywhere else. An uninstall never touches it. +pub fn data_dir_for(os: &str, home: &Path) -> PathBuf { + match os { + "macos" => home.join("Library").join("Application Support"), + "windows" => home.join("AppData").join("Local"), + _ => home.join(".local").join("share"), } } diff --git a/crates/ledger-store/tests/data_locations.rs b/crates/ledger-store/tests/data_locations.rs new file mode 100644 index 00000000..34d90567 --- /dev/null +++ b/crates/ledger-store/tests/data_locations.rs @@ -0,0 +1,84 @@ +//! Where an installed app's books live on each system (plan P15 unit C1, +//! WB-pkg-8). +//! +//! ```text +//! GOAL: the books, keys and settings sit where each system keeps an app's +//! own data, apart from the program, so installing a newer program +//! over the old one, or uninstalling it, leaves them as they are. +//! SOURCE: WB-pkg-8; each system's convention for per-user application data +//! (Apple's File System Programming Guide: Library/Application +//! Support; Microsoft's known folders: LocalAppData; the XDG base +//! directory specification: $XDG_DATA_HOME, default ~/.local/share). +//! INVARIANTS: +//! 1. macOS: /Library/Application Support/wanderbooks. +//! 2. Windows: \AppData\Local\wanderbooks (LocalAppData, the local +//! folder, never the roaming one). +//! 3. Anywhere else: /.local/share/wanderbooks, where books have +//! always lived on Linux. +//! 4. No system's data folder is inside the folder its program installs +//! to (the install folders are in docs/install.md and the packaging +//! scripts), so removing the program cannot remove the books. +//! SCENARIOS: +//! 1. The three systems' folders, from one home. +//! ``` + +use ledger_store::root::{ROOT_NAME, data_dir_for, default_root}; +use std::path::Path; + +#[test] +fn each_system_keeps_its_data_where_it_keeps_an_apps_data() { + let home = Path::new("/home/someone"); + assert_eq!( + data_dir_for("macos", home), + home.join("Library/Application Support") + ); + assert_eq!( + data_dir_for("windows", home), + home.join("AppData").join("Local") + ); + for other in ["linux", "freebsd", "openbsd"] { + assert_eq!( + data_dir_for(other, home), + home.join(".local/share"), + "{other}" + ); + } + assert_eq!(ROOT_NAME, "wanderbooks"); +} + +#[test] +fn this_systems_default_root_is_its_data_folder_and_the_name() { + let home = Path::new("/home/someone"); + assert_eq!( + default_root(home), + data_dir_for(std::env::consts::OS, home).join(ROOT_NAME) + ); +} + +/// The program's install folders (the Windows installer's, the macOS +/// bundle's, the Linux packages') are never above or below a data folder. +#[test] +fn no_data_folder_is_inside_an_install_folder() { + let home = Path::new("/home/someone"); + let installs = [ + // Windows: the installer's InstallDir (deploy/packaging/windows). + home.join("AppData/Local/Programs/Wanderbooks"), + // macOS: the app (drag-to-Applications), and the per-user folder. + Path::new("/Applications/Wanderbooks.app").to_path_buf(), + home.join("Applications/Wanderbooks.app"), + // Linux packages. + Path::new("/usr/bin").to_path_buf(), + Path::new("/usr/share/wanderbooks").to_path_buf(), + ]; + for os in ["macos", "windows", "linux"] { + let data = data_dir_for(os, home).join(ROOT_NAME); + for install in &installs { + assert!( + !data.starts_with(install) && !install.starts_with(&data), + "{os}: {} and {} overlap", + data.display(), + install.display() + ); + } + } +}