1# Copyright 2024 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"""Tests for the cc_args rule.""" 15 16load("//cc/toolchains:cc_toolchain_info.bzl", "ToolInfo") 17load("//cc/toolchains/impl:collect.bzl", _collect_tools = "collect_tools") 18load("//cc/toolchains/impl:legacy_converter.bzl", "convert_tool") 19load("//tests/rule_based_toolchain:subjects.bzl", "result_fn_wrapper", "subjects") 20 21visibility("private") 22 23collect_tools = result_fn_wrapper(_collect_tools) 24collection_result = subjects.result(subjects.collection) 25 26collect_tool = result_fn_wrapper( 27 lambda ctx, target, fail: _collect_tools(ctx, [target], fail = fail)[0], 28) 29tool_result = subjects.result(subjects.ToolInfo) 30 31# Generated by native_binary. 32_BIN_WRAPPER_SYMLINK = "tests/rule_based_toolchain/testdata/bin_wrapper" 33_BIN_WRAPPER = "tests/rule_based_toolchain/testdata/bin_wrapper.sh" 34_BIN = "tests/rule_based_toolchain/testdata/bin" 35_FILE1 = "tests/rule_based_toolchain/testdata/file1" 36_TOOL_DIRECTORY = "tests/rule_based_toolchain/testdata" 37 38def _tool_test(env, target): 39 tool = env.expect.that_target(target).provider(ToolInfo) 40 tool.exe().short_path_equals(_BIN_WRAPPER) 41 tool.execution_requirements().contains_exactly(["requires-network"]) 42 tool.runfiles().contains_exactly([ 43 _BIN_WRAPPER, 44 _BIN, 45 ]) 46 47 legacy = convert_tool(tool.actual) 48 env.expect.that_file(legacy.tool).equals(tool.actual.exe) 49 env.expect.that_collection(legacy.execution_requirements).contains_exactly(["requires-network"]) 50 env.expect.that_collection(legacy.with_features).contains_exactly([]) 51 52def _wrapped_tool_includes_runfiles_test(env, targets): 53 tool = env.expect.that_target(targets.wrapped_tool).provider(ToolInfo) 54 tool.exe().short_path_equals(_BIN_WRAPPER_SYMLINK) 55 tool.runfiles().contains_exactly([ 56 _BIN_WRAPPER_SYMLINK, 57 _BIN, 58 ]) 59 60def _tool_with_allowlist_include_directories_test(env, targets): 61 tool = env.expect.that_target(targets.tool_with_allowlist_include_directories).provider(ToolInfo) 62 tool.allowlist_include_directories().contains_exactly([_TOOL_DIRECTORY]) 63 tool.runfiles().contains_at_least([ 64 _BIN, 65 _FILE1, 66 ]) 67 68def _collect_tools_collects_tools_test(env, targets): 69 env.expect.that_value( 70 value = collect_tools(env.ctx, [targets.tool, targets.wrapped_tool]), 71 factory = collection_result, 72 ).ok().contains_exactly( 73 [targets.tool[ToolInfo], targets.wrapped_tool[ToolInfo]], 74 ).in_order() 75 76def _collect_tools_collects_binaries_test(env, targets): 77 tool_wrapper = env.expect.that_value( 78 value = collect_tool(env.ctx, targets.bin_wrapper), 79 factory = tool_result, 80 ).ok() 81 tool_wrapper.label().equals(targets.bin_wrapper.label) 82 tool_wrapper.exe().short_path_equals(_BIN_WRAPPER_SYMLINK) 83 tool_wrapper.runfiles().contains_exactly([ 84 _BIN_WRAPPER_SYMLINK, 85 _BIN, 86 ]) 87 88def _collect_tools_collects_single_files_test(env, targets): 89 bin = env.expect.that_value( 90 value = collect_tool(env.ctx, targets.bin_filegroup), 91 factory = tool_result, 92 expr = "bin_filegroup", 93 ).ok() 94 bin.label().equals(targets.bin_filegroup.label) 95 bin.exe().short_path_equals(_BIN) 96 bin.runfiles().contains_exactly([_BIN]) 97 98def _collect_tools_fails_on_non_binary_test(env, targets): 99 env.expect.that_value( 100 value = collect_tools(env.ctx, [targets.multiple]), 101 factory = collection_result, 102 expr = "multiple_non_binary", 103 ).err() 104 105TARGETS = [ 106 "//tests/rule_based_toolchain/features:direct_constraint", 107 "//tests/rule_based_toolchain/tool:tool", 108 "//tests/rule_based_toolchain/tool:wrapped_tool", 109 "//tests/rule_based_toolchain/tool:directory_tool", 110 "//tests/rule_based_toolchain/tool:tool_with_allowlist_include_directories", 111 "//tests/rule_based_toolchain/testdata:bin_wrapper", 112 "//tests/rule_based_toolchain/testdata:multiple", 113 "//tests/rule_based_toolchain/testdata:bin_filegroup", 114 "//tests/rule_based_toolchain/testdata:bin_wrapper_filegroup", 115] 116 117# @unsorted-dict-items 118TESTS = { 119 "tool_test": lambda env, targets: _tool_test(env, targets.tool), 120 "directory_tool_test": lambda env, targets: _tool_test(env, targets.directory_tool), 121 "wrapped_tool_includes_runfiles_test": _wrapped_tool_includes_runfiles_test, 122 "collect_tools_collects_tools_test": _collect_tools_collects_tools_test, 123 "collect_tools_collects_binaries_test": _collect_tools_collects_binaries_test, 124 "collect_tools_collects_single_files_test": _collect_tools_collects_single_files_test, 125 "collect_tools_fails_on_non_binary_test": _collect_tools_fails_on_non_binary_test, 126 "tool_with_allowlist_include_directories_test": _tool_with_allowlist_include_directories_test, 127} 128