Skip to content

Commit 1961ca2

Browse files
committed
fix: resolve clippy warnings and test failures in interactive mode
- Fixed unused variable warnings by prefixing with underscore - Fixed path expansion to properly handle ~/path using strip_prefix - Added async-aware Mutex for Backend.AI environment tests - All clippy warnings resolved and tests pass successfully
1 parent 9eb3fb2 commit 1961ca2

6 files changed

Lines changed: 43 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ ctrlc = "3.4"
4040
[dev-dependencies]
4141
tempfile = "3"
4242
mockito = "1"
43+
once_cell = "1.21.3"

src/commands/interactive.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,12 @@ impl InteractiveCommand {
514514
if let Some(path_str) = path.to_str() {
515515
if path_str.starts_with('~') {
516516
if let Some(home) = dirs::home_dir() {
517-
let expanded = path_str.replacen('~', home.to_str().unwrap(), 1);
518-
return Ok(PathBuf::from(expanded));
517+
// Handle ~ alone or ~/path
518+
if path_str == "~" {
519+
return Ok(home);
520+
} else if let Some(rest) = path_str.strip_prefix("~/") {
521+
return Ok(home.join(rest));
522+
}
519523
}
520524
}
521525
}

src/commands/interactive_signal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub async fn handle_terminal_resize() -> Result<tokio::sync::mpsc::UnboundedRece
9191

9292
/// Terminal state guard for automatic restoration
9393
pub struct TerminalGuard {
94-
original_hook: Option<Box<dyn Fn() + Send + Sync>>,
94+
_original_hook: Option<Box<dyn Fn() + Send + Sync>>,
9595
}
9696

9797
impl Default for TerminalGuard {
@@ -104,7 +104,7 @@ impl TerminalGuard {
104104
/// Create a new terminal guard that will restore terminal state on drop
105105
pub fn new() -> Self {
106106
// Save the current panic hook
107-
let original_hook = std::panic::take_hook();
107+
let _original_hook = std::panic::take_hook();
108108

109109
// Set a custom panic hook that restores terminal before panicking
110110
std::panic::set_hook(Box::new(move |panic_info| {
@@ -116,7 +116,7 @@ impl TerminalGuard {
116116
}));
117117

118118
Self {
119-
original_hook: None, // We can't store the original hook due to lifetime issues
119+
_original_hook: None, // We can't store the original hook due to lifetime issues
120120
}
121121
}
122122

tests/backendai_env_test.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@
1313
// limitations under the License.
1414

1515
use bssh::config::Config;
16+
use once_cell::sync::Lazy;
1617
use std::env;
18+
use tokio::sync::Mutex;
19+
20+
// Global mutex to serialize tests that modify environment variables
21+
static ENV_MUTEX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
1722

1823
#[tokio::test]
1924
async fn test_backendai_env_auto_detection() {
25+
let _guard = ENV_MUTEX.lock().await;
26+
2027
// Save original env vars
2128
let orig_hosts = env::var("BACKENDAI_CLUSTER_HOSTS").ok();
2229
let orig_host = env::var("BACKENDAI_CLUSTER_HOST").ok();
@@ -29,8 +36,12 @@ async fn test_backendai_env_auto_detection() {
2936
env::set_var("BACKENDAI_CLUSTER_ROLE", "main");
3037
}
3138

39+
// Create a temporary directory for the test
40+
let temp_dir = tempfile::tempdir().unwrap();
41+
let nonexistent_path = temp_dir.path().join("nonexistent.yaml");
42+
3243
// Load config with priority (should detect Backend.AI env)
33-
let config = Config::load_with_priority(&std::path::PathBuf::from("nonexistent.yaml"))
44+
let config = Config::load_with_priority(&nonexistent_path)
3445
.await
3546
.expect("Config should load with Backend.AI env");
3647

@@ -79,18 +90,27 @@ async fn test_backendai_env_auto_detection() {
7990

8091
#[tokio::test]
8192
async fn test_backendai_env_with_single_host() {
93+
let _guard = ENV_MUTEX.lock().await;
94+
8295
// Save original env vars
8396
let orig_hosts = env::var("BACKENDAI_CLUSTER_HOSTS").ok();
8497
let orig_host = env::var("BACKENDAI_CLUSTER_HOST").ok();
98+
let orig_role = env::var("BACKENDAI_CLUSTER_ROLE").ok();
8599

86100
// Set Backend.AI environment variables with single host
87101
unsafe {
88102
env::set_var("BACKENDAI_CLUSTER_HOSTS", "single-node.ai");
89103
env::set_var("BACKENDAI_CLUSTER_HOST", "single-node.ai");
104+
// Explicitly remove ROLE to avoid contamination from previous tests
105+
env::remove_var("BACKENDAI_CLUSTER_ROLE");
90106
}
91107

108+
// Create a temporary directory for the test
109+
let temp_dir = tempfile::tempdir().unwrap();
110+
let nonexistent_path = temp_dir.path().join("nonexistent.yaml");
111+
92112
// Load config
93-
let config = Config::load_with_priority(&std::path::PathBuf::from("nonexistent.yaml"))
113+
let config = Config::load_with_priority(&nonexistent_path)
94114
.await
95115
.expect("Config should load");
96116

@@ -117,11 +137,19 @@ async fn test_backendai_env_with_single_host() {
117137
} else {
118138
env::remove_var("BACKENDAI_CLUSTER_HOST");
119139
}
140+
141+
if let Some(val) = orig_role {
142+
env::set_var("BACKENDAI_CLUSTER_ROLE", val);
143+
} else {
144+
env::remove_var("BACKENDAI_CLUSTER_ROLE");
145+
}
120146
}
121147
}
122148

123149
#[tokio::test]
124150
async fn test_no_backendai_env() {
151+
let _guard = ENV_MUTEX.lock().await;
152+
125153
// Save and clear Backend.AI env vars
126154
let orig_hosts = env::var("BACKENDAI_CLUSTER_HOSTS").ok();
127155
let orig_host = env::var("BACKENDAI_CLUSTER_HOST").ok();

tests/interactive_integration_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ fn test_history_file_handling() {
6969

7070
/// Mock SSH server for testing
7171
struct MockSshServer {
72-
port: u16,
72+
_port: u16,
7373
running: Arc<AtomicBool>,
7474
connections: Arc<AtomicUsize>,
7575
}
7676

7777
impl MockSshServer {
7878
fn new(port: u16) -> Self {
7979
Self {
80-
port,
80+
_port: port,
8181
running: Arc::new(AtomicBool::new(false)),
8282
connections: Arc::new(AtomicUsize::new(0)),
8383
}

0 commit comments

Comments
 (0)