Skip to content

Commit

Permalink
Merge pull request #35 from green-code-initiative/ISSUE_34
Browse files Browse the repository at this point in the history
Add test to ensure all Rules are registered
  • Loading branch information
dedece35 authored Jul 26, 2024
2 parents 8f7f37b + 3403732 commit 539dfec
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 78 deletions.
21 changes: 15 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- [#34](https://github.com/green-code-initiative/ecoCode-php/issues/34) Add test to ensure all Rules are registered

### Deleted

## [1.5.1] - 2024-07-25
Expand All @@ -24,7 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- upgrade docker system
- [#30](https://github.com/green-code-initiative/ecoCode-php/issues/30) Upgrade dependencies to latest ones - technical incompatibilities for SonarQube before 9.9 version
- [#30](https://github.com/green-code-initiative/ecoCode-php/issues/30) Upgrade dependencies to latest ones - technical
incompatibilities for SonarQube before 9.9 version
- clean unit test files (to have less other issues)

## [1.4.4] - 2024-07-19
Expand All @@ -37,10 +40,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [#20](https://github.com/green-code-initiative/ecoCode-php/issues/20) Add support for SonarQube 10.4 "DownloadOnlyWhenRequired" feature
- [#20](https://github.com/green-code-initiative/ecoCode-php/issues/20) Add support for SonarQube 10.4 "
DownloadOnlyWhenRequired" feature

### Changed
- [#23](https://github.com/green-code-initiative/ecoCode-php/issues/23) deprecation of EC22 rule for PHP (waiting for measurement to prove it)

- [#23](https://github.com/green-code-initiative/ecoCode-php/issues/23) deprecation of EC22 rule for PHP (waiting for
measurement to prove it)
- check Sonarqube 10.5.1 compatibility + update docker files and README.md

## [1.4.2] - 2024-01-12
Expand All @@ -58,8 +64,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- [#9](https://github.com/green-code-initiative/ecoCode-php/pull/9) Upgrade licence system and licence headers of Java files
- [#10](https://github.com/green-code-initiative/ecoCode-php/pull/10) Adding EC35 rule : EC35 rule replaces EC34 with a specific use case ("file not found" specific)
- [#9](https://github.com/green-code-initiative/ecoCode-php/pull/9) Upgrade licence system and licence headers of Java
files
- [#10](https://github.com/green-code-initiative/ecoCode-php/pull/10) Adding EC35 rule : EC35 rule replaces EC34 with a
specific use case ("file not found" specific)
- [#13](https://github.com/green-code-initiative/ecoCode-php/issues/13) Add build number to manifest
- [#12](https://github.com/green-code-initiative/ecoCode-php/issues/12) Fix unit tests execution with Maven
- Update ecocode-rules-specifications to 1.4.6
Expand All @@ -70,7 +78,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- PHP rules moved from `ecoCode` repository to current repository
- [#121](https://github.com/green-code-initiative/ecoCode/issues/121) new PHP rule : Multiple if-else statement + refactoring implementation
- [#121](https://github.com/green-code-initiative/ecoCode/issues/121) new PHP rule : Multiple if-else statement +
refactoring implementation
- [#205](https://github.com/green-code-initiative/ecoCode/issues/205) compatibility with SonarQube 10.1

## Comparison list
Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.ecocode</groupId>
Expand Down Expand Up @@ -139,6 +140,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
63 changes: 32 additions & 31 deletions src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,17 @@
*/
package fr.greencodeinitiative.php;

import java.util.List;

import fr.greencodeinitiative.php.checks.AvoidGettingSizeCollectionInLoopCheck;
import fr.greencodeinitiative.php.checks.*;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.plugins.php.api.visitors.PHPCustomRuleRepository;
import org.sonarsource.analyzer.commons.RuleMetadataLoader;

public class PhpRuleRepository implements RulesDefinition, PHPCustomRuleRepository {

private static final String LANGUAGE = "php";
private static final String NAME = "ecoCode";
private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/php";
private static final String REPOSITORY_KEY = "ecocode-php";

private final SonarRuntime sonarRuntime;

public PhpRuleRepository(SonarRuntime sonarRuntime) {
this.sonarRuntime = sonarRuntime;
}

@Override
public void define(Context context) {
NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME);
RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, sonarRuntime);
ruleMetadataLoader.addRulesByAnnotatedClass(repository, checkClasses());
repository.done();
}
import java.util.List;

@Override
public String repositoryKey() {
return REPOSITORY_KEY;
}
public class PhpRuleRepository implements RulesDefinition, PHPCustomRuleRepository {

@Override
public List<Class<?>> checkClasses() {
return List.of(
static final List<Class<?>> ANNOTATED_RULE_CLASSES = List.of(
AvoidGettingSizeCollectionInLoopCheck.class,
AvoidDoubleQuoteCheck.class,
AvoidFullSQLRequestCheck.class,
Expand All @@ -66,5 +39,33 @@ public List<Class<?>> checkClasses() {
UseOfMethodsForBasicOperations.class,
AvoidMultipleIfElseStatementCheck.class
);
}

private static final String LANGUAGE = "php";
private static final String NAME = "ecoCode";
private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/php";
private static final String REPOSITORY_KEY = "ecocode-php";

private final SonarRuntime sonarRuntime;

public PhpRuleRepository(SonarRuntime sonarRuntime) {
this.sonarRuntime = sonarRuntime;
}

@Override
public void define(Context context) {
NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME);
RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, sonarRuntime);
ruleMetadataLoader.addRulesByAnnotatedClass(repository, checkClasses());
repository.done();
}

@Override
public String repositoryKey() {
return REPOSITORY_KEY;
}

@Override
public List<Class<?>> checkClasses() {
return ANNOTATED_RULE_CLASSES;
}
}
96 changes: 56 additions & 40 deletions src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,76 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.Version;
import org.sonar.check.Rule;

import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

class PhpRuleRepositoryTest {

private RulesDefinition.Repository repository;
private PhpRuleRepository rulesDefinition;
private RulesDefinition.Repository repository;

@BeforeEach
void init() {
final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
doReturn(Version.create(0, 0)).when(sonarRuntime).getApiVersion();
PhpRuleRepository rulesDefinition = new PhpRuleRepository(sonarRuntime);
RulesDefinition.Context context = new RulesDefinition.Context();
rulesDefinition.define(context);
repository = context.repository(rulesDefinition.repositoryKey());
}
void init() {
final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
doReturn(Version.create(0, 0)).when(sonarRuntime).getApiVersion();
rulesDefinition = new PhpRuleRepository(sonarRuntime);
RulesDefinition.Context context = new RulesDefinition.Context();
rulesDefinition.define(context);
repository = context.repository(rulesDefinition.repositoryKey());
}

@Test
@DisplayName("Test repository metadata")
void testMetadata() {
assertThat(repository.name()).isEqualTo("ecoCode");
assertThat(repository.language()).isEqualTo("php");
assertThat(repository.key()).isEqualTo("ecocode-php");
}

@Test
void testRegistredRules() {
assertThat(rulesDefinition.checkClasses())
.describedAs("All implemented rules must be registered into " + PhpRuleRepository.class)
.containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0]));
}

@Test
@DisplayName("Test repository metadata")
void testMetadata() {
assertThat(repository.name()).isEqualTo("ecoCode");
assertThat(repository.language()).isEqualTo("php");
assertThat(repository.key()).isEqualTo("ecocode-php");
}
@Test
void checkNumberRules() {
assertThat(repository.rules()).hasSize(PhpRuleRepository.ANNOTATED_RULE_CLASSES.size());
}

@Test
void testRegistredRules() {
assertThat(repository.rules()).hasSize(10);
}
@Test
@DisplayName("All rule keys must be prefixed by 'EC'")
void testRuleKeyPrefix() {
SoftAssertions assertions = new SoftAssertions();
repository.rules().forEach(
rule -> assertions.assertThat(rule.key()).startsWith("EC")
);
assertions.assertAll();
}

@Test
@DisplayName("All rule keys must be prefixed by 'EC'")
void testRuleKeyPrefix() {
SoftAssertions assertions = new SoftAssertions();
repository.rules().forEach(
rule -> assertions.assertThat(rule.key()).startsWith("EC")
);
assertions.assertAll();
}
@Test
void testAllRuleParametersHaveDescription() {
SoftAssertions assertions = new SoftAssertions();
repository.rules().stream()
.flatMap(rule -> rule.params().stream())
.forEach(param -> assertions.assertThat(param.description())
.as("description for " + param.key())
.isNotEmpty());
assertions.assertAll();
}

@Test
void testAllRuleParametersHaveDescription() {
SoftAssertions assertions = new SoftAssertions();
repository.rules().stream()
.flatMap(rule -> rule.params().stream())
.forEach(param -> assertions.assertThat(param.description())
.as("description for " + param.key())
.isNotEmpty());
assertions.assertAll();
}
private static Set<Class<?>> getDefinedRules() {
Reflections r = new Reflections(PhpRuleRepository.class.getPackageName() + ".checks");
return r.getTypesAnnotatedWith(Rule.class);
}
}

0 comments on commit 539dfec

Please sign in to comment.