Skip to content

Commit 65a12c0

Browse files
authored
feat: change dependency for process_cwd (#1001)
* feat: change dependency for process_cwd * test: add unittest for get_cwd * chore: apply clippy * test: change tty to openpty * test(e2e): update case
1 parent f6c56f6 commit 65a12c0

File tree

5 files changed

+89
-42
lines changed

5 files changed

+89
-42
lines changed

Cargo.lock

Lines changed: 24 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tests/e2e/cases.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ pub fn detach_and_attach_session() {
943943
name: "Wait for session to be attached",
944944
instruction: |remote_terminal: RemoteTerminal| -> bool {
945945
let mut step_is_complete = false;
946-
if remote_terminal.cursor_position_is(77, 2) {
946+
if remote_terminal.cursor_position_is(3, 2) {
947947
// we're back inside the session
948948
step_is_complete = true;
949949
}

zellij-server/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ log = "0.4.14"
2727
typetag = "0.1.7"
2828
chrono = "0.4.19"
2929
close_fds = "0.3.2"
30-
31-
[target.'cfg(target_os = "macos")'.dependencies]
32-
darwin-libproc = "0.2.0"
30+
sysinfo = "0.22.5"
3331

3432
[dev-dependencies]
3533
insta = "1.6.0"

zellij-server/src/os_input_output.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ use std::collections::HashMap;
22

33
use crate::panes::PaneId;
44

5-
#[cfg(target_os = "macos")]
6-
use darwin_libproc;
7-
8-
#[cfg(target_os = "linux")]
9-
use std::fs;
10-
115
use std::env;
126
use std::os::unix::io::RawFd;
137
use std::os::unix::process::CommandExt;
@@ -21,6 +15,8 @@ use async_std::fs::File as AsyncFile;
2115
use async_std::os::unix::io::FromRawFd;
2216
use interprocess::local_socket::LocalSocketStream;
2317

18+
use sysinfo::{ProcessExt, ProcessRefreshKind, System, SystemExt};
19+
2420
use nix::pty::{openpty, OpenptyResult, Winsize};
2521
use nix::sys::signal::{kill, Signal};
2622
use nix::sys::termios;
@@ -338,16 +334,15 @@ impl ServerOsApi for ServerOsInputOutput {
338334
fn load_palette(&self) -> Palette {
339335
default_palette()
340336
}
341-
#[cfg(target_os = "macos")]
342-
fn get_cwd(&self, pid: Pid) -> Option<PathBuf> {
343-
darwin_libproc::pid_cwd(pid.as_raw()).ok()
344-
}
345-
#[cfg(target_os = "linux")]
346337
fn get_cwd(&self, pid: Pid) -> Option<PathBuf> {
347-
fs::read_link(format!("/proc/{}/cwd", pid)).ok()
348-
}
349-
#[cfg(all(not(target_os = "linux"), not(target_os = "macos")))]
350-
fn get_cwd(&self, _pid: Pid) -> Option<PathBuf> {
338+
let mut system_info = System::new();
339+
// Update by minimizing information.
340+
// See https://docs.rs/sysinfo/0.22.5/sysinfo/struct.ProcessRefreshKind.html#
341+
system_info.refresh_processes_specifics(ProcessRefreshKind::default());
342+
343+
if let Some(process) = system_info.process(pid.into()) {
344+
return Some(process.cwd().to_path_buf());
345+
}
351346
None
352347
}
353348
}
@@ -376,3 +371,7 @@ pub struct ChildId {
376371
/// field is it's parent process id.
377372
pub shell: Option<Pid>,
378373
}
374+
375+
#[cfg(test)]
376+
#[path = "./unit/os_input_output_tests.rs"]
377+
mod os_input_output_tests;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use super::*;
2+
3+
use nix::{pty::openpty, unistd::close};
4+
5+
struct TestTerminal {
6+
openpty: OpenptyResult,
7+
}
8+
9+
impl TestTerminal {
10+
pub fn new() -> TestTerminal {
11+
let openpty = openpty(None, None).expect("Could not create openpty");
12+
TestTerminal { openpty }
13+
}
14+
15+
#[allow(dead_code)]
16+
pub fn master(&self) -> RawFd {
17+
self.openpty.master
18+
}
19+
20+
pub fn slave(&self) -> RawFd {
21+
self.openpty.slave
22+
}
23+
}
24+
25+
impl Drop for TestTerminal {
26+
fn drop(&mut self) {
27+
close(self.openpty.master).expect("Failed to close the master");
28+
close(self.openpty.slave).expect("Failed to close the slave");
29+
}
30+
}
31+
32+
#[test]
33+
fn get_cwd() {
34+
let test_terminal = TestTerminal::new();
35+
let test_termios =
36+
termios::tcgetattr(test_terminal.slave()).expect("Could not configure the termios");
37+
38+
let server = ServerOsInputOutput {
39+
orig_termios: Arc::new(Mutex::new(test_termios)),
40+
client_senders: Arc::default(),
41+
};
42+
43+
let pid = nix::unistd::getpid();
44+
assert!(
45+
server.get_cwd(pid).is_some(),
46+
"Get current working directory from PID {}",
47+
pid
48+
);
49+
}

0 commit comments

Comments
 (0)