From 4150a7a1feac43438ba83f9f6c22c62be0144ef2 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Tue, 23 Apr 2024 20:11:44 +0200 Subject: [PATCH] chore(cli): error handle format regex error Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- src/rule/description_format.rs | 32 +++++++++++++++++++++++++++++++- src/rule/scope_format.rs | 32 +++++++++++++++++++++++++++++++- src/rule/type_format.rs | 32 +++++++++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/rule/description_format.rs b/src/rule/description_format.rs index fb8fa35..1f948a1 100644 --- a/src/rule/description_format.rs +++ b/src/rule/description_format.rs @@ -30,7 +30,16 @@ impl Rule for DescriptionFormat { fn validate(&self, message: &Message) -> Option { if let Some(format) = &self.format { - let regex = regex::Regex::new(format).unwrap(); + let regex = match regex::Regex::new(format) { + Ok(regex) => regex, + Err(err) => { + return Some(Violation { + level: self.level.unwrap_or(Self::LEVEL), + message: err.to_string(), + }); + } + }; + if !regex.is_match(&message.description.as_ref().unwrap()) { return Some(Violation { level: self.level.unwrap_or(Self::LEVEL), @@ -98,4 +107,25 @@ mod tests { "description format does not match format: ^[a-z].*".to_string() ); } + + #[test] + fn test_invalid_regex() { + let mut rule = DescriptionFormat::default(); + rule.format = Some(r"(".to_string()); + + let message = Message { + body: None, + description: Some("Add regex".to_string()), + footers: None, + r#type: Some("feat".to_string()), + raw: "feat(scope): Add regex".to_string(), + scope: Some("scope".to_string()), + subject: None, + }; + + let violation = rule.validate(&message); + assert!(violation.is_some()); + assert_eq!(violation.clone().unwrap().level, Level::Error); + assert!(violation.unwrap().message.contains("regex parse error")); + } } diff --git a/src/rule/scope_format.rs b/src/rule/scope_format.rs index 9d31a5e..d446168 100644 --- a/src/rule/scope_format.rs +++ b/src/rule/scope_format.rs @@ -30,7 +30,16 @@ impl Rule for ScopeFormat { fn validate(&self, message: &Message) -> Option { if let Some(format) = &self.format { - let regex = regex::Regex::new(format).unwrap(); + let regex = match regex::Regex::new(format) { + Ok(regex) => regex, + Err(err) => { + return Some(Violation { + level: self.level.unwrap_or(Self::LEVEL), + message: err.to_string(), + }); + } + }; + if !regex.is_match(&message.scope.as_ref().unwrap()) { return Some(Violation { level: self.level.unwrap_or(Self::LEVEL), @@ -98,4 +107,25 @@ mod tests { "scope format does not match format: ^[a-z].*".to_string() ); } + + #[test] + fn test_invalid_regex() { + let mut rule = ScopeFormat::default(); + rule.format = Some(r"(".to_string()); + + let message = Message { + body: None, + description: Some("Add regex".to_string()), + footers: None, + r#type: Some("feat".to_string()), + raw: "feat(scope): Add regex".to_string(), + scope: Some("scope".to_string()), + subject: None, + }; + + let violation = rule.validate(&message); + assert!(violation.is_some()); + assert_eq!(violation.clone().unwrap().level, Level::Error); + assert!(violation.unwrap().message.contains("regex parse error")); + } } diff --git a/src/rule/type_format.rs b/src/rule/type_format.rs index 7ea0635..3d1a1e4 100644 --- a/src/rule/type_format.rs +++ b/src/rule/type_format.rs @@ -30,7 +30,16 @@ impl Rule for TypeFormat { fn validate(&self, message: &Message) -> Option { if let Some(format) = &self.format { - let regex = regex::Regex::new(format).unwrap(); + let regex = match regex::Regex::new(format) { + Ok(regex) => regex, + Err(err) => { + return Some(Violation { + level: self.level.unwrap_or(Self::LEVEL), + message: err.to_string(), + }); + } + }; + if !regex.is_match(&message.r#type.as_ref().unwrap()) { return Some(Violation { level: self.level.unwrap_or(Self::LEVEL), @@ -98,4 +107,25 @@ mod tests { "type format does not match format: ^[a-z].*".to_string() ); } + + #[test] + fn test_invalid_regex() { + let mut rule = TypeFormat::default(); + rule.format = Some(r"(".to_string()); + + let message = Message { + body: None, + description: Some("Invalid regex".to_string()), + footers: None, + r#type: Some("feat".to_string()), + raw: "feat(scope): Invalid regex".to_string(), + scope: Some("scope".to_string()), + subject: None, + }; + + let violation = rule.validate(&message); + assert!(violation.is_some()); + assert_eq!(violation.clone().unwrap().level, Level::Error); + assert!(violation.unwrap().message.contains("regex parse error")); + } }