Skip to content

Commit

Permalink
Test SwiftPM plugin for v2 (grpc#1957)
Browse files Browse the repository at this point in the history
Motivation:

The SwiftPM plugin integration test only tests the plugin for gRPC Swift
v1.

Modification:

- Allow the test to run for v2

Result:

Better coverage
  • Loading branch information
glbrntt authored Jul 2, 2024
1 parent 36ffcb4 commit 754bdad
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,19 @@ jobs:
include:
- image: swiftlang/swift:nightly-jammy
swift-tools-version: '6.0'
supports-v2: true
- image: swiftlang/swift:nightly-6.0-jammy
swift-tools-version: '6.0'
supports-v2: true
- image: swift:5.10.1-noble
swift-tools-version: '5.10'
supports-v2: false
- image: swift:5.9-jammy
swift-tools-version: '5.9'
supports-v2: false
- image: swift:5.8-focal
swift-tools-version: '5.8'
supports-v2: false
name: Integration Tests on ${{ matrix.image }}
runs-on: ubuntu-latest
container:
Expand All @@ -157,8 +162,11 @@ jobs:
- uses: actions/checkout@v4
- name: Install protoc
run: apt update && apt install -y protobuf-compiler
- name: SwiftPM plugin test
run: ./scripts/run-plugin-tests.sh ${{ matrix.swift-tools-version }}
- name: SwiftPM plugin test (v1)
run: ./scripts/run-plugin-tests.sh ${{ matrix.swift-tools-version }} "v1"
- name: SwiftPM plugin test (v2)
if: ${{ matrix.supports-v2 }}
run: ./scripts/run-plugin-tests.sh ${{ matrix.swift-tools-version }} "v2"
- name: Build without NIOSSL
run: swift build
env:
Expand Down
14 changes: 12 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -810,13 +810,20 @@ extension Product {
)
}

static var grpcCore: Product {
static var _grpcCore: Product {
.library(
name: "_GRPCCore",
targets: ["GRPCCore"]
)
}

static var _grpcProtobuf: Product {
.library(
name: "_GRPCProtobuf",
targets: ["GRPCProtobuf"]
)
}

static var cgrpcZlib: Product {
.library(
name: cgrpcZlibProductName,
Expand Down Expand Up @@ -851,12 +858,15 @@ extension Product {
let package = Package(
name: grpcPackageName,
products: [
// v1
.grpc,
.grpcCore,
.cgrpcZlib,
.grpcReflectionService,
.protocGenGRPCSwift,
.grpcSwiftPlugin,
// v2
._grpcCore,
._grpcProtobuf,
],
dependencies: packageDependencies,
targets: [
Expand Down
67 changes: 46 additions & 21 deletions scripts/run-plugin-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GRPC_PATH="${HERE}/.."

function generate_package_manifest {
local version=$1
local tools_version=$1
local grpc_path=$2
local grpc_version=$3

echo "// swift-tools-version: $version"
echo "// swift-tools-version: $tools_version"
echo "import PackageDescription"
echo ""
echo "let package = Package("
Expand All @@ -36,7 +37,14 @@ function generate_package_manifest {
echo " .executableTarget("
echo " name: \"Foo\","
echo " dependencies: ["
echo " .product(name: \"GRPC\", package: \"grpc-swift\"),"

if [ "$grpc_version" == "v1" ]; then
echo " .product(name: \"GRPC\", package: \"grpc-swift\"),"
elif [ "$grpc_version" == "v2" ]; then
echo " .product(name: \"_GRPCCore\", package: \"grpc-swift\"),"
echo " .product(name: \"_GRPCProtobuf\", package: \"grpc-swift\"),"
fi

echo " ],"
echo " path: \"Sources/Foo\","
echo " plugins: ["
Expand All @@ -49,20 +57,30 @@ function generate_package_manifest {
}

function generate_grpc_plugin_config {
cat <<EOF
{
"invocations": [
{
"protoFiles": ["Foo.proto"],
"visibility": "internal"
}
]
}
EOF
local grpc_version=$1

echo "{"
echo " \"invocations\": ["
echo " {"
if [ "$grpc_version" == "v2" ]; then
echo " \"_V2\": true,"
fi
echo " \"protoFiles\": [\"Foo.proto\"],"
echo " \"visibility\": \"internal\""
echo " }"
echo " ]"
echo "}"
}

function generate_protobuf_plugin_config {
generate_grpc_plugin_config
echo "{"
echo " \"invocations\": ["
echo " {"
echo " \"protoFiles\": [\"Foo.proto\"],"
echo " \"visibility\": \"internal\""
echo " }"
echo " ]"
echo "}"
}

function generate_proto {
Expand All @@ -82,31 +100,38 @@ function generate_main {
}

function generate_and_build {
local version=$1
local tools_version=$1
local grpc_path=$2
local grpc_version=$3
local protoc_path dir

protoc_path=$(which protoc)
dir=$(mktemp -d)

echo "Generating package in $dir ..."
echo "Swift tools version: $version"
echo "Swift tools version: $tools_version"
echo "grpc-swift version: $grpc_version"
echo "grpc-swift path: $grpc_path"
echo "protoc path: $protoc_path"
mkdir -p "$dir/Sources/Foo"

generate_package_manifest "$version" "$grpc_path" > "$dir/Package.swift"
generate_grpc_plugin_config > "$dir/Sources/Foo/grpc-swift-config.json"
generate_package_manifest "$tools_version" "$grpc_path" "$grpc_version" > "$dir/Package.swift"

generate_protobuf_plugin_config > "$dir/Sources/Foo/swift-protobuf-config.json"
generate_proto > "$dir/Sources/Foo/Foo.proto"
generate_main > "$dir/Sources/Foo/main.swift"
generate_grpc_plugin_config "$grpc_version" > "$dir/Sources/Foo/grpc-swift-config.json"

PROTOC_PATH=$protoc_path swift build --package-path "$dir"
}

if [[ $# -lt 1 ]]; then
echo "Usage: $0 SWIFT_TOOLS_VERSION"
if [[ $# -lt 2 ]]; then
echo "Usage: $0 SWIFT_TOOLS_VERSION GRPC_SWIFT_VERSION"
fi

if [ "$2" != "v1" ] && [ "$2" != "v2" ]; then
echo "Invalid gRPC Swift version '$2' (must be 'v1' or 'v2')"
exit 1
fi

generate_and_build "$1" "${GRPC_PATH}"
generate_and_build "$1" "${GRPC_PATH}" "$2"

0 comments on commit 754bdad

Please sign in to comment.