phoebus_os/installer-rs/crates/phoebus-installer/tests/sys_cli.rs
Phoebus c9c17cc16f
Some checks are pending
ci / test (push) Waiting to run
fix(sys): parse natural install package lanes
2026-10-02 05:19:09 -07:00

946 lines
30 KiB
Rust

//! CLI-level regression tests for the `sys` entry paths and contracts.
//!
//! Natural language is the default interaction (calling the AI assistant).
//! Deterministic operation is explicitly selected with `--legacy`.
//! JSON action entry points require `--legacy` or `--broker`.
use std::process::Command;
fn sys() -> Command {
Command::new(env!("CARGO_BIN_EXE_sys"))
}
fn effective_uid_is_root() -> bool {
std::process::Command::new("id")
.arg("-u")
.output()
.ok()
.map(|o| String::from_utf8_lossy(&o.stdout).trim() == "0")
.unwrap_or(false)
}
#[test]
fn natural_option_like_operand_is_rejected_in_legacy_mode() {
let out = sys()
.args(["--legacy", "--", "install", "--color=n"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("could not parse")
|| stderr.contains("invalid")
|| stderr.contains("legacy"),
"stderr: {stderr}"
);
}
#[test]
fn json_without_legacy_or_broker_is_rejected() {
let out = sys()
.args(["--action", r#"{"action":"history"}"#])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("require --legacy") || stderr.contains("--broker"),
"stderr: {stderr}"
);
}
#[test]
fn json_option_like_operand_is_rejected_in_legacy_mode() {
let out = sys()
.args([
"--legacy",
"--action",
r#"{"action":"install_package","atom":"-C"}"#,
])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("invalid"), "stderr: {stderr}");
}
#[test]
fn broker_allows_action_execution() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["--broker", "--action", r#"{"action":"history"}"#])
.output()
.expect("run sys");
assert!(
out.status.success(),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
assert!(
!String::from_utf8_lossy(&out.stderr).contains("Checking the exact effects"),
"broker protocol must not emit human progress"
);
}
#[test]
fn read_only_history_succeeds_deterministically_with_legacy() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["history", "--legacy"])
.output()
.expect("run sys");
assert!(
out.status.success(),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("no snapshot history") || stdout.contains("snapshots:"),
"stdout: {stdout}"
);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("Checking the exact effects"), "{stderr}");
assert!(
stderr.contains("Running the action and checking its result"),
"{stderr}"
);
assert!(stderr.contains("✓"), "{stderr}");
}
#[test]
fn history_without_legacy_and_no_model_gives_actionable_legacy_guidance() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.env("PHOEBUS_AI_PROVIDER", "none")
.arg("--root")
.arg(root.path())
.arg("history")
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("sys history --legacy"),
"expected legacy suggestion in stderr: {stderr}"
);
}
#[test]
fn version_is_reported() {
let out = sys().arg("--version").output().expect("run sys");
assert!(out.status.success());
assert!(String::from_utf8_lossy(&out.stdout).contains("sys"));
}
#[test]
fn appimage_and_custom_ebuild_inventories_are_safe_and_empty_on_a_fresh_root() {
let root = tempfile::tempdir().expect("temp root");
for args in [vec!["appimage", "list"], vec!["ebuilds"]] {
let out = sys()
.arg("--root")
.arg(root.path())
.arg("--legacy")
.args(&args)
.output()
.expect("run sys");
assert!(
out.status.success(),
"{args:?} stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("no managed AppImages")
|| stdout.contains("no Phoebus-managed custom ebuilds"),
"{args:?} stdout: {stdout}"
);
}
}
#[test]
fn invalid_snap_name_via_json_fails_closed() {
let out = sys()
.args([
"--legacy",
"--action",
r#"{"action":"snap_install","name":"Bad Name"}"#,
])
.output()
.expect("run sys");
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("invalid"));
}
#[test]
fn invalid_appimage_hash_via_json_fails_closed() {
let out = sys()
.args([
"--legacy",
"--action",
r#"{"action":"appimage_acquire","name":"x","url":"https://e/x","sha256":"00"}"#,
])
.output()
.expect("run sys");
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("SHA-256"));
}
#[test]
fn a_system_wide_preference_without_root_is_refused_before_any_command() {
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let personal = tempfile::tempdir().expect("personal dir");
let mutating = sys()
.arg("--root")
.arg(root.path())
.env("PHOEBUS_PERSONAL_DIR", personal.path())
.args([
"--legacy",
"--action",
r#"{"action":"set_package_preference","name":"firefox","backend":"flatpak","system_wide":true}"#,
])
.output()
.expect("run sys");
assert!(!mutating.status.success());
assert!(String::from_utf8_lossy(&mutating.stderr).contains("root"));
// A privileged system-wide change must never write `/etc`.
assert!(!root
.path()
.join("etc/phoebus/package-preferences.toml")
.exists());
}
#[test]
fn a_personal_preference_is_saved_by_a_normal_account_outside_etc() {
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let personal = tempfile::tempdir().expect("personal dir");
let out = sys()
.arg("--root")
.arg(root.path())
.env("PHOEBUS_PERSONAL_DIR", personal.path())
.args([
"--legacy",
"--action",
r#"{"action":"set_package_preference","name":"firefox","backend":"flatpak"}"#,
])
.output()
.expect("run sys");
assert!(
out.status.success(),
"personal preference must succeed: {}",
String::from_utf8_lossy(&out.stderr)
);
// It is stored in the user's own 0600 config, never in root-owned /etc.
let saved = personal
.path()
.join(".config/phoebus/package-preferences.toml");
assert!(saved.is_file(), "expected {}", saved.display());
let text = std::fs::read_to_string(&saved).unwrap();
assert!(
text.contains("firefox") && text.contains("flatpak"),
"{text}"
);
assert!(!root
.path()
.join("etc/phoebus/package-preferences.toml")
.exists());
}
#[test]
fn a_personal_preference_does_not_require_the_root_owned_transaction_log() {
if effective_uid_is_root() {
return;
}
use std::os::unix::fs::PermissionsExt;
let root = tempfile::tempdir().expect("temp root");
let personal = tempfile::tempdir().expect("personal dir");
// Simulate the installed system: /var/lib/phoebus is root-owned, so a normal
// account cannot write the system transaction log.
let tx_dir = root.path().join("var/lib/phoebus");
std::fs::create_dir_all(&tx_dir).expect("create tx dir");
std::fs::set_permissions(&tx_dir, std::fs::Permissions::from_mode(0o555)).expect("chmod");
let out = sys()
.arg("--root")
.arg(root.path())
.env("PHOEBUS_PERSONAL_DIR", personal.path())
.args([
"--legacy",
"--action",
r#"{"action":"set_package_preference","name":"firefox","backend":"flatpak"}"#,
])
.output()
.expect("run sys");
assert!(
out.status.success(),
"a personal preference must succeed without the system transaction log: {}",
String::from_utf8_lossy(&out.stderr)
);
// The preference was saved; no system transaction was attempted.
assert!(personal
.path()
.join(".config/phoebus/package-preferences.toml")
.is_file());
assert!(
!tx_dir.join("transactions.toml").exists(),
"no system transaction should be attempted for a personal preference"
);
}
#[test]
fn a_personal_preference_is_refused_when_the_initiating_uid_differs_from_the_euid() {
if effective_uid_is_root() {
return;
}
let uid = std::process::Command::new("id")
.arg("-u")
.output()
.ok()
.and_then(|o| {
String::from_utf8_lossy(&o.stdout)
.trim()
.parse::<u32>()
.ok()
})
.expect("current uid");
let root = tempfile::tempdir().expect("temp root");
let personal = tempfile::tempdir().expect("personal dir");
// Simulate `sudo` on behalf of another user without root: the initiating
// uid differs from the effective uid, so the write must be refused rather
// than creating a file owned by the wrong user.
let out = sys()
.arg("--root")
.arg(root.path())
.env("PHOEBUS_PERSONAL_DIR", personal.path())
.env("SUDO_UID", (uid + 1).to_string())
.args([
"--legacy",
"--action",
r#"{"action":"set_package_preference","name":"firefox","backend":"flatpak"}"#,
])
.output()
.expect("run sys");
assert!(!out.status.success());
assert!(
String::from_utf8_lossy(&out.stderr).contains("refusing to write"),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
assert!(!personal
.path()
.join(".config/phoebus/package-preferences.toml")
.exists());
}
#[test]
fn conflicting_backend_flags_are_rejected_by_the_cli() {
let out = sys()
.args(["--flatpak", "--nix", "install", "some-app"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("cannot be used with")
|| stderr.contains("conflict")
|| stderr.contains("not allowed"),
"stderr: {stderr}"
);
}
#[test]
fn a_backend_flag_without_an_install_is_refused() {
let out = sys()
.args(["--flatpak", "history", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("only apply to"), "stderr: {stderr}");
}
#[test]
fn legacy_install_without_source_flag_is_refused_with_choices() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["install", "firefox", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("requires an explicit package source flag"),
"stderr: {stderr}"
);
assert!(
stderr.contains("--flatpak") && stderr.contains("--portage"),
"stderr must show explicit options: {stderr}"
);
}
#[test]
fn source_flag_without_legacy_enters_ai_workflow_and_suggests_legacy() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.env("PHOEBUS_AI_PROVIDER", "none")
.arg("--root")
.arg(root.path())
.args(["install", "firefox", "--flatpak"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("sys install firefox --legacy --flatpak"),
"stderr must give legacy alternative: {stderr}"
);
}
#[test]
fn natural_install_lane_is_not_part_of_the_package_name() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.env("PHOEBUS_AI_PROVIDER", "none")
.arg("--root")
.arg(root.path())
.args(["install", "chatgpt", "desktop", "through", "nix"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("sys search chatgpt --legacy --nix"),
"{stderr}"
);
assert!(
!stderr.contains("sys install chatgpt desktop through nix --legacy"),
"{stderr}"
);
}
#[test]
fn an_explicit_backend_never_falls_back_to_another_lane() {
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.arg("--root")
.arg(root.path())
.args(["install", "some-app", "--legacy", "--portage"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("cannot satisfy") || stderr.contains("no real candidate"),
"stderr: {stderr}"
);
}
#[test]
fn an_unqualified_install_without_a_model_offers_options_and_installs_nothing() {
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.env("PHOEBUS_AI_PROVIDER", "none")
.env("PHOEBUS_AI_BACKEND", "direct")
.arg("--root")
.arg(root.path())
.args(["install", "firefox"])
.output()
.expect("run sys");
assert!(
!out.status.success(),
"an unqualified install must not proceed without a model"
);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("No connected model") || stderr.contains("no AI provider"),
"stderr: {stderr}"
);
assert!(
stderr.contains("--flatpak") && stderr.contains("--portage"),
"the offer must name the explicit backends: {stderr}"
);
// Nothing may have been installed or executed.
assert!(!root
.path()
.join("var/lib/phoebus/transactions.toml")
.exists());
}
#[test]
fn please_install_enters_same_ai_workflow() {
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.env("PHOEBUS_AI_PROVIDER", "none")
.env("PHOEBUS_AI_BACKEND", "direct")
.arg("--root")
.arg(root.path())
.args(["please", "install", "firefox"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("No connected model") || stderr.contains("no AI provider"),
"stderr: {stderr}"
);
assert!(
stderr.contains("--flatpak") && stderr.contains("--portage"),
"the offer must name the explicit backends: {stderr}"
);
}
#[test]
fn the_help_lists_every_backend_flag() {
let out = sys().arg("--help").output().expect("run sys");
assert!(out.status.success());
let stdout = String::from_utf8_lossy(&out.stdout);
for flag in [
"--portage",
"--flatpak",
"--nix",
"--snap",
"--appimage",
"--ebuild",
"--legacy",
"--broker",
] {
assert!(stdout.contains(flag), "help is missing {flag}");
}
}
#[test]
fn choose_never_invents_a_candidate() {
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.arg("--root")
.arg(root.path())
.args(["choose", "some-package", "--legacy"])
.output()
.expect("run sys");
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
if out.status.success() {
assert!(stdout.contains("Selected package:"), "stdout: {stdout}");
assert!(stdout.contains("source:"), "stdout: {stdout}");
} else {
assert!(
stderr.contains("no compatible") || stderr.contains("sys:"),
"stderr: {stderr}"
);
}
}
#[test]
fn remove_without_installed_copy_fails_closed() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["remove", "nonexistent-app", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("is not installed") || stderr.contains("found"),
"stderr: {stderr}"
);
}
#[test]
fn remove_ambiguous_copies_requires_explicit_lane() {
let root = tempfile::tempdir().expect("temp root");
// Create an AppImage record
let appimage_dir = root.path().join("opt/phoebus/appimages");
std::fs::create_dir_all(&appimage_dir).unwrap();
std::fs::write(
appimage_dir.join("shared-app.toml"),
"name = \"shared-app\"\nversion = \"1.0\"\nsha256 = \"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\"\nprovenance = \"https://example.com/app\"\narch = \"x86_64\"\nsize_bytes = 100\ninstalled_unix = 0\n",
).unwrap();
// Create a Portage VDB entry
let portage_pkg_dir = root.path().join("var/db/pkg/app-misc/shared-app-1.0");
std::fs::create_dir_all(&portage_pkg_dir).unwrap();
std::fs::write(portage_pkg_dir.join("CONTENTS"), "").unwrap();
// Removal without source flag must fail indicating ambiguity
let out = sys()
.arg("--root")
.arg(root.path())
.args(["remove", "shared-app", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("multiple lanes") && stderr.contains("explicitly"),
"stderr must report ambiguity: {stderr}"
);
// Disambiguating with --appimage allows targeting specifically that copy
let disambiguated = sys()
.arg("--root")
.arg(root.path())
.args(["remove", "shared-app", "--legacy", "--appimage"])
.output()
.expect("run sys");
let out_str = String::from_utf8_lossy(&disambiguated.stdout);
let err_str = String::from_utf8_lossy(&disambiguated.stderr);
assert!(
out_str.contains("Removal plan:") || err_str.contains("root"),
"stdout: {out_str}, stderr: {err_str}"
);
}
#[test]
fn broker_execute_plan_rejects_missing_file() {
let out = sys()
.args([
"--broker",
"--execute-plan",
"/tmp/nonexistent-plan-id.json",
])
.output()
.expect("run sys");
assert!(!out.status.success());
}
#[test]
fn default_install_without_a_model_stops_and_offers_legacy() {
let root = tempfile::tempdir().expect("temp root");
let plans = tempfile::tempdir().expect("plans");
let out = sys()
.arg("--root")
.arg(root.path())
.env("PHOEBUS_AI_PROVIDER", "")
.env("PHOEBUS_AI_API_KEY_FILE", "")
.env("PHOEBUS_PLANS_DIR", plans.path())
.args(["install", "firefox"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("legacy"), "stderr: {stderr}");
assert!(!stderr.contains("installed firefox"), "stderr: {stderr}");
}
#[test]
fn default_removal_without_a_model_stops_and_offers_legacy() {
let root = tempfile::tempdir().expect("temp root");
let plans = tempfile::tempdir().expect("plans");
let out = sys()
.arg("--root")
.arg(root.path())
.env("PHOEBUS_AI_PROVIDER", "")
.env("PHOEBUS_AI_API_KEY_FILE", "")
.env("PHOEBUS_PLANS_DIR", plans.path())
.args(["remove", "firefox"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("legacy"), "stderr: {stderr}");
}
#[test]
fn broker_execute_plan_requires_a_plan_uid() {
// Even a valid-looking id is refused without the initiating uid; the
// broker never falls back to an arbitrary path or to `/`.
let out = sys()
.args(["--broker", "--execute-plan", "plan-inst-deadbeef"])
.output()
.expect("run sys");
assert!(!out.status.success());
}
// ---------------------------------------------------------------------------
// AI-default routing contract for the catalogue commands
// ---------------------------------------------------------------------------
fn no_model(cmd: &mut Command) -> &mut Command {
cmd.env("PHOEBUS_AI_PROVIDER", "none")
.env("PHOEBUS_AI_API_KEY_FILE", "")
}
#[test]
fn search_without_legacy_enters_the_ai_workflow_and_never_runs_deterministically() {
let root = tempfile::tempdir().expect("temp root");
let out = no_model(&mut sys())
.arg("--root")
.arg(root.path())
.args(["search", "chrome"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("sys search chrome --legacy"),
"must offer the exact legacy alternative: {stderr}"
);
assert!(
!stdout.contains("Package catalogue"),
"the deterministic search must not run without --legacy"
);
assert!(!root
.path()
.join("var/lib/phoebus/transactions.toml")
.exists());
}
#[test]
fn list_info_and_updates_without_legacy_enter_the_ai_workflow() {
for (args, needle) in [
(vec!["list"], "sys list --legacy"),
(vec!["info", "firefox"], "sys info firefox --legacy"),
(vec!["updates"], "sys updates --legacy"),
] {
let root = tempfile::tempdir().expect("temp root");
let out = no_model(&mut sys())
.arg("--root")
.arg(root.path())
.args(&args)
.output()
.expect("run sys");
assert!(!out.status.success(), "expected failure for {args:?}");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains(needle), "{args:?} -> {stderr}");
}
}
#[test]
fn a_source_flag_alone_constrains_the_ai_and_never_bypasses_it() {
let root = tempfile::tempdir().expect("temp root");
let out = no_model(&mut sys())
.arg("--root")
.arg(root.path())
.args(["search", "chrome", "--flatpak"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stdout.contains("Package catalogue"),
"a source flag must not bypass the model"
);
assert!(
stderr.contains("sys search chrome --legacy --flatpak"),
"the deterministic alternative must keep the source flag: {stderr}"
);
}
#[test]
fn legacy_search_is_deterministic_and_never_contacts_a_model() {
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
// A configured remote provider with a dead endpoint must be ignored under
// --legacy: no network is attempted.
let out = sys()
.env("PATH", empty.path())
.env("PHOEBUS_AI_PROVIDER", "deepseek")
.env("PHOEBUS_AI_ENDPOINT", "http://127.0.0.1:1/never")
.env("PHOEBUS_AI_API_KEY_FILE", "")
.arg("--root")
.arg(root.path())
.args(["search", "chrome", "--legacy"])
.output()
.expect("run sys");
assert!(
out.status.success(),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("Package catalogue for `chrome`"),
"{stdout}"
);
}
#[test]
fn legacy_list_reads_the_vdb_and_is_read_only() {
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let dir = root.path().join("var/db/pkg/app-editors/vim-9.1.0");
std::fs::create_dir_all(&dir).expect("vdb dir");
let out = sys()
.env("PATH", empty.path())
.arg("--root")
.arg(root.path())
.args(["list", "--legacy"])
.output()
.expect("run sys");
assert!(
out.status.success(),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("app-editors/vim"), "{stdout}");
assert!(
!root
.path()
.join("var/lib/phoebus/transactions.toml")
.exists(),
"a read-only inventory must not write a transaction"
);
}
#[test]
fn legacy_inventory_honours_a_source_filter() {
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
std::fs::create_dir_all(root.path().join("var/db/pkg/app-editors/vim-9.1.0")).expect("vdb dir");
let out = sys()
.env("PATH", empty.path())
.arg("--root")
.arg(root.path())
.args(["list", "--legacy", "--flatpak"])
.output()
.expect("run sys");
assert!(out.status.success());
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("[flatpak]"), "{stdout}");
assert!(
!stdout.contains("app-editors/vim"),
"a Flatpak-filtered inventory must not list Portage packages: {stdout}"
);
}
#[test]
fn legacy_updates_check_is_read_only() {
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.arg("--root")
.arg(root.path())
.args(["updates", "--legacy"])
.output()
.expect("run sys");
assert!(
out.status.success(),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("Unified Update Overview"), "{stdout}");
assert!(!root
.path()
.join("var/lib/phoebus/transactions.toml")
.exists());
}
#[test]
fn json_search_without_legacy_or_broker_fails_closed() {
let out = sys()
.args([
"--action",
r#"{"action":"search_packages","query":"chrome"}"#,
])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("--legacy") || stderr.contains("--broker"),
"{stderr}"
);
}
#[test]
fn mutating_catalogue_maintenance_requires_root() {
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
for args in [
vec!["depclean", "--legacy"],
vec!["apply", "updates", "--legacy"],
vec!["clean", "cache", "--legacy"],
] {
let out = sys()
.arg("--root")
.arg(root.path())
.args(&args)
.output()
.expect("run sys");
assert!(!out.status.success(), "expected refusal for {args:?}");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("require root"), "{args:?} -> {stderr}");
}
}
#[test]
fn source_build_and_tuned_kernel_require_root_like_other_mutations() {
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
// The fast install choices never imply a saved preference: no lane flag
// is needed because the conversion target lane is explicit already.
let out = sys()
.arg("--root")
.arg(root.path())
.args([
"--legacy",
"--action",
r#"{"action":"convert_to_source","app":"firefox"}"#,
])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("require root"), "stderr: {stderr}");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["build", "tuned", "kernel", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("require root"), "stderr: {stderr}");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["build", "balanced", "kernel", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("require root"), "stderr: {stderr}");
}