Tuesday, October 9, 2007

CruiseControl Subversion labelincrementor


A while back I had a post about accessing the subversion revision in cruisecontrol. The code for the plugin I posted there was incomplete (err... broken) and has been a source of confusion. I'm posting some updated code here.


Here is the plugin. Very brief instructions on how to configure CC are in the comments at the top. The tests follow.

package net.sourceforge.cruisecontrol.labelincrementers;

import org.jdom.Element;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
To properly set this up put the following in your cruise config.xml



Make sure cat-ant.jar is in your cruise lib directory and restart cruise.

*/
public class SVNLabelIncrementer extends DefaultLabelIncrementer {
private String workingCopyPath = ".";

public String incrementLabel(String oldLabel, Element buildLog) {

String SVNRev = "";
try {
SVNRev = getSvnRevision();
}
catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

String label = getNonSubversionPartOfLabel(oldLabel);
if (SVNRev == null || SVNRev.equals(""))
return oldLabel;
return label + "." + SVNRev;

}

protected String getSvnRevision() throws IOException {
String rev;
Process p = null;
try {
p = Runtime.getRuntime().exec("svnversion " + workingCopyPath);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
rev = stdInput.readLine();
}
finally {
if (p != null)
p.destroy();
}

return rev;
}

public boolean isValidLabel(String label) {
return super.isValidLabel(getNonSubversionPartOfLabel(label));
}

private String getNonSubversionPartOfLabel(String label) {
int index = label.lastIndexOf(".");
if (index == -1)
index = label.length();
return label.substring(0, index);
}

public void setWorkingCopyPath(String path) {
workingCopyPath = path;
}

public String getLocalWorkingCopy(){
return workingCopyPath;
}

public void setLocalWorkingCopy(String wc){
workingCopyPath = wc;
}
}



package net.sourceforge.cruisecontrol.labelincrementers;

import junit.framework.TestCase;
import org.jdom.Element;
import junit.textui.TestRunner;
import junit.framework.TestSuite;

public class SVNLabelIncrementerTest extends TestCase {

public void testReturnsOldLabelWhenRevNotPresent() {
SVNLabelIncrementerStub inc = new SVNLabelIncrementerStub();
assertEquals("FE6.0.1", inc.incrementLabel("FE6.0.1", new Element("foo")));
}

public void testReturnsRevisionWhenRevPresent() {
SVNLabelIncrementerStub inc = new SVNLabelIncrementerStub();
inc.setSvnRevision("3");
assertEquals("FE6.0.3", inc.incrementLabel("FE6.0.1", new Element("foo")));
}

public void testReturnsIncrementedLabelWhenRevNull() {
SVNLabelIncrementerStub inc = new SVNLabelIncrementerStub();
inc.setSvnRevision(null);
assertEquals("FE6.0.1", inc.incrementLabel("FE6.0.1", new Element("foo")));
}

public void testProducesValidLabelWhenRevNotPresent() {
SVNLabelIncrementerStub inc = new SVNLabelIncrementerStub();
String label = inc.incrementLabel("FE6.0.1", new Element("foo"));
assertTrue(inc.isValidLabel(label));
}

public void testIncrementsCorrectlyOverSeveralIterations() {
SVNLabelIncrementerStub inc = new SVNLabelIncrementerStub();
inc.setSvnRevision("1");
assertEquals("FE6.0.1", inc.incrementLabel("FE6.0.1", new Element("foo")));
assertEquals("FE6.0.1", inc.incrementLabel("FE6.0.2", new Element("foo")));
assertEquals("FE6.0.1", inc.incrementLabel("FE6.0.3", new Element("foo")));
inc.setSvnRevision("3");
assertEquals("FE6.0.3", inc.incrementLabel("FE6.0.4", new Element("foo")));
}

public static void main(String[] args){
TestRunner.run(new TestSuite(SVNLabelIncrementerTest.class));
}

public class SVNLabelIncrementerStub extends SVNLabelIncrementer {
private String revision = "";
protected String getSvnRevision(){
return revision;
}

public void setSvnRevision(String revision){
this.revision = revision;
}
}
}

No comments: