1# Copyright 2020 Google LLC 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# https://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"""Compare rule output to a golden version.""" 16 17def golden_test( 18 name, 19 golden, 20 subject): 21 """Check that output from a rule matches the expected output. 22 23 Args: 24 name: test name 25 golden: expected content of subect 26 subject: build target 27 """ 28 native.sh_test( 29 name = name, 30 size = "medium", 31 srcs = ["@rules_license//tools:diff_test.sh"], 32 args = [ 33 "$(location %s)" % golden, 34 "$(location %s)" % subject, 35 ], 36 data = [ 37 subject, 38 golden, 39 ], 40 ) 41 42def golden_cmd_test( 43 name, 44 cmd, 45 golden, # Required 46 toolchains = [], 47 tools = None, 48 srcs = [], # Optional 49 **kwargs): # Rest 50 """Compares cmd output to golden output, passes if they are identical. 51 52 Args: 53 name: Name of the build rule. 54 cmd: The command to run to generate output. 55 golden: The golden file to be compared. 56 toolchains: List of toolchains needed to run the command, passed to genrule. 57 tools: List of tools needed to run the command, passed to genrule. 58 srcs: List of sources needed as input to the command, passed to genrule. 59 **kwargs: Any additional parameters for the generated golden_test. 60 """ 61 actual = name + ".output" 62 63 native.genrule( 64 name = name + "_output", 65 srcs = srcs, 66 outs = [actual], 67 cmd = cmd + " > '$@'", # Redirect to collect output 68 toolchains = toolchains, 69 tools = tools, 70 testonly = True, 71 ) 72 73 golden_test( 74 name = name, 75 subject = actual, 76 golden = golden, 77 **kwargs 78 ) 79