PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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