diff --git a/Cargo.toml b/Cargo.toml index 3f52e44..74e4831 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,6 @@ confique-macro = { version = "=0.0.2", path = "macro" } serde = { version = "1", features = ["derive"] } serde_yaml = { version = "0.8", optional = true } toml = { version = "0.5", optional = true } + +[dev-dependencies] +pretty_assertions = "1.2.1" diff --git a/src/lib.rs b/src/lib.rs index 52f8e37..67f8cfa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -186,6 +186,9 @@ pub mod toml; #[cfg(feature = "yaml")] pub mod yaml; +#[cfg(test)] +mod test_utils; + pub use serde; pub use self::{ diff --git a/src/test_utils/example1.rs b/src/test_utils/example1.rs new file mode 100644 index 0000000..72a936a --- /dev/null +++ b/src/test_utils/example1.rs @@ -0,0 +1,61 @@ +use std::{ + net::IpAddr, + path::PathBuf, +}; + +use crate::Config; +use crate as confique; + +#[derive(Debug, Config)] +/// A sample configuration for our app. +pub struct Conf { + /// Name of the website. + pub site_name: String, + + /// Configurations related to the HTTP communication. + #[config(nested)] + pub http: Http, + + /// Configuring the logging. + #[config(nested)] + pub log: LogConfig, +} + +/// Configuring the HTTP server of our app. +#[derive(Debug, Config)] +pub struct Http { + /// The port the server will listen on. + #[config(env = "PORT")] + pub port: u16, + + #[config(nested)] + pub headers: Headers, + + /// The bind address of the server. Can be set to `0.0.0.0` for example, to + /// allow other users of the network to access the server. + #[config(default = "127.0.0.1")] + pub bind: IpAddr, +} + +#[derive(Debug, Config)] +pub struct Headers { + /// The header in which the reverse proxy specifies the username. + #[config(default = "x-username")] + pub username: String, + + /// The header in which the reverse proxy specifies the display name. + #[config(default = "x-display-name")] + pub display_name: String, +} + + +#[derive(Debug, Config)] +pub struct LogConfig { + /// If set to `true`, the app will log to stdout. + #[config(default = true)] + pub stdout: bool, + + /// If this is set, the app will write logs to the given file. Of course, + /// the app has to have write access to that file. + pub file: Option, +} diff --git a/src/test_utils/mod.rs b/src/test_utils/mod.rs new file mode 100644 index 0000000..bea813d --- /dev/null +++ b/src/test_utils/mod.rs @@ -0,0 +1,10 @@ +pub(crate) mod example1; + + +macro_rules! include_format_output { + ($file:expr) => { + include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/format-output/", $file)) + }; +} + +pub(crate) use include_format_output; diff --git a/src/toml.rs b/src/toml.rs index a16ebfb..03ea1f8 100644 --- a/src/toml.rs +++ b/src/toml.rs @@ -184,3 +184,25 @@ impl fmt::Display for PrintExpr { } } } + +#[cfg(test)] +mod tests { + use crate::test_utils::{self, include_format_output}; + use super::{format, FormatOptions}; + use pretty_assertions::assert_str_eq; + + #[test] + fn default() { + let out = format::(FormatOptions::default()); + assert_str_eq!(&out, include_format_output!("1-default.toml")); + } + + #[test] + fn no_comments() { + let out = format::(FormatOptions { + comments: false, + indent: 0, + }); + assert_str_eq!(&out, include_format_output!("1-no-comments.toml")); + } +} diff --git a/tests/format-output/1-default.toml b/tests/format-output/1-default.toml new file mode 100644 index 0000000..5ce4692 --- /dev/null +++ b/tests/format-output/1-default.toml @@ -0,0 +1,43 @@ +# A sample configuration for our app. + +# Name of the website. +# +# Required! This value must be specified. +#site_name = + +# Configurations related to the HTTP communication. + +[http] +# The port the server will listen on. +# +# Required! This value must be specified. +#port = + +# The bind address of the server. Can be set to `0.0.0.0` for example, to +# allow other users of the network to access the server. +# +# Default value: "127.0.0.1" +#bind = "127.0.0.1" + +[http.headers] +# The header in which the reverse proxy specifies the username. +# +# Default value: "x-username" +#username = "x-username" + +# The header in which the reverse proxy specifies the display name. +# +# Default value: "x-display-name" +#display_name = "x-display-name" + +# Configuring the logging. + +[log] +# If set to `true`, the app will log to stdout. +# +# Default value: true +#stdout = true + +# If this is set, the app will write logs to the given file. Of course, +# the app has to have write access to that file. +#file = diff --git a/tests/format-output/1-no-comments.toml b/tests/format-output/1-no-comments.toml new file mode 100644 index 0000000..d8a0cea --- /dev/null +++ b/tests/format-output/1-no-comments.toml @@ -0,0 +1,13 @@ +#site_name = + +[http] +#port = +#bind = "127.0.0.1" + +[http.headers] +#username = "x-username" +#display_name = "x-display-name" + +[log] +#stdout = true +#file =