Commit
Author: Kevin Schoon [me@kevinschoon.com]
Hash: 711583935e27ddaf3665b7de5cdbccace208c2b6
Timestamp: Mon, 22 Jan 2024 18:03:48 +0000 (1 year ago)

+24 -7 +/-3 browse
add test to validate sample configuration
1diff --git a/config.example.toml b/config.example.toml
2index 92bbbfa..da676f7 100644
3--- a/config.example.toml
4+++ b/config.example.toml
5 @@ -160,7 +160,7 @@ keywords = [
6 # however you might prefer OCaml instead. Mappings allow you to override
7 # specific languages with your own preferences.
8 [languages.mappings]
9- "Standard ML" = "OCaml",
10+ "Standard ML" = "OCaml"
11 # "Shell" is a blanket term for sh like languages but we want to map it
12 # specifically to bash because that's the tree-sitter parser we have installed.
13 "Shell" = "Bash"
14 diff --git a/crates/config/src/reader.rs b/crates/config/src/reader.rs
15index 6e52af8..d828e77 100644
16--- a/crates/config/src/reader.rs
17+++ b/crates/config/src/reader.rs
18 @@ -2,7 +2,6 @@ use std::env;
19 use std::error::Error as StdError;
20 use std::path::{Path, PathBuf};
21
22-
23 use serde::de::DeserializeOwned;
24 use toml::from_str;
25
26 @@ -52,12 +51,11 @@ where
27 None
28 }
29
30+ /// load a configuration from a given path, if the path is not specified
31+ /// then attempt to load the config from common defaults.
32 pub fn load(path: Option<&Path>) -> Result<T, Error> {
33 let config_str = match path {
34- Some(path) => {
35-
36- std::fs::read_to_string(path)?
37- }
38+ Some(path) => std::fs::read_to_string(path)?,
39 None => match Reader::<T>::_try_read() {
40 Some(config_str) => config_str,
41 None => return Err(Error::kind(ErrorKind::ConfigNotFound)),
42 @@ -68,6 +66,14 @@ where
43 config.validate()?;
44 Ok(config)
45 }
46+
47+ /// load the config from a string
48+ pub fn from_str(config_str: &str) -> Result<T, Error> {
49+ let mut config = from_str::<T>(&config_str)?;
50+ config.initialize()?;
51+ config.validate()?;
52+ Ok(config)
53+ }
54 }
55
56 /// return the XDG_DATA_DIR
57 diff --git a/src/config.rs b/src/config.rs
58index 1fd0dd0..251642e 100644
59--- a/src/config.rs
60+++ b/src/config.rs
61 @@ -325,7 +325,7 @@ Disallow: /*/*/chart/*
62 }
63
64 fn default_site_name() -> String {
65- String::from("🌙ayllu")
66+ String::from("🌄 ayllu")
67 }
68
69 fn default_log_level() -> String {
70 @@ -370,3 +370,14 @@ Disallow: /*/*/chart/*
71 opts
72 }
73 }
74+
75+ #[cfg(test)]
76+ mod tests {
77+ use super::*;
78+ use ayllu_config::Reader;
79+
80+ #[test]
81+ fn test_example_config() {
82+ Reader::<Config>::from_str(EXAMPLE_CONFIG).unwrap();
83+ }
84+ }