You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScmMediumTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.scanner.mediumtest.scm;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.net.URISyntaxException;
  25. import java.nio.charset.StandardCharsets;
  26. import java.util.regex.Pattern;
  27. import org.apache.commons.codec.digest.DigestUtils;
  28. import org.apache.commons.io.FileUtils;
  29. import org.assertj.core.util.Files;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.rules.ExpectedException;
  33. import org.junit.rules.TemporaryFolder;
  34. import org.sonar.api.SonarEdition;
  35. import org.sonar.api.utils.log.LogTester;
  36. import org.sonar.scanner.mediumtest.ScannerMediumTester;
  37. import org.sonar.scanner.mediumtest.ScannerMediumTester.AnalysisBuilder;
  38. import org.sonar.scanner.protocol.output.ScannerReport;
  39. import org.sonar.scanner.protocol.output.ScannerReport.Changesets.Changeset;
  40. import org.sonar.scanner.protocol.output.ScannerReport.Component;
  41. import org.sonar.scanner.protocol.output.ScannerReportReader;
  42. import org.sonar.scanner.repository.FileData;
  43. import org.sonar.xoo.XooPlugin;
  44. import org.sonar.xoo.rule.XooRulesDefinition;
  45. import static org.assertj.core.api.Assertions.assertThat;
  46. public class ScmMediumTest {
  47. private static final String MISSING_BLAME_INFORMATION_FOR_THE_FOLLOWING_FILES = "Missing blame information for the following files:";
  48. private static final String CHANGED_CONTENT_SCM_ON_SERVER_XOO = "src/changed_content_scm_on_server.xoo";
  49. private static final String NO_BLAME_SCM_ON_SERVER_XOO = "src/no_blame_scm_on_server.xoo";
  50. private static final String SAME_CONTENT_SCM_ON_SERVER_XOO = "src/same_content_scm_on_server.xoo";
  51. private static final String SAME_CONTENT_NO_SCM_ON_SERVER_XOO = "src/same_content_no_scm_on_server.xoo";
  52. private static final String SAMPLE_XOO_CONTENT = "Sample xoo\ncontent";
  53. @Rule
  54. public TemporaryFolder temp = new TemporaryFolder();
  55. @Rule
  56. public ExpectedException thrown = ExpectedException.none();
  57. @Rule
  58. public LogTester logTester = new LogTester();
  59. @Rule
  60. public ScannerMediumTester tester = new ScannerMediumTester()
  61. .setEdition(SonarEdition.SONARCLOUD)
  62. .registerPlugin("xoo", new XooPlugin())
  63. .addDefaultQProfile("xoo", "Sonar Way")
  64. .addRules(new XooRulesDefinition())
  65. // active a rule just to be sure that xoo files are published
  66. .addActiveRule("xoo", "xoo:OneIssuePerFile", null, "One Issue Per File", null, null, null)
  67. .addFileData(CHANGED_CONTENT_SCM_ON_SERVER_XOO, new FileData(DigestUtils.md5Hex(SAMPLE_XOO_CONTENT), null))
  68. .addFileData(SAME_CONTENT_NO_SCM_ON_SERVER_XOO, new FileData(DigestUtils.md5Hex(SAMPLE_XOO_CONTENT), null))
  69. .addFileData(SAME_CONTENT_SCM_ON_SERVER_XOO, new FileData(DigestUtils.md5Hex(SAMPLE_XOO_CONTENT), "1.1"))
  70. .addFileData(NO_BLAME_SCM_ON_SERVER_XOO, new FileData(DigestUtils.md5Hex(SAMPLE_XOO_CONTENT), "1.1"));
  71. @Test
  72. public void testScmMeasure() throws IOException, URISyntaxException {
  73. File baseDir = prepareProject();
  74. tester.newAnalysis()
  75. .properties(ImmutableMap.<String, String>builder()
  76. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  77. .put("sonar.projectKey", "com.foo.project")
  78. .put("sonar.sources", "src")
  79. .put("sonar.scm.provider", "xoo")
  80. .build())
  81. .execute();
  82. ScannerReport.Changesets fileScm = getChangesets(baseDir, "src/sample.xoo");
  83. assertThat(fileScm.getChangesetIndexByLineList()).hasSize(5);
  84. Changeset changesetLine1 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(0));
  85. assertThat(changesetLine1.getAuthor()).isEmpty();
  86. Changeset changesetLine2 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(1));
  87. assertThat(changesetLine2.getAuthor()).isEqualTo(getNonAsciiAuthor().toLowerCase());
  88. Changeset changesetLine3 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(2));
  89. assertThat(changesetLine3.getAuthor()).isEqualTo("julien");
  90. Changeset changesetLine4 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(3));
  91. assertThat(changesetLine4.getAuthor()).isEqualTo("julien");
  92. Changeset changesetLine5 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(4));
  93. assertThat(changesetLine5.getAuthor()).isEqualTo("simon");
  94. }
  95. private ScannerReport.Changesets getChangesets(File baseDir, String path) {
  96. File reportDir = new File(baseDir, ".sonar/scanner-report");
  97. ScannerReportReader reader = new ScannerReportReader(reportDir);
  98. Component project = reader.readComponent(reader.readMetadata().getRootComponentRef());
  99. for (Integer fileRef : project.getChildRefList()) {
  100. Component file = reader.readComponent(fileRef);
  101. if (file.getProjectRelativePath().equals(path)) {
  102. return reader.readChangesets(file.getRef());
  103. }
  104. }
  105. return null;
  106. }
  107. @Test
  108. public void noScmOnEmptyFile() throws IOException, URISyntaxException {
  109. File baseDir = prepareProject();
  110. // Clear file content
  111. FileUtils.write(new File(baseDir, "src/sample.xoo"), "");
  112. tester.newAnalysis()
  113. .properties(ImmutableMap.<String, String>builder()
  114. .put("sonar.task", "scan")
  115. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  116. .put("sonar.projectKey", "com.foo.project")
  117. .put("sonar.projectName", "Foo Project")
  118. .put("sonar.projectVersion", "1.0-SNAPSHOT")
  119. .put("sonar.projectDescription", "Description of Foo Project")
  120. .put("sonar.sources", "src")
  121. .put("sonar.scm.provider", "xoo")
  122. .build())
  123. .execute();
  124. ScannerReport.Changesets changesets = getChangesets(baseDir, "src/sample.xoo");
  125. assertThat(changesets).isNull();
  126. }
  127. @Test
  128. public void log_files_with_missing_blame() throws IOException, URISyntaxException {
  129. File baseDir = prepareProject();
  130. File xooFileWithoutBlame = new File(baseDir, "src/sample_no_blame.xoo");
  131. FileUtils.write(xooFileWithoutBlame, "Sample xoo\ncontent\n3\n4\n5", StandardCharsets.UTF_8);
  132. tester.newAnalysis()
  133. .properties(ImmutableMap.<String, String>builder()
  134. .put("sonar.task", "scan")
  135. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  136. .put("sonar.projectKey", "com.foo.project")
  137. .put("sonar.projectName", "Foo Project")
  138. .put("sonar.projectVersion", "1.0-SNAPSHOT")
  139. .put("sonar.projectDescription", "Description of Foo Project")
  140. .put("sonar.sources", "src")
  141. .put("sonar.scm.provider", "xoo")
  142. .build())
  143. .execute();
  144. ScannerReport.Changesets file1Scm = getChangesets(baseDir, "src/sample.xoo");
  145. assertThat(file1Scm).isNotNull();
  146. ScannerReport.Changesets fileWithoutBlameScm = getChangesets(baseDir, "src/sample_no_blame.xoo");
  147. assertThat(fileWithoutBlameScm).isNull();
  148. assertThat(logTester.logs()).containsSubsequence("SCM Publisher 2 source files to be analyzed", MISSING_BLAME_INFORMATION_FOR_THE_FOLLOWING_FILES,
  149. " * src/sample_no_blame.xoo");
  150. assertThat(logTester.logs().stream().anyMatch(s -> Pattern.matches("SCM Publisher 1/2 source file have been analyzed \\(done\\) \\| time=[0-9]+ms", s))).isTrue();
  151. }
  152. // SONAR-6397
  153. @Test
  154. public void optimize_blame() throws IOException, URISyntaxException {
  155. File baseDir = prepareProject();
  156. File changedContentScmOnServer = new File(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO);
  157. FileUtils.write(changedContentScmOnServer, SAMPLE_XOO_CONTENT + "\nchanged", StandardCharsets.UTF_8);
  158. File xooScmFile = new File(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO + ".scm");
  159. FileUtils.write(xooScmFile,
  160. // revision,author,dateTime
  161. "1,foo,2013-01-04\n" +
  162. "1,bar,2013-01-04\n" +
  163. "2,biz,2014-01-04\n",
  164. StandardCharsets.UTF_8);
  165. File sameContentScmOnServer = new File(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO);
  166. FileUtils.write(sameContentScmOnServer, SAMPLE_XOO_CONTENT, StandardCharsets.UTF_8);
  167. // No need to write .scm file since this file should not be blamed
  168. File noBlameScmOnServer = new File(baseDir, NO_BLAME_SCM_ON_SERVER_XOO);
  169. FileUtils.write(noBlameScmOnServer, SAMPLE_XOO_CONTENT + "\nchanged", StandardCharsets.UTF_8);
  170. // No .scm file to emulate a failure during blame
  171. File sameContentNoScmOnServer = new File(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO);
  172. FileUtils.write(sameContentNoScmOnServer, SAMPLE_XOO_CONTENT, StandardCharsets.UTF_8);
  173. xooScmFile = new File(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO + ".scm");
  174. FileUtils.write(xooScmFile,
  175. // revision,author,dateTime
  176. "1,foo,2013-01-04\n" +
  177. "1,bar,2013-01-04\n",
  178. StandardCharsets.UTF_8);
  179. tester.newAnalysis()
  180. .properties(ImmutableMap.<String, String>builder()
  181. .put("sonar.task", "scan")
  182. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  183. .put("sonar.projectKey", "com.foo.project")
  184. .put("sonar.projectName", "Foo Project")
  185. .put("sonar.projectVersion", "1.0-SNAPSHOT")
  186. .put("sonar.projectDescription", "Description of Foo Project")
  187. .put("sonar.sources", "src")
  188. .put("sonar.scm.provider", "xoo")
  189. .build())
  190. .execute();
  191. assertThat(getChangesets(baseDir, "src/sample.xoo")).isNotNull();
  192. assertThat(getChangesets(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO).getCopyFromPrevious()).isFalse();
  193. assertThat(getChangesets(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO).getCopyFromPrevious()).isTrue();
  194. assertThat(getChangesets(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO).getCopyFromPrevious()).isFalse();
  195. assertThat(getChangesets(baseDir, NO_BLAME_SCM_ON_SERVER_XOO)).isNull();
  196. // 5 .xoo files + 3 .scm files, but only 4 marked for publishing. 1 file is SAME so not included in the total
  197. assertThat(logTester.logs()).containsSubsequence("8 files indexed");
  198. assertThat(logTester.logs()).containsSubsequence("SCM Publisher 4 source files to be analyzed");
  199. assertThat(logTester.logs().stream().anyMatch(s -> Pattern.matches("SCM Publisher 3/4 source files have been analyzed \\(done\\) \\| time=[0-9]+ms", s))).isTrue();
  200. assertThat(logTester.logs()).containsSubsequence(MISSING_BLAME_INFORMATION_FOR_THE_FOLLOWING_FILES, " * src/no_blame_scm_on_server.xoo");
  201. }
  202. @Test
  203. public void forceReload() throws IOException, URISyntaxException {
  204. File baseDir = prepareProject();
  205. File xooFileNoScm = new File(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO);
  206. FileUtils.write(xooFileNoScm, SAMPLE_XOO_CONTENT, StandardCharsets.UTF_8);
  207. File xooScmFile = new File(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO + ".scm");
  208. FileUtils.write(xooScmFile,
  209. // revision,author,dateTime
  210. "1,foo,2013-01-04\n" +
  211. "1,bar,2013-01-04\n",
  212. StandardCharsets.UTF_8);
  213. AnalysisBuilder analysisBuilder = tester.newAnalysis()
  214. .properties(ImmutableMap.<String, String>builder()
  215. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  216. .put("sonar.projectKey", "com.foo.project")
  217. .put("sonar.sources", "src")
  218. .put("sonar.scm.provider", "xoo")
  219. // Force reload
  220. .put("sonar.scm.forceReloadAll", "true")
  221. .build());
  222. analysisBuilder.execute();
  223. ScannerReport.Changesets file1Scm = getChangesets(baseDir, "src/sample.xoo");
  224. assertThat(file1Scm).isNotNull();
  225. ScannerReport.Changesets file2Scm = getChangesets(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO);
  226. assertThat(file2Scm).isNotNull();
  227. }
  228. @Test
  229. public void configureUsingScmURL() throws IOException, URISyntaxException {
  230. File baseDir = prepareProject();
  231. tester.newAnalysis()
  232. .properties(ImmutableMap.<String, String>builder()
  233. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  234. .put("sonar.projectKey", "com.foo.project")
  235. .put("sonar.sources", "src")
  236. .put("sonar.links.scm_dev", "scm:xoo:foobar")
  237. .build())
  238. .execute();
  239. ScannerReport.Changesets file1Scm = getChangesets(baseDir, "src/sample.xoo");
  240. assertThat(file1Scm).isNotNull();
  241. }
  242. @Test
  243. public void testAutoDetection() throws IOException, URISyntaxException {
  244. File baseDir = prepareProject();
  245. new File(baseDir, ".xoo").createNewFile();
  246. tester.newAnalysis()
  247. .properties(ImmutableMap.<String, String>builder()
  248. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  249. .put("sonar.projectKey", "com.foo.project")
  250. .put("sonar.sources", "src")
  251. .build())
  252. .execute();
  253. ScannerReport.Changesets file1Scm = getChangesets(baseDir, "src/sample.xoo");
  254. assertThat(file1Scm).isNotNull();
  255. }
  256. private String getNonAsciiAuthor() {
  257. return Files.contentOf(new File("test-resources/mediumtest/blameAuthor.txt"), StandardCharsets.UTF_8);
  258. }
  259. private File prepareProject() throws IOException, URISyntaxException {
  260. File baseDir = temp.getRoot();
  261. File srcDir = new File(baseDir, "src");
  262. srcDir.mkdir();
  263. File xooFile1 = new File(srcDir, "sample.xoo");
  264. FileUtils.write(xooFile1, "Sample xoo\ncontent\n3\n4\n5", StandardCharsets.UTF_8);
  265. File xooScmFile1 = new File(srcDir, "sample.xoo.scm");
  266. FileUtils.write(xooScmFile1,
  267. // revision,author,dateTime
  268. "1,,2013-01-04\n" +
  269. "2," + getNonAsciiAuthor() + ",2013-01-04\n" +
  270. "3,julien,2013-02-03\n" +
  271. "3,julien,2013-02-03\n" +
  272. "4,simon,2013-03-04\n",
  273. StandardCharsets.UTF_8);
  274. return baseDir;
  275. }
  276. @Test
  277. public void testDisableScmSensor() throws IOException, URISyntaxException {
  278. File baseDir = prepareProject();
  279. tester.newAnalysis()
  280. .properties(ImmutableMap.<String, String>builder()
  281. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  282. .put("sonar.projectKey", "com.foo.project")
  283. .put("sonar.sources", "src")
  284. .put("sonar.scm.disabled", "true")
  285. .put("sonar.scm.provider", "xoo")
  286. .put("sonar.cpd.xoo.skip", "true")
  287. .build())
  288. .execute();
  289. ScannerReport.Changesets changesets = getChangesets(baseDir, "src/sample.xoo");
  290. assertThat(changesets).isNull();
  291. }
  292. }