test(sysctl): add tests for the parameter module

This commit is contained in:
Orhun Parmaksız 2021-12-18 15:00:26 +03:00
parent 5a729d8acb
commit 2d744498f4
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 71 additions and 5 deletions

View file

@ -80,7 +80,7 @@ jobs:
mv cargo-tarpaulin ~/.cargo/bin/
- name: Run tests
run: cargo tarpaulin --out Xml --verbose
run: NO_COLOR=1 cargo tarpaulin --out Xml --verbose
- name: Upload reports to codecov
uses: codecov/codecov-action@v1

View file

@ -131,7 +131,7 @@ impl Parameter {
}
/// Returns the parameter documentation if it exists.
pub fn get_documentation(&self) -> Option<String> {
fn get_documentation(&self) -> Option<String> {
self.description.as_ref().map(|description| {
format!(
"{}\n{}\n{}\n-\nParameter: {}\nReference: {}",
@ -170,3 +170,72 @@ impl Parameter {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sysctl_parameter() -> Result<()> {
let mut parameter = Parameter {
name: String::from("kernel.fictional.test_param"),
value: String::from("1"),
description: Some(String::from("This is a fictional parameter for testing")),
section: Section::Kernel,
docs_path: PathBuf::from("/etc/cosmos"),
docs_title: String::from("Test Parameter"),
};
assert_eq!(Some("test_param"), parameter.get_absolute_name());
let mut config = Config::default();
config.default_color = Color::White;
*(config.section_colors.get_mut(&Section::Kernel).unwrap()) = Color::Yellow;
assert_eq!(parameter.name, parameter.get_colored_name(&config));
assert_eq!(
vec![
String::from("kernel"),
String::from("fictional"),
String::from("test_param = 1")
],
parameter.get_tree_components(&config)
);
let mut output = Vec::new();
parameter.display_value(&config, &mut output)?;
assert_eq!(
"kernel.fictional.test_param = 1\n",
String::from_utf8_lossy(&output)
);
let mut output = Vec::new();
parameter.display_documentation(&mut output)?;
assert_eq!(
"Test Parameter
==============
This is a fictional parameter for testing
-
Parameter: kernel.fictional.test_param
Reference: /etc/cosmos\n\n\n"
.lines()
.map(|line| line.trim_start())
.collect::<Vec<&str>>()
.join("\n"),
String::from_utf8_lossy(&output)
);
parameter.description = None;
let mut output = Vec::new();
parameter.display_documentation(&mut output)?;
assert_eq!(
"No documentation available\n",
String::from_utf8_lossy(&output)
);
assert!(parameter
.update_value("0", &config, &mut Vec::new())
.is_err());
Ok(())
}
}

View file

@ -132,7 +132,6 @@ impl Tree {
#[cfg(test)]
mod tests {
use super::*;
use std::env;
fn test_single_tree_creation(lines: &[&str], seperator: char, expected_tree: TreeNode) {
let tree = Tree::from_input(&mut lines.iter(), seperator);
@ -222,7 +221,6 @@ mod tests {
#[test]
fn test_tree_output() {
env::set_var("NO_COLOR", "1");
let lines = ["a", "a/b/e", "a/b", "a/b/c/d"];
let tree = Tree::from_input(&mut lines.iter(), '/');
@ -241,7 +239,6 @@ a
#[test]
fn test_print_line() {
env::set_var("NO_COLOR", "1");
let value = String::from("abc\ndef");
let mut output = Vec::new();