diff --git a/crates/summit-cli/tests/install_script.rs b/crates/summit-cli/tests/install_script.rs new file mode 100644 index 0000000..ba7c791 --- /dev/null +++ b/crates/summit-cli/tests/install_script.rs @@ -0,0 +1,78 @@ +//! `install.sh --dry-run` as a new user would run it: a prefix in their own home must need no sudo, `--no-deps` +//! must not touch the system package manager, and the dry run must show the venv where the real run makes it. + +use std::path::PathBuf; +use std::process::Command; + +fn script() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../install.sh") +} + +fn dry_run(prefix: &std::path::Path, extra: &[&str]) -> String { + let out = Command::new("sh") + .arg(script()) + .arg("--dry-run") + .args(extra) + .env("SUMMIT_PREFIX", prefix) + .env("SUMMIT_SRC_DIR", PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..")) + .output() + .expect("run install.sh"); + assert!(out.status.success(), "install.sh failed: {}", String::from_utf8_lossy(&out.stderr)); + String::from_utf8_lossy(&out.stdout).into_owned() +} + +#[test] +fn user_prefix_needs_no_sudo() { + let dir = std::env::temp_dir().join(format!("summit-install-test-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let prefix = dir.join("prefix"); + let out = dry_run(&prefix, &["--no-deps"]); + let _ = std::fs::remove_dir_all(&dir); + assert!(!out.contains("sudo"), "a prefix the user can write needs no sudo:\n{out}"); + assert!(out.contains("install -Dm755"), "{out}"); +} + +#[test] +fn no_deps_skips_the_package_manager() { + let dir = std::env::temp_dir().join(format!("summit-install-test-nd-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let out = dry_run(&dir.join("p"), &["--no-deps"]); + let with = dry_run(&dir.join("p"), &[]); + let _ = std::fs::remove_dir_all(&dir); + for pm in ["pacman", "apt-get", "dnf", "zypper"] { + assert!(!out.contains(pm), "--no-deps must not call {pm}:\n{out}"); + } + assert!(out.contains("--no-deps"), "{out}"); + // Without it the installer still installs the system packages (on a known distro). + assert!(with.contains("Installing build and runtime dependencies") || with.contains("skipping package manager"), "{with}"); +} + +#[test] +fn dry_run_shows_the_venv_in_the_install_prefix() { + let dir = std::env::temp_dir().join(format!("summit-install-test-venv-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let prefix = dir.join("p"); + let out = dry_run(&prefix, &["--no-deps"]); + let _ = std::fs::remove_dir_all(&dir); + let want = format!("uv venv --python 3.12 {}/share/summit/engine/.venv", prefix.display()); + assert!(out.contains(&want), "expected `{want}` in:\n{out}"); +} + +#[test] +fn installs_the_binaries_from_where_cargo_puts_them() { + // A user with CARGO_TARGET_DIR set has no $SRC/target: the first real run on the owner's desktop died on + // `install: cannot stat .../src/target/release/summit`. + let dir = std::env::temp_dir().join(format!("summit-install-test-tgt-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let out = Command::new("sh") + .arg(script()) + .args(["--dry-run", "--no-deps"]) + .env("SUMMIT_PREFIX", dir.join("p")) + .env("SUMMIT_SRC_DIR", PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..")) + .env("CARGO_TARGET_DIR", "/somewhere/else/target") + .output() + .unwrap(); + let _ = std::fs::remove_dir_all(&dir); + let out = String::from_utf8_lossy(&out.stdout); + assert!(out.contains("install -Dm755 /somewhere/else/target/release/summit "), "{out}"); +} diff --git a/install.sh b/install.sh index 709770a..056b934 100755 --- a/install.sh +++ b/install.sh @@ -12,11 +12,14 @@ # Flags: # --yes, -y don't ask before sudo steps # --dry-run print what would happen, change nothing +# --no-deps don't install system packages (no sudo for pacman/apt/dnf/zypper); you have the build tools +# and the runtime tools already # --uninstall remove what this installer installed # --help, -h show this message # # Environment: -# SUMMIT_PREFIX where to install (default /usr/local) +# SUMMIT_PREFIX where to install (default /usr/local). sudo is used for the copy only when the user can't +# write there; a prefix under your home needs none. # SUMMIT_SRC_DIR build this checkout as it is (no clone, no git pull) set -eu @@ -33,6 +36,7 @@ ICON_DIR="$PREFIX/share/icons/hicolor/64x64/apps" ASSUME_YES=0 DRY_RUN=0 UNINSTALL=0 +NO_DEPS=0 log() { printf '%s\n' "$*"; } step() { printf '\n==> %s\n' "$*"; } @@ -63,8 +67,24 @@ run_sudo() { run sudo "$@" } +# Copying into the prefix: sudo only when this user can't write there (the /usr/local default), never for a prefix +# in the user's own home. +can_write_prefix() { + d="$PREFIX" + while [ ! -e "$d" ] && [ "$d" != "/" ]; do d="$(dirname "$d")"; done + [ -w "$d" ] +} + +run_prefix() { + if [ "$(id -u)" -eq 0 ] || can_write_prefix; then + run "$@" + else + run_sudo "$@" + fi +} + usage() { - sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//' + sed -n '2,/^set -eu/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//' } # Tools Summit calls at run time. A missing one only disables a feature, so a failure here warns instead of @@ -76,6 +96,7 @@ for arg in "$@"; do case "$arg" in --yes|-y) ASSUME_YES=1 ;; --dry-run) DRY_RUN=1 ;; + --no-deps) NO_DEPS=1 ;; --uninstall) UNINSTALL=1 ;; --help|-h) usage; exit 0 ;; *) die "unknown flag: $arg (see --help)" ;; @@ -158,6 +179,11 @@ detect_session() { # --------------------------------------------------------------------------- install_dependencies() { + if [ "$NO_DEPS" -eq 1 ]; then + step "Skipping system packages (--no-deps)" + check_runtime_tools + return + fi step "Installing build and runtime dependencies for $DISTRO_FAMILY" case "$DISTRO_FAMILY" in arch) @@ -287,6 +313,16 @@ install_native_package() { esac } +# Where cargo put the binaries: not $SRC_DIR/target when CARGO_TARGET_DIR or build.target-dir is set. +cargo_target_dir() { + t="" + if [ "$DRY_RUN" -eq 0 ]; then + t="$(cargo metadata --format-version 1 --no-deps --manifest-path "$SRC_DIR/Cargo.toml" 2>/dev/null \ + | grep -o '"target_directory":"[^"]*"' | sed 's/^"target_directory":"//; s/"$//' || true)" + fi + printf '%s' "${t:-${CARGO_TARGET_DIR:-$SRC_DIR/target}}" +} + build_from_source() { step "No usable release package found, building from source" ensure_rust @@ -302,14 +338,15 @@ build_from_source() { run git clone "$REPO_GIT" "$SRC_DIR" fi run cargo build --release --manifest-path "$SRC_DIR/Cargo.toml" -p summit -p summit-gui - run_sudo install -Dm755 "$SRC_DIR/target/release/summit" "$BIN_DIR/summit" - run_sudo install -Dm755 "$SRC_DIR/target/release/summit-gui" "$BIN_DIR/summit-gui" - run_sudo mkdir -p "$SHARE_DIR/engine" - run_sudo cp -r "$SRC_DIR/engine/." "$SHARE_DIR/engine/" + target_dir="$(cargo_target_dir)" + run_prefix install -Dm755 "$target_dir/release/summit" "$BIN_DIR/summit" + run_prefix install -Dm755 "$target_dir/release/summit-gui" "$BIN_DIR/summit-gui" + run_prefix mkdir -p "$SHARE_DIR/engine" + run_prefix cp -r "$SRC_DIR/engine/." "$SHARE_DIR/engine/" install_desktop_entry "$SRC_DIR/packaging" # The engine's venv is made in there next, without sudo (uv and the Python it downloads live in the user's home), # so the folder has to belong to the user who runs Summit. - if [ "$(id -u)" -ne 0 ]; then + if [ "$(id -u)" -ne 0 ] && ! can_write_prefix; then run_sudo chown -R "$(id -u):$(id -g)" "$SHARE_DIR/engine" fi } @@ -318,13 +355,13 @@ install_desktop_entry() { # The app menu entry, pointing at the installed binary by its full path (a custom SUMMIT_PREFIX is often not # on the menu's PATH). pkg="$1" - run_sudo install -Dm644 "$pkg/icons/summit.png" "$ICON_DIR/summit.png" + run_prefix install -Dm644 "$pkg/icons/summit.png" "$ICON_DIR/summit.png" desktop_tmp="$(mktemp)" sed "s|^Exec=summit-gui|Exec=$BIN_DIR/summit-gui|" "$pkg/summit.desktop" > "$desktop_tmp" - run_sudo install -Dm644 "$desktop_tmp" "$APPS_DIR/summit.desktop" + run_prefix install -Dm644 "$desktop_tmp" "$APPS_DIR/summit.desktop" rm -f "$desktop_tmp" if command -v update-desktop-database >/dev/null 2>&1; then - run_sudo update-desktop-database "$APPS_DIR" || true + run_prefix update-desktop-database "$APPS_DIR" || true fi } @@ -350,10 +387,10 @@ setup_engine_venv() { step "Setting up the engine's Python environment (uv, Python 3.12)" ensure_uv engine_dir="$SHARE_DIR/engine" - if [ ! -d "$engine_dir" ]; then + if [ ! -d "$engine_dir" ] && [ "$DRY_RUN" -eq 0 ]; then engine_dir="$SRC_DIR/engine" fi - if [ ! -d "$engine_dir" ]; then + if [ ! -d "$engine_dir" ] && [ "$DRY_RUN" -eq 0 ]; then log " engine sources not found, skipping venv setup" return fi @@ -413,8 +450,8 @@ do_install() { do_uninstall() { step "Uninstalling Summit" - run_sudo rm -f "$BIN_DIR/summit" "$BIN_DIR/summit-gui" "$APPS_DIR/summit.desktop" "$ICON_DIR/summit.png" - run_sudo rm -rf "$SHARE_DIR" + run_prefix rm -f "$BIN_DIR/summit" "$BIN_DIR/summit-gui" "$APPS_DIR/summit.desktop" "$ICON_DIR/summit.png" + run_prefix rm -rf "$SHARE_DIR" log "Left in place: $SRC_DIR (build cache), ~/.config/summit, ~/.local/share/summit (your data)" }