blob: bf416bb0d4bc92139a21c32efcd8c0fcb5a4962e [file] [log] [blame]
Jonathan Niedere5a8b412019-11-25 15:59:25 -08001// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Ivan Fradeac7d5242019-05-23 15:32:11 -070015package com.google.gitiles;
16
17import static org.junit.Assert.assertFalse;
18import static org.junit.Assert.assertTrue;
19
20import java.io.IOException;
21import java.util.Arrays;
22import java.util.List;
23import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
24import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
25import org.eclipse.jgit.junit.TestRepository;
26import org.eclipse.jgit.lib.ObjectId;
27import org.eclipse.jgit.revwalk.RevCommit;
28import org.eclipse.jgit.revwalk.RevWalk;
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.junit.runners.JUnit4;
33
34@RunWith(JUnit4.class)
35public class VisibilityCheckerTest {
36 private InMemoryRepository repo;
37
38 private RevCommit baseCommit;
39 private RevCommit commit1;
40 private RevCommit commit2;
41 private RevCommit commitA;
42 private RevCommit commitB;
43 private RevCommit commitC;
44
45 private VisibilityChecker visibilityChecker;
46 private RevWalk walk;
47
48 @Before
49 public void setUp() throws Exception {
50 repo = new InMemoryRepository(new DfsRepositoryDescription());
David Pursehouse667389e2019-06-05 13:27:04 +090051 try (TestRepository<InMemoryRepository> git = new TestRepository<>(repo)) {
52 baseCommit = git.commit().message("baseCommit").create();
53 commit1 = git.commit().parent(baseCommit).message("commit1").create();
54 commit2 = git.commit().parent(commit1).message("commit2").create();
Ivan Fradeac7d5242019-05-23 15:32:11 -070055
David Pursehouse667389e2019-06-05 13:27:04 +090056 commitA = git.commit().parent(baseCommit).message("commitA").create();
57 commitB = git.commit().parent(commitA).message("commitB").create();
58 commitC = git.commit().parent(commitB).message("commitC").create();
Ivan Fradeac7d5242019-05-23 15:32:11 -070059
David Pursehouse667389e2019-06-05 13:27:04 +090060 git.update("master", commit2);
61 git.update("refs/tags/v0.1", commitA);
62 }
Ivan Fradeac7d5242019-05-23 15:32:11 -070063
Ivan Frade9ca395d2019-11-25 13:29:49 -080064 visibilityChecker = new VisibilityChecker();
Ivan Fradeac7d5242019-05-23 15:32:11 -070065 walk = new RevWalk(repo);
66 walk.setRetainBody(false);
67 }
68
69 @Test
70 public void isTip() throws IOException {
71 assertTrue(visibilityChecker.isTipOfBranch(repo.getRefDatabase(), commit2.getId()));
72 }
73
74 @Test
75 public void isNotTip() throws IOException {
76 assertFalse(visibilityChecker.isTipOfBranch(repo.getRefDatabase(), commit1.getId()));
77 }
78
79 @Test
80 public void reachableFromRef() throws IOException {
81 List<ObjectId> starters = Arrays.asList(commitC.getId());
82 assertTrue(
83 visibilityChecker.isReachableFrom("test", walk, walk.parseCommit(commitB), starters));
84 }
85
86 @Test
87 public void unreachableFromRef() throws IOException {
88 List<ObjectId> starters = Arrays.asList(commit2.getId(), commitA.getId());
89 assertFalse(
90 visibilityChecker.isReachableFrom("test", walk, walk.parseCommit(commitC), starters));
91 }
92}