Skip to content

Commit

Permalink
Break the tests dependency on the compiler plugin proto.
Browse files Browse the repository at this point in the history
The tests can't be completely isolated from all upstream protos because they do
need to use descriptor.proto, but this breaks the last dependency on an upstream
file to continue minimizing the risk of the tests breaking from an upstream
change.
  • Loading branch information
thomasvl committed Aug 23, 2024
1 parent ee2fefc commit 0e46730
Show file tree
Hide file tree
Showing 10 changed files with 1,737 additions and 2,145 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ regenerate-test-protos: build ${PROTOC_GEN_SWIFT} Protos/SwiftProtobufTests/gene
-I Protos/SwiftProtobufPluginLibrary \
-I Protos/SwiftProtobufPluginLibraryTests \
--tfiws_opt=FileNaming=DropPath \
--tfiws_opt=ProtoPathModuleMappings=Protos/SwiftProtobufPluginLibraryTests/module_mappings.pbascii \
--tfiws_out=Tests/SwiftProtobufPluginLibraryTests \
`find Protos/SwiftProtobufPluginLibraryTests -type f -name "*.proto"`

Expand All @@ -353,10 +352,10 @@ regenerate-fuzz-protos: build ${PROTOC_GEN_SWIFT}
SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS= \
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test.proto \
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test2.proto \
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test_import.proto \
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_delimited.proto \
Protos/SwiftProtobufPluginLibraryTests/unittest_delimited.proto \
Protos/SwiftProtobufPluginLibraryTests/unittest_delimited_import.proto \
Protos/SwiftProtobufPluginLibrary/google/protobuf/compiler/plugin.proto \
Protos/SwiftProtobufPluginLibrary/swift_protobuf_module_mappings.proto

Tests/SwiftProtobufPluginLibraryTests/DescriptorTestData.swift: build ${PROTOC_GEN_SWIFT} ${SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package swift_descriptor_test;
option swift_prefix = "SDT";

import "google/protobuf/descriptor.proto";
import "google/protobuf/compiler/plugin.proto";
import "pluginlib_descriptor_test_import.proto";

enum TopLevelEnum {
VALUE_ZERO = 0;
Expand Down Expand Up @@ -62,7 +62,7 @@ message TopLevelMessage2 {

message ExternalRefs {
optional google.protobuf.DescriptorProto desc = 1;
optional google.protobuf.compiler.Version ver = 2;
optional swift_descriptor_test.import.Version ver = 2;
}

extend google.protobuf.FieldOptions {
Expand All @@ -77,7 +77,7 @@ message ScoperForExt {
}

service SomeService {
rpc Foo(google.protobuf.DescriptorProto) returns (google.protobuf.compiler.Version);
rpc Foo(google.protobuf.DescriptorProto) returns (swift_descriptor_test.import.Version);
rpc Bar(TopLevelMessage) returns (TopLevelMessage2);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test_import.proto - test proto
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
// -----------------------------------------------------------------------------
///
/// Test proto for Tests/SwiftProtobufPluginLibraryTests/Test_Descriptor.swift
///
// -----------------------------------------------------------------------------

syntax = "proto2";

package swift_descriptor_test.import;

message Version {
optional int32 major = 1;
optional int32 minor = 2;
optional int32 patch = 3;
optional string suffix = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ struct SDTExternalRefs: Sendable {
/// Clears the value of `desc`. Subsequent reads from it will return its default value.
mutating func clearDesc() {self._desc = nil}

var ver: Google_Protobuf_Compiler_Version {
get {return _ver ?? Google_Protobuf_Compiler_Version()}
var ver: SwiftDescriptorTest_Import_Version {
get {return _ver ?? SwiftDescriptorTest_Import_Version()}
set {_ver = newValue}
}
/// Returns true if `ver` has been explicitly set.
Expand All @@ -267,7 +267,7 @@ struct SDTExternalRefs: Sendable {
init() {}

fileprivate var _desc: SwiftProtobuf.Google_Protobuf_DescriptorProto? = nil
fileprivate var _ver: Google_Protobuf_Compiler_Version? = nil
fileprivate var _ver: SwiftDescriptorTest_Import_Version? = nil
}

struct SDTScoperForExt: Sendable {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: pluginlib_descriptor_test_import.proto
//
// For information on using the generated types, please see the documentation:
// https://github.com/apple/swift-protobuf/

// Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test_import.proto - test proto
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
// -----------------------------------------------------------------------------
///
/// Test proto for Tests/SwiftProtobufPluginLibraryTests/Test_Descriptor.swift
///
// -----------------------------------------------------------------------------

import Foundation
import SwiftProtobuf

// If the compiler emits an error on this type, it is because this file
// was generated by a version of the `protoc` Swift plug-in that is
// incompatible with the version of SwiftProtobuf to which you are linking.
// Please ensure that you are building against the same version of the API
// that was used to generate this file.
fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck {
struct _2: SwiftProtobuf.ProtobufAPIVersion_2 {}
typealias Version = _2
}

struct SwiftDescriptorTest_Import_Version: Sendable {
// SwiftProtobuf.Message conformance is added in an extension below. See the
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
// methods supported on all messages.

var major: Int32 {
get {return _major ?? 0}
set {_major = newValue}
}
/// Returns true if `major` has been explicitly set.
var hasMajor: Bool {return self._major != nil}
/// Clears the value of `major`. Subsequent reads from it will return its default value.
mutating func clearMajor() {self._major = nil}

var minor: Int32 {
get {return _minor ?? 0}
set {_minor = newValue}
}
/// Returns true if `minor` has been explicitly set.
var hasMinor: Bool {return self._minor != nil}
/// Clears the value of `minor`. Subsequent reads from it will return its default value.
mutating func clearMinor() {self._minor = nil}

var patch: Int32 {
get {return _patch ?? 0}
set {_patch = newValue}
}
/// Returns true if `patch` has been explicitly set.
var hasPatch: Bool {return self._patch != nil}
/// Clears the value of `patch`. Subsequent reads from it will return its default value.
mutating func clearPatch() {self._patch = nil}

var suffix: String {
get {return _suffix ?? String()}
set {_suffix = newValue}
}
/// Returns true if `suffix` has been explicitly set.
var hasSuffix: Bool {return self._suffix != nil}
/// Clears the value of `suffix`. Subsequent reads from it will return its default value.
mutating func clearSuffix() {self._suffix = nil}

var unknownFields = SwiftProtobuf.UnknownStorage()

init() {}

fileprivate var _major: Int32? = nil
fileprivate var _minor: Int32? = nil
fileprivate var _patch: Int32? = nil
fileprivate var _suffix: String? = nil
}

// MARK: - Code below here is support for the SwiftProtobuf runtime.

fileprivate let _protobuf_package = "swift_descriptor_test.import"

extension SwiftDescriptorTest_Import_Version: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
static let protoMessageName: String = _protobuf_package + ".Version"
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
1: .same(proto: "major"),
2: .same(proto: "minor"),
3: .same(proto: "patch"),
4: .same(proto: "suffix"),
]

mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
while let fieldNumber = try decoder.nextFieldNumber() {
// The use of inline closures is to circumvent an issue where the compiler
// allocates stack space for every case branch when no optimizations are
// enabled. https://github.com/apple/swift-protobuf/issues/1034
switch fieldNumber {
case 1: try { try decoder.decodeSingularInt32Field(value: &self._major) }()
case 2: try { try decoder.decodeSingularInt32Field(value: &self._minor) }()
case 3: try { try decoder.decodeSingularInt32Field(value: &self._patch) }()
case 4: try { try decoder.decodeSingularStringField(value: &self._suffix) }()
default: break
}
}
}

func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
// The use of inline closures is to circumvent an issue where the compiler
// allocates stack space for every if/case branch local when no optimizations
// are enabled. https://github.com/apple/swift-protobuf/issues/1034 and
// https://github.com/apple/swift-protobuf/issues/1182
try { if let v = self._major {
try visitor.visitSingularInt32Field(value: v, fieldNumber: 1)
} }()
try { if let v = self._minor {
try visitor.visitSingularInt32Field(value: v, fieldNumber: 2)
} }()
try { if let v = self._patch {
try visitor.visitSingularInt32Field(value: v, fieldNumber: 3)
} }()
try { if let v = self._suffix {
try visitor.visitSingularStringField(value: v, fieldNumber: 4)
} }()
try unknownFields.traverse(visitor: &visitor)
}

static func ==(lhs: SwiftDescriptorTest_Import_Version, rhs: SwiftDescriptorTest_Import_Version) -> Bool {
if lhs._major != rhs._major {return false}
if lhs._minor != rhs._minor {return false}
if lhs._patch != rhs._patch {return false}
if lhs._suffix != rhs._suffix {return false}
if lhs.unknownFields != rhs.unknownFields {return false}
return true
}
}
Loading

0 comments on commit 0e46730

Please sign in to comment.