From 4065d8842beb5d1648131bd5b1951adf7a27b5b7 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 16 Aug 2026 16:25:35 +0900 Subject: refactor(env): route Shirabe's own env reads through the shim These sites have no PHP counterpart to mirror, so they read std::env directly. Going through the shim's getenv() keeps every environment read in one place and lets a lint forbid the direct form. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-php-rpc/src/composer_runtime.rs | 2 +- crates/shirabe-php-shim/src/net.rs | 6 +++++- crates/shirabe/src/main.rs | 3 ++- crates/shirabe/src/signal.rs | 4 ++-- crates/shirabe/tests/event_dispatcher/event_dispatcher_test.rs | 2 +- crates/shirabe/tests/plugin/e2e_extension_installer_test.rs | 8 ++++---- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/shirabe-php-rpc/src/composer_runtime.rs b/crates/shirabe-php-rpc/src/composer_runtime.rs index d5024fad..afa7c6e1 100644 --- a/crates/shirabe-php-rpc/src/composer_runtime.rs +++ b/crates/shirabe-php-rpc/src/composer_runtime.rs @@ -51,7 +51,7 @@ fn path_to_string(directory: std::path::PathBuf) -> anyhow::Result { /// The checkout that stands in for the bundle, if `OVERRIDE_ENV` names one. Both the worker and /// the Rust side read the runtime from there instead. fn override_directory() -> anyhow::Result> { - let Some(directory) = std::env::var_os(OVERRIDE_ENV) else { + let Some(directory) = shirabe_php_shim::getenv(OVERRIDE_ENV) else { return Ok(None); }; let directory = std::path::PathBuf::from(directory); diff --git a/crates/shirabe-php-shim/src/net.rs b/crates/shirabe-php-shim/src/net.rs index e5d3fcf0..1519e6e0 100644 --- a/crates/shirabe-php-shim/src/net.rs +++ b/crates/shirabe-php-shim/src/net.rs @@ -7,7 +7,11 @@ pub fn gethostname() -> String { .ok() .map(|s| s.trim().to_string()) .filter(|s| !s.is_empty()) - .or_else(|| std::env::var("HOSTNAME").ok().filter(|s| !s.is_empty())) + .or_else(|| { + crate::getenv("HOSTNAME") + .map(|s| s.to_string_lossy().into_owned()) + .filter(|s| !s.is_empty()) + }) .unwrap_or_else(|| "localhost".to_string()) } diff --git a/crates/shirabe/src/main.rs b/crates/shirabe/src/main.rs index c4d9d313..fb0b31f1 100644 --- a/crates/shirabe/src/main.rs +++ b/crates/shirabe/src/main.rs @@ -5,9 +5,10 @@ use std::io::IsTerminal as _; /// Initialize a tracing subscriber from the environment variable `$SHIRABE_TRACING`. fn init_tracing() { - let Ok(directives) = std::env::var("SHIRABE_TRACING") else { + let Some(directives) = shirabe_php_shim::getenv("SHIRABE_TRACING") else { return; }; + let directives = directives.to_string_lossy(); if directives.is_empty() { return; } diff --git a/crates/shirabe/src/signal.rs b/crates/shirabe/src/signal.rs index 26297cc7..6ae8dfc1 100644 --- a/crates/shirabe/src/signal.rs +++ b/crates/shirabe/src/signal.rs @@ -172,7 +172,7 @@ mod tests { #[test] fn exit_with_last_signal_kills_by_the_signal() { - if std::env::var("SIGNAL_TEST_CHILD").is_ok() { + if shirabe_php_shim::getenv("SIGNAL_TEST_CHILD").is_some() { let signals = SignalSubscription::new(); raise_sigint(); signals.exit_with_last_signal(); @@ -188,7 +188,7 @@ mod tests { #[test] fn the_default_disposition_returns_once_no_subscription_is_left() { - if std::env::var("SIGNAL_TEST_CHILD").is_ok() { + if shirabe_php_shim::getenv("SIGNAL_TEST_CHILD").is_some() { drop(SignalSubscription::new()); raise_sigint(); unreachable!("SIGINT must terminate the process"); diff --git a/crates/shirabe/tests/event_dispatcher/event_dispatcher_test.rs b/crates/shirabe/tests/event_dispatcher/event_dispatcher_test.rs index e3f5c9a5..90df0624 100644 --- a/crates/shirabe/tests/event_dispatcher/event_dispatcher_test.rs +++ b/crates/shirabe/tests/event_dispatcher/event_dispatcher_test.rs @@ -140,7 +140,7 @@ fn ensure_php_binary() -> bool { { return true; } - let path = std::env::var_os("PATH").unwrap_or_default(); + let path = shirabe_php_shim::getenv("PATH").unwrap_or_default(); for dir in std::env::split_paths(&path) { let candidate = dir.join("php"); if candidate.is_file() { diff --git a/crates/shirabe/tests/plugin/e2e_extension_installer_test.rs b/crates/shirabe/tests/plugin/e2e_extension_installer_test.rs index 9a95888c..71b40ff1 100644 --- a/crates/shirabe/tests/plugin/e2e_extension_installer_test.rs +++ b/crates/shirabe/tests/plugin/e2e_extension_installer_test.rs @@ -8,7 +8,7 @@ //! any of these is missing. Test runs themselves are offline: the fixture project resolves //! everything from local repositories. -use crate::plugin_installer_test::{lock_php_worker, php_runtime_available}; +use crate::php_worker::{lock_php_worker, php_runtime_available}; use indexmap::IndexMap; use std::path::{Path, PathBuf}; use tempfile::TempDir; @@ -21,9 +21,9 @@ fn fixture_dir() -> PathBuf { /// the worker). Absent checkout means the oracle cannot run; the test returns early, /// following the convention of the non-mock tests in `shirabe-php-rpc`. pub(crate) fn upstream_composer_bin() -> Option { - let root = match std::env::var("SHIRABE_COMPOSER_PHP_DIR") { - Ok(dir) => PathBuf::from(dir), - Err(_) => Path::new(env!("CARGO_MANIFEST_DIR")).join("../../composer"), + let root = match shirabe_php_shim::getenv("SHIRABE_COMPOSER_PHP_DIR") { + Some(dir) => PathBuf::from(dir), + None => Path::new(env!("CARGO_MANIFEST_DIR")).join("../../composer"), }; let bin = root.join("bin/composer"); if bin.is_file() && root.join("vendor/autoload.php").is_file() { -- cgit v1.3.1-4-g156e