Search in sources :

Example 1 with BlameOutput

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

the class XooBlameCommandTest method testBlameWithRelativeDate.

@Test
public void testBlameWithRelativeDate() throws IOException {
    File source = new File(baseDir, "src/foo.xoo");
    FileUtils.write(source, "sample content");
    File scm = new File(baseDir, "src/foo.xoo.scm");
    FileUtils.write(scm, "123,julien,-10\n234,julien,-10");
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage(Xoo.KEY).setModuleBaseDir(baseDir.toPath()).build();
    fs.add(inputFile);
    BlameOutput result = mock(BlameOutput.class);
    when(input.filesToBlame()).thenReturn(Arrays.asList(inputFile));
    new XooBlameCommand().blame(input, result);
    Predicate<Date> datePredicate = argument -> {
        Date approximate = DateUtils.addDays(new Date(), -10);
        return argument.getTime() > approximate.getTime() - 5000 && argument.getTime() < approximate.getTime() + 5000;
    };
    ArgumentCaptor<List<BlameLine>> blameLinesCaptor = ArgumentCaptor.forClass(List.class);
    verify(result).blameResult(eq(inputFile), blameLinesCaptor.capture());
    assertThat(blameLinesCaptor.getValue()).extracting(BlameLine::date).allMatch(datePredicate);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Arrays(java.util.Arrays) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Date(java.util.Date) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BlameInput(org.sonar.api.batch.scm.BlameCommand.BlameInput) Collections.singletonList(java.util.Collections.singletonList) ArgumentCaptor(org.mockito.ArgumentCaptor) DateUtils(org.sonar.api.utils.DateUtils) Xoo(org.sonar.xoo.Xoo) Before(org.junit.Before) Predicate(java.util.function.Predicate) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) File(java.io.File) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Mockito.verify(org.mockito.Mockito.verify) BlameLine(org.sonar.api.batch.scm.BlameLine) List(java.util.List) Rule(org.junit.Rule) BlameOutput(org.sonar.api.batch.scm.BlameCommand.BlameOutput) TemporaryFolder(org.junit.rules.TemporaryFolder) Mockito.mock(org.mockito.Mockito.mock) 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) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Date(java.util.Date) Test(org.junit.Test)

Example 2 with BlameOutput

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

the class XooBlameCommandTest method blame_containing_author_with_comma.

@Test
public void blame_containing_author_with_comma() throws IOException {
    File source = new File(baseDir, "src/foo.xoo");
    FileUtils.write(source, "sample content");
    File scm = new File(baseDir, "src/foo.xoo.scm");
    FileUtils.write(scm, "\"123\",\"john,doe\",\"2019-01-22\"");
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage(Xoo.KEY).setModuleBaseDir(baseDir.toPath()).build();
    fs.add(inputFile);
    BlameOutput result = mock(BlameOutput.class);
    when(input.filesToBlame()).thenReturn(Arrays.asList(inputFile));
    new XooBlameCommand().blame(input, result);
    verify(result).blameResult(inputFile, singletonList(new BlameLine().revision("123").author("john,doe").date(DateUtils.parseDate("2019-01-22"))));
}
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) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 3 with BlameOutput

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

the class JGitBlameCommandTest method testBlame.

@Test
public void testBlame() throws IOException {
    File projectDir = temp.newFolder();
    javaUnzip("dummy-git.zip", projectDir);
    JGitBlameCommand jGitBlameCommand = newJGitBlameCommand();
    File baseDir = new File(projectDir, "dummy-git");
    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 revisionDate1 = DateUtils.parseDateTime("2012-07-17T16:12:48+0200");
    String revision1 = "6b3aab35a3ea32c1636fee56f996e677653c48ea";
    String author1 = "david@gageot.net";
    // second commit, which has a commit date different than the author date
    Date revisionDate2 = DateUtils.parseDateTime("2015-05-19T13:31:09+0200");
    String revision2 = "0d269c1acfb8e6d4d33f3c43041eb87e0df0f5e7";
    String author2 = "duarte.meneses@sonarsource.com";
    List<BlameLine> expectedBlame = new LinkedList<>();
    for (int i = 0; i < 25; i++) {
        expectedBlame.add(new BlameLine().revision(revision1).date(revisionDate1).author(author1));
    }
    for (int i = 0; i < 3; i++) {
        expectedBlame.add(new BlameLine().revision(revision2).date(revisionDate2).author(author2));
    }
    for (int i = 0; i < 1; i++) {
        expectedBlame.add(new BlameLine().revision(revision1).date(revisionDate1).author(author1));
    }
    verify(blameResult).blameResult(inputFile, expectedBlame);
}
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) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with BlameOutput

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

the class JGitBlameCommandTest method dontFailOnNewFile.

@Test
public void dontFailOnNewFile() throws IOException {
    File projectDir = temp.newFolder();
    javaUnzip("dummy-git.zip", projectDir);
    JGitBlameCommand jGitBlameCommand = newJGitBlameCommand();
    File baseDir = new File(projectDir, "dummy-git");
    DefaultFileSystem fs = new DefaultFileSystem(baseDir);
    when(input.fileSystem()).thenReturn(fs);
    String relativePath = DUMMY_JAVA;
    String relativePath2 = "src/main/java/org/dummy/Dummy2.java";
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", relativePath).build();
    fs.add(inputFile);
    DefaultInputFile inputFile2 = new TestInputFileBuilder("foo", relativePath2).build();
    fs.add(inputFile2);
    // Emulate a new file
    FileUtils.copyFile(new File(baseDir, relativePath), new File(baseDir, relativePath2));
    BlameOutput blameResult = mock(BlameOutput.class);
    when(input.filesToBlame()).thenReturn(Arrays.asList(inputFile, inputFile2));
    jGitBlameCommand.blame(input, blameResult);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) 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) Test(org.junit.Test)

Example 5 with BlameOutput

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

the class JGitBlameCommandTest method dontFailOnModifiedFile.

@Test
public void dontFailOnModifiedFile() throws IOException {
    File projectDir = temp.newFolder();
    javaUnzip("dummy-git.zip", projectDir);
    JGitBlameCommand jGitBlameCommand = newJGitBlameCommand();
    File baseDir = new File(projectDir, "dummy-git");
    DefaultFileSystem fs = new DefaultFileSystem(baseDir);
    when(input.fileSystem()).thenReturn(fs);
    String relativePath = DUMMY_JAVA;
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", relativePath).build();
    fs.add(inputFile);
    // Emulate a modification
    Files.write(baseDir.toPath().resolve(relativePath), "modification and \n some new line".getBytes());
    BlameOutput blameResult = mock(BlameOutput.class);
    when(input.filesToBlame()).thenReturn(Arrays.asList(inputFile));
    jGitBlameCommand.blame(input, blameResult);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) 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) Test(org.junit.Test)

Aggregations

File (java.io.File)18 Test (org.junit.Test)18 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)18 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)18 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)16 InputFile (org.sonar.api.batch.fs.InputFile)15 ZipFile (java.util.zip.ZipFile)8 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)8 BlameLine (org.sonar.api.batch.scm.BlameLine)7 Date (java.util.Date)5 IOException (java.io.IOException)3 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