blob: 12f0eaf2db18b2f7a6077798c5e448bb5dbccac0 [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;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000024import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
27
28/**
29 * Unit tests for PackageGroup.
30 */
31@RunWith(JUnit4.class)
32public class PackageGroupTest {
33 private Scratch scratch = new Scratch("/workspace");
34 private EventCollectionApparatus events = new EventCollectionApparatus();
35 private PackageFactoryApparatus packages = new PackageFactoryApparatus(events.reporter());
36
37 @Test
38 public void testDoesNotFailHorribly() throws Exception {
39 scratch.file("fruits/BUILD", "package_group(name = 'apple', packages = ['//random'])");
40
41 getPackageGroup("fruits", "apple");
42 }
43
44 // Regression test for: "Package group with empty name causes Blaze exception"
45 @Test
46 public void testEmptyPackageGroupNameDoesNotThrow() throws Exception {
47 scratch.file("strawberry/BUILD", "package_group(name = '', packages=[])");
48
49 events.setFailFast(false);
50 getPackage("strawberry");
Ulf Adamsc708f962015-10-22 12:02:28 +000051 events.assertContainsError("package group has invalid name");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000052 }
53
54 @Test
55 public void testAbsolutePackagesWork() throws Exception {
56 scratch.file(
57 "fruits/BUILD",
58 "package_group(name = 'apple',",
59 " packages = ['//vegetables'])");
60
61 scratch.file("vegetables/BUILD");
62 scratch.file("fruits/vegetables/BUILD");
63
64 PackageGroup grp = getPackageGroup("fruits", "apple");
lberkiaea56b32017-05-30 12:35:33 +020065 assertThat(grp.contains(getPackage("vegetables"))).isTrue();
66 assertThat(grp.contains(getPackage("fruits/vegetables"))).isFalse();
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000067 }
68
69 @Test
70 public void testPackagesWithoutDoubleSlashDoNotWork() throws Exception {
71 scratch.file(
72 "fruits/BUILD",
73 "package_group(name = 'apple',",
74 " packages = ['vegetables'])");
75
76 scratch.file("vegetables/BUILD");
77 scratch.file("fruits/vegetables/BUILD");
78
79 events.setFailFast(false);
80 getPackageGroup("fruits", "apple");
Lukacs Berki485eb962016-01-13 10:47:29 +000081 events.assertContainsError("invalid package name 'vegetables'");
82 }
83
84 @Test
85 public void testPackagesWithRepositoryDoNotWork() throws Exception {
86 scratch.file(
87 "fruits/BUILD",
88 "package_group(name = 'banana',",
89 " packages = ['@veggies//:cucumber'])");
90
91 events.setFailFast(false);
92 getPackageGroup("fruits", "banana");
93 events.assertContainsError("invalid package name '@veggies//:cucumber'");
94 }
95
96 @Test
97 public void testAllPackagesInMainRepositoryDoesNotWork() throws Exception {
98 scratch.file(
99 "fruits/BUILD",
100 "package_group(name = 'apple',",
101 " packages = ['@//...'])");
102
103 events.setFailFast(false);
104 getPackageGroup("fruits", "apple");
105 events.assertContainsError("invalid package name '@//...'");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000106 }
107
108 @Test
109 public void testTargetNameAsPackageDoesNotWork1() throws Exception {
110 scratch.file(
111 "fruits/BUILD",
112 "package_group(name = 'apple',",
113 " packages = ['//vegetables:carrot'])");
114
115 scratch.file("vegetables/BUILD");
116 scratch.file("fruits/vegetables/BUILD");
117
118 events.setFailFast(false);
119 getPackageGroup("fruits", "apple");
Lukacs Berki485eb962016-01-13 10:47:29 +0000120 events.assertContainsError("invalid package name '//vegetables:carrot'");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000121 }
122
123 @Test
124 public void testTargetNameAsPackageDoesNotWork2() throws Exception {
125 scratch.file(
126 "fruits/BUILD", "package_group(name = 'apple',", " packages = [':carrot'])");
127
128 scratch.file("vegetables/BUILD");
129 scratch.file("fruits/vegetables/BUILD");
130
131 events.setFailFast(false);
132 getPackageGroup("fruits", "apple");
Lukacs Berki485eb962016-01-13 10:47:29 +0000133 events.assertContainsError("invalid package name ':carrot'");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000134 }
135
136 @Test
137 public void testAllBeneathSpecificationWorks() throws Exception {
138 scratch.file(
139 "fruits/BUILD",
140 "package_group(name = 'maracuja',",
141 " packages = ['//tropics/...'])");
142
143 getPackageGroup("fruits", "maracuja");
144 }
145
146 @Test
cushon7cc8efe2017-11-21 13:17:49 -0800147 public void testNegative() throws Exception {
148 scratch.file("one/BUILD");
149 scratch.file("two/BUILD");
150 scratch.file("three/BUILD");
151 scratch.file("four/BUILD");
152 scratch.file(
153 "test/BUILD",
154 "package_group(",
155 " name = 'packages',",
156 " packages = [",
157 " '//one',",
158 " '//two',",
159 " '-//three',",
160 " '-//four',",
161 " ],",
162 ")");
163
164 PackageGroup grp = getPackageGroup("test", "packages");
165 assertThat(grp.contains(getPackage("one"))).isTrue();
166 assertThat(grp.contains(getPackage("two"))).isTrue();
167 assertThat(grp.contains(getPackage("three"))).isFalse();
168 assertThat(grp.contains(getPackage("four"))).isFalse();
169 }
170
171 @Test
172 public void testNegative_noSubpackages() throws Exception {
173 scratch.file("pkg/BUILD");
174 scratch.file("pkg/one/BUILD");
175 scratch.file("pkg/one/two/BUILD");
176 scratch.file(
177 "test/BUILD",
178 "package_group(",
179 " name = 'packages',",
180 " packages = [",
181 " '//pkg/...',",
182 " '-//pkg/one',",
183 " ],",
184 ")");
185
186 PackageGroup grp = getPackageGroup("test", "packages");
187 assertThat(grp.contains(getPackage("pkg"))).isTrue();
188 assertThat(grp.contains(getPackage("pkg/one"))).isFalse();
189 assertThat(grp.contains(getPackage("pkg/one/two"))).isTrue();
190 }
191
192 @Test
193 public void testNegative_subpackages() throws Exception {
194 scratch.file("pkg/BUILD");
195 scratch.file("pkg/one/BUILD");
196 scratch.file("pkg/one/two/BUILD");
197 scratch.file(
198 "test/BUILD",
199 "package_group(",
200 " name = 'packages',",
201 " packages = [",
202 " '//pkg/...',",
203 " '-//pkg/one/...',",
204 " ],",
205 ")");
206
207 PackageGroup grp = getPackageGroup("test", "packages");
208 assertThat(grp.contains(getPackage("pkg"))).isTrue();
209 assertThat(grp.contains(getPackage("pkg/one"))).isFalse();
210 assertThat(grp.contains(getPackage("pkg/one/two"))).isFalse();
211 }
212
213 @Test
214 public void testNegative_everything() throws Exception {
215 scratch.file("pkg/BUILD");
216 scratch.file("pkg/one/BUILD");
217 scratch.file("pkg/one/two/BUILD");
218 scratch.file(
219 "test/BUILD",
220 "package_group(",
221 " name = 'packages',",
222 " packages = [",
223 " '-//...',",
224 " ],",
225 ")");
226
227 PackageGroup grp = getPackageGroup("test", "packages");
228 assertThat(grp.contains(getPackage("pkg"))).isFalse();
229 assertThat(grp.contains(getPackage("pkg/one"))).isFalse();
230 assertThat(grp.contains(getPackage("pkg/one/two"))).isFalse();
231 }
232
233 @Test
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000234 public void testEverythingSpecificationWorks() throws Exception {
235 scratch.file("fruits/BUILD", "package_group(name = 'mango', packages = ['//...'])");
236 PackageGroup packageGroup = getPackageGroup("fruits", "mango");
cushon34ff85e2017-11-15 08:59:27 -0800237 assertThat(
238 packageGroup.getPackageSpecifications().containedPackages().collect(toImmutableList()))
239 .containsExactly(PackageSpecification.everything().toString());
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000240 }
241
242 private Package getPackage(String packageName) throws Exception {
nharmatab4060b62017-04-04 17:11:39 +0000243 PathFragment buildFileFragment = PathFragment.create(packageName).getRelative("BUILD");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000244
245 Path buildFile = scratch.resolve(buildFileFragment.getPathString());
246 return packages.createPackage(packageName, buildFile);
247 }
248
249 private PackageGroup getPackageGroup(String pkg, String name) throws Exception {
250 return (PackageGroup) getPackage(pkg).getTarget(name);
251 }
252}