blob: 7b8cbe8855ec203fc025f449c9cc0734b03cff79 [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;
Florian Weikert432d1982015-12-01 14:38:06 +000017import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertFalse;
19import static org.junit.Assert.assertTrue;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000020
21import com.google.common.collect.ImmutableSet;
22import com.google.devtools.build.lib.cmdline.Label;
23import com.google.devtools.build.lib.cmdline.PackageIdentifier;
Florian Weikertcca703a2015-12-07 09:56:38 +000024import com.google.devtools.build.lib.packages.util.PackageLoadingTestCase;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000025import com.google.devtools.build.lib.testutil.TestRuleClassProvider;
26import com.google.devtools.build.lib.vfs.Path;
27
28import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
31import org.junit.runners.JUnit4;
32
33/**
34 * Tests for {@link EnvironmentGroup}. Note input validation is handled in
35 * {@link PackageFactoryTest}.
36 */
37@RunWith(JUnit4.class)
Florian Weikertcca703a2015-12-07 09:56:38 +000038public class EnvironmentGroupTest extends PackageLoadingTestCase {
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000039
40 private Package pkg;
41 private EnvironmentGroup group;
42
43 @Before
Florian Weikert432d1982015-12-01 14:38:06 +000044 public final void createPackage() throws Exception {
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000045 Path buildfile =
46 scratch.file(
47 "pkg/BUILD",
48 "environment(name='foo', fulfills = [':bar', ':baz'])",
49 "environment(name='bar', fulfills = [':baz'])",
50 "environment(name='baz')",
51 "environment(name='not_in_group')",
52 "environment_group(",
53 " name = 'group',",
54 " environments = [':foo', ':bar', ':baz'],",
55 " defaults = [':foo'],",
56 ")");
57 PackageFactory pkgFactory = new PackageFactory(TestRuleClassProvider.getRuleClassProvider());
58 pkg =
59 pkgFactory.createPackageForTesting(
60 PackageIdentifier.createInDefaultRepo("pkg"), buildfile, getPackageManager(), reporter);
61
62 group = (EnvironmentGroup) pkg.getTarget("group");
63 }
64
65 @Test
66 public void testGroupMembership() throws Exception {
67 assertEquals(
68 ImmutableSet.of(
69 Label.parseAbsolute("//pkg:foo"),
70 Label.parseAbsolute("//pkg:bar"),
71 Label.parseAbsolute("//pkg:baz")),
72 group.getEnvironments());
73 }
74
75 @Test
76 public void testDefaultsMembership() throws Exception {
77 assertEquals(ImmutableSet.of(Label.parseAbsolute("//pkg:foo")), group.getDefaults());
78 }
79
80 @Test
81 public void testIsDefault() throws Exception {
82 assertTrue(group.isDefault(Label.parseAbsolute("//pkg:foo")));
83 assertFalse(group.isDefault(Label.parseAbsolute("//pkg:bar")));
84 assertFalse(group.isDefault(Label.parseAbsolute("//pkg:baz")));
85 assertFalse(group.isDefault(Label.parseAbsolute("//pkg:not_in_group")));
86 }
87
88 @Test
89 public void testFulfillers() throws Exception {
90 assertThat(group.getFulfillers(Label.parseAbsolute("//pkg:baz")))
91 .containsExactly(Label.parseAbsolute("//pkg:foo"), Label.parseAbsolute("//pkg:bar"));
92 assertThat(group.getFulfillers(Label.parseAbsolute("//pkg:bar")))
93 .containsExactly(Label.parseAbsolute("//pkg:foo"));
94 assertThat(group.getFulfillers(Label.parseAbsolute("//pkg:foo"))).isEmpty();
95 }
96}