diff --git a/src/rule/scope.rs b/src/rule/scope.rs index 6db6817..b01db1a 100644 --- a/src/rule/scope.rs +++ b/src/rule/scope.rs @@ -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()), @@ -59,7 +63,7 @@ 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()]; @@ -67,36 +71,37 @@ mod tests { 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] @@ -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() ); } } diff --git a/src/rule/type.rs b/src/rule/type.rs index fef11b5..91deeb3 100644 --- a/src/rule/type.rs +++ b/src/rule/type.rs @@ -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()), @@ -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, @@ -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() ); } @@ -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() ); } @@ -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, }; @@ -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() ); } @@ -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() ); } diff --git a/web/src/content/docs/rules/scope.md b/web/src/content/docs/rules/scope.md index 2ebf967..0ff1301 100644 --- a/web/src/content/docs/rules/scope.md +++ b/web/src/content/docs/rules/scope.md @@ -41,3 +41,14 @@ rules: - api - web ``` + +### Disallow all scopes + +```yaml +rules: + scope: + level: error + options: [] # or [""] + scope-empty: + level: ignore +``` diff --git a/web/src/content/docs/rules/type.md b/web/src/content/docs/rules/type.md index ff96daf..eed695b 100644 --- a/web/src/content/docs/rules/type.md +++ b/web/src/content/docs/rules/type.md @@ -41,3 +41,14 @@ rules: - api - web ``` + +### Disallow all types + +```yaml +rules: + type: + level: error + options: [] # or [""] + type-empty: + level: ignore +```