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
12 changes: 1 addition & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.33</version>
<version>4.60</version>
<relativePath/>
</parent>
<groupId>io.jenkins.plugins</groupId>
Expand Down Expand Up @@ -47,16 +47,6 @@
<artifactId>matrix-project</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
61 changes: 33 additions & 28 deletions src/test/java/io/jenkins/plugins/git_push/GitPushTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import hudson.plugins.git.extensions.impl.DisableRemotePoll;
import hudson.plugins.git.extensions.impl.UserIdentity;
import hudson.tasks.Builder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -33,39 +34,43 @@
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

/** @author Réda Housni Alaoui */
public class GitPushTest {
@WithJenkins
class GitPushTest {

private static final PersonIdent IDENTITY = new PersonIdent("John Doe", "[email protected]");

@Rule public JenkinsRule jenkins = new JenkinsRule();
@Rule public TemporaryFolder originGitRepoDir = new TemporaryFolder();
@Rule public TemporaryFolder noneJenkinsGitRepoDir = new TemporaryFolder();
private JenkinsRule jenkins;
@TempDir
private File originGitRepoDir;
@TempDir
private File noneJenkinsGitRepoDir;

private Git noneJenkinsGitRepo;
private FreeStyleProject project;

@Before
public void beforeEach() throws IOException, GitAPIException {
@BeforeEach
void beforeEach(JenkinsRule rule) throws Exception {
jenkins = rule;
Git.init()
.setBare(true)
.setDirectory(originGitRepoDir.getRoot())
.setDirectory(originGitRepoDir)
.setInitialBranch("master")
.call();

Git.cloneRepository()
.setURI(originGitRepoDir.getRoot().getAbsolutePath())
.setDirectory(noneJenkinsGitRepoDir.getRoot())
.setURI(originGitRepoDir.getAbsolutePath())
.setDirectory(noneJenkinsGitRepoDir)
.call();
Files.createFile(noneJenkinsGitRepoDir.getRoot().toPath().resolve("first.txt"));
noneJenkinsGitRepo = Git.open(noneJenkinsGitRepoDir.getRoot());
Files.createFile(noneJenkinsGitRepoDir.toPath().resolve("first.txt"));
noneJenkinsGitRepo = Git.open(noneJenkinsGitRepoDir);
noneJenkinsGitRepo.add().addFilepattern("first.txt").call();
noneJenkinsGitRepo.commit().setMessage("First commit").setCommitter(IDENTITY).call();
noneJenkinsGitRepo.push().call();
Expand All @@ -74,7 +79,7 @@ public void beforeEach() throws IOException, GitAPIException {
new GitSCM(
Collections.singletonList(
new UserRemoteConfig(
originGitRepoDir.getRoot().getAbsolutePath(), "origin", "", null)),
originGitRepoDir.getAbsolutePath(), "origin", "", null)),
Collections.singletonList(new BranchSpec("master")),
null,
null,
Expand All @@ -87,13 +92,13 @@ public void beforeEach() throws IOException, GitAPIException {
project.save();
}

@After
public void afterEach() {
@AfterEach
void afterEach() {
noneJenkinsGitRepo.close();
}

@Test
public void without_it_no_commit_is_pushed() throws Exception {
void without_it_no_commit_is_pushed() throws Exception {
project.getBuildersList().add(new CommitBuilder());
project.save();

Expand All @@ -103,7 +108,7 @@ public void without_it_no_commit_is_pushed() throws Exception {
CommitAction commitAction = build.getAction(CommitAction.class);
assertThat(commitAction).isNotNull();

try (Git origin = Git.open(originGitRepoDir.getRoot())) {
try (Git origin = Git.open(originGitRepoDir)) {
assertThatThrownBy(
() ->
origin
Expand All @@ -114,7 +119,7 @@ public void without_it_no_commit_is_pushed() throws Exception {
}

@Test
public void it_pushes_commits() throws Exception {
void it_pushes_commits() throws Exception {
project.getBuildersList().add(new CommitBuilder());
project.getPublishersList().add(createGitPush("master", "origin"));
project.save();
Expand All @@ -125,15 +130,15 @@ public void it_pushes_commits() throws Exception {
CommitAction commitAction = build.getAction(CommitAction.class);
assertThat(commitAction).isNotNull();

try (Git origin = Git.open(originGitRepoDir.getRoot())) {
try (Git origin = Git.open(originGitRepoDir)) {
RevCommit commit =
origin.getRepository().parseCommit(ObjectId.fromString(commitAction.commit.name()));
assertThat(commit.getParentCount()).isEqualTo(1);
}
}

@Test
public void it_pushes_tags() throws Exception {
void it_pushes_tags() throws Exception {
project.getBuildersList().add(new CommitBuilder());
project.getBuildersList().add(new TagBuilder());
project.getPublishersList().add(createGitPush("master", "origin"));
Expand All @@ -147,7 +152,7 @@ public void it_pushes_tags() throws Exception {
TagAction tagAction = build.getAction(TagAction.class);
assertThat(tagAction).isNotNull();

try (Git origin = Git.open(originGitRepoDir.getRoot())) {
try (Git origin = Git.open(originGitRepoDir)) {
ObjectId commitId = ObjectId.fromString(commitAction.commit.name());
List<Ref> tags = origin.getRepository().getRefDatabase().getRefsByPrefix(R_TAGS);
assertThat(tags)
Expand All @@ -165,12 +170,12 @@ public void it_pushes_tags() throws Exception {
}

@Test
public void it_create_merge_commit_if_needed() throws Exception {
void it_create_merge_commit_if_needed() throws Exception {
project
.getBuildersList()
.add(
new CommitBuilder()
.gitDir(noneJenkinsGitRepoDir.getRoot().getAbsolutePath())
.gitDir(noneJenkinsGitRepoDir.getAbsolutePath())
.push(true)
.publishCommitAction(false));
project.getBuildersList().add(new CommitBuilder());
Expand All @@ -183,7 +188,7 @@ public void it_create_merge_commit_if_needed() throws Exception {
CommitAction commitAction = build.getAction(CommitAction.class);
assertThat(commitAction).isNotNull();

try (Git origin = Git.open(originGitRepoDir.getRoot())) {
try (Git origin = Git.open(originGitRepoDir)) {
ObjectId commitId = ObjectId.fromString(commitAction.commit.name());
assertThatCode(() -> origin.getRepository().parseCommit(commitId)).doesNotThrowAnyException();

Expand Down
Loading