// Copyright 2015 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.analysis; import static com.google.common.truth.Truth.assertThat; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.analysis.config.CompilationMode; import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.packages.AttributeMap; import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.packages.ConfiguredAttributeMapper; import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; import com.google.devtools.build.lib.syntax.Type; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** * Unit tests for {@link ConfiguredAttributeMapper}. * *

This is distinct from {@link * com.google.devtools.build.lib.analysis.select.ConfiguredAttributeMapperCommonTest} because the * latter needs to inherit from {@link * com.google.devtools.build.lib.analysis.select.AbstractAttributeMapperTest} to run tests common to * all attribute mappers. */ @RunWith(JUnit4.class) public class ConfiguredAttributeMapperTest extends BuildViewTestCase { /** * Returns a ConfiguredAttributeMapper bound to the given rule with the target configuration. */ private ConfiguredAttributeMapper getMapper(String label) throws Exception { ConfiguredTargetAndData ctad = getConfiguredTargetAndData(label); return getMapperFromConfiguredTargetAndTarget(ctad); } private void writeConfigRules() throws Exception { scratch.file("conditions/BUILD", "config_setting(", " name = 'a',", " values = {'define': 'mode=a'})", "config_setting(", " name = 'b',", " values = {'define': 'mode=b'})"); } /** * Tests that {@link ConfiguredAttributeMapper#get} only gets the configuration-appropriate * value. */ @Test public void testGetAttribute() throws Exception { writeConfigRules(); scratch.file("a/BUILD", "genrule(", " name = 'gen',", " srcs = [],", " outs = ['out'],", " cmd = select({", " '//conditions:a': 'a command',", " '//conditions:b': 'b command',", " '" + BuildType.Selector.DEFAULT_CONDITION_KEY + "': 'default command',", " }))"); useConfiguration("--define", "mode=a"); assertThat(getMapper("//a:gen").get("cmd", Type.STRING)).isEqualTo("a command"); useConfiguration("--define", "mode=b"); assertThat(getMapper("//a:gen").get("cmd", Type.STRING)).isEqualTo("b command"); useConfiguration("--define", "mode=c"); assertThat(getMapper("//a:gen").get("cmd", Type.STRING)).isEqualTo("default command"); } /** * Tests that label visitation only travels down configuration-appropriate paths. */ @Test public void testLabelVisitation() throws Exception { writeConfigRules(); scratch.file("a/BUILD", "sh_binary(", " name = 'bin',", " srcs = ['bin.sh'],", " deps = select({", " '//conditions:a': [':adep'],", " '//conditions:b': [':bdep'],", " '" + BuildType.Selector.DEFAULT_CONDITION_KEY + "': [':defaultdep'],", " }))", "sh_library(", " name = 'adep',", " srcs = ['adep.sh'])", "sh_library(", " name = 'bdep',", " srcs = ['bdep.sh'])", "sh_library(", " name = 'defaultdep',", " srcs = ['defaultdep.sh'])"); List