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.VfsUtil;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.testFramework.PlatformTestCase;
27 import com.intellij.testFramework.TestLoggerFactory;
28 import com.intellij.testFramework.vcs.AbstractVcsTestCase;
29 import com.intellij.util.ArrayUtil;
30 import com.intellij.util.ObjectUtils;
31 import git4idea.DialogManager;
32 import git4idea.GitPlatformFacade;
33 import git4idea.GitUtil;
34 import git4idea.GitVcs;
35 import git4idea.commands.Git;
36 import git4idea.commands.GitHandler;
37 import git4idea.config.GitVcsSettings;
38 import git4idea.repo.GitRepository;
39 import git4idea.repo.GitRepositoryManager;
40 import org.jetbrains.annotations.NotNull;
44 public abstract class GitPlatformTest extends PlatformTestCase {
46 protected static final Logger LOG = Logger.getInstance(GitPlatformTest.class);
48 protected VirtualFile myProjectRoot;
49 protected String myProjectPath;
50 protected GitRepositoryManager myGitRepositoryManager;
51 protected GitVcsSettings myGitSettings;
52 protected GitPlatformFacade myPlatformFacade;
54 protected GitVcs myVcs;
56 protected TestDialogManager myDialogManager;
57 protected TestVcsNotifier myVcsNotifier;
59 private String myTestStartedIndicator;
62 protected void setUp() throws Exception {
66 myProjectRoot = myProject.getBaseDir();
67 myProjectPath = myProjectRoot.getPath();
69 myGitSettings = GitVcsSettings.getInstance(myProject);
70 myGitSettings.getAppSettings().setPathToGit(GitExecutor.PathHolder.GIT_EXECUTABLE);
72 myDialogManager = (TestDialogManager)ServiceManager.getService(DialogManager.class);
73 myVcsNotifier = (TestVcsNotifier)ServiceManager.getService(myProject, VcsNotifier.class);
75 myGitRepositoryManager = GitUtil.getRepositoryManager(myProject);
76 myPlatformFacade = ServiceManager.getService(myProject, GitPlatformFacade.class);
77 myGit = ServiceManager.getService(myProject, Git.class);
78 myVcs = ObjectUtils.assertNotNull(GitVcs.getInstance(myProject));
81 GitTestUtil.assumeSupportedGitVersion(myVcs);
88 public String getTestName(boolean lowercaseFirstLetter) {
89 String name = super.getTestName(lowercaseFirstLetter);
90 name = StringUtil.shortenTextWithEllipsis(name.trim().replace(" ", "_"), 12, 6, "_");
91 if (name.startsWith("_")) {
92 name = name.substring(1);
98 protected void tearDown() throws Exception {
100 if (myDialogManager != null) {
101 myDialogManager.cleanup();
103 if (myVcsNotifier != null) {
104 myVcsNotifier.cleanup();
112 if (myAssertionsInTestDetected) {
113 TestLoggerFactory.dumpLogToStdout(myTestStartedIndicator);
119 private void enableDebugLogging() {
120 List<String> commonCategories = new ArrayList<String>(Arrays.asList("#" + Executor.class.getName(),
121 "#" + GitHandler.class.getName(),
122 GitHandler.class.getName()));
123 commonCategories.addAll(getDebugLogCategories());
124 TestLoggerFactory.enableDebugLogging(myTestRootDisposable, ArrayUtil.toStringArray(commonCategories));
125 myTestStartedIndicator = createTestStartedIndicator();
126 LOG.info(myTestStartedIndicator);
130 protected Collection<String> getDebugLogCategories() {
131 return Collections.emptyList();
135 private String createTestStartedIndicator() {
136 return "Starting " + getClass().getName() + "." + getTestName(false) + Math.random();
140 protected GitRepository createRepository(@NotNull String rootDir) {
141 return GitTestUtil.createRepository(myProject, rootDir);
145 * Clones the given source repository into a bare parent.git and adds the remote origin.
147 protected void prepareRemoteRepo(@NotNull GitRepository source) {
148 final String target = "parent.git";
149 final String targetName = "origin";
150 Executor.cd(myProjectRoot);
151 GitExecutor.git("clone --bare '%s' %s", source.getRoot().getPath(), target);
152 GitExecutor.cd(source);
153 GitExecutor.git("remote add %s '%s'", targetName, myProjectRoot + "/" + target);
156 protected void refresh() {
157 VfsUtil.markDirtyAndRefresh(false, true, false, myProjectRoot);
160 protected void doActionSilently(final VcsConfiguration.StandardConfirmation op) {
161 AbstractVcsTestCase.setStandardConfirmation(myProject, GitVcs.NAME, op, VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY);
164 protected void updateChangeListManager() {
165 ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
166 VcsDirtyScopeManager.getInstance(myProject).markEverythingDirty();
167 changeListManager.ensureUpToDate(false);
170 protected void addSilently() {
171 doActionSilently(VcsConfiguration.StandardConfirmation.ADD);
174 protected void removeSilently() {
175 doActionSilently(VcsConfiguration.StandardConfirmation.REMOVE);