/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
- 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 org.eclipse.jgit.api.Git;
- import org.eclipse.jgit.util.IO;
- 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 = new String(IO.readFully(gitConfig));
- 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 = new String(IO.readFully(gitConfig));
- 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 = new String(IO.readFully(gitConfig));
- 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 = new String(IO.readFully(gitConfig));
- 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 = new String(IO.readFully(gitConfig));
- 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 = new String(IO.readFully(gitConfig));
- 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 = new String(IO.readFully(gitConfig));
- 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 = new String(IO.readFully(gitConfig));
- 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 = new String(IO.readFully(gitConfig));
- 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());
- }
- @Test
- public void initInNestedFolder() throws Exception
- {
- Git git = null;
- git = RepoUtil.createRepositoryWithMaster(newDir());
- File root = git.getRepository().getWorkTree();
- File nested1 = new File(root,"nested1");
- File nested2 = new File(nested1,"nested2");
-
- nested2.mkdirs();
-
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(nested2).call();
- assertEquals(flow.getDevelopBranchName(), flow.git().getRepository().getBranch());
- File gitDir = git.getRepository().getDirectory();
- File gitConfig = new File(gitDir, "config");
- assertTrue(gitConfig.exists());
- String configText = new String(IO.readFully(gitConfig));
- 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());
- }
- }