Skip to main content
summaryrefslogtreecommitdiffstats
blob: 18e52b84ed757754ee9dae478823ecee83103a80 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
 * Copyright (C) 2015, Max Hohenegger <eclipse@hohenegger.eu>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.gitflow.op;

import static org.eclipse.egit.gitflow.GitFlowDefaults.DEVELOP;
import static org.eclipse.egit.gitflow.GitFlowDefaults.FEATURE_PREFIX;
import static org.eclipse.egit.gitflow.GitFlowDefaults.HOTFIX_PREFIX;
import static org.eclipse.egit.gitflow.GitFlowDefaults.MASTER;
import static org.eclipse.egit.gitflow.GitFlowDefaults.RELEASE_PREFIX;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;

import java.util.Iterator;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.egit.core.op.BranchOperation;
import org.eclipse.egit.gitflow.GitFlowRepository;
import org.eclipse.egit.gitflow.InitParameters;
import org.eclipse.egit.gitflow.WrongGitFlowStateException;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;

public class ReleaseFinishOperationTest extends AbstractGitFlowOperationTest {
	@Test
	public void testReleaseFinishSingleCommit() throws Exception {
		testRepository
				.createInitialCommit("testReleaseFinish\n\nfirst commit\n");

		Repository repository = testRepository.getRepository();
		InitParameters initParameters = new InitParameters();
		initParameters.setDevelop(DEVELOP);
		initParameters.setMaster(MASTER);
		initParameters.setFeature(FEATURE_PREFIX);
		initParameters.setRelease(RELEASE_PREFIX);
		initParameters.setHotfix(HOTFIX_PREFIX);
		initParameters.setVersionTag(MY_VERSION_TAG);
		new InitOperation(repository, initParameters).execute(null);
		GitFlowRepository gfRepo = new GitFlowRepository(repository);

		new ReleaseStartOperation(gfRepo, MY_RELEASE).execute(null);
		RevCommit branchCommit = testRepository
				.createInitialCommit("testReleaseFinish\n\nbranch commit\n");
		new ReleaseFinishOperation(gfRepo).execute(null);
		assertEquals(gfRepo.getConfig().getDevelopFull(), repository.getFullBranch());

		String branchName = gfRepo.getConfig().getReleaseBranchName(MY_RELEASE);
		// tag created?
		RevCommit taggedCommit = gfRepo.findCommitForTag(MY_VERSION_TAG + MY_RELEASE);
		assertEquals(formatMergeCommitMessage(branchName), taggedCommit.getShortMessage());
		// branch removed?
		assertEquals(findBranch(repository, branchName), null);

		RevCommit developHead = gfRepo.findHead(DEVELOP);
		assertNotEquals(branchCommit, developHead);

		RevCommit masterHead = gfRepo.findHead(MY_MASTER);
		assertEquals(formatMergeCommitMessage(branchName), masterHead.getShortMessage());
	}

	@Test
	public void testReleaseFinish() throws Exception {
		testRepository
				.createInitialCommit("testReleaseFinish\n\nfirst commit\n");

		Repository repository = testRepository.getRepository();
		InitParameters initParameters = new InitParameters();
		initParameters.setDevelop(DEVELOP);
		initParameters.setMaster(MASTER);
		initParameters.setFeature(FEATURE_PREFIX);
		initParameters.setRelease(RELEASE_PREFIX);
		initParameters.setHotfix(HOTFIX_PREFIX);
		initParameters.setVersionTag(MY_VERSION_TAG);
		new InitOperation(repository, initParameters).execute(null);
		GitFlowRepository gfRepo = new GitFlowRepository(repository);

		new ReleaseStartOperation(gfRepo, MY_RELEASE).execute(null);
		addFileAndCommit("foo.txt", "testReleaseFinish\n\nbranch commit 1\n");
		addFileAndCommit("bar.txt", "testReleaseFinish\n\nbranch commit 2\n");
		ReleaseFinishOperation releaseFinishOperation = new ReleaseFinishOperation(gfRepo);
		releaseFinishOperation.execute(null);
		assertEquals(gfRepo.getConfig().getDevelopFull(),
				repository.getFullBranch());

		String branchName = gfRepo.getConfig().getReleaseBranchName(MY_RELEASE);
		// tag created?
		RevCommit taggedCommit = gfRepo.findCommitForTag(MY_VERSION_TAG
				+ MY_RELEASE);
		assertEquals(formatMergeCommitMessage(branchName),
				taggedCommit.getFullMessage());

		// branch removed?
		assertEquals(findBranch(repository, branchName), null);
	}
	@Test
	public void testReleaseFinishFail() throws Exception {
		testRepository
				.createInitialCommit("testReleaseFinishFail\n\nfirst commit\n");

		Repository repository = testRepository.getRepository();
		new InitOperation(repository).execute(null);
		GitFlowRepository gfRepo = new GitFlowRepository(repository);

		new ReleaseStartOperation(gfRepo, MY_RELEASE).execute(null);
		new BranchOperation(repository, gfRepo.getConfig().getDevelop()).execute(null);

		try {
			new ReleaseFinishOperation(gfRepo).execute(null);
			fail();
		} catch (WrongGitFlowStateException e) {
			// success
		}
	}

	@Test
	public void testReleaseTagWithWrongReferenceExists() throws Exception {
		testRepository
				.createInitialCommit("testReleaseTagExists\n\nfirst commit\n");
		testRepository
				.createInitialCommit("testReleaseTagExists\n\nsecond commit\n");

		Repository repository = testRepository.getRepository();
		new InitOperation(repository).execute(null);
		GitFlowRepository gfRepo = new GitFlowRepository(repository);

		new ReleaseStartOperation(gfRepo, MY_RELEASE).execute(null);
		RevCommit next = getPreviousCommit(repository, 1);
		ReleaseFinishOperation releaseFinishOperation = new ReleaseFinishOperation(
				gfRepo);
		releaseFinishOperation.createTag(null, next, MY_RELEASE, "irrelevant");

		try {
			releaseFinishOperation.execute(null);
			fail();
		} catch (CoreException e) {
			assertFalse(e.getStatus().isOK());
		}
	}

	@Test
	public void testReleaseTagWithCorrectReferenceExists() throws Exception {
		testRepository
				.createInitialCommit("testReleaseTagExists\n\nfirst commit\n");

		Repository repository = testRepository.getRepository();
		new InitOperation(repository).execute(null);
		GitFlowRepository gfRepo = new GitFlowRepository(repository);

		new ReleaseStartOperation(gfRepo, MY_RELEASE).execute(null);
		RevCommit next = getPreviousCommit(repository, 0);
		ReleaseFinishOperation releaseFinishOperation = new ReleaseFinishOperation(
				gfRepo);
		releaseFinishOperation.createTag(null, next, MY_RELEASE, "irrelevant");
		releaseFinishOperation.execute(null);
	}

	private RevCommit getPreviousCommit(Repository repository, int count)
			throws GitAPIException, NoHeadException {
		Iterable<RevCommit> logs = Git.wrap(repository).log().call();
		Iterator<RevCommit> i = logs.iterator();
		for (int j = 0; j < count; j++) {
			i.next();
		}
		RevCommit next = i.next();
		return next;
	}
}

Back to the top