Debian Codegraph @

tests/global_state.rs

raw
1 //! Test for some global state set up by libgit2's `git_libgit2_init` function
2 //! that need to be synchronized within a single process.
3
4 use git2::opts;
5 use git2::{ConfigLevel, IntoCString};
6
7 // Test for mutating configuration file search path which is set during
8 // initialization in libgit2's `git_sysdir_global_init` function.
9 #[test]
10 fn search_path() -> Result<(), Box<dyn std::error::Error>> {
11 use std::env::join_paths;
12
13 let path = "fake_path";
14 let original = unsafe { opts::get_search_path(ConfigLevel::Global) };
15 assert_ne!(original, Ok(path.into_c_string()?));
16
17 // Set
18 unsafe {
19 opts::set_search_path(ConfigLevel::Global, &path)?;
20 }
21 assert_eq!(
22 unsafe { opts::get_search_path(ConfigLevel::Global) },
23 Ok(path.into_c_string()?)
24 );
25
26 // Append
27 let paths = join_paths(["$PATH", path].iter())?;
28 let expected_paths = join_paths([path, path].iter())?.into_c_string()?;
29 unsafe {
30 opts::set_search_path(ConfigLevel::Global, paths)?;
31 }
32 assert_eq!(
33 unsafe { opts::get_search_path(ConfigLevel::Global) },
34 Ok(expected_paths)
35 );
36
37 // Reset
38 unsafe {
39 opts::reset_search_path(ConfigLevel::Global)?;
40 }
41 assert_eq!(
42 unsafe { opts::get_search_path(ConfigLevel::Global) },
43 original
44 );
45
46 Ok(())
47 }