[git tests] refresh correctly: with marking dirty
[idea/community.git] / plugins / git4idea / tests / git4idea / test / GitPlatformTest.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package git4idea.test;
17
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;
41
42 import java.util.*;
43
44 public abstract class GitPlatformTest extends PlatformTestCase {
45
46   protected static final Logger LOG = Logger.getInstance(GitPlatformTest.class);
47
48   protected VirtualFile myProjectRoot;
49   protected String myProjectPath;
50   protected GitRepositoryManager myGitRepositoryManager;
51   protected GitVcsSettings myGitSettings;
52   protected GitPlatformFacade myPlatformFacade;
53   protected Git myGit;
54   protected GitVcs myVcs;
55
56   protected TestDialogManager myDialogManager;
57   protected TestVcsNotifier myVcsNotifier;
58
59   private String myTestStartedIndicator;
60
61   @Override
62   protected void setUp() throws Exception {
63     super.setUp();
64     enableDebugLogging();
65
66     myProjectRoot = myProject.getBaseDir();
67     myProjectPath = myProjectRoot.getPath();
68
69     myGitSettings = GitVcsSettings.getInstance(myProject);
70     myGitSettings.getAppSettings().setPathToGit(GitExecutor.PathHolder.GIT_EXECUTABLE);
71
72     myDialogManager = (TestDialogManager)ServiceManager.getService(DialogManager.class);
73     myVcsNotifier = (TestVcsNotifier)ServiceManager.getService(myProject, VcsNotifier.class);
74
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));
79     myVcs.doActivate();
80
81     GitTestUtil.assumeSupportedGitVersion(myVcs);
82     addSilently();
83     removeSilently();
84   }
85
86   @Override
87   @NotNull
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);
93     }
94     return name;
95   }
96
97   @Override
98   protected void tearDown() throws Exception {
99     try {
100       if (myDialogManager != null) {
101         myDialogManager.cleanup();
102       }
103       if (myVcsNotifier != null) {
104         myVcsNotifier.cleanup();
105       }
106     }
107     finally {
108       try {
109         super.tearDown();
110       }
111       finally {
112         if (myAssertionsInTestDetected) {
113           TestLoggerFactory.dumpLogToStdout(myTestStartedIndicator);
114         }
115       }
116     }
117   }
118
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);
127   }
128
129   @NotNull
130   protected Collection<String> getDebugLogCategories() {
131     return Collections.emptyList();
132   }
133
134   @NotNull
135   private String createTestStartedIndicator() {
136     return "Starting " + getClass().getName() + "." + getTestName(false) + Math.random();
137   }
138
139   @NotNull
140   protected GitRepository createRepository(@NotNull String rootDir) {
141     return GitTestUtil.createRepository(myProject, rootDir);
142   }
143
144   /**
145    * Clones the given source repository into a bare parent.git and adds the remote origin.
146    */
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);
154   }
155
156   protected void refresh() {
157     VfsUtil.markDirtyAndRefresh(false, true, false, myProjectRoot);
158   }
159
160   protected void doActionSilently(final VcsConfiguration.StandardConfirmation op) {
161     AbstractVcsTestCase.setStandardConfirmation(myProject, GitVcs.NAME, op, VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY);
162   }
163
164   protected void updateChangeListManager() {
165     ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
166     VcsDirtyScopeManager.getInstance(myProject).markEverythingDirty();
167     changeListManager.ensureUpToDate(false);
168   }
169
170   protected void addSilently() {
171     doActionSilently(VcsConfiguration.StandardConfirmation.ADD);
172   }
173
174   protected void removeSilently() {
175     doActionSilently(VcsConfiguration.StandardConfirmation.REMOVE);
176   }
177
178 }