Search in sources :

Example 6 with BlameLine

use of org.sonar.api.batch.scm.BlameLine in project sonarqube by SonarSource.

the class JGitBlameCommandTest method testBlameOnNestedModule.

@Test
public void testBlameOnNestedModule() throws IOException {
    File projectDir = temp.newFolder();
    javaUnzip("dummy-git-nested.zip", projectDir);
    JGitBlameCommand jGitBlameCommand = newJGitBlameCommand();
    File baseDir = new File(projectDir, "dummy-git-nested/dummy-project");
    DefaultFileSystem fs = new DefaultFileSystem(baseDir);
    when(input.fileSystem()).thenReturn(fs);
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", DUMMY_JAVA).setModuleBaseDir(baseDir.toPath()).build();
    fs.add(inputFile);
    BlameOutput blameResult = mock(BlameOutput.class);
    when(input.filesToBlame()).thenReturn(Arrays.asList(inputFile));
    jGitBlameCommand.blame(input, blameResult);
    Date revisionDate = DateUtils.parseDateTime("2012-07-17T16:12:48+0200");
    String revision = "6b3aab35a3ea32c1636fee56f996e677653c48ea";
    String author = "david@gageot.net";
    verify(blameResult).blameResult(inputFile, Arrays.asList(new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author), new BlameLine().revision(revision).date(revisionDate).author(author)));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) BlameLine(org.sonar.api.batch.scm.BlameLine) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) BlameOutput(org.sonar.api.batch.scm.BlameCommand.BlameOutput) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Date(java.util.Date) Test(org.junit.Test)

Example 7 with BlameLine

use of org.sonar.api.batch.scm.BlameLine in project sonarqube by SonarSource.

the class DefaultBlameOutputTest method shouldFailIfNullRevision.

@Test
public void shouldFailIfNullRevision() {
    InputFile file = new TestInputFileBuilder("foo", "src/main/java/Foo.java").setLines(1).build();
    var blameOUtput = new DefaultBlameOutput(null, analysisWarnings, singletonList(file));
    var lines = singletonList(new BlameLine().date(new Date()).author("guy"));
    assertThatThrownBy(() -> blameOUtput.blameResult(file, lines)).isInstanceOf(IllegalArgumentException.class).hasMessage("Blame revision is blank for file " + file + " at line 1");
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) BlameLine(org.sonar.api.batch.scm.BlameLine) Date(java.util.Date) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 8 with BlameLine

use of org.sonar.api.batch.scm.BlameLine in project sonarqube by SonarSource.

the class DefaultBlameOutputTest method shouldNotFailIfNotSameNumberOfLines.

@Test
public void shouldNotFailIfNotSameNumberOfLines() {
    InputFile file = new TestInputFileBuilder("foo", "src/main/java/Foo.java").setLines(10).build();
    new DefaultBlameOutput(null, analysisWarnings, singletonList(file)).blameResult(file, singletonList(new BlameLine().revision("1").author("guy")));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) BlameLine(org.sonar.api.batch.scm.BlameLine) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 9 with BlameLine

use of org.sonar.api.batch.scm.BlameLine in project sonarqube by SonarSource.

the class SvnBlameCommandTest method testParsingOfOutputWithMergeHistory.

