Debian Codegraph @

tests/config.rs

raw symbols SCIP
1 mod config {
2 use std::collections::VecDeque;
3 use std::process::Command;
4
5 use rust_apt::config::Config;
6
7 #[test]
8 fn clear() {
9 // Test to make sure that the config populates properly.
10 // Config will be empty if it hasn't been initialized.
11 let config = Config::new_clear();
12 config.clear_all();
13
14 let empty_config = config.find("APT::Architecture", "");
15 assert!(!config.contains("APT::Architecture"));
16 assert!(empty_config.is_empty());
17
18 // Reset the configuration which will clear and reinit.
19 config.reset();
20
21 // Now it should NOT be empty.
22 let config_dump = config.find("APT::Architecture", "");
23 assert!(config.contains("APT::Architecture"));
24 assert!(!config_dump.is_empty());
25 println!("{}", config.dump());
26 }
27
28 #[test]
29 fn find_and_set() {
30 let config = Config::new_clear();
31 let key = "rust_apt::NotExist";
32
33 // Find our key. It should not exist.
34 assert_eq!(config.find(key, "None"), "None");
35
36 // Set the key to something.
37 config.set(key, "Exists!");
38
39 // Find again and it should be there.
40 assert_eq!(config.find(key, "None"), "Exists!");
41
42 // Test other find functions on known defaults.
43 assert!(!config.bool("APT::Install-Suggests", true));
44 assert_eq!(config.int("APT::Install-Suggests", 20), 0);
45
46 // Directory is different in CI. Just check for the name
47 assert!(
48 config
49 .file("Dir::Cache::pkgcache", "")
50 .split('/')
51 .any(|x| x == "pkgcache.bin")
52 );
53 assert_eq!(
54 config.dir("Dir::Etc::sourceparts", ""),
55 "/etc/apt/sources.list.d/"
56 );
57
58 // Check if we can set a configuration list and retrieve it.
59 // Make sure that the target vector is empty.
60 assert!(config.find_vector("rust_apt::aptlist").is_empty());
61
62 // Now fill our configuration vector and set it.
63 let apt_list = vec!["this", "is", "my", "apt", "list"];
64 config.set_vector("rust_apt::aptlist", &apt_list);
65
66 // Retrieve a new vector from the configuration.
67 let apt_vector = config.find_vector("rust_apt::aptlist");
68
69 // If everything went smooth, our original vector should match the new one
70 assert_eq!(apt_list, apt_vector);
71
72 // Now test if we can remove a single value from the list.
73 config.clear_value("rust_apt::aptlist", "my");
74
75 // This will let us know if it worked!
76 assert_eq!(
77 config.find_vector("rust_apt::aptlist"),
78 vec!["this", "is", "apt", "list"]
79 );
80
81 // Finally test and see if we can clear the entire list.
82 config.clear("rust_apt::aptlist");
83 assert!(config.find_vector("rust_apt::aptlist").is_empty());
84 }
85
86 #[test]
87 fn get_architectures() {
88 let config = Config::new();
89
90 let output = dbg!(
91 String::from_utf8(
92 Command::new("dpkg")
93 .arg("--print-architecture")
94 .output()
95 .unwrap()
96 .stdout,
97 )
98 .unwrap()
99 );
100
101 let arches = dbg!(config.get_architectures());
102
103 assert!(arches.contains(&output.strip_suffix('\n').unwrap().to_string()));
104 }
105
106 #[test]
107 fn config_tree() {
108 // An example of how you might walk the entire config tree.
109 let config = Config::new();
110
111 let Some(tree) = config.root_tree() else {
112 return;
113 };
114
115 let mut stack = VecDeque::new();
116 stack.push_back((tree, 0));
117
118 while let Some((node, indent)) = stack.pop_back() {
119 let indent_str = " ".repeat(indent);
120
121 if let Some(item) = node.sibling() {
122 stack.push_back((item, indent));
123 }
124
125 if let Some(item) = node.child() {
126 stack.push_back((item, indent + 2));
127 }
128
129 if let Some(tag) = node.tag() {
130 if !tag.is_empty() {
131 println!("{}Tag: {}", indent_str, tag);
132 }
133 }
134
135 if let Some(value) = node.value() {
136 if !value.is_empty() {
137 println!("{}Value: {}", indent_str, value);
138 }
139 }
140 }
141 }
142 }