/jgit-flow-core/src/test/java/ut/com/atlassian/jgitflow/core/ReleaseFinishTest.java

https://bitbucket.org/ronsmits/jgit-flow · Java · 358 lines · 227 code · 91 blank · 40 comment · 2 complexity · 5def4127956dd359b8c89b2cbee0538a MD5 · raw file

  1. package ut.com.atlassian.jgitflow.core;
  2. import java.io.File;
  3. import java.util.List;
  4. import com.atlassian.jgitflow.core.JGitFlow;
  5. import com.atlassian.jgitflow.core.JGitFlowInitCommand;
  6. import com.atlassian.jgitflow.core.ReleaseMergeResult;
  7. import com.atlassian.jgitflow.core.exception.BranchOutOfDateException;
  8. import com.atlassian.jgitflow.core.exception.DirtyWorkingTreeException;
  9. import com.atlassian.jgitflow.core.util.GitHelper;
  10. import org.apache.commons.io.FileUtils;
  11. import org.eclipse.jgit.api.Git;
  12. import org.eclipse.jgit.api.ListBranchCommand;
  13. import org.eclipse.jgit.lib.Constants;
  14. import org.eclipse.jgit.lib.Ref;
  15. import org.eclipse.jgit.revwalk.RevCommit;
  16. import org.junit.Test;
  17. import ut.com.atlassian.jgitflow.core.testutils.RepoUtil;
  18. import static org.junit.Assert.*;
  19. /**
  20. * @since version
  21. */
  22. public class ReleaseFinishTest extends BaseGitFlowTest
  23. {
  24. @Test
  25. public void finishRelease() throws Exception
  26. {
  27. Git git = RepoUtil.createRepositoryWithMaster(newDir());
  28. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  29. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  30. flow.releaseStart("1.0").call();
  31. assertEquals(flow.getReleaseBranchPrefix() + "1.0", git.getRepository().getBranch());
  32. ReleaseMergeResult result = flow.releaseFinish("1.0").call();
  33. assertTrue(result.wasSuccessful());
  34. //we should be on develop branch
  35. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  36. //release branch should be gone
  37. Ref ref2check = git.getRepository().getRef(flow.getReleaseBranchPrefix() + "1.0");
  38. assertNull(ref2check);
  39. }
  40. @Test(expected = DirtyWorkingTreeException.class)
  41. public void finishReleaseWithUnStagedFile() throws Exception
  42. {
  43. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  44. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  45. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  46. flow.releaseStart("1.0").call();
  47. //create a new file
  48. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  49. FileUtils.writeStringToFile(junkFile, "I am junk");
  50. //try to finish
  51. ReleaseMergeResult result = flow.releaseFinish("1.0").call();
  52. assertTrue(result.wasSuccessful());
  53. }
  54. @Test(expected = DirtyWorkingTreeException.class)
  55. public void finishReleaseUnCommittedFile() throws Exception
  56. {
  57. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  58. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  59. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  60. flow.releaseStart("1.0").call();
  61. //create a new file and add it to the index
  62. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  63. FileUtils.writeStringToFile(junkFile, "I am junk");
  64. git.add().addFilepattern(junkFile.getName()).call();
  65. //try to finish
  66. ReleaseMergeResult result = flow.releaseFinish("1.0").call();
  67. assertTrue(result.wasSuccessful());
  68. }
  69. @Test
  70. public void finishReleaseWithNewCommit() throws Exception
  71. {
  72. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  73. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  74. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  75. flow.releaseStart("1.0").call();
  76. //create a new commit
  77. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  78. FileUtils.writeStringToFile(junkFile, "I am junk");
  79. git.add().addFilepattern(junkFile.getName()).call();
  80. RevCommit commit = git.commit().setMessage("committing junk file").call();
  81. //make sure develop doesn't report our commit yet
  82. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  83. //try to finish
  84. ReleaseMergeResult result = flow.releaseFinish("1.0").call();
  85. assertTrue(result.wasSuccessful());
  86. //we should be on develop branch
  87. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  88. //release branch should be gone
  89. Ref ref2check = git.getRepository().getRef(flow.getReleaseBranchPrefix() + "1.0");
  90. assertNull(ref2check);
  91. //the develop branch should have our commit
  92. assertTrue(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  93. //the master branch should have our commit
  94. assertTrue(GitHelper.isMergedInto(git, commit, flow.getMasterBranchName()));
  95. }
  96. @Test
  97. public void finishReleaseKeepBranch() throws Exception
  98. {
  99. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  100. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  101. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  102. flow.releaseStart("1.0").call();
  103. //just in case
  104. assertEquals(flow.getReleaseBranchPrefix() + "1.0", git.getRepository().getBranch());
  105. ReleaseMergeResult result = flow.releaseFinish("1.0").setKeepBranch(true).call();
  106. assertTrue(result.wasSuccessful());
  107. //we should be on develop branch
  108. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  109. //release branch should still exist
  110. Ref ref2check = git.getRepository().getRef(flow.getReleaseBranchPrefix() + "1.0");
  111. assertNotNull(ref2check);
  112. }
  113. @Test
  114. public void finishReleaseWithMultipleCommits() throws Exception
  115. {
  116. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  117. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  118. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  119. flow.releaseStart("1.0").call();
  120. //create a new commit
  121. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  122. FileUtils.writeStringToFile(junkFile, "I am junk");
  123. git.add().addFilepattern(junkFile.getName()).call();
  124. RevCommit commit = git.commit().setMessage("committing junk file").call();
  125. //create second commit
  126. File junkFile2 = new File(git.getRepository().getWorkTree(), "junk2.txt");
  127. FileUtils.writeStringToFile(junkFile2, "I am junk, and so are you");
  128. git.add().addFilepattern(junkFile2.getName()).call();
  129. RevCommit commit2 = git.commit().setMessage("updating junk file").call();
  130. //make sure develop doesn't have our commits yet
  131. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  132. assertFalse(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  133. //try to finish
  134. ReleaseMergeResult result = flow.releaseFinish("1.0").call();
  135. assertTrue(result.wasSuccessful());
  136. //we should be on develop branch
  137. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  138. //release branch should be gone
  139. Ref ref2check = git.getRepository().getRef(flow.getReleaseBranchPrefix() + "1.0");
  140. assertNull(ref2check);
  141. //the develop branch should have both of our commits now
  142. assertTrue(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  143. assertTrue(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  144. //the master branch should have both of our commits now
  145. assertTrue(GitHelper.isMergedInto(git, commit, flow.getMasterBranchName()));
  146. assertTrue(GitHelper.isMergedInto(git, commit2, flow.getMasterBranchName()));
  147. }
  148. @Test
  149. public void finishReleaseWithSquash() throws Exception
  150. {
  151. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  152. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  153. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  154. flow.releaseStart("1.0").call();
  155. //create a new commit
  156. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  157. FileUtils.writeStringToFile(junkFile, "I am junk");
  158. git.add().addFilepattern(junkFile.getName()).call();
  159. RevCommit commit = git.commit().setMessage("committing junk file").call();
  160. //create second commit
  161. File junkFile2 = new File(git.getRepository().getWorkTree(), "junk2.txt");
  162. FileUtils.writeStringToFile(junkFile2, "I am junk, and so are you");
  163. git.add().addFilepattern(junkFile2.getName()).call();
  164. RevCommit commit2 = git.commit().setMessage("updating junk file").call();
  165. //make sure develop doesn't have our commits yet
  166. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  167. assertFalse(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  168. //try to finish
  169. ReleaseMergeResult result = flow.releaseFinish("1.0").setSquash(true).call();
  170. assertTrue(result.wasSuccessful());
  171. //we should be on develop branch
  172. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  173. //release branch should be gone
  174. Ref ref2check = git.getRepository().getRef(flow.getReleaseBranchPrefix() + "1.0");
  175. assertNull(ref2check);
  176. //the develop branch should NOT have both of our commits now
  177. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  178. assertFalse(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  179. //the master branch should NOT have both of our commits now
  180. assertFalse(GitHelper.isMergedInto(git, commit, flow.getMasterBranchName()));
  181. assertFalse(GitHelper.isMergedInto(git, commit2, flow.getMasterBranchName()));
  182. //we should have the release files
  183. git.checkout().setName(flow.getDevelopBranchName()).call();
  184. File developJunk = new File(git.getRepository().getWorkTree(), "junk.txt");
  185. File developJunk2 = new File(git.getRepository().getWorkTree(), "junk2.txt");
  186. assertTrue(developJunk.exists());
  187. assertTrue(developJunk2.exists());
  188. git.checkout().setName(flow.getMasterBranchName()).call();
  189. File masterJunk = new File(git.getRepository().getWorkTree(), "junk.txt");
  190. File masterJunk2 = new File(git.getRepository().getWorkTree(), "junk2.txt");
  191. assertTrue(masterJunk.exists());
  192. assertTrue(masterJunk2.exists());
  193. }
  194. @Test(expected = BranchOutOfDateException.class)
  195. public void finishReleaseDevelopBehindRemoteWithFetch() throws Exception
  196. {
  197. Git git = null;
  198. Git remoteGit = null;
  199. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  200. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  201. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  202. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  203. flow.releaseStart("1.0").call();
  204. //do a commit to the remote develop branch
  205. remoteGit.checkout().setName(flow.getDevelopBranchName());
  206. File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
  207. FileUtils.writeStringToFile(junkFile, "I am junk");
  208. remoteGit.add().addFilepattern(junkFile.getName()).call();
  209. RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
  210. ReleaseMergeResult result = flow.releaseFinish("1.0").setFetch(true).call();
  211. assertTrue(result.wasSuccessful());
  212. }
  213. @Test(expected = BranchOutOfDateException.class)
  214. public void finishReleaseMasterBehindRemoteWithFetch() throws Exception
  215. {
  216. Git git = null;
  217. Git remoteGit = null;
  218. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  219. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  220. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  221. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  222. flow.releaseStart("1.0").call();
  223. //do a commit to the remote develop branch
  224. remoteGit.checkout().setName(flow.getMasterBranchName());
  225. File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
  226. FileUtils.writeStringToFile(junkFile, "I am junk");
  227. remoteGit.add().addFilepattern(junkFile.getName()).call();
  228. RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
  229. ReleaseMergeResult result = flow.releaseFinish("1.0").setFetch(true).call();
  230. assertTrue(result.wasSuccessful());
  231. }
  232. //TODO: add tests for push and tag flags
  233. @Test
  234. public void finishReleaseWithRemoteReleaseAndPush() throws Exception
  235. {
  236. Git git = null;
  237. Git remoteGit = null;
  238. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  239. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  240. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  241. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  242. flow.releaseStart("1.0").call();
  243. flow.git().push().setRemote("origin").call();
  244. //do a commit to the remote develop branch
  245. List<Ref> remoteBranches = remoteGit.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
  246. boolean hasRemoteRelease = false;
  247. for(Ref remoteBranch : remoteBranches)
  248. {
  249. if(remoteBranch.getName().equals(Constants.R_HEADS + flow.getReleaseBranchPrefix() + "1.0"))
  250. {
  251. hasRemoteRelease = true;
  252. break;
  253. }
  254. }
  255. assertTrue(hasRemoteRelease);
  256. File junkFile = new File(flow.git().getRepository().getWorkTree(), "junk.txt");
  257. FileUtils.writeStringToFile(junkFile, "I am junk");
  258. flow.git().add().addFilepattern(junkFile.getName()).call();
  259. RevCommit localcommit = flow.git().commit().setMessage("adding junk file").call();
  260. ReleaseMergeResult result = flow.releaseFinish("1.0").setPush(true).call();
  261. assertTrue(result.wasSuccessful());
  262. assertTrue(GitHelper.isMergedInto(remoteGit, localcommit, flow.getMasterBranchName()));
  263. assertTrue(GitHelper.isMergedInto(remoteGit, localcommit, flow.getDevelopBranchName()));
  264. assertFalse(GitHelper.remoteBranchExists(git,flow.getReleaseBranchPrefix() + "1.0"));
  265. assertFalse(GitHelper.localBranchExists(remoteGit,flow.getReleaseBranchPrefix() + "1.0"));
  266. }
  267. }