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

Add sanity check on adding a new rule property #4222

Merged
merged 2 commits into from
Sep 29, 2023
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
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