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

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

https://bitbucket.org/sbambach/jgit-flow
Java | 353 lines | 246 code | 99 blank | 8 comment | 0 complexity | 0cf8e4b16fb39b6533e432e5ac02be70 MD5 | raw file
  1. package ut.com.atlassian.jgitflow.core;
  2. import java.io.File;
  3. import com.atlassian.jgitflow.core.InitContext;
  4. import com.atlassian.jgitflow.core.JGitFlow;
  5. import com.atlassian.jgitflow.core.JGitFlowInitCommand;
  6. import com.atlassian.jgitflow.core.exception.AlreadyInitializedException;
  7. import com.google.common.base.Charsets;
  8. import com.google.common.io.Resources;
  9. import org.eclipse.jgit.api.Git;
  10. import org.junit.Test;
  11. import ut.com.atlassian.jgitflow.core.testutils.RepoUtil;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertTrue;
  14. public class GitFlowInitTest extends BaseGitFlowTest
  15. {
  16. @Test
  17. public void initInCleanRepo() throws Exception
  18. {
  19. File workDir = newDir();
  20. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  21. JGitFlow flow = initCommand.setDirectory(workDir).call();
  22. assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
  23. File gitDir = new File(workDir, ".git");
  24. File gitConfig = new File(gitDir, "config");
  25. assertTrue(gitConfig.exists());
  26. String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
  27. assertTrue(configText.contains("[gitflow \"branch\"]"));
  28. assertTrue(configText.contains("[gitflow \"prefix\"]"));
  29. assertEquals("master", flow.getMasterBranchName());
  30. assertEquals("develop", flow.getDevelopBranchName());
  31. assertEquals("feature/", flow.getFeatureBranchPrefix());
  32. assertEquals("release/", flow.getReleaseBranchPrefix());
  33. assertEquals("hotfix/", flow.getHotfixBranchPrefix());
  34. assertEquals("support/", flow.getSupportBranchPrefix());
  35. assertEquals("", flow.getVersionTagPrefix());
  36. }
  37. @Test
  38. public void initInCleanRepoWithContext() throws Exception
  39. {
  40. File workDir = newDir();
  41. InitContext ctx = new InitContext();
  42. ctx.setMaster("own").setDevelop("you");
  43. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  44. JGitFlow flow = initCommand.setDirectory(workDir).setInitContext(ctx).call();
  45. assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
  46. File gitDir = new File(workDir, ".git");
  47. File gitConfig = new File(gitDir, "config");
  48. assertTrue(gitConfig.exists());
  49. String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
  50. assertTrue(configText.contains("[gitflow \"branch\"]"));
  51. assertTrue(configText.contains("[gitflow \"prefix\"]"));
  52. assertEquals("own", flow.getMasterBranchName());
  53. assertEquals("you", flow.getDevelopBranchName());
  54. assertEquals("feature/", flow.getFeatureBranchPrefix());
  55. assertEquals("release/", flow.getReleaseBranchPrefix());
  56. assertEquals("hotfix/", flow.getHotfixBranchPrefix());
  57. assertEquals("support/", flow.getSupportBranchPrefix());
  58. assertEquals("", flow.getVersionTagPrefix());
  59. }
  60. @Test
  61. public void initInExistingRepo() throws Exception
  62. {
  63. Git git = null;
  64. git = RepoUtil.createRepositoryWithMaster(newDir());
  65. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  66. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  67. assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
  68. File gitDir = git.getRepository().getDirectory();
  69. File gitConfig = new File(gitDir, "config");
  70. assertTrue(gitConfig.exists());
  71. String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
  72. assertTrue(configText.contains("[gitflow \"branch\"]"));
  73. assertTrue(configText.contains("[gitflow \"prefix\"]"));
  74. assertEquals("master", flow.getMasterBranchName());
  75. assertEquals("develop", flow.getDevelopBranchName());
  76. assertEquals("feature/", flow.getFeatureBranchPrefix());
  77. assertEquals("release/", flow.getReleaseBranchPrefix());
  78. assertEquals("hotfix/", flow.getHotfixBranchPrefix());
  79. assertEquals("support/", flow.getSupportBranchPrefix());
  80. assertEquals("", flow.getVersionTagPrefix());
  81. }
  82. @Test
  83. public void initInExistingRepoWithContext() throws Exception
  84. {
  85. Git git = null;
  86. InitContext ctx = new InitContext();
  87. ctx.setMaster("own").setDevelop("you");
  88. git = RepoUtil.createRepositoryWithMaster(newDir());
  89. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  90. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).setInitContext(ctx).call();
  91. assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
  92. File gitDir = git.getRepository().getDirectory();
  93. File gitConfig = new File(gitDir, "config");
  94. assertTrue(gitConfig.exists());
  95. String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
  96. assertTrue(configText.contains("[gitflow \"branch\"]"));
  97. assertTrue(configText.contains("[gitflow \"prefix\"]"));
  98. assertEquals("own", flow.getMasterBranchName());
  99. assertEquals("you", flow.getDevelopBranchName());
  100. assertEquals("feature/", flow.getFeatureBranchPrefix());
  101. assertEquals("release/", flow.getReleaseBranchPrefix());
  102. assertEquals("hotfix/", flow.getHotfixBranchPrefix());
  103. assertEquals("support/", flow.getSupportBranchPrefix());
  104. assertEquals("", flow.getVersionTagPrefix());
  105. }
  106. @Test
  107. public void initInExistingRepoWithRemote() throws Exception
  108. {
  109. Git gfGit = null;
  110. Git remoteGit = null;
  111. File workDir = newDir();
  112. remoteGit = RepoUtil.createRepositoryWithMaster(newDir());
  113. gfGit = Git.cloneRepository().setDirectory(workDir).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  114. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  115. JGitFlow flow = initCommand.setDirectory(gfGit.getRepository().getWorkTree()).call();
  116. assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
  117. File gitDir = gfGit.getRepository().getDirectory();
  118. File gitConfig = new File(gitDir, "config");
  119. assertTrue(gitConfig.exists());
  120. String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
  121. assertTrue(configText.contains("[gitflow \"branch\"]"));
  122. assertTrue(configText.contains("[gitflow \"prefix\"]"));
  123. assertEquals("master", flow.getMasterBranchName());
  124. assertEquals("develop", flow.getDevelopBranchName());
  125. assertEquals("feature/", flow.getFeatureBranchPrefix());
  126. assertEquals("release/", flow.getReleaseBranchPrefix());
  127. assertEquals("hotfix/", flow.getHotfixBranchPrefix());
  128. assertEquals("support/", flow.getSupportBranchPrefix());
  129. assertEquals("", flow.getVersionTagPrefix());
  130. }
  131. @Test
  132. public void initInExistingRepoWithRemoteAndContext() throws Exception
  133. {
  134. Git gfGit = null;
  135. Git remoteGit = null;
  136. File workDir = newDir();
  137. InitContext ctx = new InitContext();
  138. ctx.setMaster("own").setDevelop("you");
  139. remoteGit = RepoUtil.createRepositoryWithMaster(newDir());
  140. gfGit = Git.cloneRepository().setDirectory(workDir).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  141. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  142. JGitFlow flow = initCommand.setDirectory(gfGit.getRepository().getWorkTree()).setInitContext(ctx).call();
  143. assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
  144. File gitDir = gfGit.getRepository().getDirectory();
  145. File gitConfig = new File(gitDir, "config");
  146. assertTrue(gitConfig.exists());
  147. String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
  148. assertTrue(configText.contains("[gitflow \"branch\"]"));
  149. assertTrue(configText.contains("[gitflow \"prefix\"]"));
  150. assertEquals("own", flow.getMasterBranchName());
  151. assertEquals("you", flow.getDevelopBranchName());
  152. assertEquals("feature/", flow.getFeatureBranchPrefix());
  153. assertEquals("release/", flow.getReleaseBranchPrefix());
  154. assertEquals("hotfix/", flow.getHotfixBranchPrefix());
  155. assertEquals("support/", flow.getSupportBranchPrefix());
  156. assertEquals("", flow.getVersionTagPrefix());
  157. }
  158. @Test
  159. public void initInExistingRepoWithCustomRemoteAndContext() throws Exception
  160. {
  161. Git gfGit = null;
  162. Git remoteGit = null;
  163. File workDir = newDir();
  164. InitContext ctx = new InitContext();
  165. ctx.setMaster("own").setDevelop("you");
  166. remoteGit = RepoUtil.createRepositoryWithBranches(newDir(), "own");
  167. gfGit = Git.cloneRepository().setDirectory(workDir).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
  168. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  169. JGitFlow flow = initCommand.setDirectory(gfGit.getRepository().getWorkTree()).setInitContext(ctx).call();
  170. assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
  171. File gitDir = gfGit.getRepository().getDirectory();
  172. File gitConfig = new File(gitDir, "config");
  173. assertTrue(gitConfig.exists());
  174. String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
  175. assertTrue(configText.contains("[gitflow \"branch\"]"));
  176. assertTrue(configText.contains("[gitflow \"prefix\"]"));
  177. assertEquals("own", flow.getMasterBranchName());
  178. assertEquals("you", flow.getDevelopBranchName());
  179. assertEquals("feature/", flow.getFeatureBranchPrefix());
  180. assertEquals("release/", flow.getReleaseBranchPrefix());
  181. assertEquals("hotfix/", flow.getHotfixBranchPrefix());
  182. assertEquals("support/", flow.getSupportBranchPrefix());
  183. assertEquals("", flow.getVersionTagPrefix());
  184. }
  185. @Test(expected = AlreadyInitializedException.class)
  186. public void initInExistingFlowRepo() throws Exception
  187. {
  188. Git git = null;
  189. git = RepoUtil.createRepositoryWithMaster(newDir());
  190. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  191. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  192. File gitDir = git.getRepository().getDirectory();
  193. File gitConfig = new File(gitDir, "config");
  194. assertTrue(gitConfig.exists());
  195. //try to re-init
  196. JGitFlowInitCommand initCommand2 = new JGitFlowInitCommand();
  197. //this should throw
  198. JGitFlow flow2 = initCommand2.setDirectory(git.getRepository().getWorkTree()).call();
  199. }
  200. @Test
  201. public void initInExistingFlowRepoWithForce() throws Exception
  202. {
  203. Git git = null;
  204. git = RepoUtil.createRepositoryWithMaster(newDir());
  205. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  206. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  207. File gitDir = git.getRepository().getDirectory();
  208. File gitConfig = new File(gitDir, "config");
  209. assertTrue(gitConfig.exists());
  210. //try to re-init
  211. JGitFlowInitCommand initCommand2 = new JGitFlowInitCommand();
  212. //this should NOT throw
  213. JGitFlow flow2 = initCommand2.setDirectory(git.getRepository().getWorkTree()).setForce(true).call();
  214. String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
  215. assertTrue(configText.contains("[gitflow \"branch\"]"));
  216. assertTrue(configText.contains("[gitflow \"prefix\"]"));
  217. assertEquals("master", flow.getMasterBranchName());
  218. assertEquals("develop", flow.getDevelopBranchName());
  219. assertEquals("feature/", flow.getFeatureBranchPrefix());
  220. assertEquals("release/", flow.getReleaseBranchPrefix());
  221. assertEquals("hotfix/", flow.getHotfixBranchPrefix());
  222. assertEquals("support/", flow.getSupportBranchPrefix());
  223. assertEquals("", flow.getVersionTagPrefix());
  224. }
  225. @Test
  226. public void initInExistingFlowRepoWithForceAndContext() throws Exception
  227. {
  228. Git git = null;
  229. InitContext ctx = new InitContext();
  230. ctx.setMaster("own").setDevelop("you");
  231. git = RepoUtil.createRepositoryWithMaster(newDir());
  232. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  233. JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
  234. File gitDir = git.getRepository().getDirectory();
  235. File gitConfig = new File(gitDir, "config");
  236. assertTrue(gitConfig.exists());
  237. //try to re-init
  238. JGitFlowInitCommand initCommand2 = new JGitFlowInitCommand();
  239. //this should NOT throw
  240. JGitFlow flow2 = initCommand2.setDirectory(git.getRepository().getWorkTree()).setForce(true).setInitContext(ctx).call();
  241. String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
  242. assertTrue(configText.contains("[gitflow \"branch\"]"));
  243. assertTrue(configText.contains("[gitflow \"prefix\"]"));
  244. //here we check the new instance
  245. assertEquals("own", flow2.getMasterBranchName());
  246. //now let's make sure the old instance also sees the config changes
  247. assertEquals("you", flow.getDevelopBranchName());
  248. assertEquals("feature/", flow.getFeatureBranchPrefix());
  249. assertEquals("release/", flow.getReleaseBranchPrefix());
  250. assertEquals("hotfix/", flow.getHotfixBranchPrefix());
  251. assertEquals("support/", flow.getSupportBranchPrefix());
  252. assertEquals("", flow.getVersionTagPrefix());
  253. }
  254. }