diff options
author | Thomas Otto <th1000s@posteo.net> | 2021-11-22 01:36:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-21 19:36:15 -0500 |
commit | 0a0a3e02addfdb33adc4e72d361cef392f47f189 (patch) | |
tree | ad8d1b7b8ae19f0a4c5ccfb8d177d20970abbfb6 /src/utils | |
parent | 916615e4d12072b0320356c72f0cc8987aa3ec01 (diff) |
Enable faking `extract_args()` parameters from process utils (#783)
The fake value is valid for exactly one call, the state for
this is stored per-thread.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/process.rs | 97 |
1 files changed, 94 insertions, 3 deletions
diff --git a/src/utils/process.rs b/src/utils/process.rs index 163160f1..5455ca3c 100644 --- a/src/utils/process.rs +++ b/src/utils/process.rs @@ -221,6 +221,15 @@ where P: ProcessInterface, F: Fn(&[String]) -> ProcessArgs<T>, { + #[cfg(test)] + { + if let Some(args) = tests::cfg::WithArgs::get() { + match extract_args(&args) { + ProcessArgs::Args(ext) => return Some(ext), + _ => return None, + } + } + } let my_pid = info.my_pid(); // 1) Try the parent process. If delta is set as the pager in git, then git is the parent process. @@ -295,12 +304,62 @@ where } #[cfg(test)] -mod tests { +pub mod tests { use super::blame::*; use super::*; use itertools::Itertools; + pub mod cfg { + use std::cell::RefCell; + + #[derive(Debug, PartialEq)] + enum TlsState<T> { + Some(T), + None, + Invalid, + } + + thread_local! { + static FAKE_ARGS: RefCell<TlsState<Vec<String>>> = RefCell::new(TlsState::None); + } + + pub struct WithArgs {} + impl WithArgs { + pub fn new(args: &str) -> Self { + let string_vec = args.split(' ').map(str::to_owned).collect(); + assert!( + FAKE_ARGS.with(|a| a.replace(TlsState::Some(string_vec))) != TlsState::Invalid, + "test logic error (in new): wrong WithArgs scope?" + ); + WithArgs {} + } + pub fn get() -> Option<Vec<String>> { + FAKE_ARGS.with(|a| { + let old_value = a.replace_with(|old_value| match old_value { + TlsState::Some(_) => TlsState::Invalid, + TlsState::None => TlsState::None, + TlsState::Invalid => TlsState::Invalid, + }); + + match old_value { + TlsState::Some(args) => Some(args), + TlsState::None => None, + TlsState::Invalid => { + panic!("test logic error (in get): wrong WithArgs scope?") + } + } + }) + } + } + impl Drop for WithArgs { + fn drop(&mut self) { + // clears an invalid state + FAKE_ARGS.with(|a| a.replace(TlsState::None)); + } + } + } + #[test] fn test_guess_git_blame_filename_extension() { use ProcessArgs::Args; @@ -435,7 +494,39 @@ mod tests { } #[test] - fn test_blame_process_info_with_parent() { + fn test_process_testing() { + { + let _args = cfg::WithArgs::new(&"git blame hello"); + assert_eq!( + calling_process_cmdline(ProcInfo::new(), blame::guess_git_blame_filename_extension), + Some("hello".into()) + ); + } + { + let _args = cfg::WithArgs::new(&"git blame world.txt"); + assert_eq!( + calling_process_cmdline(ProcInfo::new(), blame::guess_git_blame_filename_extension), + Some("txt".into()) + ); + } + } + + #[test] + #[should_panic] + fn test_process_testing_assert() { + { + let _args = cfg::WithArgs::new(&"git blame do.not.panic"); + assert_eq!( + calling_process_cmdline(ProcInfo::new(), blame::guess_git_blame_filename_extension), + Some("panic".into()) + ); + + calling_process_cmdline(ProcInfo::new(), blame::guess_git_blame_filename_extension); + } + } + + #[test] + fn test_process_blame_info_with_parent() { let no_processes = MockProcInfo::with(&[]); assert_eq!( calling_process_cmdline(no_processes, blame::guess_git_blame_filename_extension), @@ -465,7 +556,7 @@ mod tests { } #[test] - fn test_calling_process_cmdline() { + fn test_process_calling_cmdline() { // Github runs CI tests for arm under qemu where where sysinfo can not find the parent processr. if std::env::vars().any(|(key, _)| key == "CROSS_RUNNER" || key == "QEMU_LD_PREFIX") { return; |