1#!/bin/bash 2# Copyright 2023 gRPC authors. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16# Run this script via bazel test 17# It expects that protoc and grpc_csharp_plugin have already been built. 18 19# Simple test - compare generated output to expected files 20 21set -x 22 23TESTNAME=deprecated 24 25# protoc and grpc_csharp_plugin binaries are supplied as "data" in bazel 26PROTOC=./external/com_google_protobuf/protoc 27PLUGIN=./src/compiler/grpc_csharp_plugin 28 29# where to find the test data 30DATA_DIR=./test/csharp/codegen/${TESTNAME} 31 32# output directory for the generated files 33PROTO_OUT=./proto_out 34rm -rf ${PROTO_OUT} 35mkdir -p ${PROTO_OUT} 36 37# run protoc and the plugin 38$PROTOC \ 39 --plugin=protoc-gen-grpc=$PLUGIN \ 40 --csharp_out=${PROTO_OUT} \ 41 --grpc_out=${PROTO_OUT} \ 42 -I ${DATA_DIR}/proto \ 43 ${DATA_DIR}/proto/depnothing.proto \ 44 ${DATA_DIR}/proto/depservice.proto \ 45 ${DATA_DIR}/proto/depmethod.proto 46 47# log the files generated 48ls -l ./proto_out 49 50# Rather than doing a diff against a known file, just using grep to 51# check for some of the code changes when "deprecated" is specified. 52# This isn't bullet proof but does avoid tests breaking when other 53# codegen changes are made. 54 55# For depnothing.proto there should zero "ObsoleteAttribute" found 56nmatches=$(grep -c ObsoleteAttribute ${PROTO_OUT}/DepnothingGrpc.cs) 57if [ "$nmatches" -ne 0 ] 58then 59 echo >&2 "Unexpected ObsoleteAttribute in DepnothingGrpc.cs" 60 exit 1 61fi 62 63# For depservice.proto need to check ObsoleteAttribute added to three classes. 64# First check ObsoleteAttribute exists in output 65nmatches=$(grep -c ObsoleteAttribute ${PROTO_OUT}/DepserviceGrpc.cs) 66if [ "$nmatches" -eq 0 ] 67then 68 echo >&2 "Missing ObsoleteAttribute in DepserviceGrpc.cs" 69 exit 1 70fi 71 72# capture context after ObsoleteAttribute for further checking 73CTX=$(grep -A 2 ObsoleteAttribute ${PROTO_OUT}/DepserviceGrpc.cs) 74 75# Check ObsoleteAttribute before class GreeterServiceLevelDep 76nmatches=$(echo "$CTX" | grep -c "class GreeterServiceLevelDep$" ) 77if [ "$nmatches" -ne 1 ] 78then 79 echo >&2 "Missing ObsoleteAttribute on class GreeterServiceLevelDep" 80 exit 1 81fi 82# Check ObsoleteAttribute before class GreeterServiceLevelDepBase 83nmatches=$(echo "$CTX" | grep -c "class GreeterServiceLevelDepBase$" ) 84if [ "$nmatches" -ne 1 ] 85then 86 echo >&2 "Missing ObsoleteAttribute on class GreeterServiceLevelDepBase" 87 exit 1 88fi 89# Check ObsoleteAttribute before class GreeterServiceLevelDepClient 90nmatches=$(echo "$CTX" | grep -c "class GreeterServiceLevelDepClient" ) 91if [ "$nmatches" -ne 1 ] 92then 93 echo >&2 "Missing ObsoleteAttribute on class GreeterServiceLevelDepClient" 94 exit 1 95fi 96 97# For depmethod.proto need to check ObsoleteAttribute added in five places for SayHello method. 98# Check ObsoleteAttribute exists in output 99nmatches=$(grep -c ObsoleteAttribute ${PROTO_OUT}/DepmethodGrpc.cs) 100if [ "$nmatches" -eq 0 ] 101then 102 echo >&2 "Missing ObsoleteAttribute in DepmethodGrpc.cs" 103 exit 1 104fi 105# Check ObsoleteAttribute before SayHello methods 106nmatches=$(grep -A 2 ObsoleteAttribute ${PROTO_OUT}/DepmethodGrpc.cs | grep -c SayHello) 107if [ "$nmatches" -ne 5 ] 108then 109 echo >&2 "Missing ObsoleteAttribute on SayHello method" 110 exit 1 111fi 112 113# Run one extra command to clear $? before exiting the script to prevent 114# failing even when tests pass. 115echo "Plugin test: ${TESTNAME}: passed." 116