|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2012-2016 SonarSource SA |
| 4 | + * mailto:contact 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 com.sonar.python.it.plugin; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.charset.Charset; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import org.apache.commons.io.FileUtils; |
| 32 | +import org.junit.AfterClass; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.ClassRule; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.rules.TemporaryFolder; |
| 37 | +import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl; |
| 38 | +import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; |
| 39 | +import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; |
| 40 | +import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration; |
| 41 | +import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration; |
| 42 | +import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine; |
| 43 | + |
| 44 | +import static org.assertj.core.api.Assertions.assertThat; |
| 45 | +import static org.assertj.core.api.Assertions.tuple; |
| 46 | + |
| 47 | +public class SonarLintTest { |
| 48 | + |
| 49 | + @ClassRule |
| 50 | + public static TemporaryFolder temp = new TemporaryFolder(); |
| 51 | + |
| 52 | + private static StandaloneSonarLintEngine sonarlintEngine; |
| 53 | + |
| 54 | + private static File baseDir; |
| 55 | + |
| 56 | + @BeforeClass |
| 57 | + public static void prepare() throws Exception { |
| 58 | + StandaloneGlobalConfiguration sonarLintConfig = StandaloneGlobalConfiguration.builder() |
| 59 | + .addPlugin(Tests.PLUGIN_LOCATION.getFile().toURI().toURL()) |
| 60 | + .setSonarLintUserHome(temp.newFolder().toPath()) |
| 61 | + .setLogOutput((formattedMessage, level) -> { |
| 62 | + /* Don't pollute logs */ }) |
| 63 | + .build(); |
| 64 | + sonarlintEngine = new StandaloneSonarLintEngineImpl(sonarLintConfig); |
| 65 | + baseDir = temp.newFolder(); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterClass |
| 69 | + public static void stop() { |
| 70 | + sonarlintEngine.stop(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void should_raise_issues() throws IOException { |
| 75 | + ClientInputFile inputFile = prepareInputFile("foo.py", |
| 76 | + "def fooBar():\n" |
| 77 | + + " `1` \n", |
| 78 | + false); |
| 79 | + |
| 80 | + List<Issue> issues = new ArrayList<>(); |
| 81 | + sonarlintEngine.analyze( |
| 82 | + new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), new HashMap<>()), |
| 83 | + issues::add); |
| 84 | + |
| 85 | + assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path", "severity").containsOnly( |
| 86 | + tuple("python:BackticksUsage", 2, inputFile.getPath(), "MAJOR"), |
| 87 | + tuple("python:S1542", 1, inputFile.getPath(), "MAJOR")); |
| 88 | + } |
| 89 | + |
| 90 | + private ClientInputFile prepareInputFile(String relativePath, String content, final boolean isTest) throws IOException { |
| 91 | + File file = new File(baseDir, relativePath); |
| 92 | + FileUtils.write(file, content, StandardCharsets.UTF_8); |
| 93 | + return createInputFile(file.toPath(), isTest); |
| 94 | + } |
| 95 | + |
| 96 | + private ClientInputFile createInputFile(final Path path, final boolean isTest) { |
| 97 | + return new ClientInputFile() { |
| 98 | + |
| 99 | + @Override |
| 100 | + public Path getPath() { |
| 101 | + return path; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean isTest() { |
| 106 | + return isTest; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Charset getCharset() { |
| 111 | + return StandardCharsets.UTF_8; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public <G> G getClientObject() { |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + }; |
| 120 | + } |
| 121 | +} |
0 commit comments