Skip to content

Commit 1b29def

Browse files
chore(cli): error handle format regex error (#298)
Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com>
1 parent 88983e7 commit 1b29def

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

src/rule/description_format.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ impl Rule for DescriptionFormat {
3030

3131
fn validate(&self, message: &Message) -> Option<Violation> {
3232
if let Some(format) = &self.format {
33-
let regex = regex::Regex::new(format).unwrap();
33+
let regex = match regex::Regex::new(format) {
34+
Ok(regex) => regex,
35+
Err(err) => {
36+
return Some(Violation {
37+
level: self.level.unwrap_or(Self::LEVEL),
38+
message: err.to_string(),
39+
});
40+
}
41+
};
42+
3443
if !regex.is_match(&message.description.as_ref().unwrap()) {
3544
return Some(Violation {
3645
level: self.level.unwrap_or(Self::LEVEL),
@@ -98,4 +107,25 @@ mod tests {
98107
"description format does not match format: ^[a-z].*".to_string()
99108
);
100109
}
110+
111+
#[test]
112+
fn test_invalid_regex() {
113+
let mut rule = DescriptionFormat::default();
114+
rule.format = Some(r"(".to_string());
115+
116+
let message = Message {
117+
body: None,
118+
description: Some("Add regex".to_string()),
119+
footers: None,
120+
r#type: Some("feat".to_string()),
121+
raw: "feat(scope): Add regex".to_string(),
122+
scope: Some("scope".to_string()),
123+
subject: None,
124+
};
125+
126+
let violation = rule.validate(&message);
127+
assert!(violation.is_some());
128+
assert_eq!(violation.clone().unwrap().level, Level::Error);
129+
assert!(violation.unwrap().message.contains("regex parse error"));
130+
}
101131
}

src/rule/scope_format.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ impl Rule for ScopeFormat {
3030

3131
fn validate(&self, message: &Message) -> Option<Violation> {
3232
if let Some(format) = &self.format {
33-
let regex = regex::Regex::new(format).unwrap();
33+
let regex = match regex::Regex::new(format) {
34+
Ok(regex) => regex,
35+
Err(err) => {
36+
return Some(Violation {
37+
level: self.level.unwrap_or(Self::LEVEL),
38+
message: err.to_string(),
39+
});
40+
}
41+
};
42+
3443
if !regex.is_match(&message.scope.as_ref().unwrap()) {
3544
return Some(Violation {
3645
level: self.level.unwrap_or(Self::LEVEL),
@@ -98,4 +107,25 @@ mod tests {
98107
"scope format does not match format: ^[a-z].*".to_string()
99108
);
100109
}
110+
111+
#[test]
112+
fn test_invalid_regex() {
113+
let mut rule = ScopeFormat::default();
114+
rule.format = Some(r"(".to_string());
115+
116+
let message = Message {
117+
body: None,
118+
description: Some("Add regex".to_string()),
119+
footers: None,
120+
r#type: Some("feat".to_string()),
121+
raw: "feat(scope): Add regex".to_string(),
122+
scope: Some("scope".to_string()),
123+
subject: None,
124+
};
125+
126+
let violation = rule.validate(&message);
127+
assert!(violation.is_some());
128+
assert_eq!(violation.clone().unwrap().level, Level::Error);
129+
assert!(violation.unwrap().message.contains("regex parse error"));
130+
}
101131
}

src/rule/type_format.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ impl Rule for TypeFormat {
3030

3131
fn validate(&self, message: &Message) -> Option<Violation> {
3232
if let Some(format) = &self.format {
33-
let regex = regex::Regex::new(format).unwrap();
33+
let regex = match regex::Regex::new(format) {
34+
Ok(regex) => regex,
35+
Err(err) => {
36+
return Some(Violation {
37+
level: self.level.unwrap_or(Self::LEVEL),
38+
message: err.to_string(),
39+
});
40+
}
41+
};
42+
3443
if !regex.is_match(&message.r#type.as_ref().unwrap()) {
3544
return Some(Violation {
3645
level: self.level.unwrap_or(Self::LEVEL),
@@ -98,4 +107,25 @@ mod tests {
98107
"type format does not match format: ^[a-z].*".to_string()
99108
);
100109
}
110+
111+
#[test]
112+
fn test_invalid_regex() {
113+
let mut rule = TypeFormat::default();
114+
rule.format = Some(r"(".to_string());
115+
116+
let message = Message {
117+
body: None,
118+
description: Some("Invalid regex".to_string()),
119+
footers: None,
120+
r#type: Some("feat".to_string()),
121+
raw: "feat(scope): Invalid regex".to_string(),
122+
scope: Some("scope".to_string()),
123+
subject: None,
124+
};
125+
126+
let violation = rule.validate(&message);
127+
assert!(violation.is_some());
128+
assert_eq!(violation.clone().unwrap().level, Level::Error);
129+
assert!(violation.unwrap().message.contains("regex parse error"));
130+
}
101131
}

0 commit comments

Comments
 (0)