Skip to content

Commit c26f3b6

Browse files
feat(cli): improve message for disabled scopes and types (#273)
* 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]>
1 parent fe8264d commit c26f3b6

File tree

4 files changed

+57
-25
lines changed

4 files changed

+57
-25
lines changed

src/rule/scope.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ impl Rule for Scope {
2323
const LEVEL: Level = Level::Error;
2424

2525
fn message(&self, message: &Message) -> String {
26+
if self.options.len() == 0 {
27+
return "scopes are not allowed".to_string();
28+
}
29+
2630
format!(
2731
"scope {} is not allowed. Only {:?} are allowed",
2832
message.scope.as_ref().unwrap_or(&"".to_string()),
@@ -59,44 +63,45 @@ mod tests {
5963
use super::*;
6064

6165
#[test]
62-
fn test_valid_scope() {
66+
fn test_empty_scope() {
6367
let mut rule = Scope::default();
6468
rule.options = vec!["api".to_string(), "web".to_string()];
6569

6670
let message = Message {
6771
body: None,
6872
description: None,
6973
footers: None,
70-
r#type: Some("feat".to_string()),
71-
raw: "feat(web): broadcast $destroy event on scope destruction".to_string(),
72-
scope: Some("web".to_string()),
74+
r#type: None,
75+
raw: "".to_string(),
76+
scope: None,
7377
subject: None,
7478
};
7579

76-
assert!(rule.validate(&message).is_none());
80+
let violation = rule.validate(&message);
81+
assert!(violation.is_some());
82+
assert_eq!(violation.clone().unwrap().level, Level::Error);
83+
assert_eq!(
84+
violation.unwrap().message,
85+
"scope is not allowed. Only [\"api\", \"web\"] are allowed"
86+
);
7787
}
7888

7989
#[test]
80-
fn test_empty_message() {
81-
let rule = Scope::default();
90+
fn test_valid_scope() {
91+
let mut rule = Scope::default();
92+
rule.options = vec!["api".to_string(), "web".to_string()];
8293

8394
let message = Message {
8495
body: None,
8596
description: None,
8697
footers: None,
87-
r#type: None,
88-
raw: "".to_string(),
89-
scope: None,
98+
r#type: Some("feat".to_string()),
99+
raw: "feat(web): broadcast $destroy event on scope destruction".to_string(),
100+
scope: Some("web".to_string()),
90101
subject: None,
91102
};
92103

93-
let violation = rule.validate(&message);
94-
assert!(violation.is_some());
95-
assert_eq!(violation.clone().unwrap().level, Level::Error);
96-
assert_eq!(
97-
violation.unwrap().message,
98-
"scope is not allowed. Only [] are allowed"
99-
);
104+
assert!(rule.validate(&message).is_none());
100105
}
101106

102107
#[test]
@@ -142,7 +147,7 @@ mod tests {
142147
assert_eq!(violation.clone().unwrap().level, Level::Error);
143148
assert_eq!(
144149
violation.unwrap().message,
145-
"scope invalid is not allowed. Only [] are allowed".to_string()
150+
"scopes are not allowed".to_string()
146151
);
147152
}
148153
}

src/rule/type.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ impl Rule for Type {
2222
const NAME: &'static str = "type";
2323
const LEVEL: Level = Level::Error;
2424
fn message(&self, message: &Message) -> String {
25+
if self.options.len() == 0 {
26+
return "types are not allowed".to_string();
27+
}
28+
2529
format!(
2630
"type {} is not allowed. Only {:?} are allowed",
2731
message.r#type.as_ref().unwrap_or(&"".to_string()),
@@ -58,8 +62,9 @@ mod tests {
5862
use super::*;
5963

6064
#[test]
61-
fn test_empty_message() {
62-
let rule = Type::default();
65+
fn test_empty_type() {
66+
let mut rule = Type::default();
67+
rule.options = vec!["doc".to_string(), "feat".to_string()];
6368

6469
let message = Message {
6570
body: None,
@@ -73,7 +78,7 @@ mod tests {
7378
assert_eq!(rule.validate(&message).unwrap().level, Level::Error);
7479
assert_eq!(
7580
rule.validate(&message).unwrap().message,
76-
"type is not allowed. Only [] are allowed".to_string()
81+
"type is not allowed. Only [\"doc\", \"feat\"] are allowed".to_string()
7782
);
7883
}
7984

@@ -120,7 +125,7 @@ mod tests {
120125
assert_eq!(violation.clone().unwrap().level, Level::Error);
121126
assert_eq!(
122127
violation.unwrap().message,
123-
"type invalid is not allowed. Only [] are allowed".to_string()
128+
"types are not allowed".to_string()
124129
);
125130
}
126131

@@ -133,7 +138,7 @@ mod tests {
133138
description: None,
134139
footers: None,
135140
r#type: None,
136-
raw: "invalid(scope): broadcast $destroy event on scope destruction".to_string(),
141+
raw: "(scope): broadcast $destroy event on scope destruction".to_string(),
137142
scope: None,
138143
subject: None,
139144
};
@@ -143,7 +148,7 @@ mod tests {
143148
assert_eq!(violation.clone().unwrap().level, Level::Error);
144149
assert_eq!(
145150
violation.unwrap().message,
146-
"type is not allowed. Only [] are allowed".to_string()
151+
"types are not allowed".to_string()
147152
);
148153
}
149154

@@ -164,7 +169,7 @@ mod tests {
164169
assert_eq!(rule.validate(&message).unwrap().level, Level::Error);
165170
assert_eq!(
166171
rule.validate(&message).unwrap().message,
167-
"type is not allowed. Only [] are allowed"
172+
"types are not allowed".to_string()
168173
);
169174
}
170175

web/src/content/docs/rules/scope.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ rules:
4141
- api
4242
- web
4343
```
44+
45+
### Disallow all scopes
46+
47+
```yaml
48+
rules:
49+
scope:
50+
level: error
51+
options: [] # or [""]
52+
scope-empty:
53+
level: ignore
54+
```

web/src/content/docs/rules/type.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ rules:
4141
- api
4242
- web
4343
```
44+
45+
### Disallow all types
46+
47+
```yaml
48+
rules:
49+
type:
50+
level: error
51+
options: [] # or [""]
52+
type-empty:
53+
level: ignore
54+
```

0 commit comments

Comments
 (0)