view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CatCommandTest.java @ 976:7bf4d943d5bb

Update copyright
author pavel.sher
date Mon, 22 Jan 2018 11:39:20 +0100
parents 25482087e9b5
children 10dc26b32c35
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;

import jetbrains.buildServer.buildTriggers.vcs.mercurial.HgPathProvider;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.exception.UnknownFileException;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;

import static com.intellij.openapi.util.io.FileUtil.delete;
import static java.util.Arrays.asList;

/**
 * @author dmitry.neverov
 */
@Test
public class CatCommandTest extends BaseCommandTestCase {

  //TW-13178
  public void command_should_not_leave_garbage_in_temp_dir() throws IOException, VcsException {
    cleanCatResultDirs();
    setRepository("mercurial-tests/testData/rep1", true);
    final String nonExisting = "/non/existing/path";
    try {
      runCat(asList(nonExisting));
      fail("exception should be thrown for non-existing path");
    } catch (UnknownFileException e) {
      assertEquals(nonExisting, e.getPath());
      checkTempDirDoesNotContainCatResults();
    }
  }

  public void should_not_throw_exception_if_not_asked_to() throws IOException, VcsException {
    cleanCatResultDirs();
    setRepository("mercurial-tests/testData/rep1", true);
    runCommand(new CommandExecutor<File>() {
      public File execute(@NotNull HgVcsRoot root, @NotNull HgPathProvider hgPathProvider, @NotNull File workingDir) throws VcsException {
        return new CatCommand(new CommandSettings(), hgPathProvider.getHgPath(root), workingDir, root.getAuthSettings()).files("/non/existing/path").checkForFailure(false).call();
      }
    });
  }

  private void cleanCatResultDirs() {
    for (File f : getCatResultDirs())
      delete(f);
  }

  private void checkTempDirDoesNotContainCatResults() throws IOException {
    File[] catresults = getCatResultDirs();
    assertTrue("cat result dirs are not cleaned: " + asList(catresults), catresults.length == 0);
  }

  private File[] getCatResultDirs() {
    String tempDirPath = FileUtil.getTempDirectory();
    return new File(tempDirPath).listFiles(new FilenameFilter() {
      public boolean accept(File dir, String name) {
        return name.startsWith("mercurial") && name.endsWith("catresult");
      }
    });
  }

  @NotNull
  private File runCat(@NotNull final List<String> paths) throws IOException, VcsException {
    return runCommand(new CommandExecutor<File>() {
      public File execute(@NotNull HgVcsRoot root, @NotNull HgPathProvider hgPathProvider, @NotNull File workingDir) throws VcsException {
        final CatCommand cat = new CatCommand(new CommandSettings(), hgPathProvider.getHgPath(root), workingDir, root.getAuthSettings());
        return cat.files(paths).checkForFailure(true).call();
      }
    });
  }
}