Skip to content

Commit

Permalink
SONARPY-2406 Fix coverage by moving files between packages (#2208)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainpiot authored Dec 3, 2024
1 parent b138625 commit efcd828
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SonarQube Python Plugin
* Copyright (C) 2011-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.plugins.python;

import java.util.List;
import java.util.Map;
import org.sonar.python.EscapeCharPositionInfo;

public class NotebookTestUtils {

private NotebookTestUtils() {
// Utility class
}

public static List<EscapeCharPositionInfo> mapToColumnMappingList(Map<Integer, Integer> map) {
return map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(entry -> new EscapeCharPositionInfo(entry.getKey(), entry.getValue()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,37 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.internal.apachecommons.lang3.StringUtils;
import org.sonar.plugins.python.api.IssueLocation;
import org.sonar.plugins.python.api.PythonVisitorContext;
import org.sonar.python.IPythonLocation;
import org.sonar.python.TestPythonVisitorRunner;
import org.sonar.python.checks.TrailingWhitespaceCheck;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.plugins.python.TestUtils.createInputFile;
import static org.sonar.plugins.python.TestUtils.mapToColumnMappingList;
import static org.sonar.plugins.python.NotebookTestUtils.mapToColumnMappingList;

class IpynbNotebookParserTest {
private final File baseDir = new File("src/test/resources/org/sonar/plugins/python").getAbsoluteFile();

public static PythonInputFile createInputFile(File baseDir, String name, InputFile.Status status, InputFile.Type type) {
try {
return new PythonInputFileImpl(TestInputFileBuilder.create("moduleKey", name)
.setModuleBaseDir(baseDir.toPath())
.setCharset(StandardCharsets.UTF_8)
.setStatus(status)
.setType(type)
.setLanguage("py")
.initMetadata(Files.readString(new File(baseDir, name).toPath()))
.build());
} catch (IOException e) {
throw new IllegalStateException("Cannot read " + name + " from base directory" + baseDir, e);
}
}
@Test
void testParseNotebook() throws IOException {
var inputFile = createInputFile(baseDir, "notebook.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN);
Expand Down Expand Up @@ -238,65 +250,4 @@ void testParseNotebook1() throws IOException {
assertThat(result.contents()).isEmpty();
}

@Test
void trailing_whitespace() throws IOException {
var inputFile = createInputFile(baseDir, "notebook_trailing_whitespace.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN);
var result = IpynbNotebookParser.parseNotebook(inputFile).get();
var check = new TrailingWhitespaceCheck();
var context = new PythonVisitorContext(
TestPythonVisitorRunner.parseNotebookFile(result.locationMap(), result.contents()),
SonarQubePythonFile.create(result),
null,
"");
check.scanFile(context);

var issues = context.getIssues();
assertThat(issues).hasSize(3);

assertThat(issues.get(0).primaryLocation().startLine()).isEqualTo(14);
assertThat(issues.get(0).primaryLocation().endLine()).isEqualTo(14);
assertThat(issues.get(0).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);
assertThat(issues.get(0).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);

assertThat(issues.get(1).primaryLocation().startLine()).isEqualTo(17);
assertThat(issues.get(1).primaryLocation().endLine()).isEqualTo(17);
assertThat(issues.get(1).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);
assertThat(issues.get(1).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);

assertThat(issues.get(2).primaryLocation().startLine()).isEqualTo(20);
assertThat(issues.get(2).primaryLocation().endLine()).isEqualTo(20);
assertThat(issues.get(2).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);
assertThat(issues.get(2).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);
}

@Test
void trailing_whitespace_compressed() throws IOException {
var inputFile = createInputFile(baseDir, "notebook_trailing_whitespace_compressed.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN);
var result = IpynbNotebookParser.parseNotebook(inputFile).get();
var check = new TrailingWhitespaceCheck();
var context = new PythonVisitorContext(
TestPythonVisitorRunner.parseNotebookFile(result.locationMap(), result.contents()),
SonarQubePythonFile.create(result),
null,
"");
check.scanFile(context);

var issues = context.getIssues();
assertThat(issues).hasSize(3);

assertThat(issues.get(0).primaryLocation().startLine()).isEqualTo(1);
assertThat(issues.get(0).primaryLocation().endLine()).isEqualTo(1);
assertThat(issues.get(0).primaryLocation().startLineOffset()).isEqualTo(142);
assertThat(issues.get(0).primaryLocation().endLineOffset()).isEqualTo(157); // Should be 154

assertThat(issues.get(1).primaryLocation().startLine()).isEqualTo(1);
assertThat(issues.get(1).primaryLocation().endLine()).isEqualTo(1);
assertThat(issues.get(1).primaryLocation().startLineOffset()).isEqualTo(199);
assertThat(issues.get(1).primaryLocation().endLineOffset()).isEqualTo(207); // Should be 204

assertThat(issues.get(2).primaryLocation().startLine()).isEqualTo(1);
assertThat(issues.get(2).primaryLocation().endLine()).isEqualTo(1);
assertThat(issues.get(2).primaryLocation().startLineOffset()).isEqualTo(249);
assertThat(issues.get(2).primaryLocation().endLineOffset()).isEqualTo(249); // Should be 255
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* SonarQube Python Plugin
* Copyright (C) 2011-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.plugins.python;

import java.io.File;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.plugins.python.api.IssueLocation;
import org.sonar.plugins.python.api.PythonVisitorContext;
import org.sonar.python.TestPythonVisitorRunner;
import org.sonar.python.checks.TrailingWhitespaceCheck;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.plugins.python.TestUtils.createInputFile;

class IpynbNotebookParserScannerTest {

private final File baseDir = new File("src/test/resources/org/sonar/plugins/python");

@Test
void trailing_whitespace() throws IOException {
var inputFile = createInputFile(baseDir, "notebook_trailing_whitespace.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN);
var result = IpynbNotebookParser.parseNotebook(inputFile).get();
var check = new TrailingWhitespaceCheck();
var context = new PythonVisitorContext(
TestPythonVisitorRunner.parseNotebookFile(result.locationMap(), result.contents()),
SonarQubePythonFile.create(result),
null,
"");
check.scanFile(context);

var issues = context.getIssues();
assertThat(issues).hasSize(3);

assertThat(issues.get(0).primaryLocation().startLine()).isEqualTo(14);
assertThat(issues.get(0).primaryLocation().endLine()).isEqualTo(14);
assertThat(issues.get(0).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);
assertThat(issues.get(0).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);

assertThat(issues.get(1).primaryLocation().startLine()).isEqualTo(17);
assertThat(issues.get(1).primaryLocation().endLine()).isEqualTo(17);
assertThat(issues.get(1).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);
assertThat(issues.get(1).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);

assertThat(issues.get(2).primaryLocation().startLine()).isEqualTo(20);
assertThat(issues.get(2).primaryLocation().endLine()).isEqualTo(20);
assertThat(issues.get(2).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);
assertThat(issues.get(2).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET);
}

@Test
void trailing_whitespace_compressed() throws IOException {
var inputFile = createInputFile(baseDir, "notebook_trailing_whitespace_compressed.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN);
var result = IpynbNotebookParser.parseNotebook(inputFile).get();
var check = new TrailingWhitespaceCheck();
var context = new PythonVisitorContext(
TestPythonVisitorRunner.parseNotebookFile(result.locationMap(), result.contents()),
SonarQubePythonFile.create(result),
null,
"");
check.scanFile(context);

var issues = context.getIssues();
assertThat(issues).hasSize(3);

assertThat(issues.get(0).primaryLocation().startLine()).isEqualTo(1);
assertThat(issues.get(0).primaryLocation().endLine()).isEqualTo(1);
assertThat(issues.get(0).primaryLocation().startLineOffset()).isEqualTo(142);
assertThat(issues.get(0).primaryLocation().endLineOffset()).isEqualTo(157); // Should be 154

assertThat(issues.get(1).primaryLocation().startLine()).isEqualTo(1);
assertThat(issues.get(1).primaryLocation().endLine()).isEqualTo(1);
assertThat(issues.get(1).primaryLocation().startLineOffset()).isEqualTo(199);
assertThat(issues.get(1).primaryLocation().endLineOffset()).isEqualTo(207); // Should be 204

assertThat(issues.get(2).primaryLocation().startLine()).isEqualTo(1);
assertThat(issues.get(2).primaryLocation().endLine()).isEqualTo(1);
assertThat(issues.get(2).primaryLocation().startLineOffset()).isEqualTo(249);
assertThat(issues.get(2).primaryLocation().endLineOffset()).isEqualTo(249); // Should be 255
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.sonar.python.TestPythonVisitorRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.plugins.python.TestUtils.mapToColumnMappingList;
import static org.sonar.plugins.python.NotebookTestUtils.mapToColumnMappingList;

class PythonHighlighterTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.utils.Version;
import org.sonar.python.EscapeCharPositionInfo;

public final class TestUtils {

Expand Down Expand Up @@ -59,11 +56,4 @@ public static PythonInputFile createInputFile(File baseDir, String name, InputFi
.build());
}

public static List<EscapeCharPositionInfo> mapToColumnMappingList(Map<Integer, Integer> map) {
return map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(entry -> new EscapeCharPositionInfo(entry.getKey(), entry.getValue()))
.toList();
}

}

0 comments on commit efcd828

Please sign in to comment.