1 // Copyright 2010 Victor Iacoban
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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;
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;
32 import static org.testng.Assert.assertTrue;
35 * The ancestor of all hg4idea test cases.
37 public abstract class HgAbstractTestCase extends AbstractVcsTestCase {
39 public static final String HG_EXECUTABLE_PATH = "IDEA_TEST_HG_EXECUTABLE_PATH";
41 protected File myProjectRepo;
42 protected TempDirTestFixture myTempDirTestFixture;
43 protected TestChangeListManager myChangeListManager;
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";
54 protected void setUp() throws Exception {
55 setHGExecutablePath();
57 myTempDirTestFixture = IdeaTestFixtureFactory.getFixtureFactory().createTempDirTestFixture();
58 myTempDirTestFixture.setUp();
59 myProjectRepo = new File(myTempDirTestFixture.getTempDirPath());
61 ProcessOutput processOutput = runHg(myProjectRepo, "init");
62 verify(processOutput);
63 initProject(myProjectRepo);
64 activateVCS(HgVcs.VCS_NAME);
66 myChangeListManager = new TestChangeListManager(myProject);
68 enableSilentOperation(VcsConfiguration.StandardConfirmation.ADD);
69 enableSilentOperation(VcsConfiguration.StandardConfirmation.REMOVE);
72 protected void setHGExecutablePath() {
73 // setting hg executable
74 String exec = System.getenv(HG_EXECUTABLE_PATH);
75 System.out.println("exec: " + exec);
77 System.out.println("Using external");
78 myClientBinaryPath = new File(exec);
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");
86 HgVcs.setTestHgExecutablePath(myClientBinaryPath.getPath());
90 * Runs the hg command.
91 * @param commandLine the name of the command and its arguments.
93 protected ProcessOutput runHgOnProjectRepo(String... commandLine) throws IOException {
94 return runHg(myProjectRepo, commandLine);
98 * Calls "hg add ." to add everything to the index.
100 protected ProcessOutput addAll() throws IOException {
101 return runHgOnProjectRepo("add", ".");
105 * Calls "hg commit -m <commitMessage>" to commit the index.
107 protected ProcessOutput commitAll(String commitMessage) throws IOException {
108 return runHgOnProjectRepo("commit", "-m", commitMessage);
111 protected HgFile getHgFile(String... filepath) {
112 File fileToInclude = myProjectRepo;
113 for (String path : filepath) {
114 fileToInclude = new File(fileToInclude, path);
116 return new HgFile(myWorkingCopyDir, fileToInclude);
119 protected void enableSilentOperation(final VcsConfiguration.StandardConfirmation op) {
120 setStandardConfirmation(
121 HgVcs.VCS_NAME, op, VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY
125 protected void disableSilentOperation(final VcsConfiguration.StandardConfirmation op) {
126 setStandardConfirmation(
127 HgVcs.VCS_NAME, op, VcsShowConfirmationOption.Value.DO_NOTHING_SILENTLY
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);
138 protected ProcessOutput runHg(File aHgRepository, String... commandLine) throws IOException {
139 return runClient(HgVcs.HG_EXECUTABLE_FILE_NAME, null, aHgRepository, commandLine);
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());
151 File outputFile = new File(parentDir, filePath[filePath.length - 1]);
153 PrintStream printer = new PrintStream(new FileOutputStream(outputFile));
154 printer.print(fileContents);
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
165 protected void verifyStatus(String... status) throws IOException {
166 verify(runHgOnProjectRepo("status"), status);
169 public static String added(String... path) {
170 return "A " + path(path);
173 public static String removed(String... path) {
174 return "R " + path(path);
177 public static String unknown(String... path) {
178 return "? " + path(path);
181 public static String modified(String... path) {
182 return "M " + path(path);
185 public static String missing(String... path) {
186 return "! " + path(path);
189 public static String path(String... line) {
190 StringBuilder builder = new StringBuilder();
192 int linePartCount = line.length;
194 for (int i = 0; i < linePartCount; i++) {
195 String linePart = line[i];
196 builder.append(linePart);
198 if (i < linePartCount - 1) {
199 builder.append(File.separator);
203 return builder.toString();