Skip to content
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 @@ -59,7 +59,8 @@ public void size_metrics() {
String fileKey = componentKey("org/apache/struts/action/", "Action.java");
assertThat(getMeasureAsInteger(fileKey, "files")).isEqualTo(1);
assertThat(getMeasureAsInteger(PROJECT_STRUTS, "lines")).isEqualTo(65059);
assertThat(getMeasureAsInteger(PROJECT_STRUTS, "ncloc")).isEqualTo(27577);
assertThat(getMeasureAsInteger(PROJECT_STRUTS, "ncloc"))
.isIn(27577, /*FIXME SONAR-27110 Can be removed when ITs will be run with SQS 2026.2+*/33231);
// 208 getter/setter
assertThat(getMeasureAsInteger(PROJECT_STRUTS, "functions")).isEqualTo(2730 + 208);

Expand Down
6 changes: 5 additions & 1 deletion java-frontend/src/main/java/org/sonar/java/Measurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ public class TestFileMeasurer implements JavaFileScanner {
@Override
public void scanFile(JavaFileScannerContext context) {
sonarFile = context.getInputFile();
var metricsComputer = ((MetricsScannerContext)context).getMetricsComputer();
var metricsComputer = ((MetricsScannerContext) context).getMetricsComputer();
noSonarFilter.noSonarInFile(sonarFile, metricsComputer.getNoSonarLines(context.getTree()));
if (isSonarLintContext()) {
return;
}
saveMetricOnFile(CoreMetrics.NCLOC, metricsComputer.getLinesOfCode(context.getTree()));
}
}

Expand Down
15 changes: 15 additions & 0 deletions java-frontend/src/test/java/org/sonar/java/MeasurerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ void verify_ncloc_metric() {
checkMetric("EmptyFile.java", "ncloc", 0);
}

@Test
void verify_ncloc_metric_on_test_file() {
String relativePath = PathUtils.sanitize(new File(BASE_DIR, "LinesOfCode.java").getPath());
InputFile inputFile = TestUtils.inputFile("", new File(relativePath), InputFile.Type.TEST);
context.fileSystem().add(inputFile);

Measurer measurer = new Measurer(context, mock(NoSonarFilter.class));
JavaFrontend frontend = new JavaFrontend(new JavaVersionImpl(), mockSonarComponents(), measurer, new NoOpTelemetry(), null, null);

frontend.scan(Collections.emptyList(), Collections.singletonList(inputFile), Collections.emptyList());

assertThat(context.measures(inputFile.key())).hasSize(1);
assertThat(context.measure(inputFile.key(), "ncloc").value()).isEqualTo(2);
}

/**
* Utility method to quickly get metric out of a file.
*/
Expand Down