PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sbambach/jgit-flow
Java | 237 lines | 146 code | 61 blank | 30 comment | 0 complexity | f5ed8500b891590d025c3b306f4b5207 MD5 | raw file
  1. package ut.com.atlassian.jgitflow.core;
  2. import java.io.File;
  3. import com.atlassian.jgitflow.core.JGitFlow;
  4. import com.atlassian.jgitflow.core.JGitFlowInitCommand;
  5. import com.atlassian.jgitflow.core.exception.BranchOutOfDateException;
  6. import com.atlassian.jgitflow.core.exception.DirtyWorkingTreeException;
  7. import com.atlassian.jgitflow.core.util.GitHelper;
  8. import org.apache.commons.io.FileUtils;
  9. import org.eclipse.jgit.api.Git;
  10. import org.eclipse.jgit.lib.Ref;
  11. import org.eclipse.jgit.revwalk.RevCommit;
  12. import org.junit.Test;
  13. import ut.com.atlassian.jgitflow.core.testutils.RepoUtil;
  14. import static org.junit.Assert.*;
  15. import static org.junit.Assert.assertTrue;
  16. /**
  17. * @since version
  18. */
  19. public class HotfixFinishTest extends BaseGitFlowTest
  20. {
  21. @Test
  22. public void finishHotfix() throws Exception
  23. {
  24. Git git = RepoUtil.createRepositoryWithMaster(newDir());
  25. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  26. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  27. flow.hotfixStart("1.0").call();
  28. assertEquals(flow.getHotfixBranchPrefix() + "1.0", git.getRepository().getBranch());
  29. flow.hotfixFinish("1.0").call();
  30. //we should be on develop branch
  31. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  32. //release branch should be gone
  33. Ref ref2check = git.getRepository().getRef(flow.getHotfixBranchPrefix() + "1.0");
  34. assertNull(ref2check);
  35. }
  36. @Test(expected = DirtyWorkingTreeException.class)
  37. public void finishHotfixWithUnStagedFile() throws Exception
  38. {
  39. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  40. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  41. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  42. flow.hotfixStart("1.0").call();
  43. //create a new file
  44. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  45. FileUtils.writeStringToFile(junkFile, "I am junk");
  46. //try to finish
  47. flow.hotfixFinish("1.0").call();
  48. }
  49. @Test(expected = DirtyWorkingTreeException.class)
  50. public void finishHotfixUnCommittedFile() throws Exception
  51. {
  52. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  53. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  54. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  55. flow.hotfixStart("1.0").call();
  56. //create a new file and add it to the index
  57. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  58. FileUtils.writeStringToFile(junkFile, "I am junk");
  59. git.add().addFilepattern(junkFile.getName()).call();
  60. //try to finish
  61. flow.hotfixFinish("1.0").call();
  62. }
  63. @Test
  64. public void finishHotfixWithNewCommit() throws Exception
  65. {
  66. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  67. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  68. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  69. flow.hotfixStart("1.0").call();
  70. //create a new commit
  71. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  72. FileUtils.writeStringToFile(junkFile, "I am junk");
  73. git.add().addFilepattern(junkFile.getName()).call();
  74. RevCommit commit = git.commit().setMessage("committing junk file").call();
  75. //make sure develop doesn't report our commit yet
  76. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  77. //try to finish
  78. flow.hotfixFinish("1.0").call();
  79. //we should be on develop branch
  80. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  81. //release branch should be gone
  82. Ref ref2check = git.getRepository().getRef(flow.getHotfixBranchPrefix() + "1.0");
  83. assertNull(ref2check);
  84. //the develop branch should have our commit
  85. assertTrue(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  86. //the master branch should have our commit
  87. assertTrue(GitHelper.isMergedInto(git, commit, flow.getMasterBranchName()));
  88. }
  89. @Test
  90. public void finishHotfixKeepBranch() throws Exception
  91. {
  92. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  93. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  94. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  95. flow.hotfixStart("1.0").call();
  96. //just in case
  97. assertEquals(flow.getHotfixBranchPrefix() + "1.0", git.getRepository().getBranch());
  98. flow.hotfixFinish("1.0").setKeepBranch(true).call();
  99. //we should be on develop branch
  100. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  101. //release branch should still exist
  102. Ref ref2check = git.getRepository().getRef(flow.getHotfixBranchPrefix() + "1.0");
  103. assertNotNull(ref2check);
  104. }
  105. @Test
  106. public void finishHotfixWithMultipleCommits() throws Exception
  107. {
  108. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  109. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  110. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  111. flow.hotfixStart("1.0").call();
  112. //create a new commit
  113. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  114. FileUtils.writeStringToFile(junkFile, "I am junk");
  115. git.add().addFilepattern(junkFile.getName()).call();
  116. RevCommit commit = git.commit().setMessage("committing junk file").call();
  117. //create second commit
  118. File junkFile2 = new File(git.getRepository().getWorkTree(), "junk2.txt");
  119. FileUtils.writeStringToFile(junkFile2, "I am junk, and so are you");
  120. git.add().addFilepattern(junkFile2.getName()).call();
  121. RevCommit commit2 = git.commit().setMessage("updating junk file").call();
  122. //make sure develop doesn't have our commits yet
  123. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  124. assertFalse(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  125. //try to finish
  126. flow.hotfixFinish("1.0").call();
  127. //we should be on develop branch
  128. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  129. //release branch should be gone
  130. Ref ref2check = git.getRepository().getRef(flow.getHotfixBranchPrefix() + "1.0");
  131. assertNull(ref2check);
  132. //the develop branch should have both of our commits now
  133. assertTrue(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  134. assertTrue(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  135. //the master branch should have both of our commits now
  136. assertTrue(GitHelper.isMergedInto(git, commit, flow.getMasterBranchName()));
  137. assertTrue(GitHelper.isMergedInto(git, commit2, flow.getMasterBranchName()));
  138. }
  139. @Test(expected = BranchOutOfDateException.class)
  140. public void finishHotfixDevelopBehindRemoteWithFetch() throws Exception
  141. {
  142. Git git = null;
  143. Git remoteGit = null;
  144. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  145. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  146. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  147. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  148. flow.hotfixStart("1.0").call();
  149. //do a commit to the remote develop branch
  150. remoteGit.checkout().setName(flow.getDevelopBranchName());
  151. File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
  152. FileUtils.writeStringToFile(junkFile, "I am junk");
  153. remoteGit.add().addFilepattern(junkFile.getName()).call();
  154. RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
  155. flow.hotfixFinish("1.0").setFetch(true).call();
  156. }
  157. @Test(expected = BranchOutOfDateException.class)
  158. public void finishHotfixMasterBehindRemoteWithFetch() throws Exception
  159. {
  160. Git git = null;
  161. Git remoteGit = null;
  162. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  163. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  164. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  165. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  166. flow.hotfixStart("1.0").call();
  167. //do a commit to the remote develop branch
  168. remoteGit.checkout().setName(flow.getMasterBranchName());
  169. File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
  170. FileUtils.writeStringToFile(junkFile, "I am junk");
  171. remoteGit.add().addFilepattern(junkFile.getName()).call();
  172. RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
  173. flow.hotfixFinish("1.0").setFetch(true).call();
  174. }
  175. //TODO: add tests for push and tag flags
  176. }