/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
- package ut.com.atlassian.jgitflow.core;
- import java.io.File;
- import com.atlassian.jgitflow.core.InitContext;
- import com.atlassian.jgitflow.core.JGitFlow;
- import com.atlassian.jgitflow.core.JGitFlowInitCommand;
- import com.atlassian.jgitflow.core.exception.AlreadyInitializedException;
- import com.google.common.base.Charsets;
- import com.google.common.io.Resources;
- import org.eclipse.jgit.api.Git;
- import org.junit.Test;
- import ut.com.atlassian.jgitflow.core.testutils.RepoUtil;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertTrue;
- public class GitFlowInitTest extends BaseGitFlowTest
- {
- @Test
- public void initInCleanRepo() throws Exception
- {
- File workDir = newDir();
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(workDir).call();
- assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
-
- File gitDir = new File(workDir, ".git");
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
- assertTrue(configText.contains("[gitflow \"branch\"]"));
- assertTrue(configText.contains("[gitflow \"prefix\"]"));
- assertEquals("master", flow.getMasterBranchName());
- assertEquals("develop", flow.getDevelopBranchName());
- assertEquals("feature/", flow.getFeatureBranchPrefix());
- assertEquals("release/", flow.getReleaseBranchPrefix());
- assertEquals("hotfix/", flow.getHotfixBranchPrefix());
- assertEquals("support/", flow.getSupportBranchPrefix());
- assertEquals("", flow.getVersionTagPrefix());
- }
- @Test
- public void initInCleanRepoWithContext() throws Exception
- {
- File workDir = newDir();
- InitContext ctx = new InitContext();
- ctx.setMaster("own").setDevelop("you");
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(workDir).setInitContext(ctx).call();
- assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
- File gitDir = new File(workDir, ".git");
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
- assertTrue(configText.contains("[gitflow \"branch\"]"));
- assertTrue(configText.contains("[gitflow \"prefix\"]"));
- assertEquals("own", flow.getMasterBranchName());
- assertEquals("you", flow.getDevelopBranchName());
- assertEquals("feature/", flow.getFeatureBranchPrefix());
- assertEquals("release/", flow.getReleaseBranchPrefix());
- assertEquals("hotfix/", flow.getHotfixBranchPrefix());
- assertEquals("support/", flow.getSupportBranchPrefix());
- assertEquals("", flow.getVersionTagPrefix());
- }
- @Test
- public void initInExistingRepo() throws Exception
- {
- Git git = null;
- git = RepoUtil.createRepositoryWithMaster(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
-
- File gitDir = git.getRepository().getDirectory();
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
- assertTrue(configText.contains("[gitflow \"branch\"]"));
- assertTrue(configText.contains("[gitflow \"prefix\"]"));
- assertEquals("master", flow.getMasterBranchName());
- assertEquals("develop", flow.getDevelopBranchName());
- assertEquals("feature/", flow.getFeatureBranchPrefix());
- assertEquals("release/", flow.getReleaseBranchPrefix());
- assertEquals("hotfix/", flow.getHotfixBranchPrefix());
- assertEquals("support/", flow.getSupportBranchPrefix());
- assertEquals("", flow.getVersionTagPrefix());
- }
- @Test
- public void initInExistingRepoWithContext() throws Exception
- {
- Git git = null;
- InitContext ctx = new InitContext();
- ctx.setMaster("own").setDevelop("you");
- git = RepoUtil.createRepositoryWithMaster(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).setInitContext(ctx).call();
- assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
-
- File gitDir = git.getRepository().getDirectory();
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
- assertTrue(configText.contains("[gitflow \"branch\"]"));
- assertTrue(configText.contains("[gitflow \"prefix\"]"));
- assertEquals("own", flow.getMasterBranchName());
- assertEquals("you", flow.getDevelopBranchName());
- assertEquals("feature/", flow.getFeatureBranchPrefix());
- assertEquals("release/", flow.getReleaseBranchPrefix());
- assertEquals("hotfix/", flow.getHotfixBranchPrefix());
- assertEquals("support/", flow.getSupportBranchPrefix());
- assertEquals("", flow.getVersionTagPrefix());
- }
- @Test
- public void initInExistingRepoWithRemote() throws Exception
- {
- Git gfGit = null;
- Git remoteGit = null;
- File workDir = newDir();
- remoteGit = RepoUtil.createRepositoryWithMaster(newDir());
- gfGit = Git.cloneRepository().setDirectory(workDir).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(gfGit.getRepository().getWorkTree()).call();
- assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
-
- File gitDir = gfGit.getRepository().getDirectory();
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
- assertTrue(configText.contains("[gitflow \"branch\"]"));
- assertTrue(configText.contains("[gitflow \"prefix\"]"));
- assertEquals("master", flow.getMasterBranchName());
- assertEquals("develop", flow.getDevelopBranchName());
- assertEquals("feature/", flow.getFeatureBranchPrefix());
- assertEquals("release/", flow.getReleaseBranchPrefix());
- assertEquals("hotfix/", flow.getHotfixBranchPrefix());
- assertEquals("support/", flow.getSupportBranchPrefix());
- assertEquals("", flow.getVersionTagPrefix());
- }
- @Test
- public void initInExistingRepoWithRemoteAndContext() throws Exception
- {
- Git gfGit = null;
- Git remoteGit = null;
- File workDir = newDir();
- InitContext ctx = new InitContext();
- ctx.setMaster("own").setDevelop("you");
- remoteGit = RepoUtil.createRepositoryWithMaster(newDir());
- gfGit = Git.cloneRepository().setDirectory(workDir).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(gfGit.getRepository().getWorkTree()).setInitContext(ctx).call();
- assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
-
- File gitDir = gfGit.getRepository().getDirectory();
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
- assertTrue(configText.contains("[gitflow \"branch\"]"));
- assertTrue(configText.contains("[gitflow \"prefix\"]"));
- assertEquals("own", flow.getMasterBranchName());
- assertEquals("you", flow.getDevelopBranchName());
- assertEquals("feature/", flow.getFeatureBranchPrefix());
- assertEquals("release/", flow.getReleaseBranchPrefix());
- assertEquals("hotfix/", flow.getHotfixBranchPrefix());
- assertEquals("support/", flow.getSupportBranchPrefix());
- assertEquals("", flow.getVersionTagPrefix());
- }
- @Test
- public void initInExistingRepoWithCustomRemoteAndContext() throws Exception
- {
- Git gfGit = null;
- Git remoteGit = null;
- File workDir = newDir();
- InitContext ctx = new InitContext();
- ctx.setMaster("own").setDevelop("you");
- remoteGit = RepoUtil.createRepositoryWithBranches(newDir(), "own");
- gfGit = Git.cloneRepository().setDirectory(workDir).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(gfGit.getRepository().getWorkTree()).setInitContext(ctx).call();
- assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
-
- File gitDir = gfGit.getRepository().getDirectory();
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
- assertTrue(configText.contains("[gitflow \"branch\"]"));
- assertTrue(configText.contains("[gitflow \"prefix\"]"));
- assertEquals("own", flow.getMasterBranchName());
- assertEquals("you", flow.getDevelopBranchName());
- assertEquals("feature/", flow.getFeatureBranchPrefix());
- assertEquals("release/", flow.getReleaseBranchPrefix());
- assertEquals("hotfix/", flow.getHotfixBranchPrefix());
- assertEquals("support/", flow.getSupportBranchPrefix());
- assertEquals("", flow.getVersionTagPrefix());
- }
- @Test(expected = AlreadyInitializedException.class)
- public void initInExistingFlowRepo() throws Exception
- {
- Git git = null;
- git = RepoUtil.createRepositoryWithMaster(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- File gitDir = git.getRepository().getDirectory();
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- //try to re-init
- JGitFlowInitCommand initCommand2 = new JGitFlowInitCommand();
- //this should throw
- JGitFlow flow2 = initCommand2.setDirectory(git.getRepository().getWorkTree()).call();
- }
- @Test
- public void initInExistingFlowRepoWithForce() throws Exception
- {
- Git git = null;
- git = RepoUtil.createRepositoryWithMaster(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- File gitDir = git.getRepository().getDirectory();
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- //try to re-init
- JGitFlowInitCommand initCommand2 = new JGitFlowInitCommand();
- //this should NOT throw
- JGitFlow flow2 = initCommand2.setDirectory(git.getRepository().getWorkTree()).setForce(true).call();
- String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
- assertTrue(configText.contains("[gitflow \"branch\"]"));
- assertTrue(configText.contains("[gitflow \"prefix\"]"));
- assertEquals("master", flow.getMasterBranchName());
- assertEquals("develop", flow.getDevelopBranchName());
- assertEquals("feature/", flow.getFeatureBranchPrefix());
- assertEquals("release/", flow.getReleaseBranchPrefix());
- assertEquals("hotfix/", flow.getHotfixBranchPrefix());
- assertEquals("support/", flow.getSupportBranchPrefix());
- assertEquals("", flow.getVersionTagPrefix());
- }
- @Test
- public void initInExistingFlowRepoWithForceAndContext() throws Exception
- {
- Git git = null;
- InitContext ctx = new InitContext();
- ctx.setMaster("own").setDevelop("you");
- git = RepoUtil.createRepositoryWithMaster(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- File gitDir = git.getRepository().getDirectory();
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- //try to re-init
- JGitFlowInitCommand initCommand2 = new JGitFlowInitCommand();
- //this should NOT throw
- JGitFlow flow2 = initCommand2.setDirectory(git.getRepository().getWorkTree()).setForce(true).setInitContext(ctx).call();
- String configText = Resources.toString(gitConfig.toURI().toURL(), Charsets.UTF_8);
- assertTrue(configText.contains("[gitflow \"branch\"]"));
- assertTrue(configText.contains("[gitflow \"prefix\"]"));
- //here we check the new instance
- assertEquals("own", flow2.getMasterBranchName());
- //now let's make sure the old instance also sees the config changes
- assertEquals("you", flow.getDevelopBranchName());
- assertEquals("feature/", flow.getFeatureBranchPrefix());
- assertEquals("release/", flow.getReleaseBranchPrefix());
- assertEquals("hotfix/", flow.getHotfixBranchPrefix());
- assertEquals("support/", flow.getSupportBranchPrefix());
- assertEquals("", flow.getVersionTagPrefix());
- }
- }