blob: 5cbaf479b7ade89ceb96972df7d3251de883b48a [file] [log] [blame]
Greg Estren7f534232016-12-01 21:38:25 +00001// 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.
14package com.google.devtools.build.lib.analysis;
15
16import static com.google.common.truth.Truth.assertThat;
17import static org.junit.Assert.fail;
18
19import com.google.common.collect.ImmutableMap;
20import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
21import com.google.devtools.build.lib.cmdline.Label;
22import com.google.devtools.build.lib.packages.Attribute;
23import com.google.devtools.build.lib.packages.BuildType;
24import com.google.devtools.build.lib.packages.Rule;
Greg Estren7f534232016-12-01 21:38:25 +000025import com.google.devtools.build.lib.util.FileTypeSet;
Greg Estren7f534232016-12-01 21:38:25 +000026import org.junit.Before;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.JUnit4;
30
31/**
32 * Unit tests for {@link AspectAwareAttributeMapper}.
33 */
34@RunWith(JUnit4.class)
35public 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 Estren564940d2017-02-14 21:30:29 +0000105 assertThat(mapper.isConfigurable("linkstatic")).isTrue();
106 assertThat(mapper.isConfigurable("fromaspect")).isFalse();
Greg Estren7f534232016-12-01 21:38:25 +0000107 }
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 Estren564940d2017-02-14 21:30:29 +0000129 assertThat(mapper.has("srcs")).isTrue();
130 assertThat(mapper.has("fromaspect")).isTrue();
Greg Estren7f534232016-12-01 21:38:25 +0000131 }
132}
133
134