mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
Merge with api changes
This commit is contained in:
@@ -4,10 +4,13 @@ import com.google.common.base.Preconditions;
|
|||||||
import com.google.common.io.ByteSource;
|
import com.google.common.io.ByteSource;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
import com.google.common.io.Files;
|
import com.google.common.io.Files;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.repository.Person;
|
import sonia.scm.repository.Person;
|
||||||
import sonia.scm.repository.spi.ModifyCommand;
|
import sonia.scm.repository.spi.ModifyCommand;
|
||||||
import sonia.scm.repository.spi.ModifyCommandRequest;
|
import sonia.scm.repository.spi.ModifyCommandRequest;
|
||||||
import sonia.scm.repository.util.WorkdirProvider;
|
import sonia.scm.repository.util.WorkdirProvider;
|
||||||
|
import sonia.scm.util.IOUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -40,6 +43,8 @@ import java.util.function.Consumer;
|
|||||||
*/
|
*/
|
||||||
public class ModifyCommandBuilder {
|
public class ModifyCommandBuilder {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ModifyCommandBuilder.class);
|
||||||
|
|
||||||
private final ModifyCommand command;
|
private final ModifyCommand command;
|
||||||
private final File workdir;
|
private final File workdir;
|
||||||
|
|
||||||
@@ -109,8 +114,16 @@ public class ModifyCommandBuilder {
|
|||||||
* @return The revision of the new commit.
|
* @return The revision of the new commit.
|
||||||
*/
|
*/
|
||||||
public String execute() {
|
public String execute() {
|
||||||
Preconditions.checkArgument(request.isValid(), "commit message, author and branch are required");
|
try {
|
||||||
return command.execute(request);
|
Preconditions.checkArgument(request.isValid(), "commit message, author and branch are required");
|
||||||
|
return command.execute(request);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
IOUtil.delete(workdir);
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.warn("could not delete temporary workdir '{}'", workdir, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package sonia.scm.repository.spi;
|
package sonia.scm.repository.spi;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.Validateable;
|
import sonia.scm.Validateable;
|
||||||
import sonia.scm.repository.Person;
|
import sonia.scm.repository.Person;
|
||||||
|
import sonia.scm.util.IOUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -12,6 +15,8 @@ import java.util.List;
|
|||||||
|
|
||||||
public class ModifyCommandRequest implements Resetable, Validateable {
|
public class ModifyCommandRequest implements Resetable, Validateable {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ModifyCommandRequest.class);
|
||||||
|
|
||||||
private final List<PartialRequest> requests = new ArrayList<>();
|
private final List<PartialRequest> requests = new ArrayList<>();
|
||||||
|
|
||||||
private Person author;
|
private Person author;
|
||||||
@@ -108,7 +113,11 @@ public class ModifyCommandRequest implements Resetable, Validateable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cleanup() {
|
void cleanup() {
|
||||||
content.delete(); // TODO Handle errors
|
try {
|
||||||
|
IOUtil.delete(content);
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.warn("could not delete temporary file {}", content, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.junitpioneer.jupiter.TempDirectory;
|
import org.junitpioneer.jupiter.TempDirectory;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Captor;
|
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
@@ -43,20 +42,27 @@ class ModifyCommandBuilderTest {
|
|||||||
@Mock
|
@Mock
|
||||||
ModifyCommand.Worker worker;
|
ModifyCommand.Worker worker;
|
||||||
|
|
||||||
@Captor
|
|
||||||
ArgumentCaptor<ModifyCommandRequest> requestCaptor;
|
|
||||||
|
|
||||||
ModifyCommandBuilder commandBuilder;
|
ModifyCommandBuilder commandBuilder;
|
||||||
|
Path workdir;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void initWorkdir(@TempDirectory.TempDir Path temp) throws IOException {
|
void initWorkdir(@TempDirectory.TempDir Path temp) throws IOException {
|
||||||
lenient().when(workdirProvider.createNewWorkdir()).thenReturn(temp.toFile());
|
workdir = Files.createDirectory(temp.resolve("workdir"));
|
||||||
|
lenient().when(workdirProvider.createNewWorkdir()).thenReturn(workdir.toFile());
|
||||||
commandBuilder = new ModifyCommandBuilder(command, workdirProvider);
|
commandBuilder = new ModifyCommandBuilder(command, workdirProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void initRequestCaptor() {
|
void initRequestCaptor() {
|
||||||
when(command.execute(requestCaptor.capture())).thenReturn("target");
|
when(command.execute(any())).thenAnswer(
|
||||||
|
invocation -> {
|
||||||
|
ModifyCommandRequest request = invocation.getArgument(0);
|
||||||
|
for (ModifyCommandRequest.PartialRequest r : request.getRequests()) {
|
||||||
|
r.execute(worker);
|
||||||
|
}
|
||||||
|
return "target";
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -74,8 +80,6 @@ class ModifyCommandBuilderTest {
|
|||||||
.deleteFile("toBeDeleted")
|
.deleteFile("toBeDeleted")
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
executeRequest();
|
|
||||||
|
|
||||||
verify(worker).delete("toBeDeleted");
|
verify(worker).delete("toBeDeleted");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,8 +89,6 @@ class ModifyCommandBuilderTest {
|
|||||||
.moveFile("source", "target")
|
.moveFile("source", "target")
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
executeRequest();
|
|
||||||
|
|
||||||
verify(worker).move("source", "target");
|
verify(worker).move("source", "target");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,8 +102,6 @@ class ModifyCommandBuilderTest {
|
|||||||
.createFile("toBeCreated").withData(ByteSource.wrap("content".getBytes()))
|
.createFile("toBeCreated").withData(ByteSource.wrap("content".getBytes()))
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
executeRequest();
|
|
||||||
|
|
||||||
assertThat(nameCaptor.getValue()).isEqualTo("toBeCreated");
|
assertThat(nameCaptor.getValue()).isEqualTo("toBeCreated");
|
||||||
assertThat(contentCaptor).contains("content");
|
assertThat(contentCaptor).contains("content");
|
||||||
}
|
}
|
||||||
@@ -116,8 +116,6 @@ class ModifyCommandBuilderTest {
|
|||||||
.createFile("toBeCreated").withData(new ByteArrayInputStream("content".getBytes()))
|
.createFile("toBeCreated").withData(new ByteArrayInputStream("content".getBytes()))
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
executeRequest();
|
|
||||||
|
|
||||||
assertThat(nameCaptor.getValue()).isEqualTo("toBeCreated");
|
assertThat(nameCaptor.getValue()).isEqualTo("toBeCreated");
|
||||||
assertThat(contentCaptor).contains("content");
|
assertThat(contentCaptor).contains("content");
|
||||||
}
|
}
|
||||||
@@ -133,8 +131,6 @@ class ModifyCommandBuilderTest {
|
|||||||
.createFile("toBeCreated_2").withData(new ByteArrayInputStream("content_2".getBytes()))
|
.createFile("toBeCreated_2").withData(new ByteArrayInputStream("content_2".getBytes()))
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
executeRequest();
|
|
||||||
|
|
||||||
List<String> createdNames = nameCaptor.getAllValues();
|
List<String> createdNames = nameCaptor.getAllValues();
|
||||||
assertThat(createdNames.get(0)).isEqualTo("toBeCreated_1");
|
assertThat(createdNames.get(0)).isEqualTo("toBeCreated_1");
|
||||||
assertThat(createdNames.get(1)).isEqualTo("toBeCreated_2");
|
assertThat(createdNames.get(1)).isEqualTo("toBeCreated_2");
|
||||||
@@ -151,19 +147,10 @@ class ModifyCommandBuilderTest {
|
|||||||
.modifyFile("toBeModified").withData(ByteSource.wrap("content".getBytes()))
|
.modifyFile("toBeModified").withData(ByteSource.wrap("content".getBytes()))
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
executeRequest();
|
|
||||||
|
|
||||||
assertThat(nameCaptor.getValue()).isEqualTo("toBeModified");
|
assertThat(nameCaptor.getValue()).isEqualTo("toBeModified");
|
||||||
assertThat(contentCaptor).contains("content");
|
assertThat(contentCaptor).contains("content");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeRequest() throws IOException {
|
|
||||||
ModifyCommandRequest request = requestCaptor.getValue();
|
|
||||||
for (ModifyCommandRequest.PartialRequest r : request.getRequests()) {
|
|
||||||
r.execute(worker);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ModifyCommandBuilder initCommand() {
|
private ModifyCommandBuilder initCommand() {
|
||||||
return commandBuilder
|
return commandBuilder
|
||||||
.setBranch("branch")
|
.setBranch("branch")
|
||||||
@@ -181,8 +168,6 @@ class ModifyCommandBuilderTest {
|
|||||||
.modifyFile("toBeModified").withData(ByteSource.wrap("content".getBytes()))
|
.modifyFile("toBeModified").withData(ByteSource.wrap("content".getBytes()))
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
executeRequest();
|
|
||||||
|
|
||||||
assertThat(Files.list(temp)).isEmpty();
|
assertThat(Files.list(temp)).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,8 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
|
|||||||
request.addRequest(new ModifyCommandRequest.CreateFileRequest("new_file", newFile));
|
request.addRequest(new ModifyCommandRequest.CreateFileRequest("new_file", newFile));
|
||||||
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||||
|
|
||||||
|
command.execute(request);
|
||||||
|
|
||||||
TreeAssertions assertions = canonicalTreeParser -> assertThat(canonicalTreeParser.findFile("new_file")).isTrue();
|
TreeAssertions assertions = canonicalTreeParser -> assertThat(canonicalTreeParser.findFile("new_file")).isTrue();
|
||||||
|
|
||||||
assertInTree(assertions);
|
assertInTree(assertions);
|
||||||
|
|||||||
Reference in New Issue
Block a user