Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion configurator/src/app/daemon_setup/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::env;
use std::path::PathBuf;
use std::process::Command;

use wayscriber::env_vars::PATH_ENV;

#[derive(Debug)]
pub(super) struct CommandCapture {
pub(super) success: bool,
Expand All @@ -14,7 +16,7 @@ pub(super) fn command_available(program: &str) -> bool {
}

pub(super) fn find_in_path(binary_name: &str) -> Option<PathBuf> {
let path_var = env::var_os("PATH")?;
let path_var = env::var_os(PATH_ENV)?;
env::split_paths(&path_var)
.map(|directory| directory.join(binary_name))
.find(|path| path.exists())
Expand Down
9 changes: 5 additions & 4 deletions configurator/src/app/daemon_setup/hyprland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ mod tests {
use super::*;
use std::env;
use std::sync::Mutex;
use wayscriber::env_vars::HOME_ENV;

static ENV_MUTEX: Mutex<()> = Mutex::new(());

Expand Down Expand Up @@ -451,9 +452,9 @@ mod tests {
.unwrap_or_else(|poisoned| poisoned.into_inner());
let tmp = crate::test_temp::tempdir().unwrap();
let home = tmp.path();
let prev_home = env::var_os("HOME");
let prev_home = env::var_os(HOME_ENV);
unsafe {
env::set_var("HOME", home);
env::set_var(HOME_ENV, home);
}

let absolute = home
Expand All @@ -467,8 +468,8 @@ mod tests {
));

match prev_home {
Some(value) => unsafe { env::set_var("HOME", value) },
None => unsafe { env::remove_var("HOME") },
Some(value) => unsafe { env::set_var(HOME_ENV, value) },
None => unsafe { env::remove_var(HOME_ENV) },
}
}

Expand Down
12 changes: 6 additions & 6 deletions configurator/src/app/daemon_setup/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
use std::process::Command;

use crate::models::DesktopEnvironment;
use wayscriber::env_vars::{BIN_ENV, PATH_ENV};
use wayscriber::runtime_capabilities::{
RUNTIME_CAPABILITIES_FLAG, RuntimeCapabilities, parse_runtime_capabilities,
};
Expand Down Expand Up @@ -73,7 +74,7 @@ pub(super) fn require_systemctl_available() -> Result<(), String> {
if command_available("systemctl") {
Ok(())
} else {
Err("systemctl is not available in PATH.".to_string())
Err(format!("systemctl is not available in {PATH_ENV}."))
}
}

Expand Down Expand Up @@ -311,14 +312,13 @@ pub(super) fn resolve_wayscriber_binary_path() -> Result<PathBuf, String> {
return Ok(path);
}

Err(
"Unable to locate `wayscriber` binary. Set WAYSCRIBER_BIN or install `wayscriber` in PATH."
.to_string(),
)
Err(format!(
"Unable to locate `wayscriber` binary. Set {BIN_ENV} or install `wayscriber` in {PATH_ENV}."
))
}

fn explicit_wayscriber_binary_path() -> Option<PathBuf> {
env::var_os("WAYSCRIBER_BIN")
env::var_os(BIN_ENV)
.map(PathBuf::from)
.filter(|path| path.exists())
}
Expand Down
19 changes: 11 additions & 8 deletions configurator/src/app/daemon_setup/shortcut.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs;

use crate::models::{DesktopEnvironment, ShortcutApplyCapability, ShortcutBackend};
use wayscriber::env_vars::PATH_ENV;
use wayscriber::shortcut_hint::{
GNOME_MEDIA_KEYS_KEY, GNOME_MEDIA_KEYS_SCHEMA, GNOME_WAYSCRIBER_KEYBINDING_PATH,
PORTAL_APP_ID_ENV, PORTAL_SHORTCUT_ENV, PORTAL_SHORTCUT_OPT_IN_ENV, PortalShortcutDropInState,
Expand Down Expand Up @@ -163,7 +164,7 @@ fn require_gsettings_available() -> Result<(), String> {
if command_available("gsettings") {
Ok(())
} else {
Err("gsettings is not available in PATH.".to_string())
Err(format!("gsettings is not available in {PATH_ENV}."))
}
}

Expand Down Expand Up @@ -376,25 +377,27 @@ mod tests {

#[test]
fn parse_portal_shortcut_reads_dropin_value() {
let content = "[Service]\nEnvironment=\"WAYSCRIBER_PORTAL_SHORTCUT=<Ctrl><Shift>g\"\n";
let content = format!("[Service]\nEnvironment=\"{PORTAL_SHORTCUT_ENV}=<Ctrl><Shift>g\"\n");
assert_eq!(
parse_portal_shortcut_from_dropin(content),
parse_portal_shortcut_from_dropin(&content),
Some("<Ctrl><Shift>g".to_string())
);
}

#[test]
fn parse_portal_shortcut_ignores_blank_value() {
let content = "[Service]\nEnvironment=\"WAYSCRIBER_PORTAL_SHORTCUT= \"\n";
assert_eq!(parse_portal_shortcut_from_dropin(content), None);
let content = format!("[Service]\nEnvironment=\"{PORTAL_SHORTCUT_ENV}= \"\n");
assert_eq!(parse_portal_shortcut_from_dropin(&content), None);
}

#[test]
fn render_portal_shortcut_dropin_includes_explicit_opt_in_marker() {
let rendered = render_portal_shortcut_dropin("<Ctrl><Shift>g", PORTAL_APP_ID);
assert!(rendered.contains("Environment=\"WAYSCRIBER_ENABLE_PORTAL_SHORTCUTS=1\""));
assert!(rendered.contains("Environment=\"WAYSCRIBER_PORTAL_SHORTCUT=<Ctrl><Shift>g\""));
assert!(rendered.contains("Environment=\"WAYSCRIBER_PORTAL_APP_ID=wayscriber\""));
assert!(rendered.contains(&format!("Environment=\"{PORTAL_SHORTCUT_OPT_IN_ENV}=1\"")));
assert!(rendered.contains(&format!(
"Environment=\"{PORTAL_SHORTCUT_ENV}=<Ctrl><Shift>g\""
)));
assert!(rendered.contains(&format!("Environment=\"{PORTAL_APP_ID_ENV}=wayscriber\"")));
}

#[test]
Expand Down
27 changes: 13 additions & 14 deletions configurator/src/app/session_catalog/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ mod tests {
DaemonRuntimeStatus, DesktopEnvironment, LightShortcutApplyCapability,
ShortcutApplyCapability, ShortcutBackend,
};
use wayscriber::env_vars::{CATALOG_HOOKS_TEST_ENV, XDG_DATA_HOME_ENV, XDG_RUNTIME_DIR_ENV};

use super::*;

Expand All @@ -105,13 +106,13 @@ mod tests {
impl EnvGuard {
fn set_roots(path: &Path) -> Self {
let guard = crate::test_env::lock();
let catalog_hooks = std::env::var_os("WAYSCRIBER_ENABLE_CATALOG_HOOKS_IN_TESTS");
let xdg_data_home = std::env::var_os("XDG_DATA_HOME");
let xdg_runtime_dir = std::env::var_os("XDG_RUNTIME_DIR");
let catalog_hooks = std::env::var_os(CATALOG_HOOKS_TEST_ENV);
let xdg_data_home = std::env::var_os(XDG_DATA_HOME_ENV);
let xdg_runtime_dir = std::env::var_os(XDG_RUNTIME_DIR_ENV);
unsafe {
std::env::set_var("WAYSCRIBER_ENABLE_CATALOG_HOOKS_IN_TESTS", path);
std::env::set_var("XDG_DATA_HOME", path);
std::env::set_var("XDG_RUNTIME_DIR", path);
std::env::set_var(CATALOG_HOOKS_TEST_ENV, path);
std::env::set_var(XDG_DATA_HOME_ENV, path);
std::env::set_var(XDG_RUNTIME_DIR_ENV, path);
}
Self {
catalog_hooks,
Expand All @@ -125,18 +126,16 @@ mod tests {
impl Drop for EnvGuard {
fn drop(&mut self) {
match self.catalog_hooks.take() {
Some(value) => unsafe {
std::env::set_var("WAYSCRIBER_ENABLE_CATALOG_HOOKS_IN_TESTS", value)
},
None => unsafe { std::env::remove_var("WAYSCRIBER_ENABLE_CATALOG_HOOKS_IN_TESTS") },
Some(value) => unsafe { std::env::set_var(CATALOG_HOOKS_TEST_ENV, value) },
None => unsafe { std::env::remove_var(CATALOG_HOOKS_TEST_ENV) },
}
match self.xdg_data_home.take() {
Some(value) => unsafe { std::env::set_var("XDG_DATA_HOME", value) },
None => unsafe { std::env::remove_var("XDG_DATA_HOME") },
Some(value) => unsafe { std::env::set_var(XDG_DATA_HOME_ENV, value) },
None => unsafe { std::env::remove_var(XDG_DATA_HOME_ENV) },
}
match self.xdg_runtime_dir.take() {
Some(value) => unsafe { std::env::set_var("XDG_RUNTIME_DIR", value) },
None => unsafe { std::env::remove_var("XDG_RUNTIME_DIR") },
Some(value) => unsafe { std::env::set_var(XDG_RUNTIME_DIR_ENV, value) },
None => unsafe { std::env::remove_var(XDG_RUNTIME_DIR_ENV) },
}
}
}
Expand Down
27 changes: 13 additions & 14 deletions configurator/src/app/session_catalog/move_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ mod tests {
DaemonRuntimeStatus, DesktopEnvironment, LightShortcutApplyCapability,
ShortcutApplyCapability, ShortcutBackend,
};
use wayscriber::env_vars::{CATALOG_HOOKS_TEST_ENV, XDG_DATA_HOME_ENV, XDG_RUNTIME_DIR_ENV};

use super::*;

Expand All @@ -150,13 +151,13 @@ mod tests {
impl EnvGuard {
fn set_roots(path: &Path) -> Self {
let guard = crate::test_env::lock();
let catalog_hooks = std::env::var_os("WAYSCRIBER_ENABLE_CATALOG_HOOKS_IN_TESTS");
let xdg_data_home = std::env::var_os("XDG_DATA_HOME");
let xdg_runtime_dir = std::env::var_os("XDG_RUNTIME_DIR");
let catalog_hooks = std::env::var_os(CATALOG_HOOKS_TEST_ENV);
let xdg_data_home = std::env::var_os(XDG_DATA_HOME_ENV);
let xdg_runtime_dir = std::env::var_os(XDG_RUNTIME_DIR_ENV);
unsafe {
std::env::set_var("WAYSCRIBER_ENABLE_CATALOG_HOOKS_IN_TESTS", path);
std::env::set_var("XDG_DATA_HOME", path);
std::env::set_var("XDG_RUNTIME_DIR", path);
std::env::set_var(CATALOG_HOOKS_TEST_ENV, path);
std::env::set_var(XDG_DATA_HOME_ENV, path);
std::env::set_var(XDG_RUNTIME_DIR_ENV, path);
}
Self {
catalog_hooks,
Expand All @@ -170,18 +171,16 @@ mod tests {
impl Drop for EnvGuard {
fn drop(&mut self) {
match self.catalog_hooks.take() {
Some(value) => unsafe {
std::env::set_var("WAYSCRIBER_ENABLE_CATALOG_HOOKS_IN_TESTS", value)
},
None => unsafe { std::env::remove_var("WAYSCRIBER_ENABLE_CATALOG_HOOKS_IN_TESTS") },
Some(value) => unsafe { std::env::set_var(CATALOG_HOOKS_TEST_ENV, value) },
None => unsafe { std::env::remove_var(CATALOG_HOOKS_TEST_ENV) },
}
match self.xdg_data_home.take() {
Some(value) => unsafe { std::env::set_var("XDG_DATA_HOME", value) },
None => unsafe { std::env::remove_var("XDG_DATA_HOME") },
Some(value) => unsafe { std::env::set_var(XDG_DATA_HOME_ENV, value) },
None => unsafe { std::env::remove_var(XDG_DATA_HOME_ENV) },
}
match self.xdg_runtime_dir.take() {
Some(value) => unsafe { std::env::set_var("XDG_RUNTIME_DIR", value) },
None => unsafe { std::env::remove_var("XDG_RUNTIME_DIR") },
Some(value) => unsafe { std::env::set_var(XDG_RUNTIME_DIR_ENV, value) },
None => unsafe { std::env::remove_var(XDG_RUNTIME_DIR_ENV) },
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions configurator/src/app/session_catalog/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::MutexGuard;
use crate::models::{
DesktopEnvironment, LightShortcutApplyCapability, ShortcutApplyCapability, ShortcutBackend,
};
use wayscriber::env_vars::XDG_RUNTIME_DIR_ENV;

struct RuntimeEnvGuard {
previous: Option<OsString>,
Expand All @@ -15,9 +16,9 @@ struct RuntimeEnvGuard {
impl RuntimeEnvGuard {
fn set_xdg_runtime_dir(path: &Path) -> Self {
let guard = crate::test_env::lock();
let previous = std::env::var_os("XDG_RUNTIME_DIR");
let previous = std::env::var_os(XDG_RUNTIME_DIR_ENV);
unsafe {
std::env::set_var("XDG_RUNTIME_DIR", path);
std::env::set_var(XDG_RUNTIME_DIR_ENV, path);
}
Self {
previous,
Expand All @@ -29,8 +30,8 @@ impl RuntimeEnvGuard {
impl Drop for RuntimeEnvGuard {
fn drop(&mut self) {
match self.previous.take() {
Some(value) => unsafe { std::env::set_var("XDG_RUNTIME_DIR", value) },
None => unsafe { std::env::remove_var("XDG_RUNTIME_DIR") },
Some(value) => unsafe { std::env::set_var(XDG_RUNTIME_DIR_ENV, value) },
None => unsafe { std::env::remove_var(XDG_RUNTIME_DIR_ENV) },
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions configurator/src/app/update/session_catalog/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::models::{
DesktopEnvironment, LightShortcutApplyCapability, SessionCatalogState, ShortcutApplyCapability,
ShortcutBackend,
};
use wayscriber::env_vars::XDG_RUNTIME_DIR_ENV;

struct RuntimeEnvGuard {
previous: Option<OsString>,
Expand All @@ -16,9 +17,9 @@ struct RuntimeEnvGuard {
impl RuntimeEnvGuard {
fn set_xdg_runtime_dir(path: &Path) -> Self {
let guard = crate::test_env::lock();
let previous = std::env::var_os("XDG_RUNTIME_DIR");
let previous = std::env::var_os(XDG_RUNTIME_DIR_ENV);
unsafe {
std::env::set_var("XDG_RUNTIME_DIR", path);
std::env::set_var(XDG_RUNTIME_DIR_ENV, path);
}
Self {
previous,
Expand All @@ -30,8 +31,8 @@ impl RuntimeEnvGuard {
impl Drop for RuntimeEnvGuard {
fn drop(&mut self) {
match self.previous.take() {
Some(value) => unsafe { std::env::set_var("XDG_RUNTIME_DIR", value) },
None => unsafe { std::env::remove_var("XDG_RUNTIME_DIR") },
Some(value) => unsafe { std::env::set_var(XDG_RUNTIME_DIR_ENV, value) },
None => unsafe { std::env::remove_var(XDG_RUNTIME_DIR_ENV) },
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions configurator/src/models/daemon.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use wayscriber::env_vars::{
HYPRLAND_INSTANCE_SIGNATURE_ENV, XDG_CURRENT_DESKTOP_ENV, XDG_SESSION_DESKTOP_ENV,
};
use wayscriber::shortcut_hint::{
PortalShortcutDropInState, ShortcutRuntimeBackend, ShortcutRuntimeInputs, is_gnome_desktop,
resolve_shortcut_runtime_backend,
Expand All @@ -13,11 +16,11 @@ pub enum DesktopEnvironment {

impl DesktopEnvironment {
pub fn detect_current() -> Self {
if std::env::var_os("HYPRLAND_INSTANCE_SIGNATURE").is_some() {
if std::env::var_os(HYPRLAND_INSTANCE_SIGNATURE_ENV).is_some() {
return Self::Hyprland;
}
let current = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default();
let session = std::env::var("XDG_SESSION_DESKTOP").unwrap_or_default();
let current = std::env::var(XDG_CURRENT_DESKTOP_ENV).unwrap_or_default();
let session = std::env::var(XDG_SESSION_DESKTOP_ENV).unwrap_or_default();
Self::from_desktop_strings(&current, &session)
}

Expand Down
13 changes: 7 additions & 6 deletions src/app/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod tests {
use std::env;
use std::sync::Mutex;

const TEST_FLAG_ENV: &str = "WAYSCRIBER_TEST_FLAG";
static ENV_MUTEX: Mutex<()> = Mutex::new(());

#[test]
Expand All @@ -26,16 +27,16 @@ mod tests {
for value in ["1", "true", "yes", "on", "TrUe"] {
// SAFETY: serialized via ENV_MUTEX
unsafe {
env::set_var("WAYSCRIBER_TEST_FLAG", value);
env::set_var(TEST_FLAG_ENV, value);
}
assert!(
env_flag_enabled("WAYSCRIBER_TEST_FLAG"),
env_flag_enabled(TEST_FLAG_ENV),
"expected '{value}' to be treated as truthy"
);
}

unsafe {
env::remove_var("WAYSCRIBER_TEST_FLAG");
env::remove_var(TEST_FLAG_ENV);
}
}

Expand All @@ -48,16 +49,16 @@ mod tests {
for value in ["0", "false", "no", "off", "", "random"] {
// SAFETY: serialized via ENV_MUTEX
unsafe {
env::set_var("WAYSCRIBER_TEST_FLAG", value);
env::set_var(TEST_FLAG_ENV, value);
}
assert!(
!env_flag_enabled("WAYSCRIBER_TEST_FLAG"),
!env_flag_enabled(TEST_FLAG_ENV),
"expected '{value}' to be treated as falsey"
);
}

unsafe {
env::remove_var("WAYSCRIBER_TEST_FLAG");
env::remove_var(TEST_FLAG_ENV);
}
}
}
Loading
Loading