@Test
public void testParsingOfOutputWithMergeHistory() throws Exception {
    File repoDir = unzip("repo-svn-with-merge.zip");
    String scmUrl = "file:///" + unixPath(new File(repoDir, "repo-svn"));
    File baseDir = new File(checkout(scmUrl), "dummy-svn/trunk");
    when(fs.baseDir()).thenReturn(baseDir);
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", DUMMY_JAVA).setLines(27).setModuleBaseDir(baseDir.toPath()).build();
    BlameOutput blameResult = mock(BlameOutput.class);
    when(input.filesToBlame()).thenReturn(singletonList(inputFile));
    newSvnBlameCommand().blame(input, blameResult);
    ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
    verify(blameResult).blameResult(eq(inputFile), captor.capture());
    List<BlameLine> result = captor.getValue();
    assertThat(result).hasSize(27);
    Date commitDate = new Date(1342691097393L);
    Date revision6Date = new Date(1415262184300L);
    BlameLine[] expected = IntStream.rangeClosed(1, 27).mapToObj(i -> {
        if (i == 2 || i == 24) {
            return new BlameLine().date(revision6Date).revision("6").author("henryju");
        } else {
            return new BlameLine().date(commitDate).revision("2").author("dgageot");
        }
    }).toArray(BlameLine[]::new);
    assertThat(result).containsExactly(expected);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) BlameLine(org.sonar.api.batch.scm.BlameLine) Arrays(java.util.Arrays) Enumeration(java.util.Enumeration) Date(java.util.Date) SVNStatus(org.tmatesoft.svn.core.wc.SVNStatus) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BlameInput(org.sonar.api.batch.scm.BlameCommand.BlameInput) SVNClientManager(org.tmatesoft.svn.core.wc.SVNClientManager) Collections.singletonList(java.util.Collections.singletonList) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) Mockito.doThrow(org.mockito.Mockito.doThrow) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) Parameterized(org.junit.runners.Parameterized) StandardOpenOption(java.nio.file.StandardOpenOption) FileSystem(org.sonar.api.batch.fs.FileSystem) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) BlameLine(org.sonar.api.batch.scm.BlameLine) List(java.util.List) SVNStatusClient(org.tmatesoft.svn.core.wc.SVNStatusClient) Mockito.any(org.mockito.Mockito.any) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) InputFile(org.sonar.api.batch.fs.InputFile) SVNWCUtil(org.tmatesoft.svn.core.wc.SVNWCUtil) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) SVNDepth(org.tmatesoft.svn.core.SVNDepth) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) SvnCodec(org.tmatesoft.svn.core.internal.wc2.compat.SvnCodec) SVNLogClient(org.tmatesoft.svn.core.wc.SVNLogClient) ArgumentCaptor(org.mockito.ArgumentCaptor) Before(org.junit.Before) ISVNOptions(org.tmatesoft.svn.core.wc.ISVNOptions) Files(java.nio.file.Files) SVNStatusType(org.tmatesoft.svn.core.wc.SVNStatusType) SvnCheckout(org.tmatesoft.svn.core.wc2.SvnCheckout) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) File(java.io.File) SVNAuthenticationException(org.tmatesoft.svn.core.SVNAuthenticationException) Mockito.verify(org.mockito.Mockito.verify) ISVNAuthenticationManager(org.tmatesoft.svn.core.auth.ISVNAuthenticationManager) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) SVNRevision(org.tmatesoft.svn.core.wc.SVNRevision) Paths(java.nio.file.Paths) SVNURL(org.tmatesoft.svn.core.SVNURL) SVNUpdateClient(org.tmatesoft.svn.core.wc.SVNUpdateClient) BlameOutput(org.sonar.api.batch.scm.BlameCommand.BlameOutput) SvnTarget(org.tmatesoft.svn.core.wc2.SvnTarget) TemporaryFolder(org.junit.rules.TemporaryFolder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) BlameOutput(org.sonar.api.batch.scm.BlameCommand.BlameOutput) ZipFile(java.util.zip.ZipFile) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Date(java.util.Date) Test(org.junit.Test)

Example 10 with BlameLine

use of org.sonar.api.batch.scm.BlameLine in project sonarqube by SonarSource.

the class XooBlameCommand method convertToBlameLine.

private static BlameLine convertToBlameLine(Date now, CSVRecord csvRecord) {
    checkState(csvRecord.size() >= 3, "Not enough fields on line %s", csvRecord);
    String revision = trimToNull(csvRecord.get(0));
    String author = trimToNull(csvRecord.get(1));
    BlameLine blameLine = new BlameLine().revision(revision).author(author);
    String dateStr = trimToNull(csvRecord.get(2));
    if (dateStr != null) {
        // try to load a relative number of days
        try {
            int days = Integer.parseInt(dateStr);
            blameLine.date(DateUtils.addDays(now, days));
        } catch (NumberFormatException e) {
            Date dateTime = DateUtils.parseDateTimeQuietly(dateStr);
            if (dateTime != null) {
                blameLine.date(dateTime);
            } else {
                // Will throw an exception, when date is not in format "yyyy-MM-dd"
                blameLine.date(DateUtils.parseDate(dateStr));
            }
        }
    }
    return blameLine;
}
Also used : BlameLine(org.sonar.api.batch.scm.BlameLine) Date(java.util.Date)

Aggregations

BlameLine (org.sonar.api.batch.scm.BlameLine)17 Test (org.junit.Test)12 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)12 InputFile (org.sonar.api.batch.fs.InputFile)10 File (java.io.File)9 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)9 Date (java.util.Date)7 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)7 IOException (java.io.IOException)4 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)4 Path (java.nio.file.Path)3 Arrays (java.util.Arrays)3 Collections.singletonList (java.util.Collections.singletonList)3 List (java.util.List)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Before (org.junit.Before)3 Rule (org.junit.Rule)3 TemporaryFolder (org.junit.rules.TemporaryFolder)3 ArgumentCaptor (org.mockito.ArgumentCaptor)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2