view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CatCommandTest.java @ 1027:10dc26b32c35

Update code according to new Java
author nikolai.kulakov@DESKTOP-Q4QCGIH
date Wed, 05 Aug 2020 13:19:53 +0300
parents 7bf4d943d5bb
children
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.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.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((root, hgPathProvider, workingDir) ->
            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((dir, name) -> name.startsWith("mercurial") && name.endsWith("catresult"));
  }

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