xref: /aosp_15_r20/external/grpc-grpc/test/csharp/codegen/csharp_codegen_simple_test.sh (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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=simple
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/helloworld.proto
44
45# log the files generated
46ls -l ./proto_out
47
48# Verify the output files exist
49# First file is generated by protoc.
50# Check it exists but don't check the contents.
51[ -e ${PROTO_OUT}/Helloworld.cs ] || {
52    echo >&2 "missing generated output, expecting Helloworld.cs"
53    exit 1
54}
55
56# Second file is generated by the plugin.
57# Check it exists and check the contents.
58[ -e ${PROTO_OUT}/HelloworldGrpc.cs ] || {
59    echo >&2 "missing generated output, expecting HelloworldGrpc"
60    exit 1
61}
62
63DIFFCMD="diff --strip-trailing-cr"
64
65# Diff to check the contents of the generated file
66$DIFFCMD ${DATA_DIR}/expected/HelloworldGrpc.cs ${PROTO_OUT}/HelloworldGrpc.cs
67# Silence shellcheck as it doesn't allow testing of $? and there isn't an easy
68# way around it with a command with several arguments.
69# shellcheck disable=SC2181
70if [ $? -ne 0 ]
71then
72    echo >&2 "Generated code does not match for HelloworldGrpc.cs"
73    exit 1
74fi
75
76# Run one extra command to clear $? before exiting the script to prevent
77# failing even when tests pass.
78echo "Plugin test: ${TESTNAME}: passed."
79