avoid leaks from failing HgConfigTest.setUp
[idea/community.git] / plugins / hg4idea / testSrc / hg4idea / test / config / HgConfigTest.java
1 package hg4idea.test.config;
2
3 import com.intellij.openapi.util.io.FileUtil;
4 import com.intellij.openapi.vcs.VcsTestUtil;
5 import hg4idea.test.HgPlatformTest;
6 import org.zmlx.hg4idea.util.HgUtil;
7
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.Arrays;
11 import java.util.Collection;
12
13 import static com.intellij.openapi.vcs.Executor.cd;
14
15 /**
16  * @author Nadya Zabrodina
17  */
18 public class HgConfigTest extends HgPlatformTest {
19
20   @Override
21   public void setUp() throws Exception {
22     super.setUp();
23     try {
24       prepareSecondRepository();
25     }
26     catch (Throwable e) {
27       tearDown();
28     }
29   }
30
31   public void testDefaultPathInClonedRepo() {
32     cd(myChildRepo);
33     final String defaultPath = HgUtil.getRepositoryDefaultPath(myProject, myChildRepo);
34     assertNotNull(defaultPath);
35     assertEquals(myRepository.getPath(),
36                  FileUtil.toSystemIndependentName(defaultPath));
37   }
38
39   public void testPushPathInClonedRepo() throws IOException {
40     checkDefaultPushPath();
41   }
42
43   public void testPushPathInClonedRepoWithDebugOption() throws IOException {
44     cd(myChildRepo);
45     appendToHgrc(myChildRepo, "\n[ui]\n" +
46                               "debug=True");
47     checkDefaultPushPath();
48   }
49
50   private void checkDefaultPushPath() throws IOException {
51     cd(myChildRepo);
52     String pushPath = "somePath";
53     appendToHgrc(myChildRepo, "\n[paths]\n" +
54                               "default-push=" + pushPath);
55     updateRepoConfig(myProject, myChildRepo);
56     final String defaultPushPath = HgUtil.getRepositoryDefaultPushPath(myProject, myChildRepo);
57     assertNotNull(defaultPushPath);
58     assertEquals(FileUtil.toSystemIndependentName(myChildRepo.getPath() + "/" + pushPath),
59                  FileUtil.toSystemIndependentName(defaultPushPath));
60   }
61
62   public void testPushPathWithoutAppropriateConfig() {
63     cd(myChildRepo);
64     final String defaultPushPath = HgUtil.getRepositoryDefaultPushPath(myProject, myChildRepo);
65     assertNotNull(defaultPushPath);
66     assertEquals(myRepository.getPath(),
67                  FileUtil.toSystemIndependentName(defaultPushPath));
68   }
69
70   public void testMultiPathConfig() throws IOException {
71     cd(myChildRepo);
72     final String path1 = "https://bitbucket.org/nadushnik/hgtestrepo";
73     final String path2 = "https://bitbucket.org/nadushnik/javarepo";
74     final String path3 = "https://bitbucket.org/nadushnik/hgTestRepo2";
75     appendToHgrc(myChildRepo, "\n[paths]" +
76                               "\npath1=" + path1 +
77                               "\npath2=" + path2 +
78                               "\npath3=" + path3);
79     updateRepoConfig(myProject, myChildRepo);
80     final Collection<String> paths = HgUtil.getRepositoryPaths(myProject, myChildRepo);
81     final Collection<String> expectedPaths = Arrays.asList(FileUtil.toSystemDependentName(myRepository.getPath()), path1, path2, path3);
82     VcsTestUtil.assertEqualCollections(paths, expectedPaths);
83   }
84
85   public void testLargeExtensionInClonedRepo() throws IOException {
86     cd(myChildRepo);
87     File hgrc = new File(new File(myChildRepo.getPath(), ".hg"), "hgrc");
88     assert hgrc.exists();
89     FileUtil.appendToFile(hgrc, "\n[extensions]\n" +
90                                 "largefiles =");
91     updateRepoConfig(myProject, myChildRepo);
92     assertNotNull(HgUtil.getConfig(myProject, myChildRepo, "extensions", "largefiles"));
93   }
94 }