Skip to content

Commit

Permalink
Added a test for feed config backwards-compat, fixed bugs
Browse files Browse the repository at this point in the history
- Fixed language config merge bug found by a test
- Adjusted two existing tests to fully check stuff related to multiple feeds
- Added a new test for backwards-compatibility of the changes
- Fixed bugs found by the newly added test
  • Loading branch information
LunarEclipse363 committed Apr 25, 2024
1 parent 0379e03 commit 3bba21d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
4 changes: 2 additions & 2 deletions components/config/src/config/languages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct LanguageOptions {
pub generate_feeds: bool,
/// The filenames to use for feeds. Used to find the templates, too.
/// Defaults to ["atom.xml"], with "rss.xml" also having a template provided out of the box.
#[serde(deserialize_with = "might_be_single")]
#[serde(alias = "feed_filename", deserialize_with = "might_be_single")]
pub feed_filenames: Vec<String>,
pub taxonomies: Vec<taxonomies::TaxonomyConfig>,
/// Whether to generate search index for that language, defaults to `false`
Expand Down Expand Up @@ -69,7 +69,7 @@ impl LanguageOptions {
merge_field!(self.title, other.title, "title");
merge_field!(self.description, other.description, "description");
merge_field!(
self.feed_filenames.is_empty(),
self.feed_filenames.is_empty() || self.feed_filenames == LanguageOptions::default().feed_filenames,
self.feed_filenames,
other.feed_filenames,
"feed_filename"
Expand Down
93 changes: 53 additions & 40 deletions components/config/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct Config {
pub feed_limit: Option<usize>,
/// The filenames to use for feeds. Used to find the templates, too.
/// Defaults to ["atom.xml"], with "rss.xml" also having a template provided out of the box.
#[serde(deserialize_with = "might_be_single")]
#[serde(alias = "feed_filename", deserialize_with = "might_be_single")]
pub feed_filenames: Vec<String>,
/// If set, files from static/ will be hardlinked instead of copied to the output dir.
pub hard_link_static: bool,
Expand Down Expand Up @@ -400,6 +400,48 @@ impl Default for Config {
}
}

/// Used for deserializing values that can be either a single value or a vec of values
pub(crate) fn might_be_single<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
where
T: DeserializeOwned,
D: Deserializer<'de>,
{
let v = MightBeSingle::deserialize(deserializer)?;
Ok(v.into())
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub(crate) enum MightBeSingle<T> {
Multiple(Vec<T>),
One(T),
None,
}

impl<T> From<MightBeSingle<T>> for Vec<T> {
fn from(x: MightBeSingle<T>) -> Vec<T> {
use MightBeSingle::*;

match x {
Multiple(v) => v,
One(v) => vec![v],
None => vec![],
}
}
}

impl<T> From<Vec<T>> for MightBeSingle<T> {
fn from(value: Vec<T>) -> Self {
Self::Multiple(value)
}
}

impl<T> Default for MightBeSingle<T> {
fn default() -> Self {
Self::None
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -981,46 +1023,17 @@ author = "[email protected] (Some Person)"
let config = Config::parse(config).unwrap();
assert_eq!(config.author, Some("[email protected] (Some Person)".to_owned()))
}
}

/// Used for deserializing values that can be either a single value or a vec of values
pub(crate) fn might_be_single<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
where
T: DeserializeOwned,
D: Deserializer<'de>,
{
let v = MightBeSingle::deserialize(deserializer)?;
Ok(v.into())
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub(crate) enum MightBeSingle<T> {
Multiple(Vec<T>),
One(T),
None,
}

impl<T> From<MightBeSingle<T>> for Vec<T> {
fn from(x: MightBeSingle<T>) -> Vec<T> {
use MightBeSingle::*;

match x {
Multiple(v) => v,
One(v) => vec![v],
None => vec![],
}
}
}

impl<T> From<Vec<T>> for MightBeSingle<T> {
fn from(value: Vec<T>) -> Self {
Self::Multiple(value)
}
}
#[test]
fn test_backwards_compatibility_for_feeds() {
let config = r#"
base_url = "example.com"
generate_feed = true
feed_filename = "test.xml"
"#;

impl<T> Default for MightBeSingle<T> {
fn default() -> Self {
Self::None
let config = Config::parse(config).unwrap();
assert_eq!(config.generate_feeds, true);
assert_eq!(config.feed_filenames, vec!["test.xml".to_owned()]);
}
}
24 changes: 14 additions & 10 deletions components/templates/src/global_fns/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,27 +461,31 @@ title = "A title"
}

#[test]
fn can_get_feed_url_with_default_language() {
fn can_get_feed_urls_with_default_language() {
let config = Config::parse(CONFIG_DATA).unwrap();
let dir = create_temp_dir();
let static_fn =
GetUrl::new(dir.path().to_path_buf(), config.clone(), HashMap::new(), PathBuf::new());
let mut args = HashMap::new();
args.insert("path".to_string(), to_value(config.feed_filenames).unwrap());
args.insert("lang".to_string(), to_value("fr").unwrap());
assert_eq!(static_fn.call(&args).unwrap(), "https://remplace-par-ton-url.fr/atom.xml");
for feed_filename in &config.feed_filenames {
let mut args = HashMap::new();
args.insert("path".to_string(), to_value(feed_filename).unwrap());
args.insert("lang".to_string(), to_value("fr").unwrap());
assert_eq!(static_fn.call(&args).unwrap(), "https://remplace-par-ton-url.fr/atom.xml");
}
}

#[test]
fn can_get_feed_url_with_other_language() {
fn can_get_feed_urls_with_other_language() {
let config = Config::parse(CONFIG_DATA).unwrap();
let dir = create_temp_dir();
let static_fn =
GetUrl::new(dir.path().to_path_buf(), config.clone(), HashMap::new(), PathBuf::new());
let mut args = HashMap::new();
args.insert("path".to_string(), to_value(config.feed_filenames).unwrap());
args.insert("lang".to_string(), to_value("en").unwrap());
assert_eq!(static_fn.call(&args).unwrap(), "https://remplace-par-ton-url.fr/en/atom.xml");
for feed_filename in &config.feed_filenames {
let mut args = HashMap::new();
args.insert("path".to_string(), to_value(feed_filename).unwrap());
args.insert("lang".to_string(), to_value("en").unwrap());
assert_eq!(static_fn.call(&args).unwrap(), "https://remplace-par-ton-url.fr/en/atom.xml");
}
}

#[test]
Expand Down

0 comments on commit 3bba21d

Please sign in to comment.