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

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