Skip to content

Commit

Permalink
other: add test to make sure default config is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Aug 8, 2024
1 parent 4c83672 commit 3b969b2
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 42 deletions.
56 changes: 26 additions & 30 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ pub const DEFAULT_BATTERY_LAYOUT: &str = r#"
// Config and flags

// TODO: Eventually deprecate this, or grab from a file.
pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. All of the settings are commented
pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. All of the settings are commented
# out by default; if you wish to change them uncomment and modify as you see
# fit.
# This group of options represents a command-line option. Flags explicitly
# This group of options represents a command-line option. Flags explicitly
# added when running (ie: btm -a) will override this config file if an option
# is also set here.
[flags]
Expand Down Expand Up @@ -340,9 +340,9 @@ pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. Al
# How much data is stored at once in terms of time.
#retention = "10m"
# Where to place the legend for the memory widget. One of "none", "top-left", "top", "top-right", "left", "right", "bottom-left", "bottom", "bottom-right".
#memory_legend = "TopRight".
#memory_legend = "TopRight"
# Where to place the legend for the network widget. One of "none", "top-left", "top", "top-right", "left", "right", "bottom-left", "bottom", "bottom-right".
#network_legend = "TopRight".
#network_legend = "TopRight"
# Processes widget configuration
#[processes]
Expand Down Expand Up @@ -484,34 +484,12 @@ pub const CONFIG_TOP_HEAD: &str = r##"# This is bottom's config file.
"##;

pub const CONFIG_DISPLAY_OPTIONS_HEAD: &str = r#"
# These options represent settings that affect how bottom functions.
# If a setting here corresponds to command-line option, then the flag will temporarily override
# the setting.
"#;

pub const CONFIG_COLOUR_HEAD: &str = r#"
# These options represent colour values for various parts of bottom. Note that colour support
# will ultimately depend on the terminal - for example, the Terminal for macOS does NOT like
# custom colours and it may glitch out.
"#;

pub const CONFIG_LAYOUT_HEAD: &str = r#"
# These options represent how bottom will lay out its widgets. Layouts follow a pattern like this:
# [[row]] represents a row in the application.
# [[row.child]] represents either a widget or a column.
# [[row.child.child]] represents a widget.
#
# All widgets must have the valid type value set to one of ["cpu", "mem", "proc", "net", "temp", "disk", "empty"].
# All layout components have a ratio value - if this is not set, then it defaults to 1.
"#;

pub const CONFIG_FILTER_HEAD: &str = r#"
# These options represent disabled entries for the temperature and disk widgets.
"#;

#[cfg(test)]
mod test {
use regex::Regex;

use crate::options::Config;

use super::*;

#[test]
Expand Down Expand Up @@ -543,4 +521,22 @@ mod test {
Borders::ALL.difference(Borders::TOP.union(Borders::BOTTOM))
)
}

/// Checks that the default config is valid.
#[test]
fn check_default_config() {
let default_config = Regex::new(r"(?m)^#([a-zA-Z\[])")
.unwrap()
.replace_all(CONFIG_TEXT, "$1");

let default_config = Regex::new(r"(?m)^#(\s\s+)([a-zA-Z\[])")
.unwrap()
.replace_all(&default_config, "$2");

let _config: Config =
toml_edit::de::from_str(&default_config).expect("can parse default config");

// TODO: Check this.
// assert_eq!(config, Config::default());
}
}
3 changes: 2 additions & 1 deletion src/options/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use self::{cpu::CpuConfig, layout::Row, process::ProcessesConfig};
derive(schemars::JsonSchema),
schemars(title = "Schema for bottom's configs (nightly)")
)]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub struct Config {
pub(crate) flags: Option<FlagConfig>,
pub(crate) styles: Option<StyleConfig>,
Expand All @@ -39,6 +39,7 @@ pub struct Config {
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub(crate) enum StringOrNum {
String(String),
Num(u64),
Expand Down
3 changes: 2 additions & 1 deletion src/options/config/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::Deserialize;
#[derive(Clone, Copy, Debug, Default, Deserialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "lowercase")]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum CpuDefault {
#[default]
All,
Expand All @@ -15,7 +16,7 @@ pub enum CpuDefault {
/// CPU column settings.
#[derive(Clone, Debug, Default, Deserialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub struct CpuConfig {
#[serde(default)]
pub default: CpuDefault,
Expand Down
2 changes: 1 addition & 1 deletion src/options/config/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::IgnoreList;
/// Disk configuration.
#[derive(Clone, Debug, Default, Deserialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub struct DiskConfig {
/// A filter over the disk names.
pub name_filter: Option<IgnoreList>,
Expand Down
2 changes: 1 addition & 1 deletion src/options/config/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::StringOrNum;

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) struct FlagConfig {
pub(crate) hide_avg_cpu: Option<bool>,
pub(crate) dot_marker: Option<bool>,
Expand Down
2 changes: 1 addition & 1 deletion src/options/config/ignore_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn default_as_true() -> bool {

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub struct IgnoreList {
#[serde(default = "default_as_true")]
// TODO: Deprecate and/or rename, current name sounds awful.
Expand Down
6 changes: 3 additions & 3 deletions src/options/config/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{app::layout_manager::*, options::OptionResult};
/// of children.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
#[serde(rename = "row")]
pub struct Row {
pub ratio: Option<u32>,
Expand Down Expand Up @@ -218,7 +218,7 @@ impl Row {
#[derive(Clone, Deserialize, Debug, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub enum RowChildren {
Widget(FinalWidget),
Col {
Expand All @@ -230,7 +230,7 @@ pub enum RowChildren {
/// Represents a widget.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub struct FinalWidget {
pub ratio: Option<u32>,
#[serde(rename = "type")]
Expand Down
2 changes: 1 addition & 1 deletion src/options/config/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::IgnoreList;
/// Network configuration.
#[derive(Clone, Debug, Default, Deserialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub struct NetworkConfig {
/// A filter over the network interface names.
pub interface_filter: Option<IgnoreList>,
Expand Down
3 changes: 2 additions & 1 deletion src/options/config/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::widgets::ProcWidgetColumn;
/// Process configuration.
#[derive(Clone, Debug, Default, Deserialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub struct ProcessesConfig {
/// A list of process widget columns.
#[serde(default)]
Expand All @@ -18,6 +18,7 @@ pub struct ProcessesConfig {
feature = "generate_schema",
derive(schemars::JsonSchema, strum::VariantArray)
)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum ProcColumn {
Pid,
Count,
Expand Down
4 changes: 3 additions & 1 deletion src/options/config/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ use super::Config;

#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub(crate) struct ColorStr(Cow<'static, str>);

/// A style for text.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) enum TextStyleConfig {
Colour(ColorStr),
TextStyle {
Expand All @@ -60,6 +61,7 @@ pub(crate) enum TextStyleConfig {
/// Style-related configs.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) struct StyleConfig {
/// A built-in theme.
///
Expand Down
1 change: 1 addition & 0 deletions src/options/config/style/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::ColorStr;
/// Styling specific to the battery widget.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) struct BatteryStyle {
/// The colour of the battery widget bar when the battery is over 50%.
#[serde(alias = "high_battery_colour")]
Expand Down
1 change: 1 addition & 0 deletions src/options/config/style/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::ColorStr;
/// Styling specific to the CPU widget.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) struct CpuStyle {
/// The colour of the "All" CPU label.
#[serde(alias = "all_entry_colour")]
Expand Down
1 change: 1 addition & 0 deletions src/options/config/style/graphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::{ColorStr, TextStyleConfig};
/// General styling for graph widgets.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) struct GraphStyle {
/// The general colour of the parts of the graph.
#[serde(alias = "graph_colour")]
Expand Down
1 change: 1 addition & 0 deletions src/options/config/style/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::ColorStr;
/// Styling specific to the memory widget.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) struct MemoryStyle {
/// The colour of the RAM label and graph line.
#[serde(alias = "ram_colour")]
Expand Down
1 change: 1 addition & 0 deletions src/options/config/style/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::ColorStr;
/// Styling specific to the network widget.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) struct NetworkStyle {
/// The colour of the RX (download) label and graph line.
#[serde(alias = "rx_colour")]
Expand Down
1 change: 1 addition & 0 deletions src/options/config/style/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::TextStyleConfig;
/// General styling for table widgets.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) struct TableStyle {
/// Text styling for table headers.
pub(crate) headers: Option<TextStyleConfig>,
Expand Down
1 change: 1 addition & 0 deletions src/options/config/style/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::{ColorStr, TextStyleConfig};
/// General styling for generic widgets.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub(crate) struct WidgetStyle {
/// The colour of the widgets' borders.
#[serde(alias = "border_colour")]
Expand Down
2 changes: 1 addition & 1 deletion src/options/config/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::IgnoreList;
/// Temperature configuration.
#[derive(Clone, Debug, Default, Deserialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
pub struct TempConfig {
/// A filter over the sensor names.
pub sensor_filter: Option<IgnoreList>,
Expand Down

0 comments on commit 3b969b2

Please sign in to comment.