Skip to content

Commit

Permalink
Add empty proto file test
Browse files Browse the repository at this point in the history
* Add empty proto file test
* Add a preamble to `foo-messages.proto`
* Don't unconditionally add a dependency on `GRPCProtobuf` if there are
  no services
* Add the new test case code generation to `dev/protos/generate.sh`
* depend on `grpc-swift` `main` to pick up empty file generation
  changes.
  • Loading branch information
rnro committed Jan 7, 2025
1 parent 9f07522 commit f3164b2
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let products: [Product] = [
let dependencies: [Package.Dependency] = [
.package(
url: "https://github.com/grpc/grpc-swift.git",
exact: "2.0.0-beta.2"
branch: "main"
),
.package(
url: "https://github.com/apple/swift-protobuf.git",
Expand Down
8 changes: 5 additions & 3 deletions Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ package struct ProtobufCodeGenParser {

extension ProtobufCodeGenParser {
fileprivate func codeDependencies(file: FileDescriptor) -> [Dependency] {
var codeDependencies: [Dependency] = [
Dependency(module: "GRPCProtobuf", accessLevel: .internal)
]
var codeDependencies: [Dependency] = []

if file.services.count > 0 {
codeDependencies.append(Dependency(module: "GRPCProtobuf", accessLevel: .internal))
}

// If there's a dependency on a bundled proto then add the SwiftProtobuf import.
//
Expand Down
Binary file not shown.
Binary file modified Tests/GRPCProtobufCodeGenTests/Generated/foo-service.pb
Binary file not shown.
107 changes: 76 additions & 31 deletions Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,43 @@ import GRPCProtobufCodeGen
import SwiftProtobufPluginLibrary
import Testing

struct ProtobufCodeGeneratorTests: UsesDescriptorSet {
static let descriptorSetName = "test-service"
static let fileDescriptorName = "test-service"
struct ProtobufCodeGeneratorTests {
@Suite("Self-contained service (test-service.proto)")
struct TestService: UsesDescriptorSet {
static let descriptorSetName = "test-service"
static let fileDescriptorName = "test-service"

@Test("Generate", arguments: [SourceGenerator.Config.AccessLevel.internal, .public, .package])
func generate(accessLevel: SourceGenerator.Config.AccessLevel) throws {
let generator = ProtobufCodeGenerator(
config: SourceGenerator.Config(
accessLevel: accessLevel,
accessLevelOnImports: false,
client: true,
server: true,
indentation: 2
@Test("Generate", arguments: [SourceGenerator.Config.AccessLevel.internal, .public, .package])
func generate(accessLevel: SourceGenerator.Config.AccessLevel) throws {
let generator = ProtobufCodeGenerator(
config: SourceGenerator.Config(
accessLevel: accessLevel,
accessLevelOnImports: false,
client: true,
server: true,
indentation: 2
)
)
)

let access: String
switch accessLevel {
case .public:
access = "public"
case .internal:
access = "internal"
case .package:
access = "package"
default:
fatalError()
}
let access: String
switch accessLevel {
case .public:
access = "public"
case .internal:
access = "internal"
case .package:
access = "package"
default:
fatalError()
}

let generated = try generator.generateCode(
fileDescriptor: Self.fileDescriptor,
protoFileModuleMappings: ProtoFileToModuleMappings(),
extraModuleImports: []
)
let generated = try generator.generateCode(
fileDescriptor: Self.fileDescriptor,
protoFileModuleMappings: ProtoFileToModuleMappings(),
extraModuleImports: []
)

let expected = """
let expected = """
/// Leading trivia.
// DO NOT EDIT.
Expand Down Expand Up @@ -1058,6 +1060,49 @@ struct ProtobufCodeGeneratorTests: UsesDescriptorSet {
}
"""

#expect(generated == expected)
#expect(generated == expected)
}
}

@Suite("File-without-services (foo-messages.proto)")
struct NoServices: UsesDescriptorSet {
static let descriptorSetName = "foo-messages"
static let fileDescriptorName = "foo-messages"

@Test("Generate")
func generate() throws {
let generator = ProtobufCodeGenerator(
config: SourceGenerator.Config(
accessLevel: .public,
accessLevelOnImports: false,
client: true,
server: true,
indentation: 2
)
)

let generated = try generator.generateCode(
fileDescriptor: Self.fileDescriptor,
protoFileModuleMappings: ProtoFileToModuleMappings(),
extraModuleImports: []
)

let expected = """
/// Leading trivia.
// DO NOT EDIT.
// swift-format-ignore-file
//
// Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
// Source: foo-messages.proto
//
// For information on using the generated types, please see the documentation:
// https://github.com/grpc/grpc-swift
// This file contained no services.
"""

#expect(generated == expected)
}
}
}
12 changes: 12 additions & 0 deletions dev/protos/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ function generate_foo_service_descriptor_set {
--include_imports
}

function generate_foo_messages_descriptor_set {
local proto proto_path output
proto="$here/local/foo-messages.proto"
proto_path="$(dirname "$proto")"
output="$root/Tests/GRPCProtobufCodeGenTests/Generated/foo-messages.pb"

invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
--include_source_info \
--include_imports
}

function generate_bar_service_descriptor_set {
local proto proto_path output
proto="$here/local/bar-service.proto"
Expand Down Expand Up @@ -152,5 +163,6 @@ generate_error_service
# Descriptor sets for tests
generate_test_service_descriptor_set
generate_foo_service_descriptor_set
generate_foo_messages_descriptor_set
generate_bar_service_descriptor_set
generate_wkt_service_descriptor_set
1 change: 1 addition & 0 deletions dev/protos/local/foo-messages.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Leading trivia.
syntax = "proto3";

package foo;
Expand Down

0 comments on commit f3164b2

Please sign in to comment.