/jgit-flow-core/src/test/java/ut/com/atlassian/jgitflow/core/HotfixFinishTest.java
https://bitbucket.org/sbambach/jgit-flow · Java · 237 lines · 146 code · 61 blank · 30 comment · 0 complexity · f5ed8500b891590d025c3b306f4b5207 MD5 · raw file
- package ut.com.atlassian.jgitflow.core;
- import java.io.File;
- import com.atlassian.jgitflow.core.JGitFlow;
- import com.atlassian.jgitflow.core.JGitFlowInitCommand;
- import com.atlassian.jgitflow.core.exception.BranchOutOfDateException;
- import com.atlassian.jgitflow.core.exception.DirtyWorkingTreeException;
- import com.atlassian.jgitflow.core.util.GitHelper;
- import org.apache.commons.io.FileUtils;
- import org.eclipse.jgit.api.Git;
- import org.eclipse.jgit.lib.Ref;
- import org.eclipse.jgit.revwalk.RevCommit;
- import org.junit.Test;
- import ut.com.atlassian.jgitflow.core.testutils.RepoUtil;
- import static org.junit.Assert.*;
- import static org.junit.Assert.assertTrue;
- /**
- * @since version
- */
- public class HotfixFinishTest extends BaseGitFlowTest
- {
- @Test
- public void finishHotfix() throws Exception
- {
- Git git = RepoUtil.createRepositoryWithMaster(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- flow.hotfixStart("1.0").call();
- assertEquals(flow.getHotfixBranchPrefix() + "1.0", git.getRepository().getBranch());
- flow.hotfixFinish("1.0").call();
- //we should be on develop branch
- assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
- //release branch should be gone
- Ref ref2check = git.getRepository().getRef(flow.getHotfixBranchPrefix() + "1.0");
- assertNull(ref2check);
- }
- @Test(expected = DirtyWorkingTreeException.class)
- public void finishHotfixWithUnStagedFile() throws Exception
- {
- Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- flow.hotfixStart("1.0").call();
- //create a new file
- File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
- FileUtils.writeStringToFile(junkFile, "I am junk");
- //try to finish
- flow.hotfixFinish("1.0").call();
- }
- @Test(expected = DirtyWorkingTreeException.class)
- public void finishHotfixUnCommittedFile() throws Exception
- {
- Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- flow.hotfixStart("1.0").call();
- //create a new file and add it to the index
- File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
- FileUtils.writeStringToFile(junkFile, "I am junk");
- git.add().addFilepattern(junkFile.getName()).call();
- //try to finish
- flow.hotfixFinish("1.0").call();
- }
- @Test
- public void finishHotfixWithNewCommit() throws Exception
- {
- Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- flow.hotfixStart("1.0").call();
- //create a new commit
- File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
- FileUtils.writeStringToFile(junkFile, "I am junk");
- git.add().addFilepattern(junkFile.getName()).call();
- RevCommit commit = git.commit().setMessage("committing junk file").call();
- //make sure develop doesn't report our commit yet
- assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
- //try to finish
- flow.hotfixFinish("1.0").call();
- //we should be on develop branch
- assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
- //release branch should be gone
- Ref ref2check = git.getRepository().getRef(flow.getHotfixBranchPrefix() + "1.0");
- assertNull(ref2check);
- //the develop branch should have our commit
- assertTrue(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
- //the master branch should have our commit
- assertTrue(GitHelper.isMergedInto(git, commit, flow.getMasterBranchName()));
- }
- @Test
- public void finishHotfixKeepBranch() throws Exception
- {
- Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- flow.hotfixStart("1.0").call();
- //just in case
- assertEquals(flow.getHotfixBranchPrefix() + "1.0", git.getRepository().getBranch());
- flow.hotfixFinish("1.0").setKeepBranch(true).call();
- //we should be on develop branch
- assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
- //release branch should still exist
- Ref ref2check = git.getRepository().getRef(flow.getHotfixBranchPrefix() + "1.0");
- assertNotNull(ref2check);
- }
- @Test
- public void finishHotfixWithMultipleCommits() throws Exception
- {
- Git git = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- flow.hotfixStart("1.0").call();
- //create a new commit
- File junkFile = new File(git.getRepository().getWorkTree(), "junk.txt");
- FileUtils.writeStringToFile(junkFile, "I am junk");
- git.add().addFilepattern(junkFile.getName()).call();
- RevCommit commit = git.commit().setMessage("committing junk file").call();
- //create second commit
- File junkFile2 = new File(git.getRepository().getWorkTree(), "junk2.txt");
- FileUtils.writeStringToFile(junkFile2, "I am junk, and so are you");
- git.add().addFilepattern(junkFile2.getName()).call();
- RevCommit commit2 = git.commit().setMessage("updating junk file").call();
- //make sure develop doesn't have our commits yet
- assertFalse(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
- assertFalse(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
- //try to finish
- flow.hotfixFinish("1.0").call();
- //we should be on develop branch
- assertEquals(flow.getDevelopBranchName(), git.getRepository().getBranch());
- //release branch should be gone
- Ref ref2check = git.getRepository().getRef(flow.getHotfixBranchPrefix() + "1.0");
- assertNull(ref2check);
- //the develop branch should have both of our commits now
- assertTrue(GitHelper.isMergedInto(git, commit, flow.getDevelopBranchName()));
- assertTrue(GitHelper.isMergedInto(git, commit2, flow.getDevelopBranchName()));
- //the master branch should have both of our commits now
- assertTrue(GitHelper.isMergedInto(git, commit, flow.getMasterBranchName()));
- assertTrue(GitHelper.isMergedInto(git, commit2, flow.getMasterBranchName()));
- }
- @Test(expected = BranchOutOfDateException.class)
- public void finishHotfixDevelopBehindRemoteWithFetch() throws Exception
- {
- Git git = null;
- Git remoteGit = null;
- remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
- git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- flow.hotfixStart("1.0").call();
- //do a commit to the remote develop branch
- remoteGit.checkout().setName(flow.getDevelopBranchName());
- File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
- FileUtils.writeStringToFile(junkFile, "I am junk");
- remoteGit.add().addFilepattern(junkFile.getName()).call();
- RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
- flow.hotfixFinish("1.0").setFetch(true).call();
- }
- @Test(expected = BranchOutOfDateException.class)
- public void finishHotfixMasterBehindRemoteWithFetch() throws Exception
- {
- Git git = null;
- Git remoteGit = null;
- remoteGit = RepoUtil.createRepositoryWithMasterAndDevelop(newDir());
- git = Git.cloneRepository().setDirectory(newDir()).setURI("file://" + remoteGit.getRepository().getWorkTree().getPath()).call();
- JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
- JGitFlow flow = initCommand.setDirectory(git.getRepository().getWorkTree()).call();
- flow.hotfixStart("1.0").call();
- //do a commit to the remote develop branch
- remoteGit.checkout().setName(flow.getMasterBranchName());
- File junkFile = new File(remoteGit.getRepository().getWorkTree(), "junk.txt");
- FileUtils.writeStringToFile(junkFile, "I am junk");
- remoteGit.add().addFilepattern(junkFile.getName()).call();
- RevCommit remoteCommit = remoteGit.commit().setMessage("adding junk file").call();
- flow.hotfixFinish("1.0").setFetch(true).call();
- }
- //TODO: add tests for push and tag flags
- }