Skip to content

Commit 3fef6bc

Browse files
Avoid Persisting BaseDir file, but use its absolute path instead (#927)
1 parent 77bd4f4 commit 3fef6bc

File tree

7 files changed

+10
-11
lines changed

7 files changed

+10
-11
lines changed

python-checks-testkit/src/main/java/org/sonar/python/checks/utils/PythonCheckVerifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static MultiFileVerifier createVerifier(List<File> files, PythonCheck ch
8383
MultiFileVerifier multiFileVerifier = MultiFileVerifier.create(files.get(0).toPath(), UTF_8);
8484
for (File file : files) {
8585
PythonVisitorContext context = baseDir != null
86-
? TestPythonVisitorRunner.createContext(file, null, pythonPackageName(file, baseDir), projectLevelSymbolTable)
86+
? TestPythonVisitorRunner.createContext(file, null, pythonPackageName(file, baseDir.getAbsolutePath()), projectLevelSymbolTable)
8787
: TestPythonVisitorRunner.createContext(file);
8888
addFileIssues(check, multiFileVerifier, file, context);
8989
}

python-frontend/src/main/java/org/sonar/python/TestPythonVisitorRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static ProjectLevelSymbolTable globalSymbols(List<File> files, File baseD
7272
TestPythonFile pythonFile = new TestPythonFile(file);
7373
AstNode astNode = PythonParser.create().parse(pythonFile.content());
7474
FileInput astRoot = new PythonTreeMaker().fileInput(astNode);
75-
String packageName = pythonPackageName(file, baseDir);
75+
String packageName = pythonPackageName(file, baseDir.getAbsolutePath());
7676
projectLevelSymbolTable.addModule(astRoot, packageName, pythonFile);
7777
}
7878
return projectLevelSymbolTable;

python-frontend/src/main/java/org/sonar/python/semantic/SymbolUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ static List<Name> boundNamesFromExpression(@CheckForNull Tree tree) {
191191
return names;
192192
}
193193

194-
public static String pythonPackageName(File file, File projectBaseDir) {
194+
public static String pythonPackageName(File file, String projectBaseDirAbsolutePath) {
195195
File currentDirectory = file.getParentFile();
196196
Deque<String> packages = new ArrayDeque<>();
197-
while (!currentDirectory.getAbsolutePath().equals(projectBaseDir.getAbsolutePath())) {
197+
while (!currentDirectory.getAbsolutePath().equals(projectBaseDirAbsolutePath)) {
198198
File initFile = new File(currentDirectory, "__init__.py");
199199
if (!initFile.exists()) {
200200
break;

python-frontend/src/test/java/org/sonar/python/semantic/SymbolUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class SymbolUtilsTest {
4949

5050
@Test
5151
public void package_name_by_file() {
52-
File baseDir = new File("src/test/resources").getAbsoluteFile();
52+
String baseDir = new File("src/test/resources").getAbsoluteFile().getAbsolutePath();
5353
assertThat(pythonPackageName(new File(baseDir, "packages/sound/__init__.py"), baseDir)).isEqualTo("sound");
5454
assertThat(pythonPackageName(new File(baseDir, "packages/sound/formats/__init__.py"), baseDir)).isEqualTo("sound.formats");
5555
assertThat(pythonPackageName(new File(baseDir, "packages/sound/formats/wavread.py"), baseDir)).isEqualTo("sound.formats");

sonar-python-plugin/src/main/java/org/sonar/plugins/python/indexer/PythonIndexer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.sonar.plugins.python.indexer;
2121

2222
import com.sonar.sslr.api.AstNode;
23-
import java.io.File;
2423
import java.io.IOException;
2524
import java.net.URI;
2625
import java.util.HashMap;
@@ -43,7 +42,7 @@ public abstract class PythonIndexer {
4342

4443
private static final Logger LOG = Loggers.get(PythonIndexer.class);
4544

46-
protected File projectBaseDir;
45+
protected String projectBaseDirAbsolutePath;
4746

4847
private final Map<URI, String> packageNames = new HashMap<>();
4948
private final PythonParser parser = PythonParser.create();
@@ -54,7 +53,7 @@ public ProjectLevelSymbolTable projectLevelSymbolTable() {
5453
}
5554

5655
public String packageName(InputFile inputFile) {
57-
return packageNames.computeIfAbsent(inputFile.uri(), k -> pythonPackageName(inputFile.file(), projectBaseDir));
56+
return packageNames.computeIfAbsent(inputFile.uri(), k -> pythonPackageName(inputFile.file(), projectBaseDirAbsolutePath));
5857
}
5958

6059
void removeFile(InputFile inputFile) {
@@ -70,7 +69,7 @@ void removeFile(InputFile inputFile) {
7069
void addFile(InputFile inputFile) throws IOException {
7170
AstNode astNode = parser.parse(inputFile.contents());
7271
FileInput astRoot = new PythonTreeMaker().fileInput(astNode);
73-
String packageName = pythonPackageName(inputFile.file(), projectBaseDir);
72+
String packageName = pythonPackageName(inputFile.file(), projectBaseDirAbsolutePath);
7473
packageNames.put(inputFile.uri(), packageName);
7574
PythonFile pythonFile = SonarQubePythonFile.create(inputFile);
7675
projectLevelSymbolTable.addModule(astRoot, packageName, pythonFile);

sonar-python-plugin/src/main/java/org/sonar/plugins/python/indexer/SonarLintPythonIndexer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void buildOnce(SensorContext context) {
4949
if (!shouldBuildProjectSymbolTable) {
5050
return;
5151
}
52-
this.projectBaseDir = context.fileSystem().baseDir();
52+
this.projectBaseDirAbsolutePath = context.fileSystem().baseDir().getAbsolutePath();
5353
List<InputFile> files = getInputFiles(moduleFileSystem);
5454
LOG.debug("Input files for indexing: " + files);
5555
// computes "globalSymbolsByModuleName"

sonar-python-plugin/src/main/java/org/sonar/plugins/python/indexer/SonarQubePythonIndexer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public SonarQubePythonIndexer(List<InputFile> files) {
3636

3737
@Override
3838
public void buildOnce(SensorContext context) {
39-
this.projectBaseDir = context.fileSystem().baseDir();
39+
this.projectBaseDirAbsolutePath = context.fileSystem().baseDir().getAbsolutePath();
4040
LOG.debug("Input files for indexing: " + files);
4141
// computes "globalSymbolsByModuleName"
4242
long startTime = System.currentTimeMillis();

0 commit comments

Comments
 (0)