blob: a16d7ccec7d2fce4ef6f70488831c67e0fb05a10 [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
cushon34ff85e2017-11-15 08:59:27 -080016import static com.google.common.collect.ImmutableList.toImmutableList;
Carmi Grushkofd8acab2015-11-10 17:19:13 +000017import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000018
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000019import com.google.devtools.build.lib.events.util.EventCollectionApparatus;
20import com.google.devtools.build.lib.packages.util.PackageFactoryApparatus;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000021import com.google.devtools.build.lib.testutil.Scratch;
22import com.google.devtools.build.lib.vfs.Path;
23import com.google.devtools.build.lib.vfs.PathFragment;
janakr518bb872018-10-03 15:59:28 -070024import com.google.devtools.build.lib.vfs.Root;
25import com.google.devtools.build.lib.vfs.RootedPath;
26import org.junit.Before;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000027import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.JUnit4;
30
31/**
32 * Unit tests for PackageGroup.
33 */
34@RunWith(JUnit4.class)
35public class PackageGroupTest {
36 private Scratch scratch = new Scratch("/workspace");
37 private EventCollectionApparatus events = new EventCollectionApparatus();
38 private PackageFactoryApparatus packages = new PackageFactoryApparatus(events.reporter());
janakr518bb872018-10-03 15:59:28 -070039 private Root root;
40
41 @Before
42 public void setUp() throws Exception {
43 root = Root.fromPath(scratch.dir(""));
44 }
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000045
46 @Test
47 public void testDoesNotFailHorribly() throws Exception {
48 scratch.file("fruits/BUILD", "package_group(name = 'apple', packages = ['//random'])");
49
50 getPackageGroup("fruits", "apple");
51 }
52
53 // Regression test for: "Package group with empty name causes Blaze exception"
54 @Test
55 public void testEmptyPackageGroupNameDoesNotThrow() throws Exception {
56 scratch.file("strawberry/BUILD", "package_group(name = '', packages=[])");
57
58 events.setFailFast(false);
59 getPackage("strawberry");
Ulf Adamsc708f962015-10-22 12:02:28 +000060 events.assertContainsError("package group has invalid name");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000061 }
62
63 @Test
64 public void testAbsolutePackagesWork() throws Exception {
65 scratch.file(
66 "fruits/BUILD",
67 "package_group(name = 'apple',",
68 " packages = ['//vegetables'])");
69
70 scratch.file("vegetables/BUILD");
71 scratch.file("fruits/vegetables/BUILD");
72
73 PackageGroup grp = getPackageGroup("fruits", "apple");
lberkiaea56b32017-05-30 12:35:33 +020074 assertThat(grp.contains(getPackage("vegetables"))).isTrue();
75 assertThat(grp.contains(getPackage("fruits/vegetables"))).isFalse();
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000076 }
77
78 @Test
79 public void testPackagesWithoutDoubleSlashDoNotWork() throws Exception {
80 scratch.file(
81 "fruits/BUILD",
82 "package_group(name = 'apple',",
83 " packages = ['vegetables'])");
84
85 scratch.file("vegetables/BUILD");
86 scratch.file("fruits/vegetables/BUILD");
87
88 events.setFailFast(false);
89 getPackageGroup("fruits", "apple");
Lukacs Berki485eb962016-01-13 10:47:29 +000090 events.assertContainsError("invalid package name 'vegetables'");
91 }
92
93 @Test
94 public void testPackagesWithRepositoryDoNotWork() throws Exception {
95 scratch.file(
96 "fruits/BUILD",
97 "package_group(name = 'banana',",
98 " packages = ['@veggies//:cucumber'])");
99
100 events.setFailFast(false);
101 getPackageGroup("fruits", "banana");
102 events.assertContainsError("invalid package name '@veggies//:cucumber'");
103 }
104
105 @Test
106 public void testAllPackagesInMainRepositoryDoesNotWork() throws Exception {
107 scratch.file(
108 "fruits/BUILD",
109 "package_group(name = 'apple',",
110 " packages = ['@//...'])");
111
112 events.setFailFast(false);
113 getPackageGroup("fruits", "apple");
114 events.assertContainsError("invalid package name '@//...'");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000115 }
116
117 @Test
118 public void testTargetNameAsPackageDoesNotWork1() throws Exception {
119 scratch.file(
120 "fruits/BUILD",
121 "package_group(name = 'apple',",
122 " packages = ['//vegetables:carrot'])");
123
124 scratch.file("vegetables/BUILD");
125 scratch.file("fruits/vegetables/BUILD");
126
127 events.setFailFast(false);
128 getPackageGroup("fruits", "apple");
Lukacs Berki485eb962016-01-13 10:47:29 +0000129 events.assertContainsError("invalid package name '//vegetables:carrot'");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000130 }
131
132 @Test
133 public void testTargetNameAsPackageDoesNotWork2() throws Exception {
134 scratch.file(
135 "fruits/BUILD", "package_group(name = 'apple',", " packages = [':carrot'])");
136
137 scratch.file("vegetables/BUILD");
138 scratch.file("fruits/vegetables/BUILD");
139
140 events.setFailFast(false);
141 getPackageGroup("fruits", "apple");
Lukacs Berki485eb962016-01-13 10:47:29 +0000142 events.assertContainsError("invalid package name ':carrot'");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000143 }
144
145 @Test
146 public void testAllBeneathSpecificationWorks() throws Exception {
147 scratch.file(
148 "fruits/BUILD",
149 "package_group(name = 'maracuja',",
150 " packages = ['//tropics/...'])");
151
152 getPackageGroup("fruits", "maracuja");
153 }
154
155 @Test
cushon7cc8efe2017-11-21 13:17:49 -0800156 public void testNegative() throws Exception {
157 scratch.file("one/BUILD");
158 scratch.file("two/BUILD");
159 scratch.file("three/BUILD");
160 scratch.file("four/BUILD");
161 scratch.file(
162 "test/BUILD",
163 "package_group(",
164 " name = 'packages',",
165 " packages = [",
166 " '//one',",
167 " '//two',",
168 " '-//three',",
169 " '-//four',",
170 " ],",
171 ")");
172
173 PackageGroup grp = getPackageGroup("test", "packages");
174 assertThat(grp.contains(getPackage("one"))).isTrue();
175 assertThat(grp.contains(getPackage("two"))).isTrue();
176 assertThat(grp.contains(getPackage("three"))).isFalse();
177 assertThat(grp.contains(getPackage("four"))).isFalse();
178 }
179
180 @Test
181 public void testNegative_noSubpackages() throws Exception {
182 scratch.file("pkg/BUILD");
183 scratch.file("pkg/one/BUILD");
184 scratch.file("pkg/one/two/BUILD");
185 scratch.file(
186 "test/BUILD",
187 "package_group(",
188 " name = 'packages',",
189 " packages = [",
190 " '//pkg/...',",
191 " '-//pkg/one',",
192 " ],",
193 ")");
194
195 PackageGroup grp = getPackageGroup("test", "packages");
196 assertThat(grp.contains(getPackage("pkg"))).isTrue();
197 assertThat(grp.contains(getPackage("pkg/one"))).isFalse();
198 assertThat(grp.contains(getPackage("pkg/one/two"))).isTrue();
199 }
200
201 @Test
202 public void testNegative_subpackages() throws Exception {
203 scratch.file("pkg/BUILD");
204 scratch.file("pkg/one/BUILD");
205 scratch.file("pkg/one/two/BUILD");
206 scratch.file(
207 "test/BUILD",
208 "package_group(",
209 " name = 'packages',",
210 " packages = [",
211 " '//pkg/...',",
212 " '-//pkg/one/...',",
213 " ],",
214 ")");
215
216 PackageGroup grp = getPackageGroup("test", "packages");
217 assertThat(grp.contains(getPackage("pkg"))).isTrue();
218 assertThat(grp.contains(getPackage("pkg/one"))).isFalse();
219 assertThat(grp.contains(getPackage("pkg/one/two"))).isFalse();
220 }
221
222 @Test
223 public void testNegative_everything() throws Exception {
224 scratch.file("pkg/BUILD");
225 scratch.file("pkg/one/BUILD");
226 scratch.file("pkg/one/two/BUILD");
227 scratch.file(
228 "test/BUILD",
229 "package_group(",
230 " name = 'packages',",
231 " packages = [",
232 " '-//...',",
233 " ],",
234 ")");
235
236 PackageGroup grp = getPackageGroup("test", "packages");
237 assertThat(grp.contains(getPackage("pkg"))).isFalse();
238 assertThat(grp.contains(getPackage("pkg/one"))).isFalse();
239 assertThat(grp.contains(getPackage("pkg/one/two"))).isFalse();
240 }
241
242 @Test
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000243 public void testEverythingSpecificationWorks() throws Exception {
244 scratch.file("fruits/BUILD", "package_group(name = 'mango', packages = ['//...'])");
245 PackageGroup packageGroup = getPackageGroup("fruits", "mango");
cushon34ff85e2017-11-15 08:59:27 -0800246 assertThat(
247 packageGroup.getPackageSpecifications().containedPackages().collect(toImmutableList()))
248 .containsExactly(PackageSpecification.everything().toString());
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000249 }
250
251 private Package getPackage(String packageName) throws Exception {
nharmatab4060b62017-04-04 17:11:39 +0000252 PathFragment buildFileFragment = PathFragment.create(packageName).getRelative("BUILD");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000253
254 Path buildFile = scratch.resolve(buildFileFragment.getPathString());
janakr518bb872018-10-03 15:59:28 -0700255 return packages.createPackage(packageName, RootedPath.toRootedPath(root, buildFile));
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000256 }
257
258 private PackageGroup getPackageGroup(String pkg, String name) throws Exception {
259 return (PackageGroup) getPackage(pkg).getTarget(name);
260 }
261}