adnix: rudimentary tests for config parser

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-04-29 02:46:06 +05:30
parent 8ef418e113
commit 03954b3d25
No known key found for this signature in database
GPG Key ID: 366D7BBAD1031E80
2 changed files with 34 additions and 21 deletions

View File

@ -19,7 +19,7 @@ fn main() {
let matches = App::from_yaml(yaml).get_matches();
let sources: HashMap<String, Source> = if matches.is_present("sources_file") {
parse_file(&matches.value_of("sources_file").unwrap())
parse_sources_config_file(&matches.value_of("sources_file").unwrap())
} else {
let mut list: HashMap<String, Source> = HashMap::new();
list.insert(
@ -60,13 +60,9 @@ fn main() {
}
}
fn parse_file(filepath: &str) -> HashMap<String, Source> {
fn parse_sources_config_file(filepath: &str) -> HashMap<String, Source> {
let mut list: HashMap<String, Source> = HashMap::new();
let file = File::open(filepath).unwrap_or_else(|err| {
eprintln!("Problem openning file: {}", err);
process::exit(1);
});
if let Ok(file) = File::open(filepath) {
let lines = BufReader::new(file).lines();
for line in lines {
let content = line.unwrap_or_else(|err| {
@ -81,5 +77,21 @@ fn parse_file(filepath: &str) -> HashMap<String, Source> {
},
);
}
} else {
eprintln!("Problem opening file: {}", filepath);
};
list
}
#[cfg(test)]
mod tests {
use super::parse_sources_config_file;
#[test]
fn test_parse_sources_config_file() {
let source_config = parse_sources_config_file("test_data/sample_config");
assert!(source_config.contains_key("StevenBlack"));
assert!(source_config.get("StevenBlack").is_some());
assert!(source_config.get("StevenBlack").unwrap().url == String::from("https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"));
}
}

1
test_data/sample_config Normal file
View File

@ -0,0 +1 @@
StevenBlack|https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts