Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
| 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 | // http://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 | package com.google.devtools.build.lib.packages; |
| 15 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 17 | import static org.junit.Assert.fail; |
| 18 | |
| 19 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | import org.junit.runner.RunWith; |
| 22 | import org.junit.runners.JUnit4; |
| 23 | |
| 24 | /** |
| 25 | * Unit tests for {@link RelativePackageNameResolver}. |
| 26 | */ |
| 27 | @RunWith(JUnit4.class) |
| 28 | public class RelativePackageNameResolverTest { |
| 29 | private RelativePackageNameResolver resolver; |
| 30 | |
| 31 | @Test |
| 32 | public void testRelativePackagesBelowOneLevelWork() throws Exception { |
| 33 | createResolver("foo", true); |
| 34 | assertResolvesTo("bar", "foo/bar"); |
| 35 | |
| 36 | createResolver("foo/bar", true); |
| 37 | assertResolvesTo("pear", "foo/bar/pear"); |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | public void testRelativePackagesBelowTwoLevelsWork() throws Exception { |
| 42 | createResolver("foo/bar", true); |
| 43 | assertResolvesTo("pear", "foo/bar/pear"); |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void testRelativePackagesAboveOneLevelWork() throws Exception { |
| 48 | createResolver("foo", true); |
| 49 | assertResolvesTo("../bar", "bar"); |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testRelativePackagesAboveTwoLevelsWork() throws Exception { |
| 54 | createResolver("foo/bar", true); |
| 55 | assertResolvesTo("../../apple", "apple"); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testSimpleAbsolutePackagesWork() throws Exception { |
| 60 | createResolver("foo", true); |
| 61 | |
| 62 | assertResolvesTo("//foo", "foo"); |
| 63 | assertResolvesTo("//foo/bar", "foo/bar"); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testBuildNotRemoved() throws Exception { |
| 68 | createResolver("foo", false); |
| 69 | |
| 70 | assertResolvesTo("bar/BUILD", "foo/bar/BUILD"); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testBuildRemoved() throws Exception { |
| 75 | createResolver("foo", true); |
| 76 | |
| 77 | assertResolvesTo("bar/BUILD", "foo/bar"); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testEmptyOffset() throws Exception { |
| 82 | createResolver("", true); |
| 83 | |
| 84 | assertResolvesTo("bar", "bar"); |
| 85 | assertResolvesTo("bar/qux", "bar/qux"); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testTooFarUpwardsOneLevelThrows() throws Exception { |
| 90 | createResolver("foo", true); |
| 91 | |
| 92 | try { |
| 93 | resolver.resolve("../../bar"); |
| 94 | fail("InvalidPackageNameException expected"); |
| 95 | } catch (InvalidPackageNameException e) { |
| 96 | // good |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testTooFarUpwardsTwoLevelsThrows() throws Exception { |
| 102 | createResolver("foo/bar", true); |
| 103 | assertResolvesTo("../../orange", "orange"); |
| 104 | |
| 105 | try { |
| 106 | resolver.resolve("../../../orange"); |
| 107 | fail("InvalidPackageNameException expected"); |
| 108 | } catch (InvalidPackageNameException e) { |
| 109 | // good |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private void createResolver(String offset, boolean discardBuild) { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 114 | resolver = new RelativePackageNameResolver(PathFragment.create(offset), discardBuild); |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | private void assertResolvesTo(String relative, String expectedAbsolute) throws Exception { |
| 118 | String result = resolver.resolve(relative); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 119 | assertThat(result).isEqualTo(expectedAbsolute); |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 120 | } |
| 121 | } |