File tree Expand file tree Collapse file tree 3 files changed +34
-12
lines changed
Tests/GRPCCodeGenTests/Internal/Translator Expand file tree Collapse file tree 3 files changed +34
-12
lines changed Original file line number Diff line number Diff line change @@ -72,13 +72,23 @@ struct IDLToStructuredSwiftTranslator: Translator {
72
72
}
73
73
}
74
74
75
- let fileDescription = FileDescription (
76
- topComment: . preFormatted( codeGenerationRequest. leadingTrivia) ,
77
- imports: try self . makeImports (
75
+ let imports : [ ImportDescription ]
76
+ if codeBlocks. isEmpty {
77
+ imports = [ ]
78
+ codeBlocks. append (
79
+ CodeBlock ( comment: . inline( " This file contained no services. " ) )
80
+ )
81
+ } else {
82
+ imports = try self . makeImports (
78
83
dependencies: codeGenerationRequest. dependencies,
79
84
accessLevel: accessLevel,
80
85
accessLevelOnImports: accessLevelOnImports
81
- ) ,
86
+ )
87
+ }
88
+
89
+ let fileDescription = FileDescription (
90
+ topComment: . preFormatted( codeGenerationRequest. leadingTrivia) ,
91
+ imports: imports,
82
92
codeBlocks: codeBlocks
83
93
)
84
94
Original file line number Diff line number Diff line change @@ -87,21 +87,14 @@ public struct SourceGenerator: Sendable {
87
87
let translator = IDLToStructuredSwiftTranslator ( )
88
88
let textRenderer = TextBasedRenderer ( indentation: self . config. indentation)
89
89
90
- var structuredSwiftRepresentation = try translator. translate (
90
+ let structuredSwiftRepresentation = try translator. translate (
91
91
codeGenerationRequest: request,
92
92
accessLevel: self . config. accessLevel,
93
93
accessLevelOnImports: self . config. accessLevelOnImports,
94
94
client: self . config. client,
95
95
server: self . config. server
96
96
)
97
97
98
- if structuredSwiftRepresentation. file. contents. codeBlocks. isEmpty {
99
- structuredSwiftRepresentation. file. contents. imports = [ ]
100
- structuredSwiftRepresentation. file. contents. codeBlocks. append (
101
- CodeBlock ( comment: . inline( " This file contained no services. " ) )
102
- )
103
- }
104
-
105
98
let sourceFile = try textRenderer. render ( structured: structuredSwiftRepresentation)
106
99
107
100
return sourceFile
Original file line number Diff line number Diff line change @@ -298,6 +298,25 @@ final class IDLToStructuredSwiftTranslatorSnippetBasedTests: XCTestCase {
298
298
)
299
299
}
300
300
301
+ func testEmptyFileGeneration( ) throws {
302
+ let expectedSwift =
303
+ """
304
+ /// Some really exciting license header 2023.
305
+
306
+
307
+ // This file contained no services.
308
+ """
309
+ try self . assertIDLToStructuredSwiftTranslation (
310
+ codeGenerationRequest: makeCodeGenerationRequest (
311
+ services: [ ] ,
312
+ dependencies: [ ]
313
+ ) ,
314
+ expectedSwift: expectedSwift,
315
+ accessLevel: . public,
316
+ server: true
317
+ )
318
+ }
319
+
301
320
private func assertIDLToStructuredSwiftTranslation(
302
321
codeGenerationRequest: CodeGenerationRequest ,
303
322
expectedSwift: String ,
You can’t perform that action at this time.
0 commit comments