Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.packages; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
Florian Weikert | 432d198 | 2015-12-01 14:38:06 +0000 | [diff] [blame] | 17 | import static org.junit.Assert.assertEquals; |
| 18 | import static org.junit.Assert.assertFalse; |
| 19 | import static org.junit.Assert.assertTrue; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 20 | |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.devtools.build.lib.cmdline.Label; |
| 23 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame^] | 24 | import com.google.devtools.build.lib.packages.util.PackageLoadingTestCase; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.testutil.TestRuleClassProvider; |
| 26 | import com.google.devtools.build.lib.vfs.Path; |
| 27 | |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | import 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 Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame^] | 38 | public class EnvironmentGroupTest extends PackageLoadingTestCase { |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 39 | |
| 40 | private Package pkg; |
| 41 | private EnvironmentGroup group; |
| 42 | |
| 43 | @Before |
Florian Weikert | 432d198 | 2015-12-01 14:38:06 +0000 | [diff] [blame] | 44 | public final void createPackage() throws Exception { |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 45 | 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 | } |