don't try to run Windows binaries when running tests on non-Windows agents
[fedora-idea.git] / platform / testFramework / src / com / intellij / testFramework / AbstractVcsTestCase.java
blobe9be0f407fdacfdce5b42863b5adc8fa5f6e92a0
1 /*
2 * Copyright 2000-2007 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.
17 package com.intellij.testFramework;
19 import com.intellij.execution.process.CapturingProcessHandler;
20 import com.intellij.execution.process.ProcessOutput;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.command.WriteCommandAction;
23 import com.intellij.openapi.components.ProjectComponent;
24 import com.intellij.openapi.diff.LineTokenizer;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.util.Ref;
27 import com.intellij.openapi.util.SystemInfo;
28 import com.intellij.openapi.util.io.FileUtil;
29 import com.intellij.openapi.vcs.AbstractVcs;
30 import com.intellij.openapi.vcs.ProjectLevelVcsManager;
31 import com.intellij.openapi.vcs.VcsConfiguration;
32 import com.intellij.openapi.vcs.VcsShowConfirmationOption;
33 import com.intellij.openapi.vcs.changes.*;
34 import com.intellij.openapi.vfs.CharsetToolkit;
35 import com.intellij.openapi.vfs.LocalFileSystem;
36 import com.intellij.openapi.vfs.VirtualFile;
37 import com.intellij.psi.PsiNamedElement;
38 import com.intellij.testFramework.builders.EmptyModuleFixtureBuilder;
39 import com.intellij.testFramework.fixtures.IdeaProjectTestFixture;
40 import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
41 import com.intellij.testFramework.fixtures.TestFixtureBuilder;
42 import com.intellij.util.IncorrectOperationException;
43 import org.jetbrains.annotations.Nullable;
44 import org.junit.Assert;
46 import java.io.File;
47 import java.io.IOException;
48 import java.io.OutputStream;
49 import java.util.*;
51 /**
52 * @author yole
54 public class AbstractVcsTestCase {
55 protected Project myProject;
56 protected VirtualFile myWorkingCopyDir;
57 protected File myClientBinaryPath;
58 protected IdeaProjectTestFixture myProjectFixture;
60 protected ProcessOutput runClient(String exeName, @Nullable String stdin, @Nullable final File workingDir, String[] commandLine) throws IOException {
61 final List<String> arguments = new ArrayList<String>();
62 if (SystemInfo.isWindows) {
63 arguments.add(new File(myClientBinaryPath, exeName).toString());
65 else {
66 // assume client is in path
67 arguments.add(exeName);
69 Collections.addAll(arguments, commandLine);
70 final ProcessBuilder builder = new ProcessBuilder().command(arguments);
71 if (workingDir != null) {
72 builder.directory(workingDir);
74 Process clientProcess = builder.start();
76 if (stdin != null) {
77 OutputStream outputStream = clientProcess.getOutputStream();
78 try {
79 byte[] bytes = stdin.getBytes();
80 outputStream.write(bytes);
82 finally {
83 outputStream.close();
87 CapturingProcessHandler handler = new CapturingProcessHandler(clientProcess, CharsetToolkit.getDefaultSystemCharset());
88 ProcessOutput result = handler.runProcess(60*1000);
89 if (result.isTimeout()) {
90 throw new RuntimeException("Timeout waiting for VCS client to finish execution");
92 return result;
95 protected static ProcessOutput runArbitrary(final String command, final String[] args) throws IOException {
96 final List<String> arguments = new ArrayList<String>();
97 arguments.add(command);
98 Collections.addAll(arguments, args);
99 final ProcessBuilder builder = new ProcessBuilder().command(arguments);
100 Process clientProcess = builder.start();
102 CapturingProcessHandler handler = new CapturingProcessHandler(clientProcess, CharsetToolkit.getDefaultSystemCharset());
103 ProcessOutput result = handler.runProcess(60*1000);
104 if (result.isTimeout()) {
105 throw new RuntimeException("Timeout waiting for VCS client to finish execution");
107 return result;
110 protected void initProject(final File clientRoot) throws Exception {
111 final TestFixtureBuilder<IdeaProjectTestFixture> testFixtureBuilder = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder();
112 myProjectFixture = testFixtureBuilder.getFixture();
113 testFixtureBuilder.addModule(EmptyModuleFixtureBuilder.class).addContentRoot(clientRoot.toString());
114 myProjectFixture.setUp();
115 myProject = myProjectFixture.getProject();
117 ((ProjectComponent) ChangeListManager.getInstance(myProject)).projectOpened();
118 ((ProjectComponent) VcsDirtyScopeManager.getInstance(myProject)).projectOpened();
120 ApplicationManager.getApplication().runWriteAction(new Runnable() {
121 public void run() {
122 myWorkingCopyDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(clientRoot);
123 assert myWorkingCopyDir != null;
128 protected void activateVCS(final String vcsName) {
129 ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(myProject);
130 vcsManager.setDirectoryMapping(myWorkingCopyDir.getPath(), vcsName);
131 vcsManager.updateActiveVcss();
133 AbstractVcs vcs = vcsManager.findVcsByName(vcsName);
134 Assert.assertEquals(1, vcsManager.getRootsUnderVcs(vcs).length);
137 protected VirtualFile createFileInCommand(final String name, @Nullable final String content) {
138 return createFileInCommand(myWorkingCopyDir, name, content);
141 protected VirtualFile createFileInCommand(final VirtualFile parent, final String name, @Nullable final String content) {
142 final Ref<VirtualFile> result = new Ref<VirtualFile>();
143 new WriteCommandAction.Simple(myProject) {
144 protected void run() throws Throwable {
145 try {
146 VirtualFile file = parent.createChildData(this, name);
147 if (content != null) {
148 file.setBinaryContent(CharsetToolkit.getUtf8Bytes(content));
150 result.set(file);
152 catch (IOException e) {
153 throw new RuntimeException(e);
156 }.execute();
157 return result.get();
160 protected VirtualFile createDirInCommand(final VirtualFile parent, final String name) {
161 final Ref<VirtualFile> result = new Ref<VirtualFile>();
162 new WriteCommandAction.Simple(myProject) {
163 protected void run() throws Throwable {
164 try {
165 VirtualFile dir = parent.createChildDirectory(this, name);
166 result.set(dir);
168 catch (IOException e) {
169 throw new RuntimeException(e);
172 }.execute();
173 return result.get();
176 protected void tearDownProject() throws Exception {
177 if (myProject != null) {
178 ((ProjectComponent) VcsDirtyScopeManager.getInstance(myProject)).projectClosed();
179 ((ProjectComponent) ChangeListManager.getInstance(myProject)).projectClosed();
180 ((ProjectComponent) ProjectLevelVcsManager.getInstance(myProject)).projectClosed();
181 myProject = null;
183 if (myProjectFixture != null) {
184 myProjectFixture.tearDown();
185 myProjectFixture = null;
189 protected void setStandardConfirmation(final String vcsName, final VcsConfiguration.StandardConfirmation op,
190 final VcsShowConfirmationOption.Value value) {
191 ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(myProject);
192 final AbstractVcs vcs = vcsManager.findVcsByName(vcsName);
193 VcsShowConfirmationOption option = vcsManager.getStandardConfirmation(op, vcs);
194 option.setValue(value);
197 protected static void verify(final ProcessOutput runResult) {
198 Assert.assertEquals(runResult.getStderr(), 0, runResult.getExitCode());
201 protected static void verify(final ProcessOutput runResult, final String... stdoutLines) {
202 verify(runResult, false, stdoutLines);
205 protected static void verifySorted(final ProcessOutput runResult, final String... stdoutLines) {
206 verify(runResult, true, stdoutLines);
209 private static void verify(final ProcessOutput runResult, final boolean sorted, final String... stdoutLines) {
210 verify(runResult);
211 final String[] lines = new LineTokenizer(runResult.getStdout()).execute();
212 if (sorted) {
213 Arrays.sort(lines);
215 Assert.assertEquals(runResult.getStdout(), stdoutLines.length, lines.length);
216 for(int i=0; i<stdoutLines.length; i++) {
217 Assert.assertEquals(stdoutLines [i], compressWhitespace(lines [i]));
221 private static String compressWhitespace(String line) {
222 while(line.indexOf(" ") > 0) {
223 line = line.replace(" ", " ");
225 return line.trim();
228 protected VcsDirtyScope getAllDirtyScope() {
229 VcsDirtyScopeManager dirtyScopeManager = VcsDirtyScopeManager.getInstance(myProject);
230 dirtyScopeManager.markEverythingDirty();
231 List<VcsDirtyScope> scopes = dirtyScopeManager.retrieveScopes().getScopes();
232 Assert.assertEquals(1, scopes.size());
233 return scopes.get(0);
236 protected VcsDirtyScope getDirtyScopeForFile(VirtualFile file) {
237 VcsDirtyScopeManager dirtyScopeManager = VcsDirtyScopeManager.getInstance(myProject);
238 dirtyScopeManager.retrieveScopes(); // ensure that everything besides the file is clean
239 dirtyScopeManager.fileDirty(file);
240 List<VcsDirtyScope> scopes = dirtyScopeManager.retrieveScopes().getScopes();
241 Assert.assertEquals(1, scopes.size());
242 return scopes.get(0);
245 protected void renameFileInCommand(final VirtualFile file, final String newName) {
246 renameFileInCommand(myProject, file, newName);
249 public static void renameFileInCommand(final Project project, final VirtualFile file, final String newName) {
250 new WriteCommandAction.Simple(project) {
251 protected void run() throws Throwable {
252 try {
253 file.rename(this, newName);
255 catch (IOException e) {
256 throw new RuntimeException(e);
259 }.execute();
262 protected void renamePsiInCommand(final PsiNamedElement element, final String newName) {
263 new WriteCommandAction.Simple(myProject) {
264 protected void run() throws Throwable {
265 try {
266 element.setName(newName);
268 catch (IncorrectOperationException e) {
269 throw new RuntimeException(e);
272 }.execute();
275 protected void deleteFileInCommand(final VirtualFile file) {
276 deleteFileInCommand(myProject, file);
279 public static void deleteFileInCommand(final Project project, final VirtualFile file) {
280 new WriteCommandAction.Simple(project) {
281 protected void run() throws Throwable {
282 try {
283 file.delete(this);
285 catch(IOException ex) {
286 throw new RuntimeException(ex);
289 }.execute();
292 public static void editFileInCommand(final Project project, final VirtualFile file, final String newContent) {
293 new WriteCommandAction.Simple(project) {
294 protected void run() throws Throwable {
295 try {
296 file.setBinaryContent(newContent.getBytes());
298 catch(IOException ex) {
299 throw new RuntimeException(ex);
302 }.execute();
305 protected void copyFileInCommand(final VirtualFile file, final String toName) {
306 new WriteCommandAction.Simple(myProject) {
307 protected void run() throws Throwable {
308 try {
309 file.copy(this, file.getParent(), toName);
311 catch (IOException e) {
312 throw new RuntimeException(e);
315 }.execute();
318 protected void moveFileInCommand(final VirtualFile file, final VirtualFile newParent) {
319 moveFileInCommand(myProject, file, newParent);
322 public static void moveFileInCommand(final Project project, final VirtualFile file, final VirtualFile newParent) {
323 new WriteCommandAction.Simple(project) {
324 protected void run() throws Throwable {
325 try {
326 file.move(this, newParent);
328 catch (IOException e) {
329 throw new RuntimeException(e);
332 }.execute();
335 protected void verifyChange(final Change c, final String beforePath, final String afterPath) {
336 if (beforePath == null) {
337 Assert.assertNull(c.getBeforeRevision());
339 else {
340 verifyRevision(c.getBeforeRevision(), beforePath);
342 if (afterPath == null) {
343 Assert.assertNull(c.getAfterRevision());
345 else {
346 verifyRevision(c.getAfterRevision(), afterPath);
350 private void verifyRevision(final ContentRevision beforeRevision, final String beforePath) {
351 File beforeFile = new File(myWorkingCopyDir.getPath(), beforePath);
352 String beforeFullPath = FileUtil.toSystemIndependentName(beforeFile.getPath());
353 final String beforeRevPath = FileUtil.toSystemIndependentName(beforeRevision.getFile().getPath());
354 Assert.assertTrue(beforeFullPath.equalsIgnoreCase(beforeRevPath));
357 public static void sortChanges(final List<Change> changes) {
358 Collections.sort(changes, new Comparator<Change>() {
359 public int compare(final Change o1, final Change o2) {
360 final String p1 = FileUtil.toSystemIndependentName(ChangesUtil.getFilePath(o1).getPath());
361 final String p2 = FileUtil.toSystemIndependentName(ChangesUtil.getFilePath(o2).getPath());
362 return p1.compareTo(p2);