Skip to content

Commit 23dcdec

Browse files
joke1196ghislainpiot
authored andcommitted
SONARPY-2043: Deactivate CPD for Notebooks (#1894)
1 parent adf1bf9 commit 23dcdec

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

sonar-python-plugin/src/main/java/org/sonar/plugins/python/PythonScanner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ private void saveMeasures(PythonInputFile inputFile, PythonVisitorContext visito
316316
noSonarFilter.noSonarInFile(inputFile.wrappedFile(), fileLinesVisitor.getLinesWithNoSonar());
317317

318318
if (!isInSonarLint(context)) {
319-
cpdAnalyzer.pushCpdTokens(inputFile.wrappedFile(), visitorContext);
319+
if (inputFile.kind() == PythonInputFile.Kind.PYTHON) {
320+
cpdAnalyzer.pushCpdTokens(inputFile.wrappedFile(), visitorContext);
321+
}
320322

321323
Set<Integer> linesOfCode = fileLinesVisitor.getLinesOfCode();
322324
saveMetricOnFile(inputFile, CoreMetrics.NCLOC, linesOfCode.size());

sonar-python-plugin/src/test/java/org/sonar/plugins/python/IPynbSensorTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.sonar.api.batch.rule.CheckFactory;
3636
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
3737
import org.sonar.api.batch.rule.internal.NewActiveRule;
38+
import org.sonar.api.batch.sensor.cpd.internal.TokensLine;
3839
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
3940
import org.sonar.api.batch.sensor.internal.SensorContextTester;
4041
import org.sonar.api.config.internal.MapSettings;
@@ -216,4 +217,14 @@ void test_notebook_sensor_is_excuted_on_json_file() {
216217
activeRules = new ActiveRulesBuilder().build();
217218
assertDoesNotThrow(() -> notebookSensor().execute(context));
218219
}
220+
221+
@Test
222+
void test_notebook_sensor_does_not_execute_cpd_measures() {
223+
inputFile(NOTEBOOK_FILE);
224+
activeRules = new ActiveRulesBuilder().build();
225+
notebookSensor().execute(context);
226+
List<TokensLine> tokensLines = context.cpdTokens("moduleKey:notebook.ipynb");
227+
assertThat(tokensLines)
228+
.isNull();
229+
}
219230
}

0 commit comments

Comments
 (0)