Skip to content

Commit a92258b

Browse files
Fix issues with rules being run against examples from other rules. (#301)
1 parent 6d75e3a commit a92258b

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

.cirrus.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ linux_image_template: &LINUX_IMAGE
4646
namespace: default
4747
use_in_memory_disk: true
4848

49-
linux_1_cpu_1G_template: &LINUX_1_CPU_1G
49+
linux_1_cpu_1G_template: &LINUX_1_CPU_2G
5050
eks_container:
5151
<<: *LINUX_IMAGE
5252
cpu: 1
53-
memory: 1G
53+
memory: 2G
5454

5555
linux_2_cpu_6G_java_17_template: &LINUX_2_CPU_6G_JAVA_17
5656
eks_container:
@@ -97,7 +97,7 @@ build_task:
9797
ws_scan_task:
9898
depends_on:
9999
- build
100-
<<: *LINUX_1_CPU_1G
100+
<<: *LINUX_1_CPU_2G
101101
# run only on master and long-term branches
102102
only_if: $CIRRUS_USER_COLLABORATOR == 'true' && $CIRRUS_TAG == "" && ($CIRRUS_BRANCH == "master" || $CIRRUS_BRANCH =~ "branch-.*")
103103
env:
@@ -143,7 +143,7 @@ promote_task:
143143
- qa_plugin
144144
- ws_scan
145145
<<: *ONLY_IF_SONARSOURCE_QA
146-
<<: *LINUX_1_CPU_1G
146+
<<: *LINUX_1_CPU_2G
147147
env:
148148
#promotion cloud function
149149
GCF_ACCESS_TOKEN: VAULT[development/kv/data/promote data.token]

sonar-text-plugin/src/main/java/org/sonar/plugins/secrets/api/SpecificationBasedCheck.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Objects;
2727
import java.util.stream.Collectors;
28+
import java.util.function.Predicate;
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
3031
import org.sonar.api.batch.fs.TextRange;
@@ -73,11 +74,21 @@ public void initialize(SpecificationLoader loader, Map<URI, List<TextRange>> rep
7374

7475
@Override
7576
public void analyze(InputFileContext ctx) {
77+
analyze(ctx, checkId -> true);
78+
}
79+
80+
public void analyze(InputFileContext ctx, String ruleId) {
81+
analyze(ctx, checkId -> checkId.equals(ruleId));
82+
}
83+
84+
protected void analyze(InputFileContext ctx, Predicate<String> ruleFilter) {
7685
for (SecretMatcher secretMatcher : matcher) {
77-
durationStatistics.timed(secretMatcher.getRuleId() + DurationStatistics.SUFFIX_TOTAL, () -> secretMatcher.findIn(ctx))
78-
.stream()
79-
.map(match -> ctx.newTextRangeFromFileOffsets(match.getFileStartOffset(), match.getFileEndOffset()))
80-
.forEach(textRange -> reportIfNoOverlappingSecretAlreadyFound(ctx, textRange, secretMatcher));
86+
if (ruleFilter.test(secretMatcher.getRuleId())) {
87+
durationStatistics.timed(secretMatcher.getRuleId() + DurationStatistics.SUFFIX_TOTAL, () -> secretMatcher.findIn(ctx))
88+
.stream()
89+
.map(match -> ctx.newTextRangeFromFileOffsets(match.getFileStartOffset(), match.getFileEndOffset()))
90+
.forEach(textRange -> reportIfNoOverlappingSecretAlreadyFound(ctx, textRange, secretMatcher));
91+
}
8192
}
8293
}
8394

sonar-text-plugin/src/test/java/org/sonar/plugins/secrets/utils/AbstractRuleExampleTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ public abstract class AbstractRuleExampleTest {
5555

5656
private static final Logger LOG = LoggerFactory.getLogger(AbstractRuleExampleTest.class);
5757
private static SpecificationLoader specificationLoader;
58-
private final Check check;
58+
private final SpecificationBasedCheck check;
5959
private final HashMap<URI, List<TextRange>> reportedIssuesForCtx = new HashMap<>();
6060

61-
protected AbstractRuleExampleTest(Check check) {
61+
protected AbstractRuleExampleTest(SpecificationBasedCheck check) {
6262
if (specificationLoader == null) {
6363
specificationLoader = new SpecificationLoader();
6464
}
6565

6666
this.check = check;
67-
((SpecificationBasedCheck) check).initialize(specificationLoader, reportedIssuesForCtx, mockDurationStatistics());
67+
check.initialize(specificationLoader, reportedIssuesForCtx, mockDurationStatistics());
6868
}
6969

7070
@TestFactory
@@ -85,7 +85,7 @@ private Executable analyzeExample(Rule rule, RuleExample ruleExample) {
8585
String exampleFileName = ruleExample.getFileName() != null ? ruleExample.getFileName() : "file.txt";
8686
InputFileContext inputFileContext = new InputFileContext(context, inputFile(Path.of(exampleFileName), ruleExample.getText()));
8787

88-
check.analyze(inputFileContext);
88+
check.analyze(inputFileContext, rule.getId());
8989

9090
Collection<Issue> issues = context.allIssues();
9191
if (ruleExample.isContainsSecret()) {

sonar-text-plugin/src/test/java/org/sonar/plugins/secrets/utils/UpdatingSpecificationFilesGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Map;
3737
import java.util.Set;
3838
import java.util.stream.Collectors;
39+
import org.junit.jupiter.api.Disabled;
3940
import org.junit.jupiter.api.Test;
4041
import org.junit.jupiter.api.extension.RegisterExtension;
4142
import org.slf4j.Logger;
@@ -68,12 +69,14 @@ class UpdatingSpecificationFilesGenerator {
6869

6970
// Suppress warning, as there are no assertions inside here
7071
@Test
72+
@Disabled("Should only be triggered manually")
7173
@SuppressWarnings("java:S2699")
7274
void firstStep() {
7375
writeSpecificationFileDefinition();
7476
}
7577

7678
@Test
79+
@Disabled("Should only be triggered manually")
7780
void secondStep() {
7881
testDeserializationOfSpecificationFiles();
7982
SpecificationLoader specificationLoader = new SpecificationLoader();

0 commit comments

Comments
 (0)