Skip to content

Commit

Permalink
Add sanity check on adding a new rule property (#4222)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource committed Sep 29, 2023
1 parent c7d9904 commit 3e7b253
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.mockito.internal.util.collections.Sets;
import org.sonar.check.RuleProperty;
import org.sonar.css.CssRules;

class CssRuleTest {

private static final int RULES_PROPERTIES_COUNT = 9;

@Test
void class_name_should_match_stylelint_key()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Expand Down Expand Up @@ -64,6 +67,24 @@ void rules_default_is_empty()
}
}

/*
* This test raises awareness of the consequence of a rule adding or removing a rule property.
* If a new rule property is added to an existing rule, we should inform the SonarCloud team
* about it on release. Rule properties of newly added rules are not concerned by that.
*/
@Test
void rules_properties_count() {
var count = 0;
for (var clazz : CssRules.getRuleClasses()) {
for (var field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(RuleProperty.class)) {
count++;
}
}
}
assertThat(count).isEqualTo(RULES_PROPERTIES_COUNT);
}

@Test
void selector_pseudo_class_options() {
SelectorPseudoClassNoUnknown selectorPseudoClassNoUnknown = new SelectorPseudoClassNoUnknown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.javascript.api.EslintBasedCheck;
import org.sonar.plugins.javascript.api.JavaScriptCheck;

class CheckListTest {

private static final int CHECKS_PROPERTIES_COUNT = 35;

/**
* Enforces that each check declared in list.
*/
Expand Down Expand Up @@ -133,6 +136,24 @@ void testEveryCheckBelongsToLanguage() {
assertThat(allChecks).isEqualTo(tsAndJsChecks);
}

/*
* This test raises awareness of the consequence of a rule adding or removing a rule property.
* If a new rule property is added to an existing rule, we should inform the SonarCloud team
* about it on release. Rule properties of newly added rules are not concerned by that.
*/
@Test
void testChecksPropertiesCount() {
var count = 0;
for (var clazz : CheckList.getAllChecks()) {
for (var field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(RuleProperty.class)) {
count++;
}
}
}
assertThat(count).isEqualTo(CHECKS_PROPERTIES_COUNT);
}

private boolean isEslintBasedCheck(Class<? extends JavaScriptCheck> cls) {
try {
cls.getMethod("eslintKey");
Expand Down

0 comments on commit 3e7b253

Please sign in to comment.