summaryrefslogtreecommitdiff
path: root/plugins/git4idea/tests/git4idea/test/GitPlatformTest.java
blob: c02ec553dd8f71c44ebe2e364b50d3dded0fe422 (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package git4idea.test;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.*;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.testFramework.TestLoggerFactory;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.testFramework.fixtures.IdeaProjectTestFixture;
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
import com.intellij.testFramework.vcs.AbstractVcsTestCase;
import com.intellij.util.ObjectUtils;
import git4idea.DialogManager;
import git4idea.GitPlatformFacade;
import git4idea.GitUtil;
import git4idea.GitVcs;
import git4idea.commands.Git;
import git4idea.commands.GitHandler;
import git4idea.config.GitVcsSettings;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

public abstract class GitPlatformTest extends UsefulTestCase {

  static {
    Logger.setFactory(TestLoggerFactory.class);
  }

  private static final Logger LOG = Logger.getInstance(GitPlatformTest.class);

  protected Project myProject;
  protected VirtualFile myProjectRoot;
  protected String myProjectPath;
  protected GitRepositoryManager myGitRepositoryManager;
  protected GitVcsSettings myGitSettings;
  protected GitPlatformFacade myPlatformFacade;
  protected Git myGit;
  protected GitVcs myVcs;

  protected TestDialogManager myDialogManager;
  protected TestVcsNotifier myVcsNotifier;

  private IdeaProjectTestFixture myProjectFixture;
  private String myTestStartedIndicator;

  @SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors", "UnusedDeclaration"})
  protected GitPlatformTest() {
    PlatformTestCase.initPlatformLangPrefix();
    GitTestUtil.setDefaultBuiltInServerPort();
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    enableDebugLogging();

    try {
      myProjectFixture = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getTestName(true)).getFixture();
      myProjectFixture.setUp();
    }
    catch (Exception e) {
      super.tearDown();
      throw e;
    }

    try {
      myProject = myProjectFixture.getProject();
      myProjectRoot = myProject.getBaseDir();
      myProjectPath = myProjectRoot.getPath();

      myGitSettings = GitVcsSettings.getInstance(myProject);
      myGitSettings.getAppSettings().setPathToGit(GitExecutor.PathHolder.GIT_EXECUTABLE);

      myDialogManager = (TestDialogManager)ServiceManager.getService(DialogManager.class);
      myVcsNotifier = (TestVcsNotifier)ServiceManager.getService(myProject, VcsNotifier.class);

      myGitRepositoryManager = GitUtil.getRepositoryManager(myProject);
      myPlatformFacade = ServiceManager.getService(myProject, GitPlatformFacade.class);
      myGit = ServiceManager.getService(myProject, Git.class);
      myVcs = ObjectUtils.assertNotNull(GitVcs.getInstance(myProject));
      myVcs.doActivate();

      GitTestUtil.assumeSupportedGitVersion(myVcs);
      initChangeListManager();
      addSilently();
      removeSilently();
    }
    catch (Exception e) {
      tearDown();
      throw e;
    }
  }

  @Override
  @NotNull
  public String getTestName(boolean lowercaseFirstLetter) {
    String name = super.getTestName(lowercaseFirstLetter);
    name = StringUtil.shortenTextWithEllipsis(name.trim().replace(" ", "_"), 12, 6, "_");
    if (name.startsWith("_")) {
      name = name.substring(1);
    }
    return name;
  }

  private void initChangeListManager() {
    ((ProjectComponent) ChangeListManager.getInstance(myProject)).projectOpened();
    ((ProjectComponent) VcsDirtyScopeManager.getInstance(myProject)).projectOpened();
  }

  @Override
  protected void tearDown() throws Exception {
    try {
      if (myDialogManager != null) {
        myDialogManager.cleanup();
      }
      if (myVcsNotifier != null) {
        myVcsNotifier.cleanup();
      }
      if (myProjectFixture != null) {
        myProjectFixture.tearDown();
      }
    }
    finally {
      try {
        String tempTestIndicator = myTestStartedIndicator;
        clearFields(this);
        myTestStartedIndicator = tempTestIndicator;
      }
      finally {
        super.tearDown();
      }
    }
  }

  private void enableDebugLogging() {
    TestLoggerFactory.enableDebugLogging(myTestRootDisposable, "#" + Executor.class.getName(),
                                         "#" + GitHandler.class.getName(),
                                         GitHandler.class.getName());
    myTestStartedIndicator = createTestStartedIndicator();
    LOG.info(myTestStartedIndicator);
  }

  @Override
  protected void defaultRunBare() throws Throwable {
    try {
      super.defaultRunBare();
    }
    catch (Throwable throwable) {
      try {
        if (myTestStartedIndicator != null) {
          TestLoggerFactory.dumpLogToStdout(myTestStartedIndicator);
        }
        throw throwable;
      }
      catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  }

  @NotNull
  private String createTestStartedIndicator() {
    return "Starting " + getClass().getName() + "." + getTestName(false) + Math.random();
  }

  @NotNull
  protected GitRepository createRepository(@NotNull String rootDir) {
    return GitTestUtil.createRepository(myProject, rootDir);
  }

  /**
   * Clones the given source repository into a bare parent.git and adds the remote origin.
   */
  protected void prepareRemoteRepo(@NotNull GitRepository source) {
    final String target = "parent.git";
    final String targetName = "origin";
    Executor.cd(myProjectRoot);
    GitExecutor.git("clone --bare '%s' %s", source.getRoot().getPath(), target);
    GitExecutor.cd(source);
    GitExecutor.git("remote add %s '%s'", targetName, myProjectRoot + "/" + target);
  }

  protected void refresh() {
    myProjectRoot.refresh(false, true);
  }

  protected void doActionSilently(final VcsConfiguration.StandardConfirmation op) {
    AbstractVcsTestCase.setStandardConfirmation(myProject, GitVcs.NAME, op, VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY);
  }

  protected void updateChangeListManager() {
    ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
    VcsDirtyScopeManager.getInstance(myProject).markEverythingDirty();
    changeListManager.ensureUpToDate(false);
  }

  protected void addSilently() {
    doActionSilently(VcsConfiguration.StandardConfirmation.ADD);
  }

  protected void removeSilently() {
    doActionSilently(VcsConfiguration.StandardConfirmation.REMOVE);
  }

}