Introduce new API for modifications

New modifications includes list of 'renames'. Therefore we introduce
a new base class Modification.
This commit is contained in:
René Pfeuffer
2020-05-14 13:20:56 +02:00
parent 76354aa839
commit 35ffc5c4e2
18 changed files with 326 additions and 127 deletions

View File

@@ -39,11 +39,11 @@ import java.io.File;
import java.io.IOException;
import static java.nio.charset.Charset.defaultCharset;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
@@ -188,7 +188,9 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertTrue("removed list should be empty", modifications.getRemoved().isEmpty());
assertFalse("added list should not be empty", modifications.getAdded().isEmpty());
assertEquals(2, modifications.getAdded().size());
assertThat(modifications.getAdded(), contains("a.txt", "b.txt"));
assertThat(modifications.getAdded())
.extracting("path")
.containsExactly("a.txt", "b.txt");
}
@Test
@@ -198,14 +200,14 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
GitLogCommand command = createCommand();
Changeset c = command.getChangeset("435df2f061add3589cb3", request);
Assertions.assertThat(c.getBranches()).containsOnly("master");
assertThat(c.getBranches()).containsOnly("master");
}
@Test
public void shouldNotReturnCommitFromDifferentBranch() {
when(request.getBranch()).thenReturn("master");
Changeset changeset = createCommand().getChangeset("3f76a12f08a6ba0dc988c68b7f0b2cd190efc3c4", request);
Assertions.assertThat(changeset).isNull();
assertThat(changeset).isNull();
}
@Test

View File

@@ -40,6 +40,7 @@ import org.junit.jupiter.api.Assertions;
import sonia.scm.NoChangesMadeException;
import sonia.scm.NotFoundException;
import sonia.scm.repository.GitWorkdirFactory;
import sonia.scm.repository.Modification;
import sonia.scm.repository.Person;
import sonia.scm.repository.api.MergeCommandResult;
import sonia.scm.repository.api.MergeStrategy;
@@ -318,7 +319,7 @@ public class GitMergeCommandTest extends AbstractGitCommandTestBase {
assertThat(message).isEqualTo("squash three commits");
GitModificationsCommand modificationsCommand = new GitModificationsCommand(createContext());
List<String> changes = modificationsCommand.getModifications("master").getAdded();
List<Modification.Added> changes = modificationsCommand.getModifications("master").getAdded();
assertThat(changes.size()).isEqualTo(3);
}

View File

@@ -86,6 +86,25 @@ public class GitModificationsCommandTest extends AbstractRemoteCommandTestBase {
assertModifications.accept(outgoingModificationsCommand.getModifications(revision));
}
@Test
public void shouldReadRenamedFiles() throws Exception {
String originalFile = "a.txt";
write(outgoing, outgoingDirectory, originalFile, "bal bla");
commit(outgoing, "add file");
write(outgoing, outgoingDirectory, "b.txt", "bal bla");
File file = new File(outgoingDirectory, originalFile);
file.delete();
outgoing.rm().addFilepattern(originalFile).call();
RevCommit modifiedFileCommit = commit(outgoing, "rename file");
String revision = modifiedFileCommit.getName();
Consumer<Modifications> assertModifications = assertRenamedFiles("b.txt");
assertModifications.accept(outgoingModificationsCommand.getModifications(revision));
pushOutgoingAndPullIncoming();
assertModifications.accept(incomingModificationsCommand.getModifications(revision));
}
void pushOutgoingAndPullIncoming() throws IOException {
GitPushCommand cmd = new GitPushCommand(handler, new GitContext(outgoingDirectory, outgoingRepository, null));
PushCommandRequest request = new PushCommandRequest();
@@ -102,31 +121,62 @@ public class GitModificationsCommandTest extends AbstractRemoteCommandTestBase {
assertThat(modifications).isNotNull();
assertThat(modifications.getAdded())
.as("added files modifications")
.hasSize(0);
.asList()
.isEmpty();
assertThat(modifications.getModified())
.as("modified files modifications")
.hasSize(0);
.asList()
.isEmpty();
assertThat(modifications.getRemoved())
.as("removed files modifications")
.asList()
.hasSize(1)
.extracting("path")
.containsOnly(fileName);
};
}
Consumer<Modifications> assertRenamedFiles(String fileName) {
return (modifications) -> {
assertThat(modifications).isNotNull();
assertThat(modifications.getAdded())
.as("added files modifications")
.asList()
.isEmpty();
assertThat(modifications.getModified())
.as("modified files modifications")
.asList()
.isEmpty();
assertThat(modifications.getRemoved())
.as("removed files modifications")
.asList()
.isEmpty();
assertThat(modifications.getRenamed())
.as("renamed files modifications")
.asList()
.hasSize(1)
.extracting("newPath")
.containsOnly(fileName);
};
}
Consumer<Modifications> assertModifiedFiles(String file) {
return (modifications) -> {
assertThat(modifications).isNotNull();
assertThat(modifications.getAdded())
.as("added files modifications")
.hasSize(0);
.asList()
.isEmpty();
assertThat(modifications.getModified())
.as("modified files modifications")
.asList()
.extracting("path")
.hasSize(1)
.containsOnly(file);
assertThat(modifications.getRemoved())
.as("removed files modifications")
.hasSize(0);
.asList()
.isEmpty();
};
}
@@ -135,14 +185,18 @@ public class GitModificationsCommandTest extends AbstractRemoteCommandTestBase {
assertThat(modifications).isNotNull();
assertThat(modifications.getAdded())
.as("added files modifications")
.asList()
.hasSize(1)
.extracting("path")
.containsOnly(file);
assertThat(modifications.getModified())
.as("modified files modifications")
.hasSize(0);
.asList()
.isEmpty();
assertThat(modifications.getRemoved())
.as("removed files modifications")
.hasSize(0);
.asList()
.isEmpty();
};
}
}