view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/UnrelatedResitoriesTest.java @ 1020:a224ff290a9a

mark as node responsibilities aware
author Pavel Sher <pavel.sher@jetbrains.com>
date Wed, 22 Apr 2020 19:55:38 +0200
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;

import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.vcs.CheckoutRules;
import jetbrains.buildServer.vcs.RepositoryStateData;
import jetbrains.buildServer.vcs.VcsException;
import jetbrains.buildServer.vcs.impl.VcsRootImpl;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

import static jetbrains.buildServer.buildTriggers.vcs.mercurial.MercurialSupportBuilder.mercurialSupport;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

/**
 * @author dmitry.neverov
 */
@Test
public class UnrelatedResitoriesTest extends BaseMercurialTestCase {

  private final static String CURRENT_VERSION_OF_NEW_REPO = "4780519e01aa";
  private MercurialVcsSupport myVcs;
  private File myRepositoryLocation;
  private VcsRootImpl myRoot;
  private ServerPluginConfig myPluginConfig;

  @BeforeMethod
  public void setUp() throws Exception {
    super.setUp();
    myPluginConfig = new ServerPluginConfigBuilder()
            .cachesDir(myTempFiles.createTempDir())
            .build();

    myRepositoryLocation = myTempFiles.createTempDir();
    copyRepository(new File("mercurial-tests/testData/rep1"));
    myRoot = new VcsRootBuilder().withUrl(myRepositoryLocation.getCanonicalPath()).build();
  }


  public void should_be_able_to_sync_when_repository_became_unrelated() throws Exception {
    myVcs = createVcs();
    syncRepository();
    repositoryBecamesUnrelated();
    String currentVersion = syncRepository();
    assertEquals(CURRENT_VERSION_OF_NEW_REPO, currentVersion);
  }


  public void should_return_no_changes_when_fromRevision_is_from_unrelated_repository() throws Exception {
    myVcs = createVcs();
    String currentVersionOfOldRepo = syncRepository();
    repositoryBecamesUnrelated();
    String currentVersionOfNewRepo = syncRepository();
    assertTrue(myVcs.getCollectChangesPolicy().collectChanges(myRoot, currentVersionOfOldRepo, currentVersionOfNewRepo, CheckoutRules.DEFAULT).isEmpty());
  }


  private String syncRepository() throws VcsException {
    RepositoryStateData state = myVcs.getCollectChangesPolicy().getCurrentState(myRoot);
    return state.getBranchRevisions().get(state.getDefaultBranchName());
  }

  private void repositoryBecamesUnrelated() throws IOException {
    FileUtil.delete(myRepositoryLocation);
    copyRepository(new File("mercurial-tests/testData/rep2"));
  }

  private void copyRepository(@NotNull final File src) throws IOException {
    Util.copyRepository(src, myRepositoryLocation);
  }

  private MercurialVcsSupport createVcs() throws IOException {
    return mercurialSupport().withConfig(myPluginConfig).build();
  }
}