blob: 947723c8944b8b2162792e4c07510e5f8fe6809d [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
16import static com.google.common.truth.Truth.assertThat;
17
dannark90e2b4b2018-06-27 13:35:04 -070018import com.google.common.collect.ImmutableMap;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000019import com.google.common.collect.ImmutableSet;
20import com.google.devtools.build.lib.cmdline.Label;
21import com.google.devtools.build.lib.cmdline.PackageIdentifier;
Florian Weikertcca703a2015-12-07 09:56:38 +000022import com.google.devtools.build.lib.packages.util.PackageLoadingTestCase;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000023import com.google.devtools.build.lib.vfs.Path;
janakr518bb872018-10-03 15:59:28 -070024import com.google.devtools.build.lib.vfs.RootedPath;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000025import org.junit.Before;
26import org.junit.Test;
27import org.junit.runner.RunWith;
28import org.junit.runners.JUnit4;
29
30/**
31 * Tests for {@link EnvironmentGroup}. Note input validation is handled in
32 * {@link PackageFactoryTest}.
33 */
34@RunWith(JUnit4.class)
Florian Weikertcca703a2015-12-07 09:56:38 +000035public class EnvironmentGroupTest extends PackageLoadingTestCase {
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000036
37 private Package pkg;
38 private EnvironmentGroup group;
39
40 @Before
Florian Weikert432d1982015-12-01 14:38:06 +000041 public final void createPackage() throws Exception {
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000042 Path buildfile =
43 scratch.file(
44 "pkg/BUILD",
45 "environment(name='foo', fulfills = [':bar', ':baz'])",
46 "environment(name='bar', fulfills = [':baz'])",
47 "environment(name='baz')",
48 "environment(name='not_in_group')",
49 "environment_group(",
50 " name = 'group',",
51 " environments = [':foo', ':bar', ':baz'],",
52 " defaults = [':foo'],",
53 ")");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000054 pkg =
Nathan Harmata3ad56452016-06-10 16:17:45 +000055 packageFactory.createPackageForTesting(
janakr518bb872018-10-03 15:59:28 -070056 PackageIdentifier.createInMainRepo("pkg"),
57 RootedPath.toRootedPath(root, buildfile),
58 getPackageManager(),
59 reporter);
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000060
61 group = (EnvironmentGroup) pkg.getTarget("group");
62 }
63
64 @Test
65 public void testGroupMembership() throws Exception {
lberkiaea56b32017-05-30 12:35:33 +020066 assertThat(group.getEnvironments())
67 .isEqualTo(
68 ImmutableSet.of(
dannark90e2b4b2018-06-27 13:35:04 -070069 Label.parseAbsolute("//pkg:foo", ImmutableMap.of()),
70 Label.parseAbsolute("//pkg:bar", ImmutableMap.of()),
71 Label.parseAbsolute("//pkg:baz", ImmutableMap.of())));
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000072 }
73
74 @Test
gregce46618b12018-04-04 14:49:29 -070075 public void defaultsMembership() throws Exception {
dannark90e2b4b2018-06-27 13:35:04 -070076 assertThat(group.getDefaults())
77 .isEqualTo(ImmutableSet.of(Label.parseAbsolute("//pkg:foo", ImmutableMap.of())));
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000078 }
79
80 @Test
gregce46618b12018-04-04 14:49:29 -070081 public void isDefault() throws Exception {
janakrc0f7a282018-02-15 07:44:06 -080082 EnvironmentLabels unpackedGroup = group.getEnvironmentLabels();
dannark90e2b4b2018-06-27 13:35:04 -070083 assertThat(unpackedGroup.isDefault(Label.parseAbsolute("//pkg:foo", ImmutableMap.of())))
84 .isTrue();
85 assertThat(unpackedGroup.isDefault(Label.parseAbsolute("//pkg:bar", ImmutableMap.of())))
86 .isFalse();
87 assertThat(unpackedGroup.isDefault(Label.parseAbsolute("//pkg:baz", ImmutableMap.of())))
88 .isFalse();
89 assertThat(
90 unpackedGroup.isDefault(Label.parseAbsolute("//pkg:not_in_group", ImmutableMap.of())))
91 .isFalse();
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000092 }
93
94 @Test
gregce46618b12018-04-04 14:49:29 -070095 public void fulfillers() throws Exception {
janakrc0f7a282018-02-15 07:44:06 -080096 EnvironmentLabels unpackedGroup = group.getEnvironmentLabels();
dannark90e2b4b2018-06-27 13:35:04 -070097 assertThat(unpackedGroup.getFulfillers(Label.parseAbsolute("//pkg:baz", ImmutableMap.of())))
98 .containsExactly(
99 Label.parseAbsolute("//pkg:foo", ImmutableMap.of()),
100 Label.parseAbsolute("//pkg:bar", ImmutableMap.of()));
101 assertThat(unpackedGroup.getFulfillers(Label.parseAbsolute("//pkg:bar", ImmutableMap.of())))
102 .containsExactly(Label.parseAbsolute("//pkg:foo", ImmutableMap.of()));
103 assertThat(unpackedGroup.getFulfillers(Label.parseAbsolute("//pkg:foo", ImmutableMap.of())))
104 .isEmpty();
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000105 }
gregce46618b12018-04-04 14:49:29 -0700106
107 @Test
108 public void emptyGroupsNotAllowed() throws Exception {
109 Path buildfile = scratch.file(
110 "a/BUILD",
111 "environment_group(name = 'empty_group', environments = [], defaults = [])");
112 reporter.removeHandler(failFastHandler);
janakr518bb872018-10-03 15:59:28 -0700113 Package emptyGroupPkg =
114 packageFactory.createPackageForTesting(
115 PackageIdentifier.createInMainRepo("a"),
116 RootedPath.toRootedPath(root, buildfile),
117 getPackageManager(),
118 reporter);
gregce46618b12018-04-04 14:49:29 -0700119 assertThat(emptyGroupPkg.containsErrors()).isTrue();
120 assertContainsEvent(
121 "environment group empty_group must contain at least one environment");
122 }
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000123}