Skip to content

Commit

Permalink
Revised tests for JENKINS-24754
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsNielsen committed Sep 18, 2014
1 parent a3b2dd6 commit b8ea4fb
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 22 deletions.
Expand Up @@ -49,12 +49,12 @@ public void integrate(AbstractBuild<?,?> build, Launcher launcher, BuildListener

ByteArrayOutputStream out = new ByteArrayOutputStream();
BuildData gitBuildData = build.getAction(BuildData.class);
Branch gitDataBranch = gitBuildData.lastBuild.revision.getBranches().iterator().next();
Branch gitDataBranch = gitBuildData.lastBuild.revision.getBranches().iterator().next();
boolean found = false;

try {
client = Git.with(listener, build.getEnvironment(listener)).in(build.getWorkspace()).getClient();
for(Branch b : client.getRemoteBranches()) {
for(Branch b : client.getRemoteBranches()) {
if(b.getName().equals(gitDataBranch.getName())) {
found = true;
break;
Expand Down
Expand Up @@ -162,7 +162,7 @@ public Commit<String> nextCommit( AbstractBuild<?, ?> build, Launcher launcher,
Commit<String> next = null;
try {
BuildData gitBuildData = build.getAction(BuildData.class);
Branch gitDataBranch = gitBuildData.lastBuild.revision.getBranches().iterator().next();
Branch gitDataBranch = gitBuildData.lastBuild.revision.getBranches().iterator().next();
next = new Commit<String>(gitDataBranch.getSHA1String());
} catch (Exception e) {
logger.finest("Failed to find next commit");
Expand Down
Expand Up @@ -14,7 +14,6 @@
import hudson.plugins.git.extensions.impl.CleanCheckout;
import hudson.plugins.git.extensions.impl.PruneStaleBranch;
import hudson.triggers.SCMTrigger;
import junit.framework.TestCase;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.CreateBranchCommand;
Expand All @@ -27,8 +26,6 @@
import org.jenkinsci.plugins.pretestedintegration.PretestedIntegrationPostCheckout;
import org.jenkinsci.plugins.pretestedintegration.scm.git.AccumulatedCommitStrategy;
import org.jenkinsci.plugins.pretestedintegration.scm.git.GitBridge;
import org.jenkinsci.plugins.pretestedintegration.scm.git.SquashCommitStrategy;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand Down Expand Up @@ -207,23 +204,21 @@ private int countCommits() {

return commitCount;
}

private FreeStyleProject configurePretestedIntegrationPlugin() throws IOException, ANTLRException, InterruptedException {
private FreeStyleProject configurePretestedIntegrationPlugin(List<UserRemoteConfig> repos) throws IOException, ANTLRException, InterruptedException {
FreeStyleProject project = jenkinsRule.createFreeStyleProject();

GitBridge gitBridge = new GitBridge(new AccumulatedCommitStrategy(), "master");

project.getBuildWrappersList().add(new PretestedIntegrationBuildWrapper(gitBridge));
project.getPublishersList().add(new PretestedIntegrationPostCheckout());

List<UserRemoteConfig> repoList = new ArrayList<UserRemoteConfig>();
repoList.add(new UserRemoteConfig("file://" + GIT_DIR.getAbsolutePath(), null, null, null));


List<GitSCMExtension> gitSCMExtensions = new ArrayList<GitSCMExtension>();
gitSCMExtensions.add(new PruneStaleBranch());
gitSCMExtensions.add(new CleanCheckout());

GitSCM gitSCM = new GitSCM(repoList,

GitSCM gitSCM = new GitSCM(repos,
Collections.singletonList(new BranchSpec("origin/ready/**")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null, gitSCMExtensions);
Expand All @@ -241,6 +236,10 @@ private FreeStyleProject configurePretestedIntegrationPlugin() throws IOExceptio
return project;
}

private FreeStyleProject configurePretestedIntegrationPlugin() throws IOException, ANTLRException, InterruptedException {
return configurePretestedIntegrationPlugin(Collections.singletonList(new UserRemoteConfig("file://" + GIT_DIR.getAbsolutePath(), null, null, null)));
}

@Test
public void canMergeAFeatureBranchUsingAccumulatedStrategy() throws IOException, ANTLRException, InterruptedException, GitAPIException {
createValidRepository();
Expand All @@ -249,7 +248,7 @@ public void canMergeAFeatureBranchUsingAccumulatedStrategy() throws IOException,
final int COMMIT_COUNT_ON_FEATURE_BEFORE_EXECUTION = countCommits();
git.checkout().setName("master").call();

FreeStyleProject project = configurePretestedIntegrationPlugin();
FreeStyleProject project = FreeStyleProjectFactory.configurePretestedIntegrationPlugin(jenkinsRule.createFreeStyleProject(), FreeStyleProjectFactory.STRATEGY_TYPE.ACCUMULATED);

assertEquals(1, jenkinsRule.jenkins.getQueue().getItems().length);

Expand Down Expand Up @@ -278,7 +277,7 @@ public void canMergeAFeatureBranchUsingAccumulatedStrategy() throws IOException,
public void ShouldFailWithAMergeConflictPresent() throws Exception {
createRepositoryWithMergeConflict();

FreeStyleProject project = configurePretestedIntegrationPlugin();
FreeStyleProject project = FreeStyleProjectFactory.configurePretestedIntegrationPlugin(jenkinsRule.createFreeStyleProject(), FreeStyleProjectFactory.STRATEGY_TYPE.ACCUMULATED);

assertEquals(1, jenkinsRule.jenkins.getQueue().getItems().length);

Expand Down
@@ -0,0 +1,73 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.jenkinsci.plugins.pretestedintegration.integration.scm.git;

import antlr.ANTLRException;
import hudson.model.FreeStyleProject;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.SubmoduleConfig;
import hudson.plugins.git.UserRemoteConfig;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.impl.CleanCheckout;
import hudson.plugins.git.extensions.impl.PruneStaleBranch;
import hudson.triggers.SCMTrigger;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jenkinsci.plugins.pretestedintegration.PretestedIntegrationBuildWrapper;
import org.jenkinsci.plugins.pretestedintegration.PretestedIntegrationPostCheckout;
import org.jenkinsci.plugins.pretestedintegration.scm.git.AccumulatedCommitStrategy;
import org.jenkinsci.plugins.pretestedintegration.scm.git.GitBridge;
import org.jenkinsci.plugins.pretestedintegration.scm.git.SquashCommitStrategy;

/**
*
* @author Mads
*/
public class FreeStyleProjectFactory {
public enum STRATEGY_TYPE { SQUASH, ACCUMULATED };
public static final File GIT_DIR = new File("test-repo/.git");

public static FreeStyleProject configurePretestedIntegrationPlugin(FreeStyleProject project, FreeStyleProjectFactory.STRATEGY_TYPE type) throws IOException, ANTLRException, InterruptedException {
return configurePretestedIntegrationPlugin(project, type, Collections.singletonList(new UserRemoteConfig("file://" + GIT_DIR.getAbsolutePath(), null, null, null)));
}

public static FreeStyleProject configurePretestedIntegrationPlugin(FreeStyleProject project, FreeStyleProjectFactory.STRATEGY_TYPE type, List<UserRemoteConfig> repoList) throws IOException, ANTLRException, InterruptedException {
GitBridge gitBridge = null;
if(type == STRATEGY_TYPE.SQUASH) {
gitBridge = new GitBridge(new SquashCommitStrategy(), "master");
} else {
gitBridge = new GitBridge(new AccumulatedCommitStrategy(), "master");
}

project.getBuildWrappersList().add(new PretestedIntegrationBuildWrapper(gitBridge));
project.getPublishersList().add(new PretestedIntegrationPostCheckout());

List<GitSCMExtension> gitSCMExtensions = new ArrayList<GitSCMExtension>();
gitSCMExtensions.add(new PruneStaleBranch());
gitSCMExtensions.add(new CleanCheckout());

GitSCM gitSCM = new GitSCM(Collections.singletonList(new UserRemoteConfig("file://" + GIT_DIR.getAbsolutePath(), null, null, null)),
Collections.singletonList(new BranchSpec("origin/ready/**")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null, gitSCMExtensions);

project.setScm(gitSCM);

SCMTrigger scmTrigger = new SCMTrigger("@daily", true);
project.addTrigger(scmTrigger);

scmTrigger.start(project, true);
scmTrigger.new Runner().run();

Thread.sleep(1000);

return project;
}
}
Expand Up @@ -195,18 +195,19 @@ private void createRepositoryWithMergeConflict() throws IOException, GitAPIExcep
if (GIT_PARENT_DIR.exists())
FileUtils.deleteDirectory(GIT_PARENT_DIR);
}

private FreeStyleProject configurePretestedIntegrationPlugin() throws IOException, ANTLRException, InterruptedException {
return configurePretestedIntegrationPlugin(Collections.singletonList(new UserRemoteConfig("file://" + GIT_DIR.getAbsolutePath(), null, null, null)));
}

private FreeStyleProject configurePretestedIntegrationPlugin(List<UserRemoteConfig> repoList) throws IOException, ANTLRException, InterruptedException {
FreeStyleProject project = jenkinsRule.createFreeStyleProject();

GitBridge gitBridge = new GitBridge(new SquashCommitStrategy(), "master");

project.getBuildWrappersList().add(new PretestedIntegrationBuildWrapper(gitBridge));
project.getPublishersList().add(new PretestedIntegrationPostCheckout());

List<UserRemoteConfig> repoList = new ArrayList<UserRemoteConfig>();
repoList.add(new UserRemoteConfig("file://" + GIT_DIR.getAbsolutePath(), null, null, null));

List<GitSCMExtension> gitSCMExtensions = new ArrayList<GitSCMExtension>();
gitSCMExtensions.add(new PruneStaleBranch());
gitSCMExtensions.add(new CleanCheckout());
Expand Down Expand Up @@ -254,8 +255,7 @@ public void canSquashMergeAFeatureBranch() throws IOException, ANTLRException, I

final int COMMIT_COUNT_BEFORE_EXECUTION = countCommits();

FreeStyleProject project = configurePretestedIntegrationPlugin();

FreeStyleProject project = FreeStyleProjectFactory.configurePretestedIntegrationPlugin(jenkinsRule.createFreeStyleProject(), FreeStyleProjectFactory.STRATEGY_TYPE.SQUASH);
assertEquals(1, jenkinsRule.jenkins.getQueue().getItems().length);

QueueTaskFuture<Queue.Executable> future = jenkinsRule.jenkins.getQueue().getItems()[0].getFuture();
Expand Down Expand Up @@ -290,7 +290,7 @@ public void ShouldFailWithAMergeConflictPresent() throws Exception {

final int COMMIT_COUNT_BEFORE_EXECUTION = countCommits();

FreeStyleProject project = configurePretestedIntegrationPlugin();
FreeStyleProject project = FreeStyleProjectFactory.configurePretestedIntegrationPlugin(jenkinsRule.createFreeStyleProject(), FreeStyleProjectFactory.STRATEGY_TYPE.SQUASH);

assertEquals(1, jenkinsRule.jenkins.getQueue().getItems().length);

Expand Down

0 comments on commit b8ea4fb

Please sign in to comment.