bf6c44932ee223dcead468ef631a3e331a5462f6
[idea/community.git] / plugins / hg4idea / testSrc / org / zmlx / hg4idea / test / HgAbstractTestCase.java
1 // Copyright 2010 Victor Iacoban
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software distributed under
10 // the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11 // either express or implied. See the License for the specific language governing permissions and
12 // limitations under the License.
13 package org.zmlx.hg4idea.test;
14
15 import com.intellij.execution.process.ProcessOutput;
16 import com.intellij.openapi.application.PluginPathManager;
17 import com.intellij.openapi.vcs.VcsConfiguration;
18 import com.intellij.openapi.vcs.VcsShowConfirmationOption;
19 import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
20 import com.intellij.openapi.vfs.LocalFileSystem;
21 import com.intellij.openapi.vfs.VirtualFile;
22 import com.intellij.testFramework.AbstractVcsTestCase;
23 import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
24 import com.intellij.testFramework.fixtures.TempDirTestFixture;
25 import com.intellij.vcsUtil.VcsUtil;
26 import org.testng.annotations.BeforeMethod;
27 import org.zmlx.hg4idea.HgFile;
28 import org.zmlx.hg4idea.HgVcs;
29
30 import java.io.*;
31
32 import static org.testng.Assert.assertTrue;
33
34 /**
35  * The ancestor of all hg4idea test cases.
36  */
37 public abstract class HgAbstractTestCase extends AbstractVcsTestCase {
38
39   public static final String HG_EXECUTABLE_PATH = "IDEA_TEST_HG_EXECUTABLE_PATH";
40
41   protected File myProjectRepo;
42   protected TempDirTestFixture myTempDirTestFixture;
43   protected TestChangeListManager myChangeListManager;
44
45   // some shortcuts to use in tests
46   protected static final String AFILE = "a.txt";
47   protected static final String BDIR = "b";
48   protected static final String BFILE = "b.txt";
49   protected static final String BFILE_PATH = BDIR + File.separator + BFILE;
50   protected static final String FILE_CONTENT = "Sample file content.";
51   protected static final String FILE_CONTENT_2 = "some other file content";
52
53   @BeforeMethod
54   protected void setUp() throws Exception {
55     setHGExecutablePath();
56
57     myTempDirTestFixture = IdeaTestFixtureFactory.getFixtureFactory().createTempDirTestFixture();
58     myTempDirTestFixture.setUp();
59     myProjectRepo = new File(myTempDirTestFixture.getTempDirPath());
60
61     ProcessOutput processOutput = runHg(myProjectRepo, "init");
62     verify(processOutput);
63     initProject(myProjectRepo);
64     activateVCS(HgVcs.VCS_NAME);
65
66     myChangeListManager = new TestChangeListManager(myProject);
67
68     enableSilentOperation(VcsConfiguration.StandardConfirmation.ADD);
69     enableSilentOperation(VcsConfiguration.StandardConfirmation.REMOVE);
70   }
71
72   protected void setHGExecutablePath() {
73     // setting hg executable
74     String exec = System.getenv(HG_EXECUTABLE_PATH);
75     System.out.println("exec: " + exec);
76     if (exec != null) {
77       System.out.println("Using external");
78       myClientBinaryPath = new File(exec);
79     }
80     if (exec == null || !myClientBinaryPath.exists()) {
81       System.out.println("Using checked in");
82       File pluginRoot = new File(PluginPathManager.getPluginHomePath(HgVcs.VCS_NAME));
83       myClientBinaryPath = new File(pluginRoot, "testData/bin");
84     }
85
86     HgVcs.setTestHgExecutablePath(myClientBinaryPath.getPath());
87   }
88
89   /**
90    * Runs the hg command.
91    * @param commandLine the name of the command and its arguments.
92    */
93   protected ProcessOutput runHgOnProjectRepo(String... commandLine) throws IOException {
94     return runHg(myProjectRepo, commandLine);
95   }
96
97   /**
98    * Calls "hg add ." to add everything to the index.
99    */
100   protected ProcessOutput addAll() throws IOException {
101     return runHgOnProjectRepo("add", ".");
102   }
103
104   /**
105    * Calls "hg commit -m <commitMessage>" to commit the index.
106    */
107   protected ProcessOutput commitAll(String commitMessage) throws IOException {
108     return runHgOnProjectRepo("commit", "-m", commitMessage);
109   }
110
111   protected HgFile getHgFile(String... filepath) {
112     File fileToInclude = myProjectRepo;
113     for (String path : filepath) {
114       fileToInclude = new File(fileToInclude, path);
115     }
116     return new HgFile(myWorkingCopyDir, fileToInclude);
117   }
118
119   protected void enableSilentOperation(final VcsConfiguration.StandardConfirmation op) {
120     setStandardConfirmation(
121       HgVcs.VCS_NAME, op, VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY
122     );
123   }
124
125   protected void disableSilentOperation(final VcsConfiguration.StandardConfirmation op) {
126     setStandardConfirmation(
127       HgVcs.VCS_NAME, op, VcsShowConfirmationOption.Value.DO_NOTHING_SILENTLY
128     );
129   }
130
131   protected VirtualFile makeFile(File file) throws IOException {
132     file.createNewFile();
133     VcsDirtyScopeManager.getInstance(myProject).fileDirty(myWorkingCopyDir);
134     LocalFileSystem.getInstance().refresh(false);
135     return VcsUtil.getVirtualFile(file);
136   }
137
138   protected ProcessOutput runHg(File aHgRepository, String... commandLine) throws IOException {
139     return runClient(HgVcs.HG_EXECUTABLE_FILE_NAME, null, aHgRepository, commandLine);
140   }
141
142   protected File fillFile(File aParentDir, String[] filePath, String fileContents) throws FileNotFoundException {
143     File parentDir = aParentDir;
144     for (int i = 0; i < filePath.length - 1; i++) {
145       File current = new File(parentDir, filePath[i]);
146       if (!current.exists() || !current.isDirectory()) {
147         assertTrue(current.mkdir());
148       }
149       parentDir = current;
150     }
151     File outputFile = new File(parentDir, filePath[filePath.length - 1]);
152
153     PrintStream printer = new PrintStream(new FileOutputStream(outputFile));
154     printer.print(fileContents);
155     printer.close();
156
157     return outputFile;
158   }
159
160   /**
161    * Verifies the status of the file calling native 'hg status' command.
162    * @param status status as returned by {@link #added(java.lang.String)} and other methods.
163    * @throws IOException
164    */
165   protected void verifyStatus(String... status) throws IOException {
166     verify(runHgOnProjectRepo("status"), status);
167   }
168
169   public static String added(String... path) {
170     return "A " + path(path);
171   }
172
173   public static String removed(String... path) {
174     return "R " + path(path);
175   }
176
177   public static String unknown(String... path) {
178     return "? " + path(path);
179   }
180
181   public static String modified(String... path) {
182     return "M " + path(path);
183   }
184
185   public static String missing(String... path) {
186     return "! " + path(path);
187   }
188
189   public static String path(String... line) {
190     StringBuilder builder = new StringBuilder();
191
192     int linePartCount = line.length;
193
194     for (int i = 0; i < linePartCount; i++) {
195       String linePart = line[i];
196       builder.append(linePart);
197
198       if (i < linePartCount - 1) {
199         builder.append(File.separator);
200       }
201     }
202
203     return builder.toString();
204   }
205 }