Skip to content

Commit

Permalink
Add sanity check on adding a new rule property
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource committed Sep 29, 2023
1 parent f835a4a commit 4a328ec
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 @@ -22,14 +22,18 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.google.gson.Gson;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
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 +68,23 @@ 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, we should inform the SonarCloud team about it on release.
*/
@Test
void rules_properties_count() {
var count = 0;
for (Class clazz : CssRules.getRuleClasses()) {
for (Field 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 @@ -21,6 +21,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -29,11 +30,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 +137,23 @@ 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, we should inform the SonarCloud team about it on release.
*/
@Test
void testChecksPropertiesCount() {
var count = 0;
for (Class clazz : CheckList.getAllChecks()) {
for (Field 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 4a328ec

Please sign in to comment.