1# Copyright 2020 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"""Process wrapper test. 16 17This rule unit tests the different process_wrapper functionality. 18""" 19 20def _impl(ctx): 21 args = ctx.actions.args() 22 outputs = [] 23 combined = ctx.attr.test_config == "combined" 24 25 if combined or ctx.attr.test_config == "stdout": 26 stdout_output = ctx.actions.declare_file(ctx.label.name + ".stdout") 27 outputs.append(stdout_output) 28 args.add("--stdout-file", stdout_output) 29 30 if combined or ctx.attr.test_config == "stderr": 31 stderr_output = ctx.actions.declare_file(ctx.label.name + ".stderr") 32 outputs.append(stderr_output) 33 args.add("--stderr-file", stderr_output) 34 35 if combined or (ctx.attr.test_config != "stdout" and ctx.attr.test_config != "stderr"): 36 touch_output = ctx.actions.declare_file(ctx.label.name + ".touch") 37 outputs.append(touch_output) 38 args.add("--touch-file", touch_output) 39 if ctx.attr.test_config == "copy-output": 40 copy_output = ctx.actions.declare_file(ctx.label.name + ".touch.copy") 41 outputs.append(copy_output) 42 args.add_all("--copy-output", [touch_output, copy_output]) 43 44 if combined or ctx.attr.test_config == "env-files": 45 args.add_all(ctx.files.env_files, before_each = "--env-file") 46 47 if combined or ctx.attr.test_config == "arg-files": 48 args.add_all(ctx.files.arg_files, before_each = "--arg-file") 49 50 if combined or ctx.attr.test_config == "subst-pwd": 51 args.add("--subst", "pwd=${pwd}") 52 args.add("--subst", "key=value") 53 54 args.add("--") 55 56 args.add(ctx.executable._process_wrapper_tester) 57 args.add(ctx.attr.test_config) 58 args.add("--current-dir", "${pwd}") 59 args.add("--test-subst", "subst key to ${key}") 60 61 extra_args = ctx.actions.args() 62 if combined or ctx.attr.test_config == "subst-pwd": 63 extra_args.set_param_file_format("multiline") 64 extra_args.use_param_file("@%s", use_always = True) 65 extra_args.add("${pwd}") 66 67 env = {"CURRENT_DIR": "${pwd}/test_path"} 68 69 ctx.actions.run( 70 executable = ctx.executable._process_wrapper, 71 inputs = ctx.files.env_files + ctx.files.arg_files, 72 outputs = outputs, 73 arguments = [args, extra_args], 74 env = env, 75 tools = [ctx.executable._process_wrapper_tester], 76 ) 77 78 return [DefaultInfo(files = depset(outputs))] 79 80process_wrapper_tester = rule( 81 implementation = _impl, 82 attrs = { 83 "arg_files": attr.label_list(), 84 "env_files": attr.label_list(), 85 "test_config": attr.string(mandatory = True), 86 "use_param_file": attr.bool(default = False), 87 "_process_wrapper": attr.label( 88 default = Label("//util/process_wrapper"), 89 executable = True, 90 allow_single_file = True, 91 cfg = "exec", 92 ), 93 "_process_wrapper_tester": attr.label( 94 default = Label("//test/process_wrapper:process_wrapper_tester"), 95 executable = True, 96 cfg = "exec", 97 ), 98 }, 99) 100