xref: /aosp_15_r20/external/grpc-grpc/test/csharp/codegen/csharp_codegen_base_namespace_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=basenamespace
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 specifying the base_namespace options
38$PROTOC \
39    --plugin=protoc-gen-grpc-csharp=$PLUGIN \
40    --csharp_out=${PROTO_OUT} \
41    --grpc-csharp_out=${PROTO_OUT} \
42    --csharp_opt=base_namespace=Example \
43    --grpc-csharp_opt=base_namespace=Example \
44    -I ${DATA_DIR}/proto \
45    ${DATA_DIR}/proto/namespacetest.proto
46
47# log the files generated
48ls -lR ./proto_out
49
50# Verify the output files exist in the right location.
51# The base_namespace option does not change the generated code just
52# the location of the files. Contents are not checked in this test.
53
54# The C# namespace option in the proto file of "Example.V1.CodegenTest"
55# combined with the command line options above should mean the generated files
56# are created in the output directory "V1/CodegenTest"
57
58# First file is generated by protoc.
59[ -e ${PROTO_OUT}/V1/CodegenTest/Namespacetest.cs ] || {
60    echo >&2 "missing generated output, expecting V1/CodegenTest/Namespacetest.cs"
61    exit 1
62}
63
64# Second file is generated by the plugin.
65[ -e ${PROTO_OUT}/V1/CodegenTest/NamespacetestGrpc.cs ] || {
66    echo >&2 "missing generated output, expecting V1/CodegenTest/NamespacetestGrpc.cs"
67    exit 1
68}
69
70# Run again with base_namespace option set to empty value to check that the files
71# are created under a full directory structure defined by the namespace
72rm -rf ${PROTO_OUT}
73mkdir -p ${PROTO_OUT}
74
75$PROTOC \
76    --plugin=protoc-gen-grpc-csharp=$PLUGIN \
77    --csharp_out=${PROTO_OUT} \
78    --grpc-csharp_out=${PROTO_OUT} \
79    --csharp_opt=base_namespace= \
80    --grpc-csharp_opt=base_namespace= \
81    -I ${DATA_DIR}/proto \
82    ${DATA_DIR}/proto/namespacetest.proto
83
84# log the files generated
85ls -lR ./proto_out
86
87# Verify the output files exist in the right location.
88
89# The C# namespace option in the proto file of "Example.V1.CodegenTest"
90# combined with the command line options above should mean the generated files
91# are created in the output directory "Example/V1/CodegenTest"
92
93# First file is generated by protoc.
94[ -e ${PROTO_OUT}/Example/V1/CodegenTest/Namespacetest.cs ] || {
95    echo >&2 "missing generated output, expecting Example/V1/CodegenTest/Namespacetest.cs"
96    exit 1
97}
98
99# Second file is generated by the plugin.
100[ -e ${PROTO_OUT}/Example/V1/CodegenTest/NamespacetestGrpc.cs ] || {
101    echo >&2 "missing generated output, expecting Example/V1/CodegenTest/NamespacetestGrpc.cs"
102    exit 1
103}
104
105# Run again without the base_namespace options to check the files are created
106# in the root of the output directory
107
108rm -rf ${PROTO_OUT}
109mkdir -p ${PROTO_OUT}
110
111$PROTOC \
112    --plugin=protoc-gen-grpc-csharp=$PLUGIN \
113    --csharp_out=${PROTO_OUT} \
114    --grpc-csharp_out=${PROTO_OUT} \
115    -I ${DATA_DIR}/proto \
116    ${DATA_DIR}/proto/namespacetest.proto
117
118ls -lR ./proto_out
119
120[ -e ${PROTO_OUT}/Namespacetest.cs ] || {
121    echo >&2 "missing generated output, expecting Namespacetest.cs"
122    exit 1
123}
124
125[ -e ${PROTO_OUT}/NamespacetestGrpc.cs ] || {
126    echo >&2 "missing generated output, expecting NamespacetestGrpc.cs"
127    exit 1
128}
129
130# Run one extra command to clear $? before exiting the script to prevent
131# failing even when tests pass.
132echo "Plugin test: ${TESTNAME}: passed."
133