Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): panic on empty commit or empty type #212

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,20 @@ Name: Keke";
Some(("feat".to_string(), None, "add dummy option".to_string()))
);
}
#[test]
fn test_parse_subject_without_message() {
let input = "";
assert_eq!(
parse_subject(input),
None
);
}
#[test]
fn test_parse_subject_with_error_message() {
let input = "test";
assert_eq!(
parse_subject(input),
None
);
}
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
};

if args.print_config {
println!("{}", config.to_string());

Check warning on line 27 in src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

`to_string` applied to a type that implements `Display` in `println!` args

Check warning on line 27 in src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

`to_string` applied to a type that implements `Display` in `println!` args
}

let messages = match args.read() {
Expand All @@ -39,7 +39,6 @@
.into_iter()
.map(|message| {
let config = config.clone();

suyulin marked this conversation as resolved.
Show resolved Hide resolved
tokio::spawn(async move { validate(&message, &config).await })
})
.collect::<Vec<_>>();
Expand Down
25 changes: 24 additions & 1 deletion src/rule/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Rule for Scope {
fn message(&self, message: &Message) -> String {
format!(
"scope {} is not allowed. Only {:?} are allowed",
message.scope.as_ref().unwrap(),
message.scope.as_ref().unwrap_or(&"".to_string()),
self.options
)
}
Expand Down Expand Up @@ -122,4 +122,27 @@ mod tests {
"scope invalid is not allowed. Only [] are allowed".to_string()
);
}

#[test]
fn test_without_scope() {
let rule = Scope::default();

let message = Message {
body: None,
description: None,
footers: None,
r#type: None,
raw: "".to_string(),
scope: None,
subject: None,
};

let violation = rule.validate(&message);
assert!(violation.is_some());
assert_eq!(violation.clone().unwrap().level, Level::Error);
assert_eq!(
violation.unwrap().message,
"scope is not allowed. Only [] are allowed"
);
}
}
42 changes: 40 additions & 2 deletions src/rule/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ pub struct Type {
impl Rule for Type {
const NAME: &'static str = "type";
const LEVEL: Level = Level::Error;

fn message(&self, message: &Message) -> String {
format!(
"type {} is not allowed. Only {:?} are allowed",
message.r#type.as_ref().unwrap(),
message.r#type.as_ref().unwrap_or(&"".to_string()),
self.options
)
}
Expand Down Expand Up @@ -122,4 +121,43 @@ mod tests {
"type invalid is not allowed. Only [] are allowed".to_string()
);
}

#[test]
fn test_empty_message() {
let rule = Type::default();

let message = Message {
body: None,
description: None,
footers: None,
r#type: None,
raw: "".to_string(),
scope: None,
subject: None,
};
assert_eq!(rule.validate(&message).unwrap().level, Level::Error);
assert_eq!(
rule.validate(&message).unwrap().message,
"type is not allowed. Only [] are allowed".to_string());
}

#[test]
fn test_missing_type() {
let rule = Type::default();
let input = "test".to_string();

let message = Message {
body: None,
description: None,
footers: None,
r#type: None,
raw: input.clone(),
scope: None,
subject: None,
};
assert_eq!(rule.validate(&message).unwrap().level, Level::Error);
assert_eq!(
rule.validate(&message).unwrap().message,
"type is not allowed. Only [] are allowed");
}
}
Loading