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; |
| 17 | |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.devtools.build.lib.cmdline.Label; |
| 21 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.packages.util.PackageLoadingTestCase; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.Path; |
janakr | 518bb87 | 2018-10-03 15:59:28 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.vfs.RootedPath; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | import 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 Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 35 | public class EnvironmentGroupTest extends PackageLoadingTestCase { |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 36 | |
| 37 | private Package pkg; |
| 38 | private EnvironmentGroup group; |
| 39 | |
| 40 | @Before |
Florian Weikert | 432d198 | 2015-12-01 14:38:06 +0000 | [diff] [blame] | 41 | public final void createPackage() throws Exception { |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 42 | 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 Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 54 | pkg = |
Nathan Harmata | 3ad5645 | 2016-06-10 16:17:45 +0000 | [diff] [blame] | 55 | packageFactory.createPackageForTesting( |
janakr | 518bb87 | 2018-10-03 15:59:28 -0700 | [diff] [blame] | 56 | PackageIdentifier.createInMainRepo("pkg"), |
| 57 | RootedPath.toRootedPath(root, buildfile), |
| 58 | getPackageManager(), |
| 59 | reporter); |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 60 | |
| 61 | group = (EnvironmentGroup) pkg.getTarget("group"); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testGroupMembership() throws Exception { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 66 | assertThat(group.getEnvironments()) |
| 67 | .isEqualTo( |
| 68 | ImmutableSet.of( |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 69 | Label.parseAbsolute("//pkg:foo", ImmutableMap.of()), |
| 70 | Label.parseAbsolute("//pkg:bar", ImmutableMap.of()), |
| 71 | Label.parseAbsolute("//pkg:baz", ImmutableMap.of()))); |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
gregce | 46618b1 | 2018-04-04 14:49:29 -0700 | [diff] [blame] | 75 | public void defaultsMembership() throws Exception { |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 76 | assertThat(group.getDefaults()) |
| 77 | .isEqualTo(ImmutableSet.of(Label.parseAbsolute("//pkg:foo", ImmutableMap.of()))); |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
gregce | 46618b1 | 2018-04-04 14:49:29 -0700 | [diff] [blame] | 81 | public void isDefault() throws Exception { |
janakr | c0f7a28 | 2018-02-15 07:44:06 -0800 | [diff] [blame] | 82 | EnvironmentLabels unpackedGroup = group.getEnvironmentLabels(); |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 83 | 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 Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Test |
gregce | 46618b1 | 2018-04-04 14:49:29 -0700 | [diff] [blame] | 95 | public void fulfillers() throws Exception { |
janakr | c0f7a28 | 2018-02-15 07:44:06 -0800 | [diff] [blame] | 96 | EnvironmentLabels unpackedGroup = group.getEnvironmentLabels(); |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 97 | 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 Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 105 | } |
gregce | 46618b1 | 2018-04-04 14:49:29 -0700 | [diff] [blame] | 106 | |
| 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); |
janakr | 518bb87 | 2018-10-03 15:59:28 -0700 | [diff] [blame] | 113 | Package emptyGroupPkg = |
| 114 | packageFactory.createPackageForTesting( |
| 115 | PackageIdentifier.createInMainRepo("a"), |
| 116 | RootedPath.toRootedPath(root, buildfile), |
| 117 | getPackageManager(), |
| 118 | reporter); |
gregce | 46618b1 | 2018-04-04 14:49:29 -0700 | [diff] [blame] | 119 | assertThat(emptyGroupPkg.containsErrors()).isTrue(); |
| 120 | assertContainsEvent( |
| 121 | "environment group empty_group must contain at least one environment"); |
| 122 | } |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 123 | } |