Skip to content

Commit 0cd0c46

Browse files
committed
test: add core
1 parent e5f0df6 commit 0cd0c46

File tree

3 files changed

+114
-13
lines changed

3 files changed

+114
-13
lines changed

crates/libs/kill_tree/src/core.rs

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl std::fmt::Display for Error {
2727
Error::InvalidProcessId { process_id, reason } => {
2828
write!(f, "Invalid process id: {process_id}. Reason: {reason}")
2929
}
30-
Error::InvalidCast { source, reason } => {
30+
Error::InvalidCast { reason, source } => {
3131
write!(f, "Invalid cast. Reason: {reason}. Source: {source}")
3232
}
3333
Error::InvalidProcEntry {
@@ -56,6 +56,20 @@ impl From<std::io::Error> for Error {
5656
}
5757
}
5858

59+
#[cfg(windows)]
60+
impl From<::windows::core::Error> for Error {
61+
fn from(error: ::windows::core::Error) -> Self {
62+
Self::Windows(error)
63+
}
64+
}
65+
66+
#[cfg(unix)]
67+
impl From<nix::Error> for Error {
68+
fn from(error: nix::Error) -> Self {
69+
Self::Unix(error)
70+
}
71+
}
72+
5973
pub type Result<T> = std::result::Result<T, Error>;
6074

6175
pub type ProcessId = u32;
@@ -141,3 +155,102 @@ pub(crate) mod tokio {
141155
async fn get_process_infos(&self) -> Result<ProcessInfos>;
142156
}
143157
}
158+
159+
#[cfg(test)]
160+
mod tests {
161+
use super::*;
162+
163+
#[test]
164+
fn error_display_invalid_process_id() {
165+
let error = Error::InvalidProcessId {
166+
process_id: 0,
167+
reason: "reason".to_string(),
168+
};
169+
assert_eq!(
170+
format!("{}", error),
171+
"Invalid process id: 0. Reason: reason"
172+
);
173+
}
174+
175+
#[test]
176+
fn error_display_invalid_cast() {
177+
let error = Error::InvalidCast {
178+
reason: "reason".to_string(),
179+
source: u32::try_from(-1).unwrap_err(),
180+
};
181+
assert_eq!(
182+
format!("{}", error),
183+
"Invalid cast. Reason: reason. Source: out of range integral type conversion attempted"
184+
);
185+
}
186+
187+
#[test]
188+
fn error_display_invalid_proc_entry() {
189+
let error = Error::InvalidProcEntry {
190+
process_id: 0,
191+
path: "/proc/0".to_string(),
192+
reason: "reason".to_string(),
193+
source: Some("source".parse::<u32>().unwrap_err()),
194+
};
195+
assert_eq!(
196+
format!("{}", error),
197+
"Invalid proc entry. Process id: 0. Path: /proc/0. Reason: reason. Source: Some(ParseIntError { kind: InvalidDigit })"
198+
);
199+
}
200+
201+
#[test]
202+
fn error_display_io() {
203+
let error = Error::Io(std::io::Error::new(std::io::ErrorKind::Other, "error"));
204+
assert_eq!(format!("{}", error), "I/O error: error");
205+
}
206+
207+
#[cfg(windows)]
208+
#[test]
209+
fn error_display_windows() {
210+
let error = Error::Windows(windows::core::Error::OK);
211+
assert_eq!(
212+
format!("{}", error),
213+
"Windows error: The operation completed successfully. (0x00000000)"
214+
);
215+
}
216+
217+
#[cfg(unix)]
218+
#[test]
219+
fn error_display_unix() {
220+
let error = Error::Unix(nix::Error::UnsupportedOperation);
221+
assert_eq!(format!("{}", error), "Unix error: UnsupportedOperation");
222+
}
223+
224+
#[test]
225+
fn from_io_error() {
226+
let error = std::io::Error::new(std::io::ErrorKind::Other, "error");
227+
let error = Error::from(error);
228+
assert_eq!(format!("{}", error), "I/O error: error");
229+
}
230+
231+
#[cfg(windows)]
232+
#[test]
233+
fn from_windows_error() {
234+
let error = windows::core::Error::OK;
235+
let error = Error::from(error);
236+
assert_eq!(
237+
format!("{}", error),
238+
"Windows error: The operation completed successfully. (0x00000000)"
239+
);
240+
}
241+
242+
#[cfg(unix)]
243+
#[test]
244+
fn from_unix_error() {
245+
let error = nix::Error::UnsupportedOperation;
246+
let error = Error::from(error);
247+
assert_eq!(format!("{}", error), "Unix error: UnsupportedOperation");
248+
}
249+
250+
#[test]
251+
fn default_config() {
252+
let config = Config::default();
253+
assert_eq!(config.signal, "SIGTERM");
254+
assert_eq!(config.include_target, true);
255+
}
256+
}

crates/libs/kill_tree/src/unix.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ use tracing::instrument;
77
const KERNEL_PROCESS_ID: u32 = 0;
88
const INIT_PROCESS_ID: u32 = 1;
99

10-
impl From<nix::Error> for Error {
11-
fn from(error: nix::Error) -> Self {
12-
Self::Unix(error)
13-
}
14-
}
15-
1610
pub(crate) fn validate_process_id(process_id: ProcessId, available_max: ProcessId) -> Result<()> {
1711
match process_id {
1812
KERNEL_PROCESS_ID => Err(Error::InvalidProcessId {

crates/libs/kill_tree/src/windows.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ use windows::Win32::{
1515
},
1616
};
1717

18-
impl From<windows::core::Error> for Error {
19-
fn from(error: windows::core::Error) -> Self {
20-
Self::Windows(error)
21-
}
22-
}
23-
2418
/// process id of System Idle Process
2519
const SYSTEM_IDLE_PROCESS_PROCESS_ID: u32 = 0;
2620

0 commit comments

Comments
 (0)