bug: fix broken check from last commit, add tests

This commit is contained in:
ClementTsang 2020-09-01 03:08:38 -04:00
parent d24a797ce9
commit 663ae6c5c2
5 changed files with 57 additions and 22 deletions

View file

@ -1,2 +1,7 @@
echo "Running pre-push hook: cargo +nightly clippy -- -D clippy::all"
cargo +nightly clippy -- -D clippy::all
echo "Running pre-push hook:"
echo "Executing: cargo +nightly clippy -- -D clippy::all"
cargo +nightly clippy -- -D clippy::all
echo "Executing: cargo test"
cargo test

View file

@ -592,30 +592,33 @@ fn get_default_widget_and_count(
None
};
if widget_type.is_some() {
let widget_count = if let Some(widget_count) = matches.value_of("DEFAULT_WIDGET_COUNT") {
widget_count.parse::<u128>()?
} else if let Some(flags) = &config.flags {
if let Some(widget_count) = flags.default_widget_count {
widget_count as u128
} else {
1 as u128
}
let widget_count = if let Some(widget_count) = matches.value_of("DEFAULT_WIDGET_COUNT") {
Some(widget_count.parse::<u128>()?)
} else if let Some(flags) = &config.flags {
if let Some(widget_count) = flags.default_widget_count {
Some(widget_count as u128)
} else {
1 as u128
};
if widget_count > std::u64::MAX as u128 {
Err(BottomError::ConfigError(
"set your widget count to be at most unsigned INT_MAX.".to_string(),
))
} else {
Ok((widget_type, widget_count as u64))
None
}
} else {
Err(BottomError::ConfigError(
None
};
match (widget_type, widget_count) {
(Some(widget_type), Some(widget_count)) => {
if widget_count > std::u64::MAX as u128 {
Err(BottomError::ConfigError(
"set your widget count to be at most unsigned INT_MAX.".to_string(),
))
} else {
Ok((Some(widget_type), widget_count as u64))
}
}
(Some(widget_type), None) => Ok((Some(widget_type), 1)),
(None, Some(_widget_count)) => Err(BottomError::ConfigError(
"cannot set 'default_widget_count' by itself, it must be used with 'default_widget_type'.".to_string(),
))
)),
(None, None) => Ok((None, 1))
}
}

View file

@ -156,3 +156,17 @@ fn test_invalid_default_widget_2() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
#[test]
fn test_missing_default_widget_type() -> Result<(), Box<dyn std::error::Error>> {
Command::new(get_binary_location())
.arg("--default_widget_count")
.arg("3")
.assert()
.failure()
.stderr(predicate::str::contains(
"The following required arguments were not provided",
));
Ok(())
}

View file

@ -147,3 +147,14 @@ fn test_empty_battery() -> Result<(), Box<dyn std::error::Error>> {
));
Ok(())
}
#[test]
fn test_invalid_default_widget_count() -> Result<(), Box<dyn std::error::Error>> {
Command::new(get_binary_location())
.arg("-C")
.arg("./tests/invalid_configs/invalid_default_widget_count.toml")
.assert()
.failure()
.stderr(predicate::str::contains("it must be used with"));
Ok(())
}

View file

@ -0,0 +1,2 @@
[flags]
default_widget_count = 3