diff --git a/.cargo-husky/hooks/pre-push b/.cargo-husky/hooks/pre-push index 46fb32d2..3a69d89a 100755 --- a/.cargo-husky/hooks/pre-push +++ b/.cargo-husky/hooks/pre-push @@ -1,2 +1,7 @@ -echo "Running pre-push hook: cargo +nightly clippy -- -D clippy::all" -cargo +nightly clippy -- -D clippy::all \ No newline at end of file +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 \ No newline at end of file diff --git a/src/options.rs b/src/options.rs index 1d25ca85..e1a6e226 100644 --- a/src/options.rs +++ b/src/options.rs @@ -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::()? - } 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::()?) + } 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)) } } diff --git a/tests/arg_tests.rs b/tests/arg_tests.rs index f6dac340..6f19eb8c 100644 --- a/tests/arg_tests.rs +++ b/tests/arg_tests.rs @@ -156,3 +156,17 @@ fn test_invalid_default_widget_2() -> Result<(), Box> { Ok(()) } + +#[test] +fn test_missing_default_widget_type() -> Result<(), Box> { + 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(()) +} diff --git a/tests/invalid_config_tests.rs b/tests/invalid_config_tests.rs index cda6beb5..49db85da 100644 --- a/tests/invalid_config_tests.rs +++ b/tests/invalid_config_tests.rs @@ -147,3 +147,14 @@ fn test_empty_battery() -> Result<(), Box> { )); Ok(()) } + +#[test] +fn test_invalid_default_widget_count() -> Result<(), Box> { + 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(()) +} diff --git a/tests/invalid_configs/invalid_default_widget_count.toml b/tests/invalid_configs/invalid_default_widget_count.toml new file mode 100644 index 00000000..0e299d12 --- /dev/null +++ b/tests/invalid_configs/invalid_default_widget_count.toml @@ -0,0 +1,2 @@ +[flags] +default_widget_count = 3 \ No newline at end of file