The peer (T2) attach path had a per-pair access-control layer (`[daemon.policy].allow_inbound`): a target-side allow-list of daemon ids permitted to broker-attach, enforced inside JWT verification, hot-reloadable via SIGHUP / `POST /api/policy/reload`, with a `maestro policy` CLI. On a flat-trust perimeter (the operator's VPN/firewall is the boundary for which hosts connect) that granularity is unused, and deny-by-default made it config friction — cross-daemon attach silently failed until every peer was listed. Remove the allow-list; keep the registry-minted pair-attach JWT and ALL its binding checks (iss=registry, aud=target, sid=session, exp). The firewall gates host->port reachability; it does not stop a reachable host from attaching to a session it wasn't handed or replaying a token elsewhere — that's the JWT's job and it stays. Only the `sub in allow_inbound` check is dropped. Removed: DaemonPolicySection / [daemon.policy].allow_inbound (config + daemon), policy.rs (SIGHUP listener + GET/POST /api/policy), the `maestro policy` CLI, PairAttachVerifier's allow-list field/reload/snapshot + JwtError::NotAllowed, and the e2e broker_rejects_when_target_doesnt_allow_inbound test. The NAP-265 pre-upgrade-502 path stays covered by broker_attach_returns_502_on_perimeter_mismatch. Also dropped the unit's ExecReload=kill -HUP: with no SIGHUP handler left, default SIGHUP disposition is terminate, so wiring `systemctl reload` to it would kill the daemon. Re-introducing a per-pair gate later is a verify-time change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
587 lines
19 KiB
Rust
587 lines
19 KiB
Rust
//! Daemon view over the unified Maestro config.
|
|
//!
|
|
//! Reads `[shared]` (registry_url, token) and `[daemon]` (daemon_id,
|
|
//! hostname, daemon_url, bind_addr, state_dir, worktree_root) from
|
|
//! `~/.config/maestro/config.toml`.
|
|
//!
|
|
//! Precedence per field: **env > file > default**. The daemon refuses
|
|
//! to start if `registry_url`, `daemon_url`, or `token` are absent
|
|
//! from both env and file.
|
|
//!
|
|
//! Default config path: `~/.config/maestro/config.toml` (override via
|
|
//! `$MAESTRO_CONFIG`). If the file is missing entirely the daemon
|
|
//! errors with a pointer at `maestrod configure` — legacy
|
|
//! `~/.config/maestrod/config.toml` is no longer read.
|
|
|
|
use std::net::SocketAddr;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use maestro_config::{derive_daemon_ws_url, MaestroConfigFile};
|
|
use maestro_protocol::PerimeterCandidate;
|
|
|
|
pub use maestro_config::{
|
|
default_config_path, resolve_config_path, DaemonSection, SharedSection, CONFIG_PATH_ENV,
|
|
};
|
|
|
|
/// Default bind address when neither env nor file specify one.
|
|
pub const DEFAULT_BIND_ADDR: &str = "127.0.0.1:7070";
|
|
|
|
/// Env var that overrides `[shared].registry_url`.
|
|
pub const ENV_REGISTRY_URL: &str = "MAESTRO_REGISTRY_URL";
|
|
/// Env var that overrides `[shared].token`.
|
|
pub const ENV_TOKEN: &str = "MAESTRO_TOKEN";
|
|
/// Env var that overrides `[daemon].daemon_id`.
|
|
pub const ENV_DAEMON_ID: &str = "MAESTRO_DAEMON_ID";
|
|
/// Env var that overrides `[daemon].daemon_url`.
|
|
pub const ENV_DAEMON_URL: &str = "MAESTRO_DAEMON_URL";
|
|
/// Env var that overrides `[daemon].bind_addr`.
|
|
pub const ENV_DAEMON_BIND_ADDR: &str = "MAESTRO_DAEMON_BIND_ADDR";
|
|
/// Env var that overrides `[daemon].perimeter_id`.
|
|
pub const ENV_DAEMON_PERIMETER_ID: &str = "MAESTRO_DAEMON_PERIMETER_ID";
|
|
/// Env var that overrides `[daemon].private_addr`.
|
|
pub const ENV_DAEMON_PRIVATE_ADDR: &str = "MAESTRO_DAEMON_PRIVATE_ADDR";
|
|
/// Env var that overrides `[daemon].tmux_tmpdir`.
|
|
pub const ENV_DAEMON_TMUX_TMPDIR: &str = "MAESTRO_DAEMON_TMUX_TMPDIR";
|
|
|
|
fn default_bind_addr() -> SocketAddr {
|
|
DEFAULT_BIND_ADDR.parse().expect("valid default bind_addr")
|
|
}
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum ConfigError {
|
|
#[error(transparent)]
|
|
Inner(#[from] maestro_config::ConfigError),
|
|
#[error("io: {0}")]
|
|
Io(#[from] std::io::Error),
|
|
#[error("missing required config field `{field}` — set it in {path:?} or via env var `{env}`")]
|
|
Missing {
|
|
field: &'static str,
|
|
env: &'static str,
|
|
path: PathBuf,
|
|
},
|
|
#[error("invalid {env} ({input:?}): {source}")]
|
|
InvalidBindAddr {
|
|
env: &'static str,
|
|
input: String,
|
|
source: std::net::AddrParseError,
|
|
},
|
|
}
|
|
|
|
/// Resolved, validated daemon configuration ready to drive the binary.
|
|
#[derive(Debug, Clone)]
|
|
pub struct DaemonConfig {
|
|
pub daemon_id: String,
|
|
pub hostname: String,
|
|
/// Home directory of the OS user the daemon runs as, advertised to clients
|
|
/// so they can prefill a spawn form's working directory. Empty when `$HOME`
|
|
/// can't be resolved (NAP-328).
|
|
pub home_dir: String,
|
|
/// Registry WS URL the daemon dials. Derived from `[shared].registry_url`
|
|
/// via [`maestro_config::derive_daemon_ws_url`].
|
|
pub registry_url: String,
|
|
pub bind_addr: SocketAddr,
|
|
pub daemon_url: String,
|
|
pub token: String,
|
|
pub state_dir: PathBuf,
|
|
pub worktree_root: PathBuf,
|
|
/// Operator-set perimeter identifier. Advertised to the registry in
|
|
/// heartbeats so peers can dial this daemon over T2 when they share it.
|
|
pub perimeter_id: Option<String>,
|
|
/// Daemon's reachable address on the declared perimeter. Only meaningful
|
|
/// when `perimeter_id` is set.
|
|
pub private_addr: Option<String>,
|
|
/// Per-session PTY replay buffer cap in bytes (NAP-229). Each new attach
|
|
/// receives this much of the prior PTY output before the live stream.
|
|
/// Default `pty::DEFAULT_REPLAY_BYTES`; `0` disables.
|
|
pub pty_replay_bytes: usize,
|
|
/// `TMUX_TMPDIR` exported for every `tmux` invocation (NAP-234).
|
|
/// `None` lets tmux pick its own default (`/tmp/tmux-$UID`); `Some(p)`
|
|
/// forces sockets under `p` so bare `tmux ls` in a matching user shell
|
|
/// sees maestro sessions.
|
|
pub tmux_tmpdir: Option<PathBuf>,
|
|
}
|
|
|
|
impl DaemonConfig {
|
|
pub fn sidecar_dir(&self) -> PathBuf {
|
|
self.state_dir.join("sessions")
|
|
}
|
|
|
|
pub fn fifo_dir(&self) -> PathBuf {
|
|
self.state_dir.join("fifos")
|
|
}
|
|
|
|
/// Combine the two perimeter config keys into the typed advertisement the
|
|
/// daemon broadcasts. A perimeter is only advertised when BOTH the id and
|
|
/// the private address are set — this is the single place the "both or
|
|
/// neither" invariant lives (NAP-272). A half-configured pair is dropped
|
|
/// with a warning rather than silently advertising a useless value.
|
|
pub fn perimeter(&self) -> Option<PerimeterCandidate> {
|
|
match (self.perimeter_id.clone(), self.private_addr.clone()) {
|
|
(Some(perimeter_id), Some(private_addr)) => Some(PerimeterCandidate {
|
|
perimeter_id,
|
|
private_addr,
|
|
}),
|
|
(None, None) => None,
|
|
(id, addr) => {
|
|
tracing::warn!(
|
|
perimeter_id = ?id,
|
|
private_addr = ?addr,
|
|
"ignoring half-configured perimeter — set both [daemon].perimeter_id and \
|
|
[daemon].private_addr, or neither"
|
|
);
|
|
None
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Env-var overrides applied by [`load_config`]. Tests pass explicit
|
|
/// `Option`s; production reads from real env vars in `main`.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct EnvOverrides {
|
|
pub daemon_id: Option<String>,
|
|
pub registry_url: Option<String>,
|
|
pub bind_addr: Option<String>,
|
|
pub daemon_url: Option<String>,
|
|
pub token: Option<String>,
|
|
pub perimeter_id: Option<String>,
|
|
pub private_addr: Option<String>,
|
|
pub tmux_tmpdir: Option<String>,
|
|
}
|
|
|
|
impl EnvOverrides {
|
|
/// Read every supported env var from the process environment.
|
|
pub fn from_env() -> Self {
|
|
Self {
|
|
daemon_id: std::env::var(ENV_DAEMON_ID).ok(),
|
|
registry_url: std::env::var(ENV_REGISTRY_URL).ok(),
|
|
bind_addr: std::env::var(ENV_DAEMON_BIND_ADDR).ok(),
|
|
daemon_url: std::env::var(ENV_DAEMON_URL).ok(),
|
|
token: std::env::var(ENV_TOKEN).ok(),
|
|
perimeter_id: std::env::var(ENV_DAEMON_PERIMETER_ID).ok(),
|
|
private_addr: std::env::var(ENV_DAEMON_PRIVATE_ADDR).ok(),
|
|
tmux_tmpdir: std::env::var(ENV_DAEMON_TMUX_TMPDIR).ok(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Resolve the runtime configuration. Applies env overrides on top of the
|
|
/// unified file, fills defaults, validates the three required fields, and
|
|
/// derives the daemon's WS endpoint from the shared registry URL.
|
|
///
|
|
/// Returns [`maestro_config::ConfigError::MissingFile`] (wrapped) when the
|
|
/// file is absent and not all required fields come from env vars — this is
|
|
/// the hard-cutover error that points the operator at `maestrod configure`.
|
|
pub fn load_config(path: &Path, env: EnvOverrides) -> Result<DaemonConfig, ConfigError> {
|
|
let file = if path.exists() {
|
|
MaestroConfigFile::load(path)?
|
|
} else if env_has_all_required(&env) {
|
|
// Container / systemd path: every required field comes from env,
|
|
// so the on-disk file is genuinely optional.
|
|
MaestroConfigFile::default()
|
|
} else {
|
|
return Err(maestro_config::ConfigError::MissingFile {
|
|
path: path.to_path_buf(),
|
|
}
|
|
.into());
|
|
};
|
|
|
|
let shared = file.shared;
|
|
let daemon = file.daemon;
|
|
|
|
let hostname = daemon.hostname.unwrap_or_else(system_hostname);
|
|
let home_dir = dirs::home_dir()
|
|
.map(|p| p.to_string_lossy().into_owned())
|
|
.unwrap_or_default();
|
|
let daemon_id = env
|
|
.daemon_id
|
|
.or(daemon.daemon_id)
|
|
.unwrap_or_else(|| hostname.clone());
|
|
|
|
let registry_url = derive_daemon_ws_url(&env.registry_url.or(shared.registry_url).ok_or(
|
|
ConfigError::Missing {
|
|
field: "shared.registry_url",
|
|
env: ENV_REGISTRY_URL,
|
|
path: path.to_path_buf(),
|
|
},
|
|
)?)?;
|
|
|
|
let daemon_url = env
|
|
.daemon_url
|
|
.or(daemon.daemon_url)
|
|
.ok_or(ConfigError::Missing {
|
|
field: "daemon.daemon_url",
|
|
env: ENV_DAEMON_URL,
|
|
path: path.to_path_buf(),
|
|
})?;
|
|
let token = env.token.or(shared.token).ok_or(ConfigError::Missing {
|
|
field: "shared.token",
|
|
env: ENV_TOKEN,
|
|
path: path.to_path_buf(),
|
|
})?;
|
|
|
|
let bind_addr = match env.bind_addr {
|
|
Some(raw) => raw.parse().map_err(|source| ConfigError::InvalidBindAddr {
|
|
env: ENV_DAEMON_BIND_ADDR,
|
|
input: raw,
|
|
source,
|
|
})?,
|
|
None => daemon.bind_addr.unwrap_or_else(default_bind_addr),
|
|
};
|
|
|
|
let state_dir = match daemon.state_dir {
|
|
Some(p) => p,
|
|
None => default_state_dir()?,
|
|
};
|
|
let worktree_root = daemon
|
|
.worktree_root
|
|
.unwrap_or_else(|| state_dir.join("worktrees"));
|
|
|
|
let perimeter_id = env.perimeter_id.or(daemon.perimeter_id);
|
|
let private_addr = env.private_addr.or(daemon.private_addr);
|
|
let pty_replay_bytes = daemon
|
|
.pty_replay_bytes
|
|
.unwrap_or(crate::pty::DEFAULT_REPLAY_BYTES);
|
|
let tmux_tmpdir = env
|
|
.tmux_tmpdir
|
|
.map(PathBuf::from)
|
|
.or(daemon.tmux_tmpdir)
|
|
.or_else(dirs::runtime_dir);
|
|
|
|
Ok(DaemonConfig {
|
|
daemon_id,
|
|
hostname,
|
|
home_dir,
|
|
registry_url,
|
|
bind_addr,
|
|
daemon_url,
|
|
token,
|
|
state_dir,
|
|
worktree_root,
|
|
perimeter_id,
|
|
private_addr,
|
|
pty_replay_bytes,
|
|
tmux_tmpdir,
|
|
})
|
|
}
|
|
|
|
fn env_has_all_required(env: &EnvOverrides) -> bool {
|
|
env.registry_url.is_some() && env.daemon_url.is_some() && env.token.is_some()
|
|
}
|
|
|
|
pub fn default_state_dir() -> Result<PathBuf, ConfigError> {
|
|
let base = dirs::state_dir()
|
|
.or_else(dirs::data_local_dir)
|
|
.ok_or(maestro_config::ConfigError::NoConfigDir)?;
|
|
Ok(base.join("maestro"))
|
|
}
|
|
|
|
/// Read the kernel hostname via `gethostname(2)`. Falls back to `"unknown"`
|
|
/// on the (very unlikely) syscall failure.
|
|
pub fn system_hostname() -> String {
|
|
let mut buf = [0u8; 256];
|
|
let r = unsafe { libc::gethostname(buf.as_mut_ptr().cast(), buf.len()) };
|
|
if r != 0 {
|
|
return "unknown".into();
|
|
}
|
|
let nul = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
|
|
String::from_utf8_lossy(&buf[..nul]).into_owned()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn minimal_file() -> &'static str {
|
|
r#"
|
|
[shared]
|
|
registry_url = "https://r.example"
|
|
token = "abc"
|
|
|
|
[daemon]
|
|
daemon_url = "ws://localhost:7070"
|
|
"#
|
|
}
|
|
|
|
#[test]
|
|
fn minimal_file_defaults_the_rest() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(&path, minimal_file()).unwrap();
|
|
let cfg = load_config(&path, EnvOverrides::default()).unwrap();
|
|
assert_eq!(cfg.registry_url, "wss://r.example/daemon");
|
|
assert_eq!(cfg.daemon_url, "ws://localhost:7070");
|
|
assert_eq!(cfg.token, "abc");
|
|
assert_eq!(cfg.bind_addr, default_bind_addr());
|
|
assert!(cfg.state_dir.ends_with("maestro"));
|
|
assert!(cfg.worktree_root.ends_with("worktrees"));
|
|
assert_eq!(cfg.daemon_id, cfg.hostname);
|
|
assert!(!cfg.hostname.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn missing_file_with_full_env_works() {
|
|
// systemd / container scenario: no config file, everything in env.
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("does-not-exist.toml");
|
|
let env = EnvOverrides {
|
|
registry_url: Some("http://r.example".into()),
|
|
daemon_url: Some("ws://x:7070".into()),
|
|
token: Some("t".into()),
|
|
..EnvOverrides::default()
|
|
};
|
|
let cfg = load_config(&path, env).unwrap();
|
|
assert_eq!(cfg.registry_url, "ws://r.example/daemon");
|
|
assert_eq!(cfg.token, "t");
|
|
assert!(!path.exists(), "load_config must not create the file");
|
|
}
|
|
|
|
#[test]
|
|
fn missing_file_without_full_env_errors_with_hard_cutover_message() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("does-not-exist.toml");
|
|
let err = load_config(&path, EnvOverrides::default()).unwrap_err();
|
|
let msg = err.to_string();
|
|
assert!(msg.contains("maestrod configure"), "msg: {msg}");
|
|
assert!(msg.contains("Legacy"), "msg: {msg}");
|
|
}
|
|
|
|
#[test]
|
|
fn env_overrides_file() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(
|
|
&path,
|
|
r#"
|
|
[shared]
|
|
registry_url = "http://from-file"
|
|
token = "from-file"
|
|
|
|
[daemon]
|
|
daemon_id = "from-file"
|
|
daemon_url = "ws://from-file:1"
|
|
bind_addr = "127.0.0.1:1"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let env = EnvOverrides {
|
|
daemon_id: Some("from-env".into()),
|
|
registry_url: Some("https://from-env".into()),
|
|
bind_addr: Some("0.0.0.0:9999".into()),
|
|
daemon_url: Some("ws://from-env:1".into()),
|
|
token: Some("from-env".into()),
|
|
..EnvOverrides::default()
|
|
};
|
|
let cfg = load_config(&path, env).unwrap();
|
|
assert_eq!(cfg.daemon_id, "from-env");
|
|
assert_eq!(cfg.registry_url, "wss://from-env/daemon");
|
|
assert_eq!(cfg.daemon_url, "ws://from-env:1");
|
|
assert_eq!(cfg.token, "from-env");
|
|
assert_eq!(cfg.bind_addr, "0.0.0.0:9999".parse().unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn missing_required_field_reports_path_and_env_name() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(
|
|
&path,
|
|
r#"
|
|
[shared]
|
|
registry_url = "http://r"
|
|
|
|
[daemon]
|
|
daemon_url = "ws://x"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let err = load_config(&path, EnvOverrides::default()).unwrap_err();
|
|
match err {
|
|
ConfigError::Missing {
|
|
field,
|
|
env,
|
|
path: p,
|
|
} => {
|
|
assert_eq!(field, "shared.token");
|
|
assert_eq!(env, ENV_TOKEN);
|
|
assert_eq!(p, path);
|
|
}
|
|
other => panic!("expected Missing(shared.token), got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_env_bind_addr_returns_diagnostic() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(&path, minimal_file()).unwrap();
|
|
let env = EnvOverrides {
|
|
bind_addr: Some("not-an-addr".into()),
|
|
..EnvOverrides::default()
|
|
};
|
|
let err = load_config(&path, env).unwrap_err();
|
|
match err {
|
|
ConfigError::InvalidBindAddr { input, env, .. } => {
|
|
assert_eq!(input, "not-an-addr");
|
|
assert_eq!(env, ENV_DAEMON_BIND_ADDR);
|
|
}
|
|
other => panic!("expected InvalidBindAddr, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn daemon_id_defaults_to_hostname() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(
|
|
&path,
|
|
r#"
|
|
[shared]
|
|
registry_url = "http://r"
|
|
token = "t"
|
|
|
|
[daemon]
|
|
hostname = "vm-42"
|
|
daemon_url = "ws://x"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let cfg = load_config(&path, EnvOverrides::default()).unwrap();
|
|
assert_eq!(cfg.daemon_id, "vm-42");
|
|
assert_eq!(cfg.hostname, "vm-42");
|
|
}
|
|
|
|
#[test]
|
|
fn registry_url_must_be_http_or_https() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(
|
|
&path,
|
|
r#"
|
|
[shared]
|
|
registry_url = "ws://r/daemon"
|
|
token = "t"
|
|
|
|
[daemon]
|
|
daemon_url = "ws://x"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let err = load_config(&path, EnvOverrides::default()).unwrap_err();
|
|
let msg = err.to_string();
|
|
assert!(msg.contains("registry_url"), "msg: {msg}");
|
|
}
|
|
|
|
#[test]
|
|
fn perimeter_fields_round_trip_through_file_and_env() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(
|
|
&path,
|
|
r#"
|
|
[shared]
|
|
registry_url = "http://r"
|
|
token = "t"
|
|
|
|
[daemon]
|
|
daemon_url = "ws://x"
|
|
perimeter_id = "wg-naps62"
|
|
private_addr = "10.0.0.7:7070"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let cfg = load_config(&path, EnvOverrides::default()).unwrap();
|
|
assert_eq!(cfg.perimeter_id.as_deref(), Some("wg-naps62"));
|
|
assert_eq!(cfg.private_addr.as_deref(), Some("10.0.0.7:7070"));
|
|
|
|
// Env beats file.
|
|
let env = EnvOverrides {
|
|
perimeter_id: Some("lan-home".into()),
|
|
private_addr: Some("192.168.1.10:7070".into()),
|
|
..EnvOverrides::default()
|
|
};
|
|
let cfg = load_config(&path, env).unwrap();
|
|
assert_eq!(cfg.perimeter_id.as_deref(), Some("lan-home"));
|
|
assert_eq!(cfg.private_addr.as_deref(), Some("192.168.1.10:7070"));
|
|
}
|
|
|
|
#[test]
|
|
fn perimeter_fields_default_to_none() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(&path, minimal_file()).unwrap();
|
|
let cfg = load_config(&path, EnvOverrides::default()).unwrap();
|
|
assert!(cfg.perimeter_id.is_none());
|
|
assert!(cfg.private_addr.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn system_hostname_is_nonempty() {
|
|
let h = system_hostname();
|
|
assert!(!h.is_empty(), "gethostname returned empty string");
|
|
}
|
|
|
|
// NAP-234: tmux_tmpdir precedence is env > file > dirs::runtime_dir().
|
|
// Tested without mutating the process env (which races other tests) by
|
|
// feeding `EnvOverrides` directly.
|
|
|
|
#[test]
|
|
fn tmux_tmpdir_env_beats_file_and_runtime_dir() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(
|
|
&path,
|
|
r#"
|
|
[shared]
|
|
registry_url = "http://r"
|
|
token = "t"
|
|
|
|
[daemon]
|
|
daemon_url = "ws://x"
|
|
tmux_tmpdir = "/from-file"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let env = EnvOverrides {
|
|
tmux_tmpdir: Some("/from-env".into()),
|
|
..EnvOverrides::default()
|
|
};
|
|
let cfg = load_config(&path, env).unwrap();
|
|
assert_eq!(cfg.tmux_tmpdir.as_deref(), Some(Path::new("/from-env")));
|
|
}
|
|
|
|
#[test]
|
|
fn tmux_tmpdir_file_beats_runtime_dir() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(
|
|
&path,
|
|
r#"
|
|
[shared]
|
|
registry_url = "http://r"
|
|
token = "t"
|
|
|
|
[daemon]
|
|
daemon_url = "ws://x"
|
|
tmux_tmpdir = "/from-file"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let cfg = load_config(&path, EnvOverrides::default()).unwrap();
|
|
assert_eq!(cfg.tmux_tmpdir.as_deref(), Some(Path::new("/from-file")));
|
|
}
|
|
|
|
#[test]
|
|
fn tmux_tmpdir_defaults_to_xdg_runtime_dir_when_unset() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("config.toml");
|
|
std::fs::write(&path, minimal_file()).unwrap();
|
|
let cfg = load_config(&path, EnvOverrides::default()).unwrap();
|
|
// dirs::runtime_dir() is `Some(_)` on any Linux host with
|
|
// $XDG_RUNTIME_DIR set (CI included). When it's `None`, the daemon
|
|
// falls through to None and tmux uses /tmp; either is acceptable —
|
|
// but on this test runner the env var is always present.
|
|
assert_eq!(cfg.tmux_tmpdir, dirs::runtime_dir());
|
|
}
|
|
}
|