cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
jcater | 7052f3c | 2020-06-02 15:43:14 -0700 | [diff] [blame] | 18 | import static com.google.devtools.build.lib.analysis.testing.ToolchainContextSubject.assertThat; |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 19 | |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.RuleContext; |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 21 | import com.google.devtools.build.lib.analysis.platform.ToolchainInfo; |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 22 | import com.google.devtools.build.lib.cmdline.Label; |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 23 | import com.google.devtools.build.lib.rules.platform.ToolchainTestCase; |
| 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
| 28 | /** Tests for toolchains computed in BuildViewTestCase. */ |
| 29 | @RunWith(JUnit4.class) |
| 30 | public class RuleContextTest extends ToolchainTestCase { |
| 31 | |
| 32 | @Test |
jcater | 0a1e9eb | 2019-12-17 09:58:38 -0800 | [diff] [blame] | 33 | public void testToolchains() throws Exception { |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 34 | mockToolsConfig.create("x/BUILD", "mock_toolchain_rule(name='x')"); |
jcater | 0a1e9eb | 2019-12-17 09:58:38 -0800 | [diff] [blame] | 35 | useConfiguration("--host_platform=//platforms:linux", "--platforms=//platforms:mac"); |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 36 | RuleContext ruleContext = getRuleContext(getConfiguredTarget("//x")); |
John Cater | c8bd74f | 2018-06-22 14:20:22 -0700 | [diff] [blame] | 37 | assertThat(ruleContext.getToolchainContext().resolvedToolchainLabels()) |
Googler | 876bf37 | 2022-07-05 04:56:30 -0700 | [diff] [blame] | 38 | .contains(Label.parseCanonical("//toolchain:toolchain_1_impl")); |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 39 | |
jcater | 7052f3c | 2020-06-02 15:43:14 -0700 | [diff] [blame] | 40 | assertThat(ruleContext.getToolchainContext()).hasToolchainType("//toolchain:test_toolchain"); |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 41 | ToolchainInfo toolchain = |
Googler | 928e0fe | 2023-01-18 02:31:45 -0800 | [diff] [blame] | 42 | ruleContext.getToolchainInfo(Label.parseCanonical("//toolchain:test_toolchain")); |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 43 | assertThat(toolchain.getValue("data")).isEqualTo("foo"); |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 44 | } |
jcater | 0a1e9eb | 2019-12-17 09:58:38 -0800 | [diff] [blame] | 45 | |
| 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 | } |
Googler | c2d06b9 | 2023-04-07 06:16:23 -0700 | [diff] [blame] | 63 | |
| 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 | } |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 128 | } |