setup-plugin-tests.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/bin/bash
  2. ## Copyright 2024, gRPC Authors All rights reserved.
  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. set -euo pipefail
  16. log() { printf -- "** %s\n" "$*" >&2; }
  17. error() { printf -- "** ERROR: %s\n" "$*" >&2; }
  18. fatal() { error "$@"; exit 1; }
  19. output_directory="${PLUGIN_TESTS_OUTPUT_DIRECTORY:=$(mktemp -d)}"
  20. here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  21. grpc_swift_protobuf_directory="$(readlink -f "${here}/..")"
  22. resources_directory="$(readlink -f "${grpc_swift_protobuf_directory}/IntegrationTests/PluginTests/Resources")"
  23. config="${resources_directory}/Config"
  24. sources="${resources_directory}/Sources"
  25. protos="${resources_directory}/Protos"
  26. scratch_directory="$(mktemp -d)"
  27. package_manifest="${scratch_directory}/Package.swift"
  28. echo "Output directory: $output_directory"
  29. echo "grpc-swift-protobuf directory: $grpc_swift_protobuf_directory"
  30. # modify Package.swift
  31. cp "${resources_directory}/Sources/Package.swift" "${scratch_directory}/"
  32. cat >> "${package_manifest}" <<- EOM
  33. package.dependencies.append(
  34. .package(path: "$grpc_swift_protobuf_directory")
  35. )
  36. EOM
  37. function test_dir_name {
  38. # $FUNCNAME is a stack of function names. The 0th element is the name of this
  39. # function, so the 1st element is the calling function.
  40. echo "${output_directory}/${FUNCNAME[1]}"
  41. }
  42. function test_01_top_level_config_file {
  43. # .
  44. # ├── Package.swift
  45. # └── Sources
  46. # ├── HelloWorldAdopter.swift
  47. # ├── Protos
  48. # │ └── HelloWorld.proto
  49. # └── grpc-swift-proto-generator-config.json
  50. local -r test_dir=$(test_dir_name)
  51. mkdir -p "${test_dir}/Sources/Protos"
  52. cp "${package_manifest}" "${test_dir}/"
  53. cp "${sources}/HelloWorldAdopter.swift" "${test_dir}/Sources/"
  54. cp "${protos}/HelloWorld/HelloWorld.proto" "${test_dir}/Sources/Protos"
  55. cp "${config}/internal-grpc-swift-proto-generator-config.json" "${test_dir}/Sources/grpc-swift-proto-generator-config.json"
  56. }
  57. function test_02_peer_config_file {
  58. # .
  59. # ├── Package.swift
  60. # └── Sources
  61. # ├── HelloWorldAdopter.swift
  62. # └── Protos
  63. # ├── HelloWorld.proto
  64. # └── grpc-swift-proto-generator-config.json
  65. local -r test_dir=$(test_dir_name)
  66. mkdir -p "${test_dir}/Sources/Protos"
  67. cp "${package_manifest}" "${test_dir}/"
  68. cp "${sources}/HelloWorldAdopter.swift" "${test_dir}/Sources/"
  69. cp "${protos}/HelloWorld/HelloWorld.proto" "${test_dir}/Sources/Protos/"
  70. cp "${config}/internal-grpc-swift-proto-generator-config.json" "${test_dir}/Sources/Protos/grpc-swift-proto-generator-config.json"
  71. }
  72. function test_03_separate_service_message_protos {
  73. # .
  74. # ├── Package.swift
  75. # └── Sources
  76. # ├── HelloWorldAdopter.swift
  77. # └── Protos
  78. # ├── Messages.proto
  79. # ├── Service.proto
  80. # └── grpc-swift-proto-generator-config.json
  81. local -r test_dir=$(test_dir_name)
  82. mkdir -p "${test_dir}/Sources/Protos"
  83. cp "${package_manifest}" "${test_dir}/"
  84. cp "${sources}/HelloWorldAdopter.swift" "${test_dir}/Sources/"
  85. cp "${config}/internal-grpc-swift-proto-generator-config.json" "${test_dir}/Sources/Protos/grpc-swift-proto-generator-config.json"
  86. cp "${protos}/HelloWorld/Service.proto" "${test_dir}/Sources/Protos/"
  87. cp "${protos}/HelloWorld/Messages.proto" "${test_dir}/Sources/Protos/"
  88. }
  89. function test_04_cross_directory_imports {
  90. # .
  91. # ├── Package.swift
  92. # └── Sources
  93. # ├── HelloWorldAdopter.swift
  94. # └── Protos
  95. # ├── directory_1
  96. # │ ├── Messages.proto
  97. # │ └── grpc-swift-proto-generator-config.json
  98. # └── directory_2
  99. # ├── Service.proto
  100. # └── grpc-swift-proto-generator-config.json
  101. local -r test_dir=$(test_dir_name)
  102. mkdir -p "${test_dir}/Sources/Protos/directory_1"
  103. mkdir -p "${test_dir}/Sources/Protos/directory_2"
  104. cp "${package_manifest}" "${test_dir}/"
  105. cp "${sources}/HelloWorldAdopter.swift" "${test_dir}/Sources/"
  106. cp "${config}/internal-grpc-swift-proto-generator-config.json" "${test_dir}/Sources/Protos/directory_1/grpc-swift-proto-generator-config.json"
  107. cp "${config}/import-directory-1-grpc-swift-proto-generator-config.json" "${test_dir}/Sources/Protos/directory_2/grpc-swift-proto-generator-config.json"
  108. cp "${protos}/HelloWorld/Service.proto" "${test_dir}/Sources/Protos/directory_2/"
  109. cp "${protos}/HelloWorld/Messages.proto" "${test_dir}/Sources/Protos/directory_1/"
  110. }
  111. function test_05_two_definitions {
  112. # .
  113. # ├── Package.swift
  114. # └── Sources
  115. # ├── FooHelloWorldAdopter.swift
  116. # └── Protos
  117. # ├── Foo
  118. # │ ├── foo-messages.proto
  119. # │ └── foo-service.proto
  120. # ├── HelloWorld
  121. # │ └── HelloWorld.proto
  122. # └── grpc-swift-proto-generator-config.json
  123. local -r test_dir=$(test_dir_name)
  124. mkdir -p "${test_dir}/Sources/Protos/HelloWorld"
  125. mkdir -p "${test_dir}/Sources/Protos/Foo"
  126. cp "${package_manifest}" "${test_dir}/"
  127. cp "${sources}/FooHelloWorldAdopter.swift" "${test_dir}/Sources/"
  128. cp "${protos}/HelloWorld/HelloWorld.proto" "${test_dir}/Sources/Protos/HelloWorld/"
  129. cp "${config}/internal-grpc-swift-proto-generator-config.json" "${test_dir}/Sources/Protos/grpc-swift-proto-generator-config.json"
  130. cp "${protos}/Foo/foo-messages.proto" "${test_dir}/Sources/Protos/Foo/"
  131. cp "${protos}/Foo/foo-service.proto" "${test_dir}/Sources/Protos/Foo/"
  132. }
  133. function test_06_nested_definitions {
  134. # .
  135. # ├── Package.swift
  136. # └── Sources
  137. # ├── FooHelloWorldAdopter.swift
  138. # └── Protos
  139. # └── HelloWorld
  140. # ├── FooDefinitions
  141. # │ ├── Foo
  142. # │ │ ├── foo-messages.proto
  143. # │ │ └── foo-service.proto
  144. # │ └── grpc-swift-proto-generator-config.json
  145. # ├── HelloWorld.proto
  146. # └── grpc-swift-proto-generator-config.json
  147. local -r test_dir=$(test_dir_name)
  148. mkdir -p "${test_dir}/Sources/Protos/HelloWorld/FooDefinitions/Foo"
  149. cp "${package_manifest}" "${test_dir}/"
  150. cp "${sources}/FooHelloWorldAdopter.swift" "${test_dir}/Sources/"
  151. cp "${protos}/HelloWorld/HelloWorld.proto" "${test_dir}/Sources/Protos/HelloWorld/"
  152. cp "${config}/internal-grpc-swift-proto-generator-config.json" "${test_dir}/Sources/Protos/HelloWorld/grpc-swift-proto-generator-config.json"
  153. cp "${config}/public-grpc-swift-proto-generator-config.json" "${test_dir}/Sources/Protos/HelloWorld/FooDefinitions/grpc-swift-proto-generator-config.json"
  154. cp "${protos}/Foo/foo-messages.proto" "${test_dir}/Sources/Protos/HelloWorld/FooDefinitions/Foo/"
  155. cp "${protos}/Foo/foo-service.proto" "${test_dir}/Sources/Protos/HelloWorld/FooDefinitions/Foo/"
  156. }
  157. function test_07_duplicated_proto_file_name {
  158. # .
  159. # ├── Package.swift
  160. # └── Sources
  161. # ├── NoOp.swift
  162. # └── Protos
  163. # ├── grpc-swift-proto-generator-config.json
  164. # ├── noop
  165. # │ └── noop.proto
  166. # └── noop2
  167. # └── noop.proto
  168. local -r test_dir=$(test_dir_name)
  169. mkdir -p "${test_dir}/Sources/Protos"
  170. cp "${package_manifest}" "${test_dir}/"
  171. mkdir -p "${test_dir}/Sources/Protos"
  172. cp -rp "${protos}/noop" "${test_dir}/Sources/Protos"
  173. cp -rp "${protos}/noop2" "${test_dir}/Sources/Protos"
  174. cp "${sources}/NoOp.swift" "${test_dir}/Sources"
  175. cp "${config}/internal-grpc-swift-proto-generator-config.json" "${test_dir}/Sources/Protos/grpc-swift-proto-generator-config.json"
  176. }
  177. test_01_top_level_config_file
  178. test_02_peer_config_file
  179. test_03_separate_service_message_protos
  180. test_04_cross_directory_imports
  181. test_05_two_definitions
  182. test_06_nested_definitions
  183. test_07_duplicated_proto_file_name