2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package git4idea.test;
18 import com.intellij.openapi.components.ServiceManager;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.openapi.util.text.StringUtil;
21 import com.intellij.openapi.vcs.*;
22 import com.intellij.openapi.vcs.changes.ChangeListManager;
23 import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import com.intellij.testFramework.PlatformTestCase;
26 import com.intellij.testFramework.TestLoggerFactory;
27 import com.intellij.testFramework.vcs.AbstractVcsTestCase;
28 import com.intellij.util.ArrayUtil;
29 import com.intellij.util.ObjectUtils;
30 import git4idea.DialogManager;
31 import git4idea.GitPlatformFacade;
32 import git4idea.GitUtil;
33 import git4idea.GitVcs;
34 import git4idea.commands.Git;
35 import git4idea.commands.GitHandler;
36 import git4idea.config.GitVcsSettings;
37 import git4idea.repo.GitRepository;
38 import git4idea.repo.GitRepositoryManager;
39 import org.jetbrains.annotations.NotNull;
43 public abstract class GitPlatformTest extends PlatformTestCase {
45 protected static final Logger LOG = Logger.getInstance(GitPlatformTest.class);
47 protected VirtualFile myProjectRoot;
48 protected String myProjectPath;
49 protected GitRepositoryManager myGitRepositoryManager;
50 protected GitVcsSettings myGitSettings;
51 protected GitPlatformFacade myPlatformFacade;
53 protected GitVcs myVcs;
55 protected TestDialogManager myDialogManager;
56 protected TestVcsNotifier myVcsNotifier;
58 private String myTestStartedIndicator;
61 protected void setUp() throws Exception {
65 myProjectRoot = myProject.getBaseDir();
66 myProjectPath = myProjectRoot.getPath();
68 myGitSettings = GitVcsSettings.getInstance(myProject);
69 myGitSettings.getAppSettings().setPathToGit(GitExecutor.PathHolder.GIT_EXECUTABLE);
71 myDialogManager = (TestDialogManager)ServiceManager.getService(DialogManager.class);
72 myVcsNotifier = (TestVcsNotifier)ServiceManager.getService(myProject, VcsNotifier.class);
74 myGitRepositoryManager = GitUtil.getRepositoryManager(myProject);
75 myPlatformFacade = ServiceManager.getService(myProject, GitPlatformFacade.class);
76 myGit = ServiceManager.getService(myProject, Git.class);
77 myVcs = ObjectUtils.assertNotNull(GitVcs.getInstance(myProject));
80 GitTestUtil.assumeSupportedGitVersion(myVcs);
87 public String getTestName(boolean lowercaseFirstLetter) {
88 String name = super.getTestName(lowercaseFirstLetter);
89 name = StringUtil.shortenTextWithEllipsis(name.trim().replace(" ", "_"), 12, 6, "_");
90 if (name.startsWith("_")) {
91 name = name.substring(1);
97 protected void tearDown() throws Exception {
99 if (myDialogManager != null) {
100 myDialogManager.cleanup();
102 if (myVcsNotifier != null) {
103 myVcsNotifier.cleanup();
111 if (myAssertionsInTestDetected) {
112 TestLoggerFactory.dumpLogToStdout(myTestStartedIndicator);
118 private void enableDebugLogging() {
119 List<String> commonCategories = new ArrayList<String>(Arrays.asList("#" + Executor.class.getName(),
120 "#" + GitHandler.class.getName(),
121 GitHandler.class.getName()));
122 commonCategories.addAll(getDebugLogCategories());
123 TestLoggerFactory.enableDebugLogging(myTestRootDisposable, ArrayUtil.toStringArray(commonCategories));
124 myTestStartedIndicator = createTestStartedIndicator();
125 LOG.info(myTestStartedIndicator);
129 protected Collection<String> getDebugLogCategories() {
130 return Collections.emptyList();
134 private String createTestStartedIndicator() {
135 return "Starting " + getClass().getName() + "." + getTestName(false) + Math.random();
139 protected GitRepository createRepository(@NotNull String rootDir) {
140 return GitTestUtil.createRepository(myProject, rootDir);
144 * Clones the given source repository into a bare parent.git and adds the remote origin.
146 protected void prepareRemoteRepo(@NotNull GitRepository source) {
147 final String target = "parent.git";
148 final String targetName = "origin";
149 Executor.cd(myProjectRoot);
150 GitExecutor.git("clone --bare '%s' %s", source.getRoot().getPath(), target);
151 GitExecutor.cd(source);
152 GitExecutor.git("remote add %s '%s'", targetName, myProjectRoot + "/" + target);
155 protected void refresh() {
156 myProjectRoot.refresh(false, true);
159 protected void doActionSilently(final VcsConfiguration.StandardConfirmation op) {
160 AbstractVcsTestCase.setStandardConfirmation(myProject, GitVcs.NAME, op, VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY);
163 protected void updateChangeListManager() {
164 ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
165 VcsDirtyScopeManager.getInstance(myProject).markEverythingDirty();
166 changeListManager.ensureUpToDate(false);
169 protected void addSilently() {
170 doActionSilently(VcsConfiguration.StandardConfirmation.ADD);
173 protected void removeSilently() {
174 doActionSilently(VcsConfiguration.StandardConfirmation.REMOVE);