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

https://bitbucket.org/sbambach/jgit-flow · Java · 297 lines · 204 code · 76 blank · 17 comment · 0 complexity · ea7f01537337ff842d94036d0a3eb4eb 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.InitContext;
  5. import com.atlassian.jgitflow.core.JGitFlow;
  6. import com.atlassian.jgitflow.core.JGitFlowInitCommand;
  7. import com.atlassian.jgitflow.core.exception.BranchExistsException;
  8. import com.atlassian.jgitflow.core.exception.BranchOutOfDateException;
  9. import com.atlassian.jgitflow.core.exception.DirtyWorkingTreeException;
  10. import com.atlassian.jgitflow.core.exception.NotInitializedException;
  11. import com.atlassian.jgitflow.core.util.GitHelper;
  12. import org.apache.commons.io.FileUtils;
  13. import org.eclipse.jgit.api.Git;
  14. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  15. import org.eclipse.jgit.lib.Constants;
  16. import org.eclipse.jgit.lib.Ref;
  17. import org.eclipse.jgit.revwalk.RevCommit;
  18. import org.junit.Test;
  19. import ut.com.atlassian.jgitflow.core.testutils.RepoUtil;
  20. import static org.junit.Assert.assertEquals;
  21. import static org.junit.Assert.assertTrue;
  22. /**
  23. * @since version
  24. */
  25. public class HotfixStartTest extends BaseGitFlowTest
  26. {
  27. @Test
  28. public void startHotfix() throws Exception
  29. {
  30. Git git = RepoUtil.createRepositoryWithMaster(newDir());
  31. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  32. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  33. flow.hotfixStart("1.0").call();
  34. assertEquals(flow.getHotfixBranchPrefix() + "1.0", git.getRepository().getBranch());
  35. }
  36. @Test
  37. public void startHotfixWithFetch() throws Exception
  38. {
  39. Git git = null;
  40. Git remoteGit = null;
  41. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  42. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  43. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  44. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  45. git.push().setRemote("origin").add("develop").call();
  46. //do a commit to the remote develop branch
  47. remoteGit.checkout().setName("master").call();
  48. File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
  49. FileUtils.writeStringToFile(junkFile, "I am junk");
  50. remoteGit.add().addFilepattern(junkFile.getName()).call();
  51. RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
  52. //update local
  53. git.checkout().setName("master").call();
  54. git.pull().call();
  55. git.checkout().setName("develop").call();
  56. flow.hotfixStart("1.0").setFetch(true).call();
  57. assertEquals(flow.getHotfixBranchPrefix() + "1.0", git.getRepository().getBranch());
  58. }
  59. @Test(expected = BranchOutOfDateException.class)
  60. public void startHotfixWithFetchLocalBehindMaster() throws Exception
  61. {
  62. Git git = null;
  63. Git remoteGit = null;
  64. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  65. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  66. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  67. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  68. git.push().setRemote("origin").add("master").call();
  69. //do a commit to the remote develop branch
  70. remoteGit.checkout().setName("master").call();
  71. File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
  72. FileUtils.writeStringToFile(junkFile, "I am junk");
  73. remoteGit.add().addFilepattern(junkFile.getName()).call();
  74. RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
  75. flow.hotfixStart("1.0").setFetch(true).call();
  76. }
  77. @Test
  78. public void startHotfixWithFetchLocalBehindMasterNoFetch() throws Exception
  79. {
  80. Git git = null;
  81. Git remoteGit = null;
  82. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  83. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  84. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  85. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  86. git.push().setRemote("origin").add("master").call();
  87. //do a commit to the remote develop branch
  88. remoteGit.checkout().setName("master").call();
  89. File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
  90. FileUtils.writeStringToFile(junkFile, "I am junk");
  91. remoteGit.add().addFilepattern(junkFile.getName()).call();
  92. RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
  93. flow.hotfixStart("1.0").call();
  94. assertEquals(flow.getHotfixBranchPrefix() + "1.0", git.getRepository().getBranch());
  95. }
  96. @Test(expected = InvalidRemoteException.class)
  97. public void startHotfixWithFetchNoRemote() throws Exception
  98. {
  99. Git git = RepoUtil.createRepositoryWithMaster(newDir());
  100. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  101. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  102. flow.hotfixStart("1.0").setFetch(true).call();
  103. }
  104. @Test(expected = NotInitializedException.class)
  105. public void startHotfixWithoutFlowInit() throws Exception
  106. {
  107. Git git = RepoUtil.createRepositoryWithMaster(newDir());
  108. JGitFlow flow = JGitFlow.get(git.getRepository().getWorkTree());
  109. flow.hotfixStart("1.0").call();
  110. }
  111. @Test(expected = DirtyWorkingTreeException.class)
  112. public void startHotfixWithUnStagedFile() throws Exception
  113. {
  114. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  115. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  116. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  117. //create a new file
  118. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  119. FileUtils.writeStringToFile(junkFile, "I am junk");
  120. flow.hotfixStart("1.0").call();
  121. }
  122. @Test(expected = DirtyWorkingTreeException.class)
  123. public void startHotfixUnCommittedFile() throws Exception
  124. {
  125. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  126. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  127. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  128. //create a new file and add it to the index
  129. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  130. FileUtils.writeStringToFile(junkFile, "I am junk");
  131. git.add().addFilepattern(junkFile.getName()).call();
  132. flow.hotfixStart("1.0").call();
  133. }
  134. @Test
  135. public void startHotfixWithNewCommit() throws Exception
  136. {
  137. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  138. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  139. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  140. //we should be on develop branch
  141. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  142. //switch to master
  143. git.checkout().setName("master").call();
  144. //create a new commit
  145. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  146. FileUtils.writeStringToFile(junkFile, "I am junk");
  147. git.add().addFilepattern(junkFile.getName()).call();
  148. RevCommit commit = git.commit().setMessage("committing junk file").call();
  149. //make sure develop has our commit
  150. assertTrue(GitHelper.isMergedInto(git, commit, flow.getMasterBranchName()));
  151. flow.hotfixStart("1.0").call();
  152. assertEquals(flow.getHotfixBranchPrefix() + "1.0", git.getRepository().getBranch());
  153. //the hotfix branch should have our commit
  154. assertTrue(GitHelper.isMergedInto(git, commit, flow.getHotfixBranchPrefix() + "1.0"));
  155. }
  156. @Test(expected = BranchExistsException.class)
  157. public void startHotfixWithExistingLocalBranch() throws Exception
  158. {
  159. Git git = null;
  160. git = RepoUtil.createRepositoryWithMaster(newDir());
  161. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  162. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  163. git.branchCreate().setName(flow.getHotfixBranchPrefix() + "1.0").call();
  164. flow.hotfixStart("1.0").call();
  165. }
  166. @Test(expected = BranchExistsException.class)
  167. public void startHotfixWithExistingLocalTag() throws Exception
  168. {
  169. Git git = null;
  170. git = RepoUtil.createRepositoryWithMaster(newDir());
  171. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  172. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  173. git.tag().setName(flow.getVersionTagPrefix() + "1.0").call();
  174. flow.hotfixStart("1.0").call();
  175. }
  176. @Test(expected = BranchExistsException.class)
  177. public void startHotfixWithExistingLocalPrefixedTag() throws Exception
  178. {
  179. Git git = null;
  180. git = RepoUtil.createRepositoryWithMaster(newDir());
  181. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  182. InitContext ctx = new InitContext();
  183. ctx.setVersiontag("vtag/");
  184. JGitFlow flow = initCommand.setInitContext(ctx).setDirectory(git.getRepository().getWorkTree()).call();
  185. git.tag().setName(flow.getVersionTagPrefix() + "1.0").call();
  186. //just to make sure
  187. List<Ref> refs = git.tagList().call();
  188. String name = refs.get(0).getName();
  189. assertEquals(Constants.R_TAGS + "vtag/1.0",name);
  190. flow.hotfixStart("1.0").call();
  191. }
  192. @Test(expected = BranchExistsException.class)
  193. public void startHotfixWithExistingRemoteTagAndFetch() throws Exception
  194. {
  195. Git git = null;
  196. Git remoteGit = null;
  197. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  198. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  199. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  200. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  201. git.push().setRemote("origin").add("develop").call();
  202. //add the remote tag
  203. remoteGit.tag().setName(flow.getVersionTagPrefix() + "1.0").call();
  204. flow.hotfixStart("1.0").setFetch(true).call();
  205. }
  206. @Test
  207. public void startHotfixWithExistingRemoteTagNoFetch() throws Exception
  208. {
  209. Git git = null;
  210. Git remoteGit = null;
  211. remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  212. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  213. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  214. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  215. git.push().setRemote("origin").add("develop").call();
  216. //add the remote tag
  217. remoteGit.tag().setName(flow.getVersionTagPrefix() + "1.0").call();
  218. flow.hotfixStart("1.0").call();
  219. assertEquals(flow.getHotfixBranchPrefix() + "1.0", git.getRepository().getBranch());
  220. }
  221. }