view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/BookmarksTest.java @ 1124:a14a2f7e74d8 development/2024.03.x tip

2024.03.x branch is created
author Nadia Burnasheva <nadezhda.burnasheva@jetbrains.com>
date Thu, 15 Feb 2024 11:33:35 +0100
parents 10dc26b32c35
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.vcs.RepositoryStateData;

import jetbrains.buildServer.vcs.VcsRoot;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.*;

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

import static com.intellij.openapi.util.io.FileUtil.delete;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.BookmarksTest.RepositoryStateDataMatcher.hasBranch;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.BookmarksTest.RepositoryStateDataMatcher.hasNoBranch;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.MercurialSupportBuilder.mercurialSupport;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.ServerPluginConfigBuilder.serverPluginConfig;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.VcsRootBuilder.vcsRoot;
import static org.hamcrest.MatcherAssert.assertThat;


@RequiredHgVersion(min = "2.4")
@Test(dataProviderClass = HgVersionConstraint.class, dataProvider = "installedHgVersion")
public class BookmarksTest extends BaseMercurialTestCase {

  private File myRemoteRepository;
  private ServerPluginConfig myConfig;
  private MercurialVcsSupport myVcs;
  private VcsRoot myRoot;

  @BeforeMethod
  public void setUp() throws Exception {
    super.setUp();
    myRemoteRepository = myTempFiles.createTempDir();
    myConfig = serverPluginConfig()
            .cachesDir(myTempFiles.createTempDir())
            .hgPath(Util.getHgPath())
            .build();
    myVcs = mercurialSupport().withConfig(myConfig).build();

    myRoot = vcsRoot().withUrl(myRemoteRepository.getAbsolutePath()).build();
  }


  public void current_state_should_include_bookmarks(@NotNull HgVersion version) throws Exception {
    setupRemoteRepositoryWithTwoBookmarks();
    RepositoryStateData state = myVcs.getCollectChangesPolicy().getCurrentState(myRoot);
    assertThat(state, hasBranch("bookmark1").withRevision("15b4a116520d"));
    assertThat(state, hasBranch("bookmark2").withRevision("e9f8f67888f5"));
  }


  public void remove_bookmark_when_it_is_removed_from_remote_repo(@NotNull HgVersion version) throws Exception {
    cloneRepositoryWithTwoBookmarks();

    allBookmarksRemoved();

    RepositoryStateData state = myVcs.getCollectChangesPolicy().getCurrentState(myRoot);
    assertThat(state, hasNoBranch("bookmark1"));
    assertThat(state, hasNoBranch("bookmark2"));
  }


  public void update_bookmark_when_it_is_updated_in_remote_repo(@NotNull HgVersion version) throws Exception {
    cloneRepositoryWithTwoBookmarks();

    bookmark1Updated();

    RepositoryStateData state = myVcs.getCollectChangesPolicy().getCurrentState(myRoot);
    assertThat(state, hasBranch("bookmark1").withRevision("7597a4da7195"));
  }


  public void update_bookmark_when_it_is_deverged_in_remote_repo(@NotNull HgVersion version) throws Exception {
    cloneRepositoryWithTwoBookmarks();

    bookmark1Diverged();

    RepositoryStateData state = myVcs.getCollectChangesPolicy().getCurrentState(myRoot);
    assertThat(state, hasBranch("bookmark1").withRevision("e9f8f67888f5"));
  }


  public void prefer_branch_to_bookmark_when_their_names_collide(@NotNull HgVersion version) throws Exception {
    cloneRepositoryWithTwoBookmarks();

    branchWithNameBookmark2Created();

    RepositoryStateData state = myVcs.getCollectChangesPolicy().getCurrentState(myRoot);
    assertThat(state, hasBranch("bookmark2").withRevision("f63a14e873d6"));
  }


  private void setupRemoteRepositoryWithTwoBookmarks() throws IOException {
    Util.copyRepository(new File("mercurial-tests/testData/bookmarks/1"), myRemoteRepository);
  }

  private void cloneRepositoryWithTwoBookmarks() throws Exception {
    setupRemoteRepositoryWithTwoBookmarks();
    myVcs.getCollectChangesPolicy().getCurrentState(myRoot);
  }

  private void bookmark1Updated() throws IOException {
    updateRepository("mercurial-tests/testData/bookmarks/2");
  }

  private void allBookmarksRemoved() throws IOException {
    updateRepository("mercurial-tests/testData/bookmarks/3");
  }

  private void bookmark1Diverged() throws IOException {
    updateRepository("mercurial-tests/testData/bookmarks/4");
  }

  private void branchWithNameBookmark2Created() throws IOException {
    updateRepository("mercurial-tests/testData/bookmarks/");
    delete(myRemoteRepository);
    Util.copyRepository(new File("mercurial-tests/testData/bookmarks/5"), myRemoteRepository);
  }

  private void updateRepository(@NotNull String path) throws IOException {
    delete(myRemoteRepository);
    Util.copyRepository(new File(path), myRemoteRepository);
  }


  static class RepositoryStateDataMatcher extends TypeSafeMatcher<RepositoryStateData> {

    private String myBranch;
    private String myRevision;

    RepositoryStateDataMatcher(@NotNull String branch) {
      myBranch = branch;
    }

    public static RepositoryStateDataMatcher hasBranch(@NotNull String branch) {
      return new RepositoryStateDataMatcher(branch);
    }

    public static RepositoryStateDataMatcher hasNoBranch(@NotNull String branch) {
      return new RepositoryStateDataMatcher(branch);
    }

    public RepositoryStateDataMatcher withRevision(@NotNull String revision) {
      myRevision = revision;
      return this;
    }

    @Override
    public boolean matchesSafely(RepositoryStateData repositoryStateData) {
      String actualRevision = repositoryStateData.getBranchRevisions().get(myBranch);
      if (myRevision == null && actualRevision != null ||
          myRevision != null && !myRevision.equals(actualRevision))
        return false;
      return true;
    }

    public void describeTo(Description description) {
      if (myRevision == null) {
        description.appendText("a repository state without branch ").appendValue(myBranch);
      } else {
        description.appendText("a repository state where branch ").appendValue(myBranch).appendText(" has revision ").appendValue(myRevision);
      }
    }
  }
}