Skip to content
Permalink
Browse files

Added test for JENKINS-25618

  • Loading branch information...
MadsNielsen committed Nov 24, 2014
1 parent 22965d9 commit f7ce71420e2f434ac3545663552b9be45c51af1c
12 pom.xml
@@ -79,6 +79,18 @@

<build>
<plugins>
<!--
Use java 1.7. We need this for our testing environment
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

<!--
Allows integration tests to clean test environment before build fails
use 'mvn verify' to run integration tests to allow 'post-integration-test' phase
@@ -0,0 +1,107 @@
/*
* 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 hudson.model.AbstractBuild;
import hudson.model.Result;
import java.util.Iterator;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;

/**
*
* @author Mads
*/
public class BuildResultValidator implements AutoCloseable {

public AbstractBuild<?,?> build;
public Repository repo;
public Result result;
public String[] contents;


public BuildResultValidator(AbstractBuild<?,?> build, Repository repo) {
this.repo = repo;
this.build = build;
}

public BuildResultValidator hasResult(Result result) {
this.result = result;
return this;
}

public BuildResultValidator hasHeadCommitContents(String... contents) {
this.contents = contents;
return this;
}


public void validate() throws Exception {
if(result != null) {
validateResult();
}

if(contents != null) {
validateHeadCommitMessage();
}
}



private void validateResult() throws ValidationException {
boolean res = build.getResult().equals(result);
if(!res) {
throw new ValidationException(String.format("The results did not match, expected result is %s, actual result is %s", result, build.getResult()));
}
}

private boolean validateHeadCommitMessage() throws ValidationException,Exception {
Git git = new Git(repo);
RevWalk walk = new RevWalk(repo);

Iterable<RevCommit> logs = git.log().call();
Iterator<RevCommit> i = logs.iterator();

ObjectId head = repo.resolve("HEAD");


RevCommit commit = null;
while(i.hasNext()) {
commit = walk.parseCommit(i.next());
if(commit.equals(head)) {
boolean match = true;
boolean matched = false;
for(String s : contents) {
matched = true;
System.out.println(commit.getFullMessage());
match &= commit.getFullMessage().contains(s);
}

if(!(matched && match)) {
throw new ValidationException("The head commit did not match any of the strings specified");
} else {
return true;
}

}
}
throw new ValidationException("No head commit found");
}

@Override
public void close() throws Exception {
TestUtilsFactory.destroyRepo(repo);
}

public class ValidationException extends Exception {
public ValidationException(String message) {
super(message);
}
}
}
@@ -5,6 +5,7 @@
*/
package org.jenkinsci.plugins.pretestedintegration.integration.scm.git;

import hudson.model.AbstractBuild;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
@@ -314,5 +315,24 @@ public void testOriginAndEmptyInDualConfig() throws Exception {
assertTrue(build.getResult().equals(Result.NOT_BUILT));
}
}
}
}

@Bug(25618)
@Test
public void validateSquashCommitMessageContents() throws Exception {
Repository repository1 = TestUtilsFactory.createValidRepository("test-repo1");
FreeStyleProject project = TestUtilsFactory.configurePretestedIntegrationPlugin(jenkinsRule, TestUtilsFactory.STRATEGY_TYPE.SQUASH, repository1);

TestUtilsFactory.triggerProject(project);

jenkinsRule.waitUntilNoActivityUpTo(60000);

AbstractBuild<?,?> build = project.getFirstBuild();
//Squashed commit of the following
try(BuildResultValidator brv = new BuildResultValidator(build, repository1).hasResult(Result.SUCCESS)
.hasHeadCommitContents("Squashed commit of the following", "feature 1 commit 1-ready/feature_1-test-repo1")) {
brv.validate();
}

}
}

0 comments on commit f7ce714

Please sign in to comment.
You can’t perform that action at this time.