| Jonathan Nieder | e5a8b41 | 2019-11-25 15:59:25 -0800 | [diff] [blame] | 1 | // 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 Frade | ac7d524 | 2019-05-23 15:32:11 -0700 | [diff] [blame] | 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static org.junit.Assert.assertFalse; |
| 18 | import static org.junit.Assert.assertTrue; |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.util.Arrays; |
| 22 | import java.util.List; |
| 23 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 24 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 25 | import org.eclipse.jgit.junit.TestRepository; |
| 26 | import org.eclipse.jgit.lib.ObjectId; |
| 27 | import org.eclipse.jgit.revwalk.RevCommit; |
| 28 | import org.eclipse.jgit.revwalk.RevWalk; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.junit.runners.JUnit4; |
| 33 | |
| 34 | @RunWith(JUnit4.class) |
| 35 | public 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 Pursehouse | 667389e | 2019-06-05 13:27:04 +0900 | [diff] [blame] | 51 | 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 Frade | ac7d524 | 2019-05-23 15:32:11 -0700 | [diff] [blame] | 55 | |
| David Pursehouse | 667389e | 2019-06-05 13:27:04 +0900 | [diff] [blame] | 56 | 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 Frade | ac7d524 | 2019-05-23 15:32:11 -0700 | [diff] [blame] | 59 | |
| David Pursehouse | 667389e | 2019-06-05 13:27:04 +0900 | [diff] [blame] | 60 | git.update("master", commit2); |
| 61 | git.update("refs/tags/v0.1", commitA); |
| 62 | } |
| Ivan Frade | ac7d524 | 2019-05-23 15:32:11 -0700 | [diff] [blame] | 63 | |
| Ivan Frade | 9ca395d | 2019-11-25 13:29:49 -0800 | [diff] [blame] | 64 | visibilityChecker = new VisibilityChecker(); |
| Ivan Frade | ac7d524 | 2019-05-23 15:32:11 -0700 | [diff] [blame] | 65 | 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 | } |