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
This commit is contained in:
Jae-Heon Ji 2022-01-13 20:41:13 +09:00 committed by GitHub
parent f6c56f6ea3
commit 65a12c0f5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 42 deletions

47
Cargo.lock generated
View file

@ -415,6 +415,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]] [[package]]
name = "cranelift-bforest" name = "cranelift-bforest"
version = "0.68.0" version = "0.68.0"
@ -612,26 +618,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "darwin-libproc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc629b7cf42586fee31dae31f9ab73fa5ff5f0170016aa61be5fcbc12a90c516"
dependencies = [
"darwin-libproc-sys",
"libc",
"memchr",
]
[[package]]
name = "darwin-libproc-sys"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef0aa083b94c54aa4cfd9bbfd37856714c139d1dc511af80270558c7ba3b4816"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "derivative" name = "derivative"
version = "2.2.0" version = "2.2.0"
@ -1157,9 +1143,9 @@ checksum = "4d234d89ecf5621c935b69a4c7266c9a634a95e465081682be47358617ce825b"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.106" version = "0.2.112"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a60553f9a9e039a333b4e9b20573b9e9b9c0bb3a11e201ccc48ef4283456d673" checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]] [[package]]
name = "libloading" name = "libloading"
@ -2147,6 +2133,21 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "sysinfo"
version = "0.22.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f1bfab07306a27332451a662ca9c8156e3a9986f82660ba9c8e744fe8455d43"
dependencies = [
"cfg-if 1.0.0",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
]
[[package]] [[package]]
name = "tab-bar" name = "tab-bar"
version = "0.1.0" version = "0.1.0"
@ -2871,11 +2872,11 @@ dependencies = [
"chrono", "chrono",
"close_fds", "close_fds",
"daemonize", "daemonize",
"darwin-libproc",
"highway", "highway",
"insta", "insta",
"log", "log",
"serde_json", "serde_json",
"sysinfo",
"typetag", "typetag",
"unicode-width", "unicode-width",
"url", "url",

View file

@ -943,7 +943,7 @@ pub fn detach_and_attach_session() {
name: "Wait for session to be attached", name: "Wait for session to be attached",
instruction: |remote_terminal: RemoteTerminal| -> bool { instruction: |remote_terminal: RemoteTerminal| -> bool {
let mut step_is_complete = false; let mut step_is_complete = false;
if remote_terminal.cursor_position_is(77, 2) { if remote_terminal.cursor_position_is(3, 2) {
// we're back inside the session // we're back inside the session
step_is_complete = true; step_is_complete = true;
} }

View file

@ -27,9 +27,7 @@ log = "0.4.14"
typetag = "0.1.7" typetag = "0.1.7"
chrono = "0.4.19" chrono = "0.4.19"
close_fds = "0.3.2" close_fds = "0.3.2"
sysinfo = "0.22.5"
[target.'cfg(target_os = "macos")'.dependencies]
darwin-libproc = "0.2.0"
[dev-dependencies] [dev-dependencies]
insta = "1.6.0" insta = "1.6.0"

View file

@ -2,12 +2,6 @@ use std::collections::HashMap;
use crate::panes::PaneId; use crate::panes::PaneId;
#[cfg(target_os = "macos")]
use darwin_libproc;
#[cfg(target_os = "linux")]
use std::fs;
use std::env; use std::env;
use std::os::unix::io::RawFd; use std::os::unix::io::RawFd;
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
@ -21,6 +15,8 @@ use async_std::fs::File as AsyncFile;
use async_std::os::unix::io::FromRawFd; use async_std::os::unix::io::FromRawFd;
use interprocess::local_socket::LocalSocketStream; use interprocess::local_socket::LocalSocketStream;
use sysinfo::{ProcessExt, ProcessRefreshKind, System, SystemExt};
use nix::pty::{openpty, OpenptyResult, Winsize}; use nix::pty::{openpty, OpenptyResult, Winsize};
use nix::sys::signal::{kill, Signal}; use nix::sys::signal::{kill, Signal};
use nix::sys::termios; use nix::sys::termios;
@ -338,16 +334,15 @@ impl ServerOsApi for ServerOsInputOutput {
fn load_palette(&self) -> Palette { fn load_palette(&self) -> Palette {
default_palette() default_palette()
} }
#[cfg(target_os = "macos")]
fn get_cwd(&self, pid: Pid) -> Option<PathBuf> { fn get_cwd(&self, pid: Pid) -> Option<PathBuf> {
darwin_libproc::pid_cwd(pid.as_raw()).ok() let mut system_info = System::new();
} // Update by minimizing information.
#[cfg(target_os = "linux")] // See https://docs.rs/sysinfo/0.22.5/sysinfo/struct.ProcessRefreshKind.html#
fn get_cwd(&self, pid: Pid) -> Option<PathBuf> { system_info.refresh_processes_specifics(ProcessRefreshKind::default());
fs::read_link(format!("/proc/{}/cwd", pid)).ok()
} if let Some(process) = system_info.process(pid.into()) {
#[cfg(all(not(target_os = "linux"), not(target_os = "macos")))] return Some(process.cwd().to_path_buf());
fn get_cwd(&self, _pid: Pid) -> Option<PathBuf> { }
None None
} }
} }
@ -376,3 +371,7 @@ pub struct ChildId {
/// field is it's parent process id. /// field is it's parent process id.
pub shell: Option<Pid>, pub shell: Option<Pid>,
} }
#[cfg(test)]
#[path = "./unit/os_input_output_tests.rs"]
mod os_input_output_tests;

View file

@ -0,0 +1,49 @@
use super::*;
use nix::{pty::openpty, unistd::close};
struct TestTerminal {
openpty: OpenptyResult,
}
impl TestTerminal {
pub fn new() -> TestTerminal {
let openpty = openpty(None, None).expect("Could not create openpty");
TestTerminal { openpty }
}
#[allow(dead_code)]
pub fn master(&self) -> RawFd {
self.openpty.master
}
pub fn slave(&self) -> RawFd {
self.openpty.slave
}
}
impl Drop for TestTerminal {
fn drop(&mut self) {
close(self.openpty.master).expect("Failed to close the master");
close(self.openpty.slave).expect("Failed to close the slave");
}
}
#[test]
fn get_cwd() {
let test_terminal = TestTerminal::new();
let test_termios =
termios::tcgetattr(test_terminal.slave()).expect("Could not configure the termios");
let server = ServerOsInputOutput {
orig_termios: Arc::new(Mutex::new(test_termios)),
client_senders: Arc::default(),
};
let pid = nix::unistd::getpid();
assert!(
server.get_cwd(pid).is_some(),
"Get current working directory from PID {}",
pid
);
}