|
22 | 22 | import org.junit.Before; |
23 | 23 | import org.junit.Test; |
24 | 24 |
|
| 25 | +import com.google.common.collect.Maps; |
| 26 | + |
25 | 27 | import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Map; |
26 | 30 | import java.util.Properties; |
27 | 31 |
|
28 | 32 | import static org.fest.assertions.Assertions.assertThat; |
29 | 33 | import static org.mockito.Mockito.*; |
| 34 | +import static org.junit.Assert.assertEquals; |
30 | 35 |
|
31 | 36 | /** |
32 | 37 | * 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 { |
90 | 95 | verify(mojo, never()).putGitDescribe(any(Properties.class), any(Repository.class)); |
91 | 96 | } |
92 | 97 |
|
| 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 | + } |
93 | 133 | } |
0 commit comments