Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void blame(FileSystem fs, InputFile inputFile, BlameOutput output) {
LOG.debug("The mercurial blame command [" + cl.toString() + "] failed: " + stderr.getOutput());
}
List<BlameLine> lines = consumer.getLines();
if (lines.size() == inputFile.lines() - 1) {
if (!lines.isEmpty() && lines.size() == inputFile.lines() - 1) {
// SONARPLUGINS-3097 Mercurial do not report blame on last empty line
lines.add(lines.get(lines.size() - 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -40,6 +39,7 @@
import org.sonar.api.utils.command.CommandExecutor;
import org.sonar.api.utils.command.StreamConsumer;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
Expand Down Expand Up @@ -68,9 +68,7 @@ public void prepare() throws IOException {
}

@Test
public void testParsingOfOutput() throws IOException {
File source = new File(baseDir, "src/foo.xoo");
FileUtils.write(source, "sample content");
public void testParsingOfOutput() {
InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
.setLines(3)
.setModuleBaseDir(baseDir.toPath())
Expand Down Expand Up @@ -103,9 +101,7 @@ public void testParsingOfOutput() throws IOException {
}

@Test
public void testAddMissingLastLine() throws IOException {
File source = new File(baseDir, "src/foo.xoo");
FileUtils.write(source, "sample content");
public void testAddMissingLastLine() {
InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
.setLines(4)
.setModuleBaseDir(baseDir.toPath())
Expand All @@ -127,16 +123,36 @@ public void testAddMissingLastLine() throws IOException {
when(input.filesToBlame()).thenReturn(singletonList(inputFile));
new MercurialBlameCommand(commandExecutor, new MapSettings()).blame(input, result);
verify(result).blameResult(inputFile,
Arrays.asList(new BlameLine().date(DateUtils.parseDateTime("2014-11-04T11:01:10+0100")).revision("d45dafac0d9a").author("[email protected]"),
Arrays.asList(
new BlameLine().date(DateUtils.parseDateTime("2014-11-04T11:01:10+0100")).revision("d45dafac0d9a").author("[email protected]"),
new BlameLine().date(DateUtils.parseDateTime("2014-11-04T11:01:10+0100")).revision("d45dafac0d9a").author("[email protected]"),
new BlameLine().date(DateUtils.parseDateTime("2014-11-04T11:01:10+0100")).revision("d45dafac0d9a").author("[email protected]"),
new BlameLine().date(DateUtils.parseDateTime("2014-11-04T11:01:10+0100")).revision("d45dafac0d9a").author("[email protected]")));
}

@Test
public void shouldNotFailOnFileUncommitted() throws IOException {
File source = new File(baseDir, "src/foo.xoo");
FileUtils.write(source, "sample content");
public void empty_file_has_always_an_empty_last_line() {
InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
.setLines(1)
.setModuleBaseDir(baseDir.toPath())
.build();
fs.add(inputFile);

BlameOutput result = mock(BlameOutput.class);
CommandExecutor commandExecutor = mock(CommandExecutor.class);

when(commandExecutor.execute(any(), any(), any(), anyLong())).thenAnswer((Answer<Integer>) invocation -> {
// Hg doesn't blame last empty line
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the comment // SONARPLUGINS-3097 Mercurial do not report blame on last empty line already exists, is there a need for both? If so is the behaviour better described in a docstring instead of inline comment?

return 0;
});

when(input.filesToBlame()).thenReturn(singletonList(inputFile));
new MercurialBlameCommand(commandExecutor, new MapSettings()).blame(input, result);
verify(result).blameResult(inputFile, emptyList());
}

@Test
public void shouldNotFailOnFileUncommitted() {
InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
.setModuleBaseDir(baseDir.toPath())
.build();
Expand Down