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 17KB

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