Skip to content

Commit 0ee7e5b

Browse files
author
Rob Schoening
committed
add unit tests
1 parent 9ec8a31 commit 0ee7e5b

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,18 +530,28 @@ private boolean directoryDoesNotExits(File fileLocation) {
530530
return !directoryExists(fileLocation);
531531
}
532532

533+
/**
534+
* If running within Jenkins/Hudosn, honor the branch name passed via GIT_BRANCH env var. This
535+
* is necessary because Jenkins/Hudson alwways invoke build in a detached head state.
536+
*
537+
* @param git
538+
* @return results of git.getBranch() or, if in Jenkins/Hudson, value of GIT_BRANCH
539+
*/
533540
protected String determineBranchName(Repository git) throws IOException {
541+
Map<String,String> env = System.getenv();
542+
return determineBranchName(git,env);
543+
}
544+
545+
protected String determineBranchName(Repository git, Map<String,String> env) throws IOException {
534546
String branch = git.getBranch();
535547

536548
// Special processing if we're in Jenkins/Hudson
537-
Map<String,String> env = System.getenv();
538549
if (env.containsKey("HUDSON_URL") || env.containsKey("JENKINS_URL")) {
539550
String branchName = env.get("GIT_BRANCH");
540551
if (branchName!=null && branchName.length()>0) {
541552
branch=branchName;
542553
}
543554
}
544-
545555
return branch;
546556
}
547557

src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@
2222
import org.junit.Before;
2323
import org.junit.Test;
2424

25+
import com.google.common.collect.Maps;
26+
2527
import java.io.File;
28+
import java.io.IOException;
29+
import java.util.Map;
2630
import java.util.Properties;
2731

2832
import static org.fest.assertions.Assertions.assertThat;
2933
import static org.mockito.Mockito.*;
34+
import static org.junit.Assert.assertEquals;
3035

3136
/**
3237
* I'm not a big fan of this test - let's move to integration test from now on.
@@ -90,4 +95,39 @@ public void shouldSkipDescribeWhenConfiguredToDoSo() throws Exception {
9095
verify(mojo, never()).putGitDescribe(any(Properties.class), any(Repository.class));
9196
}
9297

98+
@Test
99+
public void shouldUseJenkinsBranchInfoWhenAvailable() throws IOException {
100+
Repository git = mock(Repository.class);
101+
Map<String,String> env = Maps.newHashMap();
102+
103+
String detachedHeadSHA1 = "16bb801934e652f5e291a003db05e364d83fba25";
104+
String ciUrl = "http://myciserver.com";
105+
106+
when(git.getBranch()).thenReturn(detachedHeadSHA1);
107+
108+
// In a detached head state, getBranch() will return the SHA1...standard behavior
109+
assertEquals(detachedHeadSHA1, mojo.determineBranchName(git, env));
110+
111+
// Again, SHA1 will be returned if we're in jenkins, but GIT_BRANCH is not set
112+
env.put("JENKINS_URL", "http://myjenkinsserver.com");
113+
assertEquals(detachedHeadSHA1, mojo.determineBranchName(git, env));
114+
115+
// Now set GIT_BRANCH too and see that the branch name from env var is returned
116+
env.clear();
117+
env.put("JENKINS_URL", ciUrl);
118+
env.put("GIT_BRANCH", "mybranch");
119+
assertEquals("mybranch", mojo.determineBranchName(git, env));
120+
121+
122+
// Same, but for hudson
123+
env.clear();
124+
env.put("GIT_BRANCH", "mybranch");
125+
env.put("HUDSON_URL", ciUrl);
126+
assertEquals("mybranch", mojo.determineBranchName(git, env));
127+
128+
// GIT_BRANCH but no HUDSON_URL or JENKINS_URL
129+
env.clear();
130+
env.put("GIT_BRANCH", "mybranch");
131+
assertEquals(detachedHeadSHA1, mojo.determineBranchName(git, env));
132+
}
93133
}

0 commit comments

Comments
 (0)