|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2021 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.plugins.python; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.charset.StandardCharsets; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | +import java.util.stream.Collectors; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.sonar.api.batch.fs.InputFile; |
| 33 | +import org.sonar.api.batch.fs.internal.DefaultInputFile; |
| 34 | +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; |
| 35 | +import org.sonar.api.batch.sensor.internal.SensorContextTester; |
| 36 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 37 | +import org.sonar.python.semantic.ProjectLevelSymbolTable; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | + |
| 41 | +public class PythonIndexerTest { |
| 42 | + |
| 43 | + private final File baseDir = new File("src/test/resources/org/sonar/plugins/python/indexer").getAbsoluteFile(); |
| 44 | + private SensorContextTester context; |
| 45 | + private static Path workDir; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void init() throws IOException { |
| 49 | + context = SensorContextTester.create(baseDir); |
| 50 | + workDir = Files.createTempDirectory("workDir"); |
| 51 | + context.fileSystem().setWorkDir(workDir); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void test_indexer() { |
| 56 | + PythonIndexer pythonIndexer = new PythonIndexer(); |
| 57 | + InputFile file1 = inputFile("main.py"); |
| 58 | + InputFile file2 = inputFile("mod.py"); |
| 59 | + pythonIndexer.buildOnce(context, Arrays.asList(file1, file2)); |
| 60 | + ProjectLevelSymbolTable projectLevelSymbolTable = pythonIndexer.projectLevelSymbolTable(); |
| 61 | + assertThat(projectLevelSymbolTable.getSymbolsFromModule("main")).hasSize(1); |
| 62 | + assertThat(projectLevelSymbolTable.getSymbolsFromModule("mod")).hasSize(1); |
| 63 | + Symbol modAddSymbol = projectLevelSymbolTable.getSymbol("mod.add"); |
| 64 | + assertThat(modAddSymbol).isNotNull(); |
| 65 | + assertThat(modAddSymbol.is(Symbol.Kind.FUNCTION)).isTrue(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void test_indexer_removed_file() { |
| 70 | + PythonIndexer pythonIndexer = new PythonIndexer(); |
| 71 | + InputFile file1 = inputFile("main.py"); |
| 72 | + InputFile file2 = inputFile("mod.py"); |
| 73 | + pythonIndexer.buildOnce(context, Arrays.asList(file1, file2)); |
| 74 | + ProjectLevelSymbolTable projectLevelSymbolTable = pythonIndexer.projectLevelSymbolTable(); |
| 75 | + |
| 76 | + pythonIndexer.removeFile(file2); |
| 77 | + assertThat(projectLevelSymbolTable.getSymbolsFromModule("main")).hasSize(1); |
| 78 | + assertThat(projectLevelSymbolTable.getSymbolsFromModule("mod")).isNull(); |
| 79 | + Symbol modAddSymbol = projectLevelSymbolTable.getSymbol("mod.add"); |
| 80 | + assertThat(modAddSymbol).isNull(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void test_indexer_added_file() throws IOException { |
| 85 | + PythonIndexer pythonIndexer = new PythonIndexer(); |
| 86 | + InputFile file1 = inputFile("main.py"); |
| 87 | + InputFile file2 = inputFile("mod.py"); |
| 88 | + pythonIndexer.buildOnce(context, Arrays.asList(file1, file2)); |
| 89 | + ProjectLevelSymbolTable projectLevelSymbolTable = pythonIndexer.projectLevelSymbolTable(); |
| 90 | + |
| 91 | + InputFile file3 = createInputFile("added.py"); |
| 92 | + pythonIndexer.addFile(file3); |
| 93 | + assertThat(projectLevelSymbolTable.getSymbolsFromModule("main")).hasSize(1); |
| 94 | + assertThat(projectLevelSymbolTable.getSymbolsFromModule("added")).hasSize(1); |
| 95 | + Symbol newFuncSymbol = projectLevelSymbolTable.getSymbol("added.new_func"); |
| 96 | + assertThat(newFuncSymbol).isNotNull(); |
| 97 | + assertThat(newFuncSymbol.is(Symbol.Kind.FUNCTION)).isTrue(); |
| 98 | + } |
| 99 | + |
| 100 | + private InputFile inputFile(String name) { |
| 101 | + DefaultInputFile inputFile = createInputFile(name); |
| 102 | + context.fileSystem().add(inputFile); |
| 103 | + return inputFile; |
| 104 | + } |
| 105 | + |
| 106 | + private DefaultInputFile createInputFile(String name) { |
| 107 | + return TestInputFileBuilder.create("moduleKey", name) |
| 108 | + .setModuleBaseDir(baseDir.toPath()) |
| 109 | + .setCharset(StandardCharsets.UTF_8) |
| 110 | + .setType(InputFile.Type.MAIN) |
| 111 | + .setLanguage(Python.KEY) |
| 112 | + .initMetadata(TestUtils.fileContent(new File(baseDir, name), StandardCharsets.UTF_8)) |
| 113 | + .build(); |
| 114 | + } |
| 115 | +} |
0 commit comments