Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add git methods to Project Repository #266

Merged
merged 1 commit into from
Dec 1, 2021
Merged
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 @@ -12,4 +12,8 @@ public interface ProjectRepository {
void template(Project project, String source, String sourceFilename, String destination, String destinationFilename);

void write(Project project, String text, String destination, String destinationFilename);

void gitInit(Project project);
void gitAddAndCommit(Project project, String message);
void gitApplyPatch(Project project, String patchFilename);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -95,4 +96,31 @@ public void write(Project project, String text, String destination, String desti
throw new GeneratorException("Error when writing text to '" + projectDestinationFilename + "'");
}
}

@Override
public void gitInit(Project project) {
try {
GitUtils.init(project.getFolder());
} catch (GitAPIException e) {
throw new GeneratorException("Error when git init", e);
}
}

@Override
public void gitAddAndCommit(Project project, String message) {
try {
GitUtils.addAndCommit(project.getFolder(), message);
} catch (GitAPIException | IOException e) {
throw new GeneratorException("Error when git add and commit", e);
}
}

@Override
public void gitApplyPatch(Project project, String patchFilename) {
try {
GitUtils.apply(project.getFolder(), patchFilename);
} catch (GitAPIException | IOException e) {
throw new GeneratorException("Error when git apply patch", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
import static tech.jhipster.light.TestUtils.*;
import static tech.jhipster.light.common.domain.FileUtils.getPath;
import static tech.jhipster.light.generator.project.domain.Constants.MAIN_RESOURCES;
import static tech.jhipster.light.generator.project.domain.Constants.TEST_TEMPLATE_RESOURCES;

import com.github.mustachejava.MustacheNotFoundException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidConfigurationException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -181,4 +186,72 @@ void shouldNotWriteNullText() {
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("text");
}

@Test
void shouldGitInit() {
Project project = tmpProject();

repository.gitInit(project);

assertFileExist(project, ".git/config");
}

@Test
void shouldNotGitInit() {
Project project = tmpProject();

try (MockedStatic<GitUtils> gitUtils = Mockito.mockStatic(GitUtils.class)) {
gitUtils.when(() -> GitUtils.init(anyString())).thenThrow(new InvalidConfigurationException("error"));

assertThatThrownBy(() -> repository.gitInit(project)).isExactlyInstanceOf(GeneratorException.class);
assertFileNotExist(project, ".git/config");
}
}

@Test
void shouldInitThenAddAndCommit() throws Exception {
Project project = tmpProject();

repository.gitInit(project);
File file = File.createTempFile("hello", ".world", new File(project.getFolder()));
repository.gitAddAndCommit(project, "1st commit");

assertFileExist(project, ".git");
assertFileExist(project, file.getName());
}

@Test
void shouldNotAddAndCommit() {
Project project = tmpProject();

try (MockedStatic<GitUtils> gitUtils = Mockito.mockStatic(GitUtils.class)) {
gitUtils.when(() -> GitUtils.addAndCommit(anyString(), anyString())).thenThrow(new InvalidConfigurationException("error"));

assertThatThrownBy(() -> repository.gitAddAndCommit(project, "1st commit")).isExactlyInstanceOf(GeneratorException.class);
assertFileNotExist(project, ".git/config");
}
}

@Test
void shouldGitApplyPatch() {
Project project = tmpProject();

repository.gitInit(project);
repository.gitApplyPatch(project, getPath(TEST_TEMPLATE_RESOURCES, "utils", "example.patch"));

assertFileExist(project, "example.md");
}

@Test
void shouldNotApplyPatch() {
Project project = tmpProject();

try (MockedStatic<GitUtils> gitUtils = Mockito.mockStatic(GitUtils.class)) {
gitUtils.when(() -> GitUtils.apply(anyString(), anyString())).thenThrow(new InvalidConfigurationException("error"));

assertThatThrownBy(() -> repository.gitApplyPatch(project, getPath(TEST_TEMPLATE_RESOURCES, "utils", "example.patch")))
.isExactlyInstanceOf(GeneratorException.class);
assertFileNotExist(project, "example.md");
}
}
}