123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.scanner.report;
-
- import com.google.common.collect.ImmutableMap;
- import java.io.File;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.Set;
- import org.junit.Before;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.TemporaryFolder;
- import org.sonar.api.batch.fs.internal.DefaultInputFile;
- import org.sonar.api.batch.fs.internal.DefaultInputProject;
- import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
- import org.sonar.api.batch.scm.ScmProvider;
- import org.sonar.api.utils.log.LogTester;
- import org.sonar.scanner.fs.InputModuleHierarchy;
- import org.sonar.scanner.protocol.output.ScannerReportReader;
- import org.sonar.scanner.protocol.output.ScannerReportWriter;
- import org.sonar.scanner.scan.branch.BranchConfiguration;
- import org.sonar.scanner.scan.filesystem.InputComponentStore;
- import org.sonar.scanner.scm.ScmConfiguration;
-
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.assertj.core.api.Assumptions.assumeThat;
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.verifyZeroInteractions;
- import static org.mockito.Mockito.when;
-
- public class ChangedLinesPublisherTest {
- private static final String TARGET_BRANCH = "target";
- private static final Path BASE_DIR = Paths.get("/root");
-
- private ScmConfiguration scmConfiguration = mock(ScmConfiguration.class);
- private InputModuleHierarchy inputModuleHierarchy = mock(InputModuleHierarchy.class);
- private InputComponentStore inputComponentStore = mock(InputComponentStore.class);
- private BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
- private ScannerReportWriter writer;
- private ScmProvider provider = mock(ScmProvider.class);
- private DefaultInputProject project = mock(DefaultInputProject.class);
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Rule
- public LogTester logTester = new LogTester();
-
- private ChangedLinesPublisher publisher = new ChangedLinesPublisher(scmConfiguration, project, inputComponentStore, branchConfiguration);
-
- @Before
- public void setUp() {
- writer = new ScannerReportWriter(temp.getRoot());
- when(branchConfiguration.isPullRequest()).thenReturn(true);
- when(scmConfiguration.isDisabled()).thenReturn(false);
- when(scmConfiguration.provider()).thenReturn(provider);
- when(branchConfiguration.targetBranchName()).thenReturn(TARGET_BRANCH);
- when(project.getBaseDir()).thenReturn(BASE_DIR);
- }
-
- @Test
- public void skip_if_scm_is_disabled() {
- when(scmConfiguration.isDisabled()).thenReturn(true);
- publisher.publish(writer);
- verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
- assertNotPublished();
- }
-
- @Test
- public void skip_if_not_pr() {
- when(branchConfiguration.isPullRequest()).thenReturn(false);
- publisher.publish(writer);
- verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
- assertNotPublished();
- }
-
- @Test
- public void skip_if_target_branch_is_null() {
- when(branchConfiguration.targetBranchName()).thenReturn(null);
- publisher.publish(writer);
- verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
- assertNotPublished();
- }
-
- @Test
- public void skip_if_no_scm_provider() {
- when(scmConfiguration.provider()).thenReturn(null);
- publisher.publish(writer);
- verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
- assertNotPublished();
- }
-
- @Test
- public void skip_if_scm_provider_returns_null() {
- publisher.publish(writer);
- assertNotPublished();
- }
-
- @Test
- public void write_changed_files() {
- DefaultInputFile fileWithChangedLines = createInputFile("path1", "l1\nl2\nl3\n");
- DefaultInputFile fileNotReturned = createInputFile("path2", "l1\nl2\nl3\n");
- DefaultInputFile fileWithoutChangedLines = createInputFile("path3", "l1\nl2\nl3\n");
-
- Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2"), BASE_DIR.resolve("path3")));
- Set<Integer> lines = new HashSet<>(Arrays.asList(1, 10));
- when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths))
- .thenReturn(ImmutableMap.of(BASE_DIR.resolve("path1"), lines, BASE_DIR.resolve("path3"), Collections.emptySet()));
- when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileNotReturned, fileWithoutChangedLines));
-
- publisher.publish(writer);
-
- assertPublished(fileWithChangedLines, new HashSet<>(Arrays.asList(1, 10)));
- assertPublished(fileWithoutChangedLines, Collections.emptySet());
- assertPublished(fileNotReturned, Collections.emptySet());
-
- assumeThat(logTester.logs()).contains("File '/root/path2' was detected as changed but without having changed lines");
- }
-
- @Test
- public void write_last_line_as_changed_if_all_other_lines_are_changed_and_last_line_is_empty() {
- DefaultInputFile fileWithChangedLines = createInputFile("path1", "l1\nl2\nl3\n");
- DefaultInputFile fileWithoutChangedLines = createInputFile("path2", "l1\nl2\nl3\n");
- Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2")));
- Set<Integer> lines = new HashSet<>(Arrays.asList(1, 2, 3));
- when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths)).thenReturn(Collections.singletonMap(BASE_DIR.resolve("path1"), lines));
- when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileWithoutChangedLines));
-
- publisher.publish(writer);
-
- assertPublished(fileWithChangedLines, new HashSet<>(Arrays.asList(1, 2, 3, 4)));
- assertPublished(fileWithoutChangedLines, Collections.emptySet());
- }
-
- @Test
- public void dont_write_last_line_as_changed_if_its_not_empty() {
- DefaultInputFile fileWithChangedLines = createInputFile("path1", "l1\nl2\nl3\nl4");
- DefaultInputFile fileWithoutChangedLines = createInputFile("path2", "l1\nl2\nl3\nl4");
- Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2")));
- Set<Integer> lines = new HashSet<>(Arrays.asList(1, 2, 3));
- when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths)).thenReturn(Collections.singletonMap(BASE_DIR.resolve("path1"), lines));
- when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileWithoutChangedLines));
-
- publisher.publish(writer);
-
- assertPublished(fileWithChangedLines, new HashSet<>(Arrays.asList(1, 2, 3)));
- assertPublished(fileWithoutChangedLines, Collections.emptySet());
- }
-
- private DefaultInputFile createInputFile(String path, String contents) {
- return new TestInputFileBuilder("module", path)
- .setContents(contents)
- .setProjectBaseDir(BASE_DIR)
- .setModuleBaseDir(BASE_DIR)
- .build();
- }
-
- private void assertPublished(DefaultInputFile file, Set<Integer> lines) {
- assertThat(new File(temp.getRoot(), "changed-lines-" + file.scannerId() + ".pb")).exists();
- ScannerReportReader reader = new ScannerReportReader(temp.getRoot());
- assertThat(reader.readComponentChangedLines(file.scannerId()).getLineList()).containsExactlyElementsOf(lines);
- }
-
- private void assertNotPublished() {
- assertThat(temp.getRoot().list()).isEmpty();
- }
-
- }
|