Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3a6739e1c2a1020d160749975ae185c8f091223c (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (C) 2009, Robin Rosenberg
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * See LICENSE for the full license text, also available.
 *******************************************************************************/
package org.eclipse.egit.ui.variables;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Collections;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.variables.IDynamicVariable;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.egit.core.GitProvider;
import org.eclipse.egit.core.project.GitProjectData;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.common.EGitTestCase;
import org.eclipse.egit.ui.test.TestUtil;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.ui.PlatformUI;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(SWTBotJunit4ClassRunner.class)
public class DynamicVariablesTest extends EGitTestCase {

	private File gitDir;
	private File gitDir2;
	private IProject project;
	private IProject project2;
	private Repository repository;
	private Repository repository2;
	private Git git;

	private static final String TEST_PROJECT = "TestProject";
	private static final String TEST_PROJECT_LOC = "Sub/TestProject";
	private static final String TEST_PROJECT2 = "TestProject2";

	private static final String TEST_FILE = "TestFile";
	private static final String TEST_FILE2 = "TestFile2";

	@Before
	public void setUp() throws Exception {

		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

		FileUtils.mkdir(new File(root.getLocation().toFile(),"Sub"), true);
		gitDir = new File(new File(root.getLocation().toFile(), "Sub"), Constants.DOT_GIT);

		repository = FileRepositoryBuilder.create(gitDir);
		repository.create();

		project = root.getProject(TEST_PROJECT);
		project.create(null);
		project.open(null);
		IProjectDescription description = project.getDescription();
		description.setLocation(root.getLocation().append(TEST_PROJECT_LOC));
		project.move(description, IResource.FORCE, null);

		project2 = root.getProject(TEST_PROJECT2);
		project2.create(null);
		project2.open(null);
		gitDir2 = new File(project2.getLocation().toFile().getAbsoluteFile(), Constants.DOT_GIT);
		repository2 = FileRepositoryBuilder.create(gitDir2);
		repository2.create();

		RepositoryMapping mapping = new RepositoryMapping(project, gitDir);
		RepositoryMapping mapping2 = new RepositoryMapping(project2, gitDir2);

		GitProjectData projectData = new GitProjectData(project);
		GitProjectData projectData2 = new GitProjectData(project2);
		projectData.setRepositoryMappings(Collections.singletonList(mapping));
		projectData.store();
		projectData2.setRepositoryMappings(Collections.singletonList(mapping2));
		projectData2.store();
		GitProjectData.add(project, projectData);
		GitProjectData.add(project2, projectData2);

		RepositoryProvider.map(project, GitProvider.class.getName());
		RepositoryProvider.map(project2, GitProvider.class.getName());

		File f = new File(repository.getWorkTree(), TEST_PROJECT + "/"
				+ TEST_FILE);
		Writer fileWriter = new OutputStreamWriter(new FileOutputStream(f),
				"UTF-8");
		fileWriter.write("Some data");
		fileWriter.close();
		FileWriter fileWriter2 = new FileWriter(new File(repository2.getWorkTree(), TEST_FILE2));
		fileWriter2.write("Some other data");
		fileWriter2.close();
		project.refreshLocal(IResource.DEPTH_INFINITE, null);
		project2.refreshLocal(IResource.DEPTH_INFINITE, null);
		git = new Git(repository);
		git.add().addFilepattern(".").call();
		git.commit().setMessage("Initial commit").call();

		git = new Git(repository2);
		git.add().addFilepattern(".").call();
		git.commit().setMessage("Initial commit").call();
		git.branchRename().setNewName("main").call();
	}

	@After
	public void tearDown() throws Exception {

		Thread.sleep(1000); // FIXME: We need a good way to wait for things to settle

		RepositoryProvider.unmap(project);
		RepositoryProvider.unmap(project2);

		GitProjectData.delete(project);
		GitProjectData.delete(project2);

		project.delete(true, true, null);
		project2.delete(true, true, null);

		repository.close();
		repository2.close();

		org.eclipse.egit.core.Activator.getDefault().getRepositoryCache().clear();

		FileUtils.delete(gitDir, FileUtils.RECURSIVE);
		// gitDir2 is inside project, already gone
	}

	@Test
	public void testGitDir() throws CoreException {
		assertVariable(gitDir.getPath(), "git_dir", null);
		assertVariable(gitDir.getPath(), "git_dir", TEST_PROJECT);
		assertVariable(gitDir2.getPath(), "git_dir", TEST_PROJECT2);
		assertVariable(gitDir2.getPath(), "git_dir", TEST_PROJECT2 + "/" + TEST_FILE2);
	}

	@Test
	public void testGitWorkTree() throws CoreException {
		assertVariable(gitDir.getParentFile().getPath(), "git_work_tree", null);
		assertVariable(gitDir.getParentFile().getPath(), "git_work_tree", TEST_PROJECT);
		assertVariable(gitDir2.getParentFile().getPath(), "git_work_tree", TEST_PROJECT2);
	}

	@Test
	public void testGitPath() throws CoreException {
		assertVariable(TEST_PROJECT + File.separatorChar + TEST_FILE, "git_repo_relative_path", null);
		assertVariable(TEST_FILE2, "git_repo_relative_path", TEST_PROJECT2 + "/" + TEST_FILE2);
	}

	@Test
	public void testGitBranch() throws CoreException {
		assertVariable("master", "git_branch", null);
		assertVariable("master", "git_branch", TEST_PROJECT + "/" + TEST_FILE);
		assertVariable("main", "git_branch", TEST_PROJECT2 + "/" + TEST_FILE2);
		assertVariable("main", "git_branch", TEST_PROJECT2);
	}

	private void assertVariable(String expected, String variableName,
			String argument) throws CoreException {
		IResource findMember = project.findMember(TEST_FILE);

		SWTBotView explorerView = TestUtil.showExplorerView();
		final ISelectionProvider selectionProvider = explorerView
				.getViewReference().getView(true).getSite()
				.getSelectionProvider();
		final StructuredSelection structuredSelection = new StructuredSelection(
				findMember);
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

			public void run() {
				selectionProvider.setSelection(structuredSelection);
			}
		});

		IDynamicVariable dynamicVariable = VariablesPlugin.getDefault()
				.getStringVariableManager().getDynamicVariable(variableName);
		String value = dynamicVariable.getValue(argument);
		assertEquals(expected, value);
	}
}

Back to the top