aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scm/git/CompositeBlameCommandIT.java
blob: c14c2f6b78ee439084be391849477425f5500b56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * SonarQube
 * Copyright (C) 2009-2024 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.scm.git;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.scm.BlameCommand;
import org.sonar.api.batch.scm.BlameLine;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.System2;
import org.sonar.scm.git.strategy.DefaultBlameStrategy.BlameAlgorithmEnum;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class CompositeBlameCommandIT {

  private final AnalysisWarnings analysisWarnings = mock(AnalysisWarnings.class);

  private final BlameCommand.BlameInput input = mock(BlameCommand.BlameInput.class);
  private final JGitBlameCommand jGitBlameCommand = new JGitBlameCommand();

  private final ProcessWrapperFactory processWrapperFactory = new ProcessWrapperFactory();
  private final NativeGitBlameCommand nativeGitBlameCommand = new NativeGitBlameCommand(System2.INSTANCE, processWrapperFactory);

  @TempDir
  private File temp;

  @ParameterizedTest
  @MethodSource("namesOfTheTestRepositoriesWithBlameAlgorithm")
  void testThatBlameAlgorithmOutputsTheSameDataAsGitNativeBlame(String folder, BlameAlgorithmEnum blameAlgorithm) throws Exception {
    CompositeBlameCommand underTest = new CompositeBlameCommand(analysisWarnings, new PathResolver(), jGitBlameCommand, nativeGitBlameCommand, (p, f) -> blameAlgorithm);

    TestBlameOutput output = new TestBlameOutput();
    File gitFolder = unzipGitRepository(folder);

    setUpBlameInputWithFile(gitFolder.toPath());

    underTest.blame(input, output);

    assertBlameMatchesExpectedBlame(output.blame, gitFolder);
  }

  private static Stream<Arguments> namesOfTheTestRepositoriesWithBlameAlgorithm() {
    List<String> testCases = List.of(
      "one-file-one-commit",
      "one-file-two-commits",
      "two-files-one-commit",
      "merge-commits",
      "5lines-5commits",
      "5files-5commits",
      "two-files-moved-around-with-conflicts",
      "one-file-renamed-many-times",
      "one-file-many-merges-and-renames",
      "two-merge-commits",
      "dummy-git",
      "dummy-git-few-comitters"
      );

    List<BlameAlgorithmEnum> blameStrategies = Arrays.stream(BlameAlgorithmEnum.values()).toList();
    return testCases.stream()
      .flatMap(t -> blameStrategies.stream().map(b -> arguments(t, b)))
      .toList().stream();
  }


  private void assertBlameMatchesExpectedBlame(Map<InputFile, List<BlameLine>> blame, File gitFolder) throws Exception {
    Map<Path, List<BlameLine>> expectedBlame = readExpectedBlame(gitFolder.getName());

    assertThat(blame.entrySet())
      .as("Blamed files: " + blame.keySet() + ". Expected blamed files " + expectedBlame.keySet())
      .hasSize(expectedBlame.size());

    for (Map.Entry<InputFile, List<BlameLine>> actualBlame : blame.entrySet()) {
      Path relativeFilePath = gitFolder.toPath().relativize(actualBlame.getKey().path());
      assertThat(actualBlame.getValue())
        .as("A difference is found in file " + relativeFilePath)
        .isEqualTo(expectedBlame.get(relativeFilePath));
    }
  }

  // --- helper methods --- //

  private Map<Path, List<BlameLine>> readExpectedBlame(String expectedBlameFolder) throws Exception {
    Path expectedBlameFiles = new File(Utils.class.getResource("expected-blame/" + expectedBlameFolder).toURI()).toPath();
    Map<Path, List<BlameLine>> expectedBlame = new HashMap<>();

    List<Path> filesInExpectedBlameFolder = Files.walk(expectedBlameFiles).filter(Files::isRegularFile).toList();
    for (Path expectedFileBlamePath : filesInExpectedBlameFolder) {
      List<BlameLine> blameLines = new ArrayList<>();
      List<String> expectedBlameStrings = Files.readAllLines(expectedFileBlamePath);
      for (String line : expectedBlameStrings) {
        String revision = line.substring(0, 40);

        int beginningEmail = line.indexOf("<") + 1, endEmail = line.indexOf(">");
        String email = line.substring(beginningEmail, endEmail);

        int beginningDate = line.indexOf("2", endEmail), dateLength = 25;
        String sDate = line.substring(beginningDate, beginningDate + dateLength);
        Date parsedDate = new Date(OffsetDateTime.parse(sDate).toInstant().toEpochMilli());

        BlameLine blameLine = new BlameLine()
          .revision(revision)
          .author(email)
          .date(parsedDate);

        blameLines.add(blameLine);
      }
      expectedBlame.put(expectedBlameFiles.relativize(expectedFileBlamePath), blameLines);
    }
    return expectedBlame;
  }

  private File unzipGitRepository(String repositoryName) throws IOException {
    File gitFolderForEachTest = temp.toPath().toRealPath(LinkOption.NOFOLLOW_LINKS).toFile();
    Utils.javaUnzip(repositoryName + ".zip", gitFolderForEachTest);
    return gitFolderForEachTest.toPath().resolve(repositoryName).toFile();
  }

  private static class TestBlameOutput implements BlameCommand.BlameOutput {
    private final Map<InputFile, List<BlameLine>> blame = new ConcurrentHashMap<>();

    @Override
    public void blameResult(InputFile inputFile, List<BlameLine> list) {
      blame.put(inputFile, list);
    }
  }

  private void setUpBlameInputWithFile(Path baseDir) throws IOException {
    DefaultFileSystem fs = new DefaultFileSystem(baseDir);
    when(input.fileSystem()).thenReturn(fs);

    try (Stream<Path> stream = Files.walk(baseDir)) {
      List<InputFile> inputFiles = stream.filter(Files::isRegularFile)
        .map(f -> new TestInputFileBuilder("foo", baseDir.toFile(), f.toFile()).build())
        .filter(f -> !f.toString().startsWith(".git") && !f.toString().endsWith(".class"))
        .collect(Collectors.toList());
      when(input.filesToBlame()).thenReturn(inputFiles);
    }
  }
}