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

feat(cli): improve message for disabled scopes and types #273

Merged
merged 2 commits into from
Feb 14, 2024
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
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 @@
const LEVEL: Level = Level::Error;

fn message(&self, message: &Message) -> String {
if self.options.len() == 0 {

Check warning on line 26 in src/rule/scope.rs

View workflow job for this annotation

GitHub Actions / clippy

length comparison to zero
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 @@
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 @@
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 @@
const NAME: &'static str = "type";
const LEVEL: Level = Level::Error;
fn message(&self, message: &Message) -> String {
if self.options.len() == 0 {

Check warning on line 25 in src/rule/type.rs

View workflow job for this annotation

GitHub Actions / clippy

length comparison to zero
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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
```
Loading