//! The packages (plan P15 unit C1; WB-pkg-1 to WB-pkg-7): what can be checked //! on Linux about the Windows installer, the macOS app, the Linux entries and //! the one source of the version. For Windows and macOS the release workflow //! is the real test (each packaging step fails loudly); these tests keep the //! files it builds from true to the rules. //! //! ```text //! GOAL: an installed Wanderbooks is one executable at a stable path, named //! Wanderbooks everywhere a person sees it, with the licences beside //! it; installing a newer one over it, or uninstalling it, never //! touches the person's books. //! SOURCE: WB-pkg-1 (installs like a normal program), WB-pkg-2 (one //! executable, a stable bridge path), WB-pkg-6 (one version, one //! name), WB-pkg-7 (the release carries the licences); D-77. //! INVARIANTS: //! 1. The version is written once (ledger-cli's Cargo.toml); the packaging //! scripts read it through deploy/packaging/version.sh. //! 2. The Windows installer is per user (no administrator), installs one //! program, and its uninstaller names nothing but what the installer //! put in its own folder: no recursive removal, no wildcard, no data //! folder. //! 3. The macOS bundle holds one program at Contents/MacOS/wanderbooks, an //! Info.plist whose identifier, name, version and executable agree with //! the scripts, and the licences. //! 4. The Linux desktop entries launch plain `wanderbooks` (an AppImage's //! launcher appends what it is given) with the icon, the name, and the //! window's app id. //! 5. Every package carries LICENSE-MIT, LICENSE-APACHE and //! THIRD-PARTY-NOTICES.txt. //! SCENARIOS: //! 1. The bundle script run on Linux with a stand-in program makes a //! bundle whose plist and files match the rules above. //! ``` use std::path::{Path, PathBuf}; use std::process::Command; fn root() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("../..") } fn read(path: &str) -> String { std::fs::read_to_string(root().join(path)).unwrap_or_else(|e| panic!("{path}: {e}")) } /// The lines of `text` that are not comments (`;` for NSIS, `#` for shell /// and TOML). fn code(text: &str) -> Vec<&str> { text.lines() .map(str::trim) .filter(|l| !l.is_empty() && !l.starts_with(';') && !l.starts_with('#')) .collect() } const LICENCE_FILES: [&str; 3] = ["LICENSE-MIT", "LICENSE-APACHE", "THIRD-PARTY-NOTICES.txt"]; // ---- the version, written once --------------------------------------------------- #[cfg(unix)] #[test] fn the_version_is_written_once_and_every_script_reads_it_from_there() { let out = Command::new("sh") .arg(root().join("deploy/packaging/version.sh")) .output() .unwrap(); assert!(out.status.success(), "{out:?}"); let said = String::from_utf8(out.stdout).unwrap(); assert_eq!(said.trim(), env!("CARGO_PKG_VERSION")); // The Linux packager's own file carries a version the build script // overwrites; it must not drift from the source. let packager = read("deploy/packaging/packager.toml"); let line = code(&packager) .into_iter() .find(|l| l.starts_with("version = ")) .unwrap(); assert_eq!(line, format!("version = \"{}\"", env!("CARGO_PKG_VERSION"))); // No packaging script or workflow assigns a literal version. for file in [ "deploy/packaging/windows/build-windows.sh", "deploy/packaging/macos/build-macos.sh", ".github/workflows/release.yml", ] { for line in code(&read(file)) { let assigns = line.starts_with("version=") || line.starts_with("version:"); let literal = line.split(['=', ':']).nth(1).is_some_and(|v| { v.trim() .trim_matches('"') .chars() .next() .is_some_and(|c| c.is_ascii_digit()) }); assert!(!(assigns && literal), "{file}: a literal version: {line}"); } } for file in [ "deploy/packaging/windows/build-windows.sh", "deploy/packaging/macos/build-macos.sh", "deploy/packaging/build-linux.sh", ] { let text = read(file); assert!( text.contains("version.sh") || text.contains("ledger-cli/Cargo.toml"), "{file} does not read the version from its one source" ); } } #[cfg(unix)] #[test] fn a_version_that_is_not_three_numbers_is_refused() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("deploy/packaging")).unwrap(); std::fs::create_dir_all(dir.path().join("crates/ledger-cli")).unwrap(); std::fs::copy( root().join("deploy/packaging/version.sh"), dir.path().join("deploy/packaging/version.sh"), ) .unwrap(); for (toml, ok) in [ ("version = \"0.2.0\"\n", true), ("version = \"0.2.0-rc1\"\n", false), ("version = \"two\"\n", false), ("", false), ] { std::fs::write(dir.path().join("crates/ledger-cli/Cargo.toml"), toml).unwrap(); let out = Command::new("sh") .arg(dir.path().join("deploy/packaging/version.sh")) .output() .unwrap(); assert_eq!(out.status.success(), ok, "{toml:?}: {out:?}"); } } // ---- Windows -------------------------------------------------------------------- #[test] fn the_windows_installer_is_per_user_and_installs_one_program() { let nsi = read("deploy/packaging/windows/wanderbooks.nsi"); let lines = code(&nsi); let has = |needle: &str| lines.iter().any(|l| l.contains(needle)); // No administrator prompt; everything in the person's own profile. assert!(has("RequestExecutionLevel user")); assert!(has(r#"InstallDir "$LOCALAPPDATA\Programs\Wanderbooks""#)); assert!( !lines.iter().any(|l| l.contains("HKLM")), "per-machine registry" ); assert!(!has("SetShellVarContext all"), "all users' Start Menu"); // A Start Menu entry named Wanderbooks, with the icon. assert!(has( r#"CreateShortCut "$SMPROGRAMS\${NAME}.lnk" "$INSTDIR\wanderbooks.exe""# )); assert!(has(r#"!define NAME "Wanderbooks""#)); assert!(has("wanderbooks.ico")); // An uninstaller, listed in Settings > Apps, whose name is the product's. assert!(has("WriteUninstaller")); assert!(has(r#""DisplayName" "${NAME}""#)); assert!(has("UninstallString")); // One program: the only .exe installed besides the uninstaller. let programs: Vec<&&str> = lines .iter() .filter(|l| l.starts_with("File ") && l.contains(".exe")) .collect(); assert_eq!(programs.len(), 1, "{programs:?}"); assert!(programs[0].contains("/oname=wanderbooks.exe")); // The licences travel with it. for file in LICENCE_FILES { assert!(has(&format!("/oname={file}")), "{file} is not installed"); } } /// WB-pkg-1: the uninstaller removes what the installer put in its own /// folder and nothing else; the books are in the data folder, which it never /// names. #[test] fn the_uninstaller_never_removes_the_books() { let nsi = read("deploy/packaging/windows/wanderbooks.nsi"); let lines = code(&nsi); let start = lines .iter() .position(|l| l.starts_with(r#"Section "Uninstall""#)) .expect("an uninstall section"); let section = &lines[start..]; let mut removals = 0; for line in section { let word = line.split_whitespace().next().unwrap_or(""); if matches!(word, "Delete" | "RMDir" | "DeleteRegKey" | "DeleteRegValue") { removals += 1; assert!(!line.contains("/r"), "a recursive removal: {line}"); assert!( !line.contains('*') && !line.contains('?'), "a wildcard: {line}" ); let ok = line.contains("$INSTDIR") || line.contains("$SMPROGRAMS") || line.contains("${UNINSTALL_KEY}") || line.contains("${APP_KEY}"); assert!( ok, "removes something that is not the program's own: {line}" ); } } assert!(removals >= 6, "the uninstaller removes the installed files"); // The data folder, the roaming settings and the keys are named nowhere // in the script's code (the comments say where the books are). for line in &lines { for data in [ "$LOCALAPPDATA\\wanderbooks", "$APPDATA", "\\link", "legacy", "books\\", ] { assert!(!line.contains(data), "{data} named: {line}"); } } // Only the registry keys of its own. let keys: Vec<&&str> = lines .iter() .filter(|l| l.starts_with("!define") && l.contains("_KEY")) .collect(); assert_eq!(keys.len(), 2, "{keys:?}"); assert!(keys[0].contains(r"Software\Microsoft\Windows\CurrentVersion\Uninstall\Wanderbooks")); assert!(keys[1].contains(r"Software\Wanderbooks")); } /// If NSIS is installed here, the script compiles with stand-in files /// (the release workflow compiles it for real on Windows). #[test] fn the_installer_script_compiles_when_nsis_is_here() { let Some(makensis) = std::env::var_os("PATH").and_then(|p| { std::env::split_paths(&p) .map(|d| d.join("makensis")) .find(|f| f.is_file()) }) else { eprintln!( "makensis is not installed here: the Windows job of the release workflow compiles the installer" ); return; }; let dir = tempfile::tempdir().unwrap(); let p = dir.path(); std::fs::write(p.join("wanderbooks.exe"), b"MZ").unwrap(); for f in LICENCE_FILES { std::fs::write(p.join(f), b"x").unwrap(); } std::fs::copy( root().join("deploy/packaging/icons/wanderbooks.ico"), p.join("wanderbooks.ico"), ) .unwrap(); let out = Command::new(makensis) .arg("-V2") .arg(format!("-DVERSION={}", env!("CARGO_PKG_VERSION"))) .arg(format!("-DPROGRAM={}", p.join("wanderbooks.exe").display())) .arg(format!("-DICON={}", p.join("wanderbooks.ico").display())) .arg(format!("-DLICENSES={}", p.display())) .arg(format!("-DOUTFILE={}", p.join("setup.exe").display())) .arg(root().join("deploy/packaging/windows/wanderbooks.nsi")) .output() .unwrap(); assert!( out.status.success(), "{}", String::from_utf8_lossy(&out.stdout) ); assert!(p.join("setup.exe").metadata().unwrap().len() > 0); } /// WB-pkg-2: on Windows the one executable is built for the GUI subsystem /// (no console window for the app), and gives a terminal its words back. #[test] fn the_windows_program_opens_no_console_window_and_still_speaks_in_a_terminal() { let main = read("crates/ledger-cli/src/main.rs"); assert!( main.contains( r#"#![cfg_attr(all(windows, feature = "gui"), windows_subsystem = "windows")]"# ) ); assert!(main.contains("console::attach_parent_console()")); let console = read("crates/ledger-cli/src/console.rs"); assert!(console.contains("AttachConsole(ATTACH_PARENT_PROCESS)")); // The server child the app starts has no console window of its own. assert!(read("crates/ledger-host/src/app_server.rs").contains("NO_WINDOW")); } // ---- macOS ---------------------------------------------------------------------- /// The ``/value pairs of a plist's top-level dict, as text. Good enough /// for the plain plists here (no nesting). fn plist(text: &str) -> Vec<(String, String)> { let mut pairs = Vec::new(); let mut key: Option = None; for line in text.lines().map(str::trim) { if let Some(k) = line .strip_prefix("") .and_then(|l| l.strip_suffix("")) { assert!(key.is_none(), "a key with no value: {k}"); key = Some(k.to_string()); } else if let Some(k) = key.take_if(|_| line.starts_with('<') && !line.starts_with("