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