package hg4idea.test.config;

import com.intellij.openapi.util.io.FileUtil;
import hg4idea.test.HgPlatformTest;
import org.zmlx.hg4idea.util.HgUtil;

import java.io.File;
import java.io.IOException;

import static com.intellij.dvcs.test.Executor.cd;

/**
 * @author Nadya Zabrodina
 */
public class HgConfigTest extends HgPlatformTest {

  @Override
  public void setUp() throws Exception {
    super.setUp();
    prepareSecondRepository();
  }

  public void testDefaultPathInClonedRepo() {
    cd(myChildRepo);
    final String defaultPath = HgUtil.getRepositoryDefaultPath(myProject, myChildRepo);
    assertNotNull(defaultPath);
    assertEquals(myRepository.getPath(),
                 FileUtil.toSystemIndependentName(defaultPath));
  }

  public void testPushPathInClonedRepo() {
    cd(myChildRepo);
    String pushPath = "somePath";
    appendToHgrc(myChildRepo, "\n[paths]\n" +
                              "default-push=" + pushPath);
    updateRepoConfig(myProject, myChildRepo);
    final String defaultPushPath = HgUtil.getRepositoryDefaultPushPath(myProject, myChildRepo);
    assertNotNull(defaultPushPath);
    assertEquals(FileUtil.toSystemIndependentName(myChildRepo.getPath() + "/" + pushPath),
                 FileUtil.toSystemIndependentName(defaultPushPath));
  }

  public void testPushPathWithoutAppropriateConfig() {
    cd(myChildRepo);
    final String defaultPushPath = HgUtil.getRepositoryDefaultPushPath(myProject, myChildRepo);
    assertNotNull(defaultPushPath);
    assertEquals(myRepository.getPath(),
                 FileUtil.toSystemIndependentName(defaultPushPath));
  }

  public void testLargeExtensionInClonedRepo() {
    cd(myChildRepo);
    File hgrc = new File(new File(myChildRepo.getPath(), ".hg"), "hgrc");
    assert hgrc.exists();
    try {
      FileUtil.appendToFile(hgrc, "\n[extensions]\n" +
                                  "largefiles =");
    }
    catch (IOException e) {
      e.printStackTrace();
      fail("Can not update hgrc file.");
    }
    updateRepoConfig(myProject, myChildRepo);
    assertNotNull(HgUtil.getRepositoryNamedConfig(myProject, myChildRepo, "extensions", "largefiles"));
  }
}

@@ -1644,35 +1644,54 @@
 onInClonedRepo()
+ throws IOException
  %7B%0A
-
     cd(myChildRe
@@ -1799,20 +1799,8 @@
 ();%0A
-    try %7B%0A  
     
@@ -1880,18 +1880,16 @@
         
-  
 %22largefi
@@ -1901,116 +1901,8 @@
 %22);%0A
-    %7D%0A    catch (IOException e) %7B%0A      e.printStackTrace();%0A      fail(%22Can not update hgrc file.%22);%0A    %7D%0A
     

remove try-catch block from test