1*14675a02SAndroid Build Coastguard Worker# Copyright 2018 Google LLC 2*14675a02SAndroid Build Coastguard Worker# 3*14675a02SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*14675a02SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*14675a02SAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*14675a02SAndroid Build Coastguard Worker# 7*14675a02SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*14675a02SAndroid Build Coastguard Worker# 9*14675a02SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*14675a02SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*14675a02SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*14675a02SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*14675a02SAndroid Build Coastguard Worker# limitations under the License. 14*14675a02SAndroid Build Coastguard Worker 15*14675a02SAndroid Build Coastguard Worker"""Provides the golden_file_test() rule.""" 16*14675a02SAndroid Build Coastguard Worker 17*14675a02SAndroid Build Coastguard WorkerGOLDEN_FILE_CHECKER_TEMPLATE = """ 18*14675a02SAndroid Build Coastguard Worker#!/bin/bash 19*14675a02SAndroid Build Coastguard Workerset -o nounset 20*14675a02SAndroid Build Coastguard Workerset -o errexit 21*14675a02SAndroid Build Coastguard Worker 22*14675a02SAndroid Build Coastguard Workerreadonly EXPECTED={EXPECTED} 23*14675a02SAndroid Build Coastguard Workerreadonly ACTUAL={ACTUAL} 24*14675a02SAndroid Build Coastguard Worker 25*14675a02SAndroid Build Coastguard Workerdiff -u --label EXPECTED "$EXPECTED" --label ACTUAL "$ACTUAL" 26*14675a02SAndroid Build Coastguard Workerreadonly r=$? 27*14675a02SAndroid Build Coastguard Worker 28*14675a02SAndroid Build Coastguard Workerif (( $r != 0 )); then 29*14675a02SAndroid Build Coastguard Worker echo "***" >&2 30*14675a02SAndroid Build Coastguard Worker echo "*** FAIL: Contents of $ACTUAL do not match $EXPECTED" >&2 31*14675a02SAndroid Build Coastguard Worker echo "***" >&2 32*14675a02SAndroid Build Coastguard Workerfi 33*14675a02SAndroid Build Coastguard Worker 34*14675a02SAndroid Build Coastguard Workerexit $r 35*14675a02SAndroid Build Coastguard Worker""" 36*14675a02SAndroid Build Coastguard Worker 37*14675a02SAndroid Build Coastguard Workerdef _golden_file_test_impl(ctx): 38*14675a02SAndroid Build Coastguard Worker replacements = { 39*14675a02SAndroid Build Coastguard Worker "{EXPECTED}": repr(ctx.file.expected.short_path), 40*14675a02SAndroid Build Coastguard Worker "{ACTUAL}": repr(ctx.file.actual.short_path), 41*14675a02SAndroid Build Coastguard Worker } 42*14675a02SAndroid Build Coastguard Worker 43*14675a02SAndroid Build Coastguard Worker contents = GOLDEN_FILE_CHECKER_TEMPLATE 44*14675a02SAndroid Build Coastguard Worker for k, v in replacements.items(): 45*14675a02SAndroid Build Coastguard Worker contents = contents.replace(k, v) 46*14675a02SAndroid Build Coastguard Worker 47*14675a02SAndroid Build Coastguard Worker ctx.actions.write( 48*14675a02SAndroid Build Coastguard Worker ctx.outputs.sh, 49*14675a02SAndroid Build Coastguard Worker contents, 50*14675a02SAndroid Build Coastguard Worker is_executable = True, 51*14675a02SAndroid Build Coastguard Worker ) 52*14675a02SAndroid Build Coastguard Worker 53*14675a02SAndroid Build Coastguard Worker runfiles = ctx.runfiles(files = [ctx.file.expected, ctx.file.actual]) 54*14675a02SAndroid Build Coastguard Worker return [DefaultInfo(executable = ctx.outputs.sh, runfiles = runfiles)] 55*14675a02SAndroid Build Coastguard Worker 56*14675a02SAndroid Build Coastguard Workergolden_file_test = rule( 57*14675a02SAndroid Build Coastguard Worker implementation = _golden_file_test_impl, 58*14675a02SAndroid Build Coastguard Worker outputs = {"sh": "%{name}.sh"}, 59*14675a02SAndroid Build Coastguard Worker test = True, 60*14675a02SAndroid Build Coastguard Worker attrs = { 61*14675a02SAndroid Build Coastguard Worker "actual": attr.label(allow_single_file = True), 62*14675a02SAndroid Build Coastguard Worker "expected": attr.label(allow_single_file = True), 63*14675a02SAndroid Build Coastguard Worker }, 64*14675a02SAndroid Build Coastguard Worker) 65*14675a02SAndroid Build Coastguard Worker"""Checks that two files are equal; fails with a text diff otherwise.""" 66