Skip to content

Commit

Permalink
feat(cli): improve message for disabled scopes and types (#273)
Browse files Browse the repository at this point in the history
* feat(cli): improve error message for empty scopes and types

Signed-off-by: KeisukeYamashita <[email protected]>

* feat(web): add disable scopes and types

Signed-off-by: KeisukeYamashita <[email protected]>

---------

Signed-off-by: KeisukeYamashita <[email protected]>
  • Loading branch information
KeisukeYamashita authored Feb 14, 2024
1 parent fe8264d commit c26f3b6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
41 changes: 23 additions & 18 deletions src/rule/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ impl Rule for Scope {
const LEVEL: Level = Level::Error;

fn message(&self, message: &Message) -> String {
if self.options.len() == 0 {
return "scopes are not allowed".to_string();
}

format!(
"scope {} is not allowed. Only {:?} are allowed",
message.scope.as_ref().unwrap_or(&"".to_string()),
Expand Down Expand Up @@ -59,44 +63,45 @@ mod tests {
use super::*;

#[test]
fn test_valid_scope() {
fn test_empty_scope() {
let mut rule = Scope::default();
rule.options = vec!["api".to_string(), "web".to_string()];

let message = Message {
body: None,
description: None,
footers: None,
r#type: Some("feat".to_string()),
raw: "feat(web): broadcast $destroy event on scope destruction".to_string(),
scope: Some("web".to_string()),
r#type: None,
raw: "".to_string(),
scope: None,
subject: None,
};

assert!(rule.validate(&message).is_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 [\"api\", \"web\"] are allowed"
);
}

#[test]
fn test_empty_message() {
let rule = Scope::default();
fn test_valid_scope() {
let mut rule = Scope::default();
rule.options = vec!["api".to_string(), "web".to_string()];

let message = Message {
body: None,
description: None,
footers: None,
r#type: None,
raw: "".to_string(),
scope: None,
r#type: Some("feat".to_string()),
raw: "feat(web): broadcast $destroy event on scope destruction".to_string(),
scope: Some("web".to_string()),
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"
);
assert!(rule.validate(&message).is_none());
}

#[test]
Expand Down Expand Up @@ -142,7 +147,7 @@ mod tests {
assert_eq!(violation.clone().unwrap().level, Level::Error);
assert_eq!(
violation.unwrap().message,
"scope invalid is not allowed. Only [] are allowed".to_string()
"scopes are not allowed".to_string()
);
}
}
19 changes: 12 additions & 7 deletions src/rule/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ impl Rule for Type {
const NAME: &'static str = "type";
const LEVEL: Level = Level::Error;
fn message(&self, message: &Message) -> String {
if self.options.len() == 0 {
return "types are not allowed".to_string();
}

format!(
"type {} is not allowed. Only {:?} are allowed",
message.r#type.as_ref().unwrap_or(&"".to_string()),
Expand Down Expand Up @@ -58,8 +62,9 @@ mod tests {
use super::*;

#[test]
fn test_empty_message() {
let rule = Type::default();
fn test_empty_type() {
let mut rule = Type::default();
rule.options = vec!["doc".to_string(), "feat".to_string()];

let message = Message {
body: None,
Expand All @@ -73,7 +78,7 @@ mod tests {
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()
"type is not allowed. Only [\"doc\", \"feat\"] are allowed".to_string()
);
}

Expand Down Expand Up @@ -120,7 +125,7 @@ mod tests {
assert_eq!(violation.clone().unwrap().level, Level::Error);
assert_eq!(
violation.unwrap().message,
"type invalid is not allowed. Only [] are allowed".to_string()
"types are not allowed".to_string()
);
}

Expand All @@ -133,7 +138,7 @@ mod tests {
description: None,
footers: None,
r#type: None,
raw: "invalid(scope): broadcast $destroy event on scope destruction".to_string(),
raw: "(scope): broadcast $destroy event on scope destruction".to_string(),
scope: None,
subject: None,
};
Expand All @@ -143,7 +148,7 @@ mod tests {
assert_eq!(violation.clone().unwrap().level, Level::Error);
assert_eq!(
violation.unwrap().message,
"type is not allowed. Only [] are allowed".to_string()
"types are not allowed".to_string()
);
}

Expand All @@ -164,7 +169,7 @@ mod tests {
assert_eq!(rule.validate(&message).unwrap().level, Level::Error);
assert_eq!(
rule.validate(&message).unwrap().message,
"type is not allowed. Only [] are allowed"
"types are not allowed".to_string()
);
}

Expand Down
11 changes: 11 additions & 0 deletions web/src/content/docs/rules/scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ rules:
- api
- web
```
### Disallow all scopes
```yaml
rules:
scope:
level: error
options: [] # or [""]
scope-empty:
level: ignore
```
11 changes: 11 additions & 0 deletions web/src/content/docs/rules/type.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ rules:
- api
- web
```
### Disallow all types
```yaml
rules:
type:
level: error
options: [] # or [""]
type-empty:
level: ignore
```

0 comments on commit c26f3b6

Please sign in to comment.