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

https://bitbucket.org/ronsmits/jgit-flow · Java · 357 lines · 233 code · 80 blank · 44 comment · 1 complexity · 122320b517abd9f0162f83fd6a2fdfb7 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.JGitFlowConstants;
  5. import com.atlassian.jgitflow.core.JGitFlowInitCommand;
  6. import com.atlassian.jgitflow.core.exception.BranchOutOfDateException;
  7. import com.atlassian.jgitflow.core.exception.DirtyWorkingTreeException;
  8. import com.atlassian.jgitflow.core.exception.MergeConflictsNotResolvedException;
  9. import com.atlassian.jgitflow.core.util.FileHelper;
  10. import com.atlassian.jgitflow.core.util.GitHelper;
  11. import org.apache.commons.io.FileUtils;
  12. import org.eclipse.jgit.api.Git;
  13. import org.eclipse.jgit.lib.Ref;
  14. import org.eclipse.jgit.revwalk.RevCommit;
  15. import org.junit.Test;
  16. import ut.com.atlassian.jgitflow.core.testutils.RepoUtil;
  17. import static org.junit.Assert.*;
  18. /**
  19. * @since version
  20. */
  21. public class FeatureFinishTest extends BaseGitFlowTest
  22. {
  23. @Test
  24. public void finishFeature() throws Exception
  25. {
  26. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  27. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  28. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  29. flow.featureStart("my-feature").call();
  30. //just in case
  31. assertEquals(flow.getFeatureBranchPrefix() + "my-feature", git.getRepository().getBranch());
  32. flow.featureFinish("my-feature").call();
  33. //we should be on develop branch
  34. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  35. //feature branch should be gone
  36. Ref ref2check = git.getRepository().getRef(flow.getFeatureBranchPrefix() + "my-feature");
  37. assertNull(ref2check);
  38. }
  39. @Test(expected = DirtyWorkingTreeException.class)
  40. public void finishFeatureWithUnStagedFile() throws Exception
  41. {
  42. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  43. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  44. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  45. flow.featureStart("my-feature").call();
  46. //create a new file
  47. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  48. FileUtils.writeStringToFile(junkFile, "I am junk");
  49. //try to finish
  50. flow.featureFinish("my-feature").call();
  51. }
  52. @Test(expected = DirtyWorkingTreeException.class)
  53. public void finishFeatureUnCommittedFile() throws Exception
  54. {
  55. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  56. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  57. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  58. flow.featureStart("my-feature").call();
  59. //create a new file and add it to the index
  60. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  61. FileUtils.writeStringToFile(junkFile, "I am junk");
  62. git.add().addFilepattern(junkFile.getName()).call();
  63. //try to finish
  64. flow.featureFinish("my-feature").call();
  65. }
  66. @Test
  67. public void finishFeatureWithNewCommit() throws Exception
  68. {
  69. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  70. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  71. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  72. flow.featureStart("my-feature").call();
  73. //create a new commit
  74. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  75. FileUtils.writeStringToFile(junkFile, "I am junk");
  76. git.add().addFilepattern(junkFile.getName()).call();
  77. RevCommit commit = git.commit().setMessage("committing junk file").call();
  78. //make sure develop doesn't report our commit yet
  79. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  80. //try to finish
  81. flow.featureFinish("my-feature").call();
  82. //we should be on develop branch
  83. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  84. //feature branch should be gone
  85. Ref ref2check = git.getRepository().getRef(flow.getFeatureBranchPrefix() + "my-feature");
  86. assertNull(ref2check);
  87. //the develop branch should have our commit
  88. assertTrue(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  89. }
  90. @Test
  91. public void finishFeatureKeepBranch() throws Exception
  92. {
  93. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  94. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  95. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  96. flow.featureStart("my-feature").call();
  97. //just in case
  98. assertEquals(flow.getFeatureBranchPrefix() + "my-feature", git.getRepository().getBranch());
  99. flow.featureFinish("my-feature").setKeepBranch(true).call();
  100. //we should be on develop branch
  101. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  102. //feature branch should still exist
  103. Ref ref2check = git.getRepository().getRef(flow.getFeatureBranchPrefix() + "my-feature");
  104. assertNotNull(ref2check);
  105. }
  106. @Test
  107. public void finishFeatureWithMultipleCommits() throws Exception
  108. {
  109. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  110. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  111. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  112. flow.featureStart("my-feature").call();
  113. //create a new commit
  114. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  115. FileUtils.writeStringToFile(junkFile, "I am junk");
  116. git.add().addFilepattern(junkFile.getName()).call();
  117. RevCommit commit = git.commit().setMessage("committing junk file").call();
  118. //create second commit
  119. File junkFile2 = new File(git.getRepository().getWorkTree(), "junk2.txt");
  120. FileUtils.writeStringToFile(junkFile2, "I am junk, and so are you");
  121. git.add().addFilepattern(junkFile2.getName()).call();
  122. RevCommit commit2 = git.commit().setMessage("updating junk file").call();
  123. //make sure develop doesn't have our commits yet
  124. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  125. assertFalse(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  126. //try to finish
  127. flow.featureFinish("my-feature").call();
  128. //we should be on develop branch
  129. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  130. //feature branch should be gone
  131. Ref ref2check = git.getRepository().getRef(flow.getFeatureBranchPrefix() + "my-feature");
  132. assertNull(ref2check);
  133. //the develop branch should have both of our commits now
  134. assertTrue(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  135. assertTrue(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  136. }
  137. @Test
  138. public void finishFeatureWithSquash() throws Exception
  139. {
  140. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  141. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  142. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  143. flow.featureStart("my-feature").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. //create second commit
  150. File junkFile2 = new File(git.getRepository().getWorkTree(), "junk2.txt");
  151. FileUtils.writeStringToFile(junkFile2, "I am junk, and so are you");
  152. git.add().addFilepattern(junkFile2.getName()).call();
  153. RevCommit commit2 = git.commit().setMessage("updating junk file").call();
  154. //make sure develop doesn't have our commits yet
  155. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  156. assertFalse(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  157. //try to finish
  158. flow.featureFinish("my-feature").setSquash(true).call();
  159. //we should be on develop branch
  160. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  161. //feature branch should be gone
  162. Ref ref2check = git.getRepository().getRef(flow.getFeatureBranchPrefix() + "my-feature");
  163. assertNull(ref2check);
  164. //the develop branch should NOT have both of our commits now
  165. assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
  166. assertFalse(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
  167. //we should have the feature files
  168. File developJunk = new File(git.getRepository().getWorkTree(), "junk.txt");
  169. File developJunk2 = new File(git.getRepository().getWorkTree(), "junk2.txt");
  170. assertTrue(developJunk.exists());
  171. assertTrue(developJunk2.exists());
  172. }
  173. @Test(expected = BranchOutOfDateException.class)
  174. public void finishFeatureBehindRemoteWithFetch() throws Exception
  175. {
  176. Git git = null;
  177. Git remoteGit = null;
  178. remoteGit = RepoUtil.createRepositoryWithBranches(newDir(), "develop", "feature/my-feature");
  179. git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  180. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  181. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  182. flow.featureStart("my-feature").call();
  183. //do a commit to the remote feature branch
  184. remoteGit.checkout().setName(flow.getFeatureBranchPrefix() + "my-feature");
  185. File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
  186. FileUtils.writeStringToFile(junkFile, "I am junk");
  187. remoteGit.add().addFilepattern(junkFile.getName()).call();
  188. RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
  189. flow.featureFinish("my-feature").setFetchDevelop(true).call();
  190. }
  191. @Test(expected = MergeConflictsNotResolvedException.class)
  192. public void finishFeatureMergeConflict() throws Exception
  193. {
  194. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  195. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  196. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  197. flow.featureStart("my-feature").call();
  198. //go back to develop and do a commit
  199. git.checkout().setName(flow.getDevelopBranchName()).call();
  200. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  201. FileUtils.writeStringToFile(junkFile, "A");
  202. git.add().addFilepattern(junkFile.getName()).call();
  203. git.commit().setMessage("committing junk file").call();
  204. //commit the same file in feature to create a conflict
  205. git.checkout().setName(flow.getFeatureBranchPrefix() + "my-feature").call();
  206. File junkFile2 = new File(git.getRepository().getWorkTree(), "junk.txt");
  207. FileUtils.writeStringToFile(junkFile, "B");
  208. git.add().addFilepattern(junkFile.getName()).call();
  209. git.commit().setMessage("committing junk file").call();
  210. //try to finish
  211. try
  212. {
  213. flow.featureFinish("my-feature").setKeepBranch(true).call();
  214. }
  215. catch (Exception e)
  216. {
  217. File gitFlowDir = new File(git.getRepository().getDirectory(), JGitFlowConstants.GITFLOW_DIR);
  218. File mergeBase = new File(gitFlowDir, JGitFlowConstants.MERGE_BASE);
  219. assertTrue(mergeBase.exists());
  220. assertEquals(flow.getDevelopBranchName(), FileHelper.readFirstLine(mergeBase));
  221. throw e;
  222. }
  223. }
  224. @Test
  225. public void finishFeatureConflictRestore() throws Exception
  226. {
  227. Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
  228. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  229. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  230. flow.featureStart("my-feature").call();
  231. //go back to develop and do a commit
  232. git.checkout().setName(flow.getDevelopBranchName()).call();
  233. File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
  234. FileUtils.writeStringToFile(junkFile, "A");
  235. git.add().addFilepattern(junkFile.getName()).call();
  236. RevCommit commit1 = git.commit().setMessage("committing junk file").call();
  237. //commit the same file in feature to create a conflict
  238. git.checkout().setName(flow.getFeatureBranchPrefix() + "my-feature").call();
  239. File junkFile2 = new File(git.getRepository().getWorkTree(), "junk.txt");
  240. FileUtils.writeStringToFile(junkFile, "B");
  241. git.add().addFilepattern(junkFile.getName()).call();
  242. RevCommit commit2 = git.commit().setMessage("committing junk file").call();
  243. boolean gotException = false;
  244. //try to finish
  245. try
  246. {
  247. flow.featureFinish("my-feature").call();
  248. }
  249. catch (Exception e)
  250. {
  251. gotException = true;
  252. File gitFlowDir = new File(git.getRepository().getDirectory(), JGitFlowConstants.GITFLOW_DIR);
  253. File mergeBase = new File(gitFlowDir, JGitFlowConstants.MERGE_BASE);
  254. assertTrue(mergeBase.exists());
  255. assertEquals(flow.getDevelopBranchName(), FileHelper.readFirstLine(mergeBase));
  256. }
  257. if(!gotException)
  258. {
  259. fail("Merge Conflict not detected!!");
  260. }
  261. assertEquals(flow.getDevelopBranchName(),git.getRepository().getBranch());
  262. FileUtils.writeStringToFile(junkFile, "A");
  263. git.add().addFilepattern(junkFile.getName()).setUpdate(true).call();
  264. git.commit().setMessage("merging").call();
  265. //try to finish again
  266. git.checkout().setName(flow.getFeatureBranchPrefix() + "my-feature").call();
  267. flow.featureFinish("my-feature").call();
  268. //we should be on develop branch
  269. assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
  270. //feature branch should be gone
  271. Ref ref2check = git.getRepository().getRef(flow.getFeatureBranchPrefix() + "my-feature");
  272. assertNull(ref2check);
  273. File gitFlowDir2 = new File(git.getRepository().getDirectory(), JGitFlowConstants.GITFLOW_DIR);
  274. File mergeBase2 = new File(gitFlowDir2, JGitFlowConstants.MERGE_BASE);
  275. assertFalse(mergeBase2.exists());
  276. }
  277. }