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.

ChangedLinesPublisherTest.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-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.scanner.report;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.io.File;
  23. import java.nio.file.Path;
  24. import java.nio.file.Paths;
  25. import java.util.Arrays;
  26. import java.util.Collections;
  27. import java.util.HashSet;
  28. import java.util.Set;
  29. import org.junit.Before;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.rules.TemporaryFolder;
  33. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  34. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  35. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  36. import org.sonar.api.batch.scm.ScmProvider;
  37. import org.sonar.api.utils.log.LogTester;
  38. import org.sonar.scanner.fs.InputModuleHierarchy;
  39. import org.sonar.scanner.protocol.output.ScannerReportReader;
  40. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  41. import org.sonar.scanner.scan.branch.BranchConfiguration;
  42. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  43. import org.sonar.scanner.scm.ScmConfiguration;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.assertj.core.api.Assumptions.assumeThat;
  46. import static org.mockito.Mockito.mock;
  47. import static org.mockito.Mockito.verifyZeroInteractions;
  48. import static org.mockito.Mockito.when;
  49. public class ChangedLinesPublisherTest {
  50. private static final String TARGET_BRANCH = "target";
  51. private static final Path BASE_DIR = Paths.get("/root");
  52. private ScmConfiguration scmConfiguration = mock(ScmConfiguration.class);
  53. private InputModuleHierarchy inputModuleHierarchy = mock(InputModuleHierarchy.class);
  54. private InputComponentStore inputComponentStore = mock(InputComponentStore.class);
  55. private BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
  56. private ScannerReportWriter writer;
  57. private ScmProvider provider = mock(ScmProvider.class);
  58. private DefaultInputProject project = mock(DefaultInputProject.class);
  59. @Rule
  60. public TemporaryFolder temp = new TemporaryFolder();
  61. @Rule
  62. public LogTester logTester = new LogTester();
  63. private ChangedLinesPublisher publisher = new ChangedLinesPublisher(scmConfiguration, project, inputComponentStore, branchConfiguration);
  64. @Before
  65. public void setUp() {
  66. writer = new ScannerReportWriter(temp.getRoot());
  67. when(branchConfiguration.isPullRequest()).thenReturn(true);
  68. when(scmConfiguration.isDisabled()).thenReturn(false);
  69. when(scmConfiguration.provider()).thenReturn(provider);
  70. when(branchConfiguration.targetBranchName()).thenReturn(TARGET_BRANCH);
  71. when(project.getBaseDir()).thenReturn(BASE_DIR);
  72. }
  73. @Test
  74. public void skip_if_scm_is_disabled() {
  75. when(scmConfiguration.isDisabled()).thenReturn(true);
  76. publisher.publish(writer);
  77. verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
  78. assertNotPublished();
  79. }
  80. @Test
  81. public void skip_if_not_pr() {
  82. when(branchConfiguration.isPullRequest()).thenReturn(false);
  83. publisher.publish(writer);
  84. verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
  85. assertNotPublished();
  86. }
  87. @Test
  88. public void skip_if_target_branch_is_null() {
  89. when(branchConfiguration.targetBranchName()).thenReturn(null);
  90. publisher.publish(writer);
  91. verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
  92. assertNotPublished();
  93. }
  94. @Test
  95. public void skip_if_no_scm_provider() {
  96. when(scmConfiguration.provider()).thenReturn(null);
  97. publisher.publish(writer);
  98. verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
  99. assertNotPublished();
  100. }
  101. @Test
  102. public void skip_if_scm_provider_returns_null() {
  103. publisher.publish(writer);
  104. assertNotPublished();
  105. }
  106. @Test
  107. public void write_changed_files() {
  108. DefaultInputFile fileWithChangedLines = createInputFile("path1", "l1\nl2\nl3\n");
  109. DefaultInputFile fileNotReturned = createInputFile("path2", "l1\nl2\nl3\n");
  110. DefaultInputFile fileWithoutChangedLines = createInputFile("path3", "l1\nl2\nl3\n");
  111. Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2"), BASE_DIR.resolve("path3")));
  112. Set<Integer> lines = new HashSet<>(Arrays.asList(1, 10));
  113. when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths))
  114. .thenReturn(ImmutableMap.of(BASE_DIR.resolve("path1"), lines, BASE_DIR.resolve("path3"), Collections.emptySet()));
  115. when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileNotReturned, fileWithoutChangedLines));
  116. publisher.publish(writer);
  117. assertPublished(fileWithChangedLines, new HashSet<>(Arrays.asList(1, 10)));
  118. assertPublished(fileWithoutChangedLines, Collections.emptySet());
  119. assertPublished(fileNotReturned, Collections.emptySet());
  120. assumeThat(logTester.logs()).contains("File '/root/path2' was detected as changed but without having changed lines");
  121. }
  122. @Test
  123. public void write_last_line_as_changed_if_all_other_lines_are_changed_and_last_line_is_empty() {
  124. DefaultInputFile fileWithChangedLines = createInputFile("path1", "l1\nl2\nl3\n");
  125. DefaultInputFile fileWithoutChangedLines = createInputFile("path2", "l1\nl2\nl3\n");
  126. Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2")));
  127. Set<Integer> lines = new HashSet<>(Arrays.asList(1, 2, 3));
  128. when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths)).thenReturn(Collections.singletonMap(BASE_DIR.resolve("path1"), lines));
  129. when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileWithoutChangedLines));
  130. publisher.publish(writer);
  131. assertPublished(fileWithChangedLines, new HashSet<>(Arrays.asList(1, 2, 3, 4)));
  132. assertPublished(fileWithoutChangedLines, Collections.emptySet());
  133. }
  134. @Test
  135. public void dont_write_last_line_as_changed_if_its_not_empty() {
  136. DefaultInputFile fileWithChangedLines = createInputFile("path1", "l1\nl2\nl3\nl4");
  137. DefaultInputFile fileWithoutChangedLines = createInputFile("path2", "l1\nl2\nl3\nl4");
  138. Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2")));
  139. Set<Integer> lines = new HashSet<>(Arrays.asList(1, 2, 3));
  140. when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths)).thenReturn(Collections.singletonMap(BASE_DIR.resolve("path1"), lines));
  141. when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileWithoutChangedLines));
  142. publisher.publish(writer);
  143. assertPublished(fileWithChangedLines, new HashSet<>(Arrays.asList(1, 2, 3)));
  144. assertPublished(fileWithoutChangedLines, Collections.emptySet());
  145. }
  146. private DefaultInputFile createInputFile(String path, String contents) {
  147. return new TestInputFileBuilder("module", path)
  148. .setContents(contents)
  149. .setProjectBaseDir(BASE_DIR)
  150. .setModuleBaseDir(BASE_DIR)
  151. .build();
  152. }
  153. private void assertPublished(DefaultInputFile file, Set<Integer> lines) {
  154. assertThat(new File(temp.getRoot(), "changed-lines-" + file.scannerId() + ".pb")).exists();
  155. ScannerReportReader reader = new ScannerReportReader(temp.getRoot());
  156. assertThat(reader.readComponentChangedLines(file.scannerId()).getLineList()).containsExactlyElementsOf(lines);
  157. }
  158. private void assertNotPublished() {
  159. assertThat(temp.getRoot().list()).isEmpty();
  160. }
  161. }