Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 1 | // Copyright 2016 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.analysis; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | import static org.junit.Assert.fail; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
| 21 | import com.google.devtools.build.lib.cmdline.Label; |
| 22 | import com.google.devtools.build.lib.packages.Attribute; |
| 23 | import com.google.devtools.build.lib.packages.BuildType; |
| 24 | import com.google.devtools.build.lib.packages.Rule; |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.util.FileTypeSet; |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.junit.runners.JUnit4; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for {@link AspectAwareAttributeMapper}. |
| 33 | */ |
| 34 | @RunWith(JUnit4.class) |
| 35 | public class AspectAwareAttributeMapperTest extends BuildViewTestCase { |
| 36 | private Rule rule; |
| 37 | private ImmutableMap<String, Attribute> aspectAttributes; |
| 38 | private AspectAwareAttributeMapper mapper; |
| 39 | |
| 40 | @Before |
| 41 | public final void createMapper() throws Exception { |
| 42 | RuleConfiguredTarget ct = (RuleConfiguredTarget) scratchConfiguredTarget("foo", "myrule", |
| 43 | "cc_binary(", |
| 44 | " name = 'myrule',", |
| 45 | " srcs = [':a.cc'],", |
| 46 | " linkstatic = select({'//conditions:default': 1}))"); |
| 47 | rule = ct.getTarget(); |
| 48 | Attribute aspectAttr = new Attribute.Builder<Label>("fromaspect", BuildType.LABEL) |
| 49 | .allowedFileTypes(FileTypeSet.ANY_FILE) |
| 50 | .build(); |
| 51 | aspectAttributes = ImmutableMap.<String, Attribute>of(aspectAttr.getName(), aspectAttr); |
| 52 | mapper = new AspectAwareAttributeMapper(ConfiguredAttributeMapper.of(ct), aspectAttributes); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void getName() throws Exception { |
| 57 | assertThat(mapper.getName()).isEqualTo(rule.getName()); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void getLabel() throws Exception { |
| 62 | assertThat(mapper.getLabel()).isEqualTo(rule.getLabel()); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void getRuleAttributeValue() throws Exception { |
| 67 | assertThat(mapper.get("srcs", BuildType.LABEL_LIST)) |
| 68 | .containsExactly(Label.parseAbsolute("//foo:a.cc")); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void getAspectAttributeValue() throws Exception { |
| 73 | try { |
| 74 | mapper.get("fromaspect", BuildType.LABEL); |
| 75 | fail("Expected failure because value queries aren't supported for aspect attributes"); |
| 76 | } catch (UnsupportedOperationException e) { |
| 77 | // Expected. |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void getAspectValueWrongType() throws Exception { |
| 83 | try { |
| 84 | mapper.get("fromaspect", BuildType.LABEL_LIST); |
| 85 | fail("Expected failure on wrong-typed attribute"); |
| 86 | } catch (IllegalArgumentException e) { |
| 87 | assertThat(e.getMessage()) |
| 88 | .isEqualTo("attribute fromaspect has type label, not expected type list(label)"); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void getMissingAttributeValue() throws Exception { |
| 94 | try { |
| 95 | mapper.get("noexist", BuildType.LABEL); |
| 96 | fail("Expected failure on non-existent attribute"); |
| 97 | } catch (IllegalArgumentException e) { |
| 98 | assertThat(e.getMessage()) |
| 99 | .isEqualTo("no attribute 'noexist' in either //foo:myrule or its aspects"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void isConfigurable() throws Exception { |
Greg Estren | 564940d | 2017-02-14 21:30:29 +0000 | [diff] [blame] | 105 | assertThat(mapper.isConfigurable("linkstatic")).isTrue(); |
| 106 | assertThat(mapper.isConfigurable("fromaspect")).isFalse(); |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void getAttributeNames() throws Exception { |
| 111 | assertThat(mapper.getAttributeNames()).containsAllOf("srcs", "linkstatic", "fromaspect"); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void getAttributeType() throws Exception { |
| 116 | assertThat(mapper.getAttributeType("srcs")).isEqualTo(BuildType.LABEL_LIST); |
| 117 | assertThat(mapper.getAttributeType("fromaspect")).isEqualTo(BuildType.LABEL); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void getAttributeDefinition() throws Exception { |
| 122 | assertThat(mapper.getAttributeDefinition("srcs").getName()).isEqualTo("srcs"); |
| 123 | assertThat(mapper.getAttributeDefinition("fromaspect").getName()).isEqualTo("fromaspect"); |
| 124 | |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void has() throws Exception { |
Greg Estren | 564940d | 2017-02-14 21:30:29 +0000 | [diff] [blame] | 129 | assertThat(mapper.has("srcs")).isTrue(); |
| 130 | assertThat(mapper.has("fromaspect")).isTrue(); |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | |