implement Modify Command

This commit is contained in:
Eduard Heimbuch
2019-10-02 10:00:57 +02:00
parent fb5617d940
commit 75346c925e
2 changed files with 83 additions and 24 deletions

View File

@@ -3,6 +3,7 @@ package sonia.scm.repository.spi;
import com.aragost.javahg.Changeset;
import com.aragost.javahg.Repository;
import com.aragost.javahg.commands.CommitCommand;
import com.aragost.javahg.commands.ExecutionException;
import com.aragost.javahg.commands.PushCommand;
import com.aragost.javahg.commands.RemoveCommand;
import org.apache.commons.lang.StringUtils;
@@ -21,6 +22,7 @@ import java.util.List;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static sonia.scm.AlreadyExistsException.alreadyExists;
import static sonia.scm.ContextEntry.ContextBuilder.entity;
import static sonia.scm.NotFoundException.notFound;
public class HgModifyCommand implements ModifyCommand {
@@ -51,6 +53,7 @@ public class HgModifyCommand implements ModifyCommand {
@Override
public void create(String toBeCreated, File file, boolean overwrite) throws IOException {
Path targetFile = new File(workingRepository.getDirectory(), toBeCreated).toPath();
createDirectories(targetFile);
if (overwrite) {
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
} else {
@@ -62,19 +65,28 @@ public class HgModifyCommand implements ModifyCommand {
}
try {
addFileToHg(targetFile.toFile());
} catch (Exception e) {
} catch (ExecutionException e) {
throwInternalRepositoryException("could not add new file to index", e);
}
}
@Override
public void modify(String path, File file) {
public void modify(String path, File file) throws IOException {
Path targetFile = new File(workingRepository.getDirectory(), path).toPath();
createDirectories(targetFile);
if (!targetFile.toFile().exists()) {
throw notFound(createFileContext(path));
}
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
try {
addFileToHg(targetFile.toFile());
} catch (ExecutionException e) {
throwInternalRepositoryException("could not modify existing file", e);
}
}
@Override
public void move(String sourcePath, String targetPath) {
}
private void createDirectories(Path targetFile) throws IOException {
@@ -97,20 +109,18 @@ public class HgModifyCommand implements ModifyCommand {
private void addFileToHg(File file) {
workingRepository.workingCopy().add(file.getAbsolutePath());
}
});
} catch (IOException e) {
e.printStackTrace(); // TODO
throwInternalRepositoryException("could not execute command on repository", e);
}
}
);
CommitCommand.on(workingRepository).user(String.format("%s <%s>", request.getAuthor().getName(), request.getAuthor().getMail())).message(request.getCommitMessage()).execute();
List<Changeset> execute = PushCommand.on(workingRepository).execute();
System.out.println(execute);
return execute.get(0).getNode();
} catch (IOException e) {
e.printStackTrace();
} catch (IOException | ExecutionException e) {
throwInternalRepositoryException("could not execute command on repository", e);
return null;
}
}

View File

@@ -7,6 +7,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import sonia.scm.AlreadyExistsException;
import sonia.scm.NotFoundException;
import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.Person;
import sonia.scm.repository.util.WorkdirProvider;
@@ -56,8 +57,7 @@ public class HgModifyCommandTest extends AbstractHgCommandTestBase {
@Test
public void shouldCreateFilesWithoutOverwrite() throws IOException {
File testFile = temporaryFolder.newFile("Answer.txt");
File testFile = temporaryFolder.newFile();
ModifyCommandRequest request = new ModifyCommandRequest();
request.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, false));
@@ -72,20 +72,11 @@ public class HgModifyCommandTest extends AbstractHgCommandTestBase {
@Test
public void shouldOverwriteExistingFiles() throws IOException {
File testFile = temporaryFolder.newFile("Answer.txt");
new FileOutputStream(testFile).write(21);
ModifyCommandRequest request = new ModifyCommandRequest();
request.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, false));
request.setCommitMessage("I found the answer");
request.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
hgModifyCommand.execute(request);
File testFile = temporaryFolder.newFile();
new FileOutputStream(testFile).write(42);
ModifyCommandRequest request2 = new ModifyCommandRequest();
request2.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, true));
request2.addRequest(new ModifyCommandRequest.CreateFileRequest("a.txt", testFile, true));
request2.setCommitMessage(" Now i really found the answer");
request2.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
@@ -93,13 +84,13 @@ public class HgModifyCommandTest extends AbstractHgCommandTestBase {
assertThat(cmdContext.open().tip().getNode()).isEqualTo(changeSet2);
assertThat(cmdContext.open().tip().getModifiedFiles().size()).isEqualTo(1);
assertThat(cmdContext.open().tip().getModifiedFiles().get(0)).isEqualTo(testFile.getName());
assertThat(cmdContext.open().tip().getModifiedFiles().get(0)).isEqualTo("a.txt");
}
@Test(expected = AlreadyExistsException.class)
public void shouldThrowFileAlreadyExistsException() throws IOException {
File testFile = temporaryFolder.newFile("Answer.txt");
File testFile = temporaryFolder.newFile();
new FileOutputStream(testFile).write(21);
ModifyCommandRequest request = new ModifyCommandRequest();
@@ -117,4 +108,62 @@ public class HgModifyCommandTest extends AbstractHgCommandTestBase {
hgModifyCommand.execute(request2);
}
@Test
public void shouldModifyExistingFile() throws IOException {
File testFile = temporaryFolder.newFile("a.txt");
new FileOutputStream(testFile).write(42);
ModifyCommandRequest request2 = new ModifyCommandRequest();
request2.addRequest(new ModifyCommandRequest.ModifyFileRequest("a.txt", testFile));
request2.setCommitMessage(" Now i really found the answer");
request2.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
String changeSet2 = hgModifyCommand.execute(request2);
assertThat(cmdContext.open().tip().getNode()).isEqualTo(changeSet2);
assertThat(cmdContext.open().tip().getModifiedFiles().size()).isEqualTo(1);
assertThat(cmdContext.open().tip().getModifiedFiles().get(0)).isEqualTo(testFile.getName());
}
@Test(expected = NotFoundException.class)
public void shouldThrowNotFoundExceptionIfFileDoesNotExist() throws IOException {
File testFile = temporaryFolder.newFile("Answer.txt");
new FileOutputStream(testFile).write(42);
ModifyCommandRequest request2 = new ModifyCommandRequest();
request2.addRequest(new ModifyCommandRequest.ModifyFileRequest("Answer.txt", testFile));
request2.setCommitMessage(" Now i really found the answer");
request2.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
hgModifyCommand.execute(request2);
}
@Test(expected = NullPointerException.class)
public void shouldThrowNPEIfAuthorIsMissing() throws IOException {
File testFile = temporaryFolder.newFile();
ModifyCommandRequest request = new ModifyCommandRequest();
request.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, false));
request.setCommitMessage("I found the answer");
hgModifyCommand.execute(request);
}
@Test(expected = NullPointerException.class)
public void shouldThrowNPEIfCommitMessageIsMissing() throws IOException {
File testFile = temporaryFolder.newFile();
ModifyCommandRequest request = new ModifyCommandRequest();
request.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, false));
request.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
hgModifyCommand.execute(request);
}
@Test(expected = IndexOutOfBoundsException.class)
public void shouldThrowIndexOutOfBoundExceptionIfRequestIsMissing() throws IOException {
ModifyCommandRequest request = new ModifyCommandRequest();
request.setCommitMessage("I found the answer");
request.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
hgModifyCommand.execute(request);
}
}