PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

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