From 5c16fda90cb79f784b3844190de2483b8b92d8cc Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sun, 29 Oct 2023 15:07:57 -0300 Subject: [PATCH 1/4] feat(config): Search in parent folders for commit.json configuration file Closes #42 --- src/config/mod.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 552f462..6526f12 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -35,17 +35,29 @@ fn select_custom_config_path(config: Option) -> Result { } } -fn get_config_path() -> Result { +fn search_config_file_on_parents() -> Result> { let current_dir = std::env::current_dir()?; - let current_file = current_dir.join("commit.json"); - if current_file.is_file() { - Ok(current_file) - } else { - let config_file = dirs::config_dir() - .ok_or_else(|| anyhow!("Could not find config directory"))? - .join("commit/commit.json"); - Ok(config_file) + let mut current_dir = current_dir.as_path(); + loop { + let config_file = current_dir.join("commit.json"); + if config_file.is_file() { + return Ok(Some(config_file)); + } + if let Some(parent) = current_dir.parent() { + current_dir = parent; + } else { + break; + } } + Ok(None) +} + +fn get_config_path() -> Result { + Ok(search_config_file_on_parents()?.unwrap_or_else(|| { + dirs::config_dir() + .expect("Could not find config directory") + .join("commit/commit.json") + })) } pub fn get_pattern(config_path: Option) -> Result { From 593f952c2046c5807f645739c05d26d01357d309 Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sun, 29 Oct 2023 15:22:22 -0300 Subject: [PATCH 2/4] test(config): Add tests for searching config file in parents folders --- src/config/tests.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/config/tests.rs b/src/config/tests.rs index fef8fb4..697bc32 100644 --- a/src/config/tests.rs +++ b/src/config/tests.rs @@ -1,4 +1,4 @@ -use std::env::set_current_dir; +use std::{env::set_current_dir, fs::remove_file}; use super::*; use assert_fs::prelude::*; @@ -32,7 +32,15 @@ fn select_custom_config_path_test() -> Result<()> { #[serial] fn get_config_path_test() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; - set_current_dir(temp_dir.path())?; + temp_dir.child("commit.json").touch()?; + temp_dir.child("some/sub/dir").create_dir_all()?; + set_current_dir(temp_dir.path().join("some/sub/dir"))?; + let config_path = get_config_path()?; + assert_eq!( + config_path.to_str(), + temp_dir.path().join("commit.json").to_str() + ); + remove_file(temp_dir.path().join("commit.json"))?; let config_file = dirs::config_dir() .ok_or_else(|| anyhow!("Could not find config directory"))? .join("commit/commit.json"); From 695a1a22fbfb8ebe9fa417b21ef8c50124806033 Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sun, 29 Oct 2023 15:28:11 -0300 Subject: [PATCH 3/4] refactor(config): Use `BufReader` to read and parse contents of the config file --- src/commit_pattern/mod.rs | 12 ++++++++++++ src/config/mod.rs | 24 ++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/commit_pattern/mod.rs b/src/commit_pattern/mod.rs index 230dd78..8541a6e 100644 --- a/src/commit_pattern/mod.rs +++ b/src/commit_pattern/mod.rs @@ -83,3 +83,15 @@ impl CommitPattern { ] } } + +impl Default for CommitPattern { + fn default() -> Self { + Self { + config: Config::default(), + commit_types: Self::commit_types(), + commit_scopes: Self::commit_scopes(), + skip_commit: vec![], + msg: Messages::default(), + } + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index 6526f12..24be087 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -2,17 +2,11 @@ mod tests; use anyhow::{anyhow, Context, Result}; -use std::fs; -use std::path::Path; +use std::fs::File; use std::path::PathBuf; use crate::commit_pattern::CommitPattern; -fn get_config_path_content(config_path: impl AsRef) -> Result { - let content = fs::read_to_string(config_path)?; - Ok(content) -} - fn select_custom_config_path(config: Option) -> Result { match config { Some(config_path) => { @@ -62,7 +56,17 @@ fn get_config_path() -> Result { pub fn get_pattern(config_path: Option) -> Result { let selected_config_path = select_custom_config_path(config_path)?; - let pattern_str = - get_config_path_content(selected_config_path).unwrap_or_else(|_| "{}".to_owned()); - serde_json::from_str(&pattern_str).context("Failed to parse commit pattern from file") + if selected_config_path.exists() { + let file = File::open(&selected_config_path).context(format!( + "Could not open config file: {}", + selected_config_path.display() + ))?; + let reader = std::io::BufReader::new(file); + Ok(serde_json::from_reader(reader).context(format!( + "Could not parse config file: {}", + selected_config_path.display() + ))?) + } else { + Ok(CommitPattern::default()) + } } From 2e209f22e8cc0e92737d89f8cc820af4ad803bcf Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sun, 29 Oct 2023 15:29:57 -0300 Subject: [PATCH 4/4] test(config): Remove `get_config_path_content` test --- src/config/tests.rs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/config/tests.rs b/src/config/tests.rs index 697bc32..f953ba9 100644 --- a/src/config/tests.rs +++ b/src/config/tests.rs @@ -49,23 +49,6 @@ fn get_config_path_test() -> Result<()> { Ok(()) } -#[test] -#[serial] -fn get_config_path_content_test() -> Result<()> { - let temp_dir = assert_fs::TempDir::new()?; - let config_file = temp_dir.child("config.json"); - config_file.touch()?; - let config_path = config_file.path(); - let content = get_config_path_content(config_path)?; - assert_eq!(content, ""); - - let expected = include_str!("../../commit.json"); - config_file.write_str(expected)?; - let content = get_config_path_content(config_path)?; - assert_eq!(content, expected); - Ok(()) -} - #[test] #[serial] fn get_pattern_test() -> Result<()> {