blob: 7ddc2bc83e2c3461aa45f2ddba74165286ddd9fe [file] [log] [blame]
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +00001// 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.
14package com.google.devtools.build.lib.packages;
15
lberkiaea56b32017-05-30 12:35:33 +020016import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000017import static org.junit.Assert.fail;
18
19import com.google.devtools.build.lib.vfs.PathFragment;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000020import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.junit.runners.JUnit4;
23
24/**
25 * Unit tests for {@link RelativePackageNameResolver}.
26 */
27@RunWith(JUnit4.class)
28public 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) {
nharmatab4060b62017-04-04 17:11:39 +0000114 resolver = new RelativePackageNameResolver(PathFragment.create(offset), discardBuild);
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000115 }
116
117 private void assertResolvesTo(String relative, String expectedAbsolute) throws Exception {
118 String result = resolver.resolve(relative);
lberkiaea56b32017-05-30 12:35:33 +0200119 assertThat(result).isEqualTo(expectedAbsolute);
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000120 }
121}