blob: 11605aac2660c7ebf039bc81498028847d783817 [file] [log] [blame]
cpeyser1099b542017-09-06 18:19:20 +02001// Copyright 2017 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
15package com.google.devtools.build.lib.skyframe;
16
17import static com.google.common.truth.Truth.assertThat;
jcater7052f3c2020-06-02 15:43:14 -070018import static com.google.devtools.build.lib.analysis.testing.ToolchainContextSubject.assertThat;
cpeyser1099b542017-09-06 18:19:20 +020019
cpeyser1099b542017-09-06 18:19:20 +020020import com.google.devtools.build.lib.analysis.RuleContext;
cpeyserb38e3af2017-09-11 16:58:58 +020021import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
cpeyser1099b542017-09-06 18:19:20 +020022import com.google.devtools.build.lib.cmdline.Label;
cpeyser1099b542017-09-06 18:19:20 +020023import com.google.devtools.build.lib.rules.platform.ToolchainTestCase;
24import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
27
28/** Tests for toolchains computed in BuildViewTestCase. */
29@RunWith(JUnit4.class)
30public class RuleContextTest extends ToolchainTestCase {
31
32 @Test
jcater0a1e9eb2019-12-17 09:58:38 -080033 public void testToolchains() throws Exception {
cpeyser1099b542017-09-06 18:19:20 +020034 mockToolsConfig.create("x/BUILD", "mock_toolchain_rule(name='x')");
jcater0a1e9eb2019-12-17 09:58:38 -080035 useConfiguration("--host_platform=//platforms:linux", "--platforms=//platforms:mac");
cpeyser1099b542017-09-06 18:19:20 +020036 RuleContext ruleContext = getRuleContext(getConfiguredTarget("//x"));
John Caterc8bd74f2018-06-22 14:20:22 -070037 assertThat(ruleContext.getToolchainContext().resolvedToolchainLabels())
Googler876bf372022-07-05 04:56:30 -070038 .contains(Label.parseCanonical("//toolchain:toolchain_1_impl"));
cpeyserb38e3af2017-09-11 16:58:58 +020039
jcater7052f3c2020-06-02 15:43:14 -070040 assertThat(ruleContext.getToolchainContext()).hasToolchainType("//toolchain:test_toolchain");
cpeyserb38e3af2017-09-11 16:58:58 +020041 ToolchainInfo toolchain =
Googler928e0fe2023-01-18 02:31:45 -080042 ruleContext.getToolchainInfo(Label.parseCanonical("//toolchain:test_toolchain"));
cpeyserb38e3af2017-09-11 16:58:58 +020043 assertThat(toolchain.getValue("data")).isEqualTo("foo");
cpeyser1099b542017-09-06 18:19:20 +020044 }
jcater0a1e9eb2019-12-17 09:58:38 -080045
46 @Test
47 public void testTargetPlatformHasConstraint_mac() throws Exception {
48 scratch.file("a/BUILD", "filegroup(name = 'a')");
49 useConfiguration("--platforms=//platforms:mac");
50 RuleContext ruleContext = getRuleContext(getConfiguredTarget("//a"));
51 assertThat(ruleContext.targetPlatformHasConstraint(macConstraint)).isTrue();
52 assertThat(ruleContext.targetPlatformHasConstraint(linuxConstraint)).isFalse();
53 }
54
55 @Test
56 public void testTargetPlatformHasConstraint_linux() throws Exception {
57 scratch.file("a/BUILD", "filegroup(name = 'a')");
58 useConfiguration("--platforms=//platforms:linux");
59 RuleContext ruleContext = getRuleContext(getConfiguredTarget("//a"));
60 assertThat(ruleContext.targetPlatformHasConstraint(macConstraint)).isFalse();
61 assertThat(ruleContext.targetPlatformHasConstraint(linuxConstraint)).isTrue();
62 }
Googlerc2d06b92023-04-07 06:16:23 -070063
64 @Test
65 public void testTestonlyToolchain_allowed() throws Exception {
66 createTestonlyToolchain();
67
68 scratch.file(
69 "p0/BUILD",
70 "load('//foo:rule_def.bzl', 'foo_rule')",
71 "foo_rule(",
72 " name = 'p0',",
73 " testonly = True,",
74 ")");
75 // This should succeed.
76 getConfiguredTarget("//p0:p0");
77 }
78
79 @Test
80 public void testTestonlyToolchain_invalid() throws Exception {
81 createTestonlyToolchain();
82
83 checkError(
84 "p0",
85 "p0",
86 // error:
87 "non-test target '//p0:p0' depends on testonly target",
88 // build file:
89 "load('//foo:rule_def.bzl', 'foo_rule')",
90 "foo_rule(",
91 " name = 'p0',",
92 " testonly = False,", // False is the default, we set it here for clarity.
93 ")");
94 }
95
96 private void createTestonlyToolchain() throws Exception {
97 // Define a custom rule with a testonly toolchain.
98 scratch.file(
99 "foo/toolchain_def.bzl",
100 "def _impl(ctx):",
101 " return [platform_common.ToolchainInfo()]",
102 "foo_toolchain = rule(",
103 " implementation = _impl,",
104 " attrs = {})");
105 scratch.file(
106 "foo/rule_def.bzl",
107 "def _impl(ctx):",
108 " pass",
109 "foo_rule = rule(",
110 " implementation = _impl,",
111 " toolchains = ['//foo:toolchain_type'])");
112 scratch.file("foo/BUILD", "toolchain_type(name = 'toolchain_type')");
113 // Create an instance of the toolchain.
114 scratch.file(
115 "bar/BUILD",
116 "load('//foo:toolchain_def.bzl', 'foo_toolchain')",
117 "toolchain(",
118 " name = 'foo_toolchain_impl',",
119 " toolchain_type = '//foo:toolchain_type',",
120 " toolchain = ':foo_toolchain_def')",
121 "foo_toolchain(",
122 " name = 'foo_toolchain_def',",
123 " testonly = True,",
124 ")");
125
126 useConfiguration("--extra_toolchains=//bar:all");
127 }
cpeyser1099b542017-09-06 18:19:20 +0200128}