chore(lint): fix lints for tests

This commit is contained in:
Orhun Parmaksız 2022-07-02 12:24:24 +02:00
parent 16dd491d4c
commit d1099654f8
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
8 changed files with 46 additions and 32 deletions

View file

@ -149,7 +149,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
args: --tests -- -D warnings
- name: Run cargo-deny
uses: EmbarkStudios/cargo-deny-action@v1

View file

@ -48,7 +48,7 @@ impl Sysctl {
/// Returns the first found parameter in the available parameters.
#[cfg(test)]
fn get_parameter(&self, query: &str) -> Option<&Parameter> {
self.get_parameters(query).first().map(|v| *v)
self.get_parameters(query).first().copied()
}
/// Returns the parameters that matches the given query.
@ -178,21 +178,27 @@ mod tests {
assert!(sysctl.get_parameter("kernel.hostname").is_some());
assert!(sysctl.get_parameter("unexisting.param").is_none());
assert_eq!(
"Linux",
sysctl.get_parameters("ostype").first().unwrap().value
Some(String::from("Linux")),
sysctl
.get_parameters("ostype")
.first()
.map(|v| v.value.to_string())
);
assert!(sysctl.get_parameters("---").is_empty());
sysctl.update_docs_from_cache(None, &Cache::init()?)?;
let parameter = sysctl.get_parameter("kernel.hostname").unwrap().clone();
let parameter = sysctl
.get_parameter("kernel.hostname")
.expect("failed to get parameter")
.clone();
let old_value = parameter.docs_title;
let parameters = sysctl.parameters.clone();
sysctl
.parameters
.iter_mut()
.find(|param| param.name == parameter.name)
.unwrap()
.expect("parameter not found")
.docs_title = String::from("-");
sysctl.update_params(parameters);
assert_eq!(
@ -201,24 +207,24 @@ mod tests {
.parameters
.iter_mut()
.find(|param| param.name == parameter.name)
.unwrap()
.expect("parameter not found")
.docs_title
);
assert!(sysctl
.get_parameter("vm.zone_reclaim_mode")
.unwrap()
.expect("failed to get parameter")
.description
.as_ref()
.unwrap()
.expect("parameter has no description")
.contains("zone_reclaim_mode is disabled by default."));
assert!(sysctl
.get_parameter("user.max_user_namespaces")
.unwrap()
.expect("failed to get parameter")
.description
.as_ref()
.unwrap()
.expect("parameter has no description")
.contains("The maximum number of user namespaces"));
Ok(())

View file

@ -210,9 +210,14 @@ mod tests {
};
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;
let mut config = Config {
default_color: Color::White,
..Default::default()
};
*(config
.section_colors
.get_mut(&Section::Kernel)
.expect("failed to get color")) = Color::Yellow;
assert_eq!(parameter.name, parameter.get_colored_name(&config));
assert_eq!(

View file

@ -220,12 +220,12 @@ mod tests {
}
#[test]
fn test_tree_output() {
fn test_tree_output() -> IoResult<()> {
let lines = ["a", "a/b/e", "a/b", "a/b/c/d"];
let tree = Tree::from_input(&mut lines.iter(), '/');
let mut output = Vec::new();
tree.print(&mut output, Color::White).unwrap();
tree.print(&mut output, Color::White)?;
let expected_output = "\
a
@ -235,10 +235,12 @@ a
d\n";
assert_eq!(expected_output, String::from_utf8_lossy(&output));
Ok(())
}
#[test]
fn test_print_line() {
fn test_print_line() -> IoResult<()> {
let value = String::from("abc\ndef");
let mut output = Vec::new();
@ -246,8 +248,7 @@ a
value: value.to_string(),
childs: Vec::new(),
}
.print_line(&mut output, &[], Color::White)
.unwrap();
.print_line(&mut output, &[], Color::White)?;
assert_eq!(b"abc\ndef\n", &*output);
let mut output = Vec::new();
@ -255,17 +256,17 @@ a
value: value.to_string(),
childs: Vec::new(),
}
.print_line(&mut output, &[true, false, true], Color::White)
.unwrap();
.print_line(&mut output, &[true, false, true], Color::White)?;
assert_eq!(" │ └── abc\ndef\n".as_bytes(), &*output);
let mut output = Vec::new();
TreeNode {
value: value.to_string(),
value,
childs: Vec::new(),
}
.print_line(&mut output, &[true, false, false], Color::White)
.unwrap();
.print_line(&mut output, &[true, false, false], Color::White)?;
assert_eq!(" │ ├── abc\ndef\n".as_bytes(), &*output);
Ok(())
}
}

View file

@ -136,7 +136,7 @@ mod tests {
String::from("-q"),
String::from("test"),
])
.unwrap();
.expect("failed to parse arguments");
assert_eq!(1000, args.tick_rate);
assert_eq!(Some(PathBuf::from("/docs")), args.kernel_docs);

View file

@ -154,7 +154,7 @@ mod tests {
"scroll list bottom 1",
),
] {
assert_eq!(command, Command::from_str(value).unwrap());
assert_eq!(Ok(command), Command::from_str(value));
}
assert!(Command::from_str("---").is_err());
assert_command_parser! {

View file

@ -58,13 +58,14 @@ impl EventHandler {
#[cfg(test)]
mod tests {
use super::*;
use crate::error::Result;
use std::char;
use std::time::Instant;
const TICK_RATE_MS: u64 = 100;
#[test]
fn test_event() {
fn test_event() -> Result<()> {
let start_time = Instant::now();
let event_handler = EventHandler::new(TICK_RATE_MS);
let mut tick_count = 0;
@ -73,9 +74,9 @@ mod tests {
thread::spawn(move || {
let key = Key::Char(char::from_digit(i, 10).unwrap_or('9'));
let event = Event::KeyPress(key);
sender.send(event).unwrap();
sender.send(event).expect("failed to send event");
});
match event_handler.next().unwrap() {
match event_handler.next()? {
Event::KeyPress(key) => {
if key == Key::Char('9') {
break;
@ -88,5 +89,6 @@ mod tests {
}
}
assert!(start_time.elapsed() > Duration::from_millis(tick_count * TICK_RATE_MS));
Ok(())
}
}

View file

@ -222,20 +222,20 @@ mod tests {
String::from("-A"),
String::from("-T"),
])
.unwrap();
.expect("failed to parse arguments");
assert!(args.verbose);
assert!(args.write);
assert_eq!(OutputType::Tree, args.output_type);
assert!(!Args::parse(vec![String::new(), String::from("-p")])
.unwrap()
.expect("failed to parse arguments")
.values
.is_empty());
assert_eq!(
DisplayType::Binary,
Args::parse(vec![String::new(), String::from("-A"), String::from("-b")])
.unwrap()
.expect("failed to parse arguments")
.display_type
);
}