Skip to content

Commit

Permalink
chore(cli): error handle format regex error (#298)
Browse files Browse the repository at this point in the history
Signed-off-by: KeisukeYamashita <[email protected]>
  • Loading branch information
KeisukeYamashita authored Apr 23, 2024
1 parent 88983e7 commit 1b29def
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
32 changes: 31 additions & 1 deletion src/rule/description_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ impl Rule for DescriptionFormat {

fn validate(&self, message: &Message) -> Option<Violation> {
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),
Expand Down Expand Up @@ -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"));
}
}
32 changes: 31 additions & 1 deletion src/rule/scope_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ impl Rule for ScopeFormat {

fn validate(&self, message: &Message) -> Option<Violation> {
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),
Expand Down Expand Up @@ -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"));
}
}
32 changes: 31 additions & 1 deletion src/rule/type_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ impl Rule for TypeFormat {

fn validate(&self, message: &Message) -> Option<Violation> {
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),
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit 1b29def

Please sign in to comment.