Skip to content

Commit 0e46730

Browse files
committed
Break the tests dependency on the compiler plugin proto.
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.
1 parent ee2fefc commit 0e46730

File tree

10 files changed

+1737
-2145
lines changed

10 files changed

+1737
-2145
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ regenerate-test-protos: build ${PROTOC_GEN_SWIFT} Protos/SwiftProtobufTests/gene
335335
-I Protos/SwiftProtobufPluginLibrary \
336336
-I Protos/SwiftProtobufPluginLibraryTests \
337337
--tfiws_opt=FileNaming=DropPath \
338-
--tfiws_opt=ProtoPathModuleMappings=Protos/SwiftProtobufPluginLibraryTests/module_mappings.pbascii \
339338
--tfiws_out=Tests/SwiftProtobufPluginLibraryTests \
340339
`find Protos/SwiftProtobufPluginLibraryTests -type f -name "*.proto"`
341340

@@ -353,10 +352,10 @@ regenerate-fuzz-protos: build ${PROTOC_GEN_SWIFT}
353352
SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS= \
354353
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test.proto \
355354
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test2.proto \
355+
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test_import.proto \
356356
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_delimited.proto \
357357
Protos/SwiftProtobufPluginLibraryTests/unittest_delimited.proto \
358358
Protos/SwiftProtobufPluginLibraryTests/unittest_delimited_import.proto \
359-
Protos/SwiftProtobufPluginLibrary/google/protobuf/compiler/plugin.proto \
360359
Protos/SwiftProtobufPluginLibrary/swift_protobuf_module_mappings.proto
361360

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

Protos/SwiftProtobufPluginLibraryTests/module_mappings.pbascii

Lines changed: 0 additions & 4 deletions
This file was deleted.

Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test.proto

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package swift_descriptor_test;
2222
option swift_prefix = "SDT";
2323

2424
import "google/protobuf/descriptor.proto";
25-
import "google/protobuf/compiler/plugin.proto";
25+
import "pluginlib_descriptor_test_import.proto";
2626

2727
enum TopLevelEnum {
2828
VALUE_ZERO = 0;
@@ -62,7 +62,7 @@ message TopLevelMessage2 {
6262

6363
message ExternalRefs {
6464
optional google.protobuf.DescriptorProto desc = 1;
65-
optional google.protobuf.compiler.Version ver = 2;
65+
optional swift_descriptor_test.import.Version ver = 2;
6666
}
6767

6868
extend google.protobuf.FieldOptions {
@@ -77,7 +77,7 @@ message ScoperForExt {
7777
}
7878

7979
service SomeService {
80-
rpc Foo(google.protobuf.DescriptorProto) returns (google.protobuf.compiler.Version);
80+
rpc Foo(google.protobuf.DescriptorProto) returns (swift_descriptor_test.import.Version);
8181
rpc Bar(TopLevelMessage) returns (TopLevelMessage2);
8282
}
8383

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test_import.proto - test proto
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
// -----------------------------------------------------------------------------
12+
///
13+
/// Test proto for Tests/SwiftProtobufPluginLibraryTests/Test_Descriptor.swift
14+
///
15+
// -----------------------------------------------------------------------------
16+
17+
syntax = "proto2";
18+
19+
package swift_descriptor_test.import;
20+
21+
message Version {
22+
optional int32 major = 1;
23+
optional int32 minor = 2;
24+
optional int32 patch = 3;
25+
optional string suffix = 4;
26+
}

Reference/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test.pb.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ struct SDTExternalRefs: Sendable {
253253
/// Clears the value of `desc`. Subsequent reads from it will return its default value.
254254
mutating func clearDesc() {self._desc = nil}
255255

256-
var ver: Google_Protobuf_Compiler_Version {
257-
get {return _ver ?? Google_Protobuf_Compiler_Version()}
256+
var ver: SwiftDescriptorTest_Import_Version {
257+
get {return _ver ?? SwiftDescriptorTest_Import_Version()}
258258
set {_ver = newValue}
259259
}
260260
/// Returns true if `ver` has been explicitly set.
@@ -267,7 +267,7 @@ struct SDTExternalRefs: Sendable {
267267
init() {}
268268

269269
fileprivate var _desc: SwiftProtobuf.Google_Protobuf_DescriptorProto? = nil
270-
fileprivate var _ver: Google_Protobuf_Compiler_Version? = nil
270+
fileprivate var _ver: SwiftDescriptorTest_Import_Version? = nil
271271
}
272272

273273
struct SDTScoperForExt: Sendable {
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// DO NOT EDIT.
2+
// swift-format-ignore-file
3+
// swiftlint:disable all
4+
//
5+
// Generated by the Swift generator plugin for the protocol buffer compiler.
6+
// Source: pluginlib_descriptor_test_import.proto
7+
//
8+
// For information on using the generated types, please see the documentation:
9+
// https://github.com/apple/swift-protobuf/
10+
11+
// Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test_import.proto - test proto
12+
//
13+
// This source file is part of the Swift.org open source project
14+
//
15+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
16+
// Licensed under Apache License v2.0 with Runtime Library Exception
17+
//
18+
// See http://swift.org/LICENSE.txt for license information
19+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
20+
//
21+
// -----------------------------------------------------------------------------
22+
///
23+
/// Test proto for Tests/SwiftProtobufPluginLibraryTests/Test_Descriptor.swift
24+
///
25+
// -----------------------------------------------------------------------------
26+
27+
import Foundation
28+
import SwiftProtobuf
29+
30+
// If the compiler emits an error on this type, it is because this file
31+
// was generated by a version of the `protoc` Swift plug-in that is
32+
// incompatible with the version of SwiftProtobuf to which you are linking.
33+
// Please ensure that you are building against the same version of the API
34+
// that was used to generate this file.
35+
fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck {
36+
struct _2: SwiftProtobuf.ProtobufAPIVersion_2 {}
37+
typealias Version = _2
38+
}
39+
40+
struct SwiftDescriptorTest_Import_Version: Sendable {
41+
// SwiftProtobuf.Message conformance is added in an extension below. See the
42+
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
43+
// methods supported on all messages.
44+
45+
var major: Int32 {
46+
get {return _major ?? 0}
47+
set {_major = newValue}
48+
}
49+
/// Returns true if `major` has been explicitly set.
50+
var hasMajor: Bool {return self._major != nil}
51+
/// Clears the value of `major`. Subsequent reads from it will return its default value.
52+
mutating func clearMajor() {self._major = nil}
53+
54+
var minor: Int32 {
55+
get {return _minor ?? 0}
56+
set {_minor = newValue}
57+
}
58+
/// Returns true if `minor` has been explicitly set.
59+
var hasMinor: Bool {return self._minor != nil}
60+
/// Clears the value of `minor`. Subsequent reads from it will return its default value.
61+
mutating func clearMinor() {self._minor = nil}
62+
63+
var patch: Int32 {
64+
get {return _patch ?? 0}
65+
set {_patch = newValue}
66+
}
67+
/// Returns true if `patch` has been explicitly set.
68+
var hasPatch: Bool {return self._patch != nil}
69+
/// Clears the value of `patch`. Subsequent reads from it will return its default value.
70+
mutating func clearPatch() {self._patch = nil}
71+
72+
var suffix: String {
73+
get {return _suffix ?? String()}
74+
set {_suffix = newValue}
75+
}
76+
/// Returns true if `suffix` has been explicitly set.
77+
var hasSuffix: Bool {return self._suffix != nil}
78+
/// Clears the value of `suffix`. Subsequent reads from it will return its default value.
79+
mutating func clearSuffix() {self._suffix = nil}
80+
81+
var unknownFields = SwiftProtobuf.UnknownStorage()
82+
83+
init() {}
84+
85+
fileprivate var _major: Int32? = nil
86+
fileprivate var _minor: Int32? = nil
87+
fileprivate var _patch: Int32? = nil
88+
fileprivate var _suffix: String? = nil
89+
}
90+
91+
// MARK: - Code below here is support for the SwiftProtobuf runtime.
92+
93+
fileprivate let _protobuf_package = "swift_descriptor_test.import"
94+
95+
extension SwiftDescriptorTest_Import_Version: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
96+
static let protoMessageName: String = _protobuf_package + ".Version"
97+
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
98+
1: .same(proto: "major"),
99+
2: .same(proto: "minor"),
100+
3: .same(proto: "patch"),
101+
4: .same(proto: "suffix"),
102+
]
103+
104+
mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
105+
while let fieldNumber = try decoder.nextFieldNumber() {
106+
// The use of inline closures is to circumvent an issue where the compiler
107+
// allocates stack space for every case branch when no optimizations are
108+
// enabled. https://github.com/apple/swift-protobuf/issues/1034
109+
switch fieldNumber {
110+
case 1: try { try decoder.decodeSingularInt32Field(value: &self._major) }()
111+
case 2: try { try decoder.decodeSingularInt32Field(value: &self._minor) }()
112+
case 3: try { try decoder.decodeSingularInt32Field(value: &self._patch) }()
113+
case 4: try { try decoder.decodeSingularStringField(value: &self._suffix) }()
114+
default: break
115+
}
116+
}
117+
}
118+
119+
func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
120+
// The use of inline closures is to circumvent an issue where the compiler
121+
// allocates stack space for every if/case branch local when no optimizations
122+
// are enabled. https://github.com/apple/swift-protobuf/issues/1034 and
123+
// https://github.com/apple/swift-protobuf/issues/1182
124+
try { if let v = self._major {
125+
try visitor.visitSingularInt32Field(value: v, fieldNumber: 1)
126+
} }()
127+
try { if let v = self._minor {
128+
try visitor.visitSingularInt32Field(value: v, fieldNumber: 2)
129+
} }()
130+
try { if let v = self._patch {
131+
try visitor.visitSingularInt32Field(value: v, fieldNumber: 3)
132+
} }()
133+
try { if let v = self._suffix {
134+
try visitor.visitSingularStringField(value: v, fieldNumber: 4)
135+
} }()
136+
try unknownFields.traverse(visitor: &visitor)
137+
}
138+
139+
static func ==(lhs: SwiftDescriptorTest_Import_Version, rhs: SwiftDescriptorTest_Import_Version) -> Bool {
140+
if lhs._major != rhs._major {return false}
141+
if lhs._minor != rhs._minor {return false}
142+
if lhs._patch != rhs._patch {return false}
143+
if lhs._suffix != rhs._suffix {return false}
144+
if lhs.unknownFields != rhs.unknownFields {return false}
145+
return true
146+
}
147+
}

0 commit comments

Comments
 (0)