-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathFileBasedReferenceTests.swift
249 lines (220 loc) · 9.26 KB
/
FileBasedReferenceTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import OpenAPIKit
import Yams
@testable import _OpenAPIGeneratorCore
struct TestConfig: Encodable {
var docFilePath: String
var mode: GeneratorMode
var additionalImports: [String]?
var featureFlags: FeatureFlags?
var referenceOutputDirectory: String
}
extension TestConfig {
var asConfig: Config {
.init(mode: mode, access: .public, additionalImports: additionalImports ?? [], featureFlags: featureFlags ?? [])
}
}
/// Tests that the generator produces Swift files that match a reference.
final class FileBasedReferenceTests: XCTestCase {
/// Setup method called before the invocation of each test method in the class.
override func setUp() {
super.setUp()
continueAfterFailure = false
#if !canImport(FoundationNetworking)
executionTimeAllowance = 60 * 60
#endif
}
func testPetstore() throws { try _test(referenceProject: .init(name: .petstore), featureFlags: [.uuidSupport]) }
// MARK: - Private
var referenceTestResourcesDirectory: URL! = nil
/// Setup method called before the invocation of each test method in the class.
override func setUpWithError() throws {
self.referenceTestResourcesDirectory = try XCTUnwrap(
Bundle.module.url(forResource: "Resources", withExtension: nil),
"Could not find reference test resources directory."
)
}
func performReferenceTest(_ referenceTest: TestConfig, ignoredDiagnosticMessages: Set<String> = []) throws {
print(
"""
\(String(repeating: "=", count: 60))
Performing reference test
\(h2("begin test config"))
\(try YAMLEncoder().encode(referenceTest))
\(h2("end test config"))
"""
)
// Load the doc file into memory
let docFilePath = referenceTest.docFilePath
let docFileURL = URL(fileURLWithPath: docFilePath, relativeTo: referenceTestResourcesDirectory)
let input = InMemoryInputFile(absolutePath: docFileURL, contents: try Data(contentsOf: docFileURL))
// Run the requested generator invocation
let generatorPipeline = self.makeGeneratorPipeline(
config: referenceTest.asConfig,
ignoredDiagnosticMessages: ignoredDiagnosticMessages
)
let generatedOutputSource = try generatorPipeline.run(input)
// Write generated sources to temporary directory
let generatedOutputDir = try self.temporaryDirectory()
let generatedOutputFile = URL(fileURLWithPath: generatedOutputSource.baseName, relativeTo: generatedOutputDir)
try generatedOutputSource.contents.write(to: generatedOutputFile)
// Compare the generated directory with the reference directory
let referenceOutputDir = URL(
fileURLWithPath: referenceTest.referenceOutputDirectory,
relativeTo: referenceTestResourcesDirectory
)
let referenceOutputFile = referenceOutputDir.appendingPathComponent(generatedOutputSource.baseName)
self.assert(
contentsOf: generatedOutputFile,
equalsContentsOf: referenceOutputFile,
runDiffWhenContentsDiffer: true
)
}
enum ReferenceProjectName: String, Hashable, CaseIterable {
case petstore
var openAPIDocFileName: String { "\(rawValue).yaml" }
var fixtureCodeDirectoryName: String { rawValue.capitalized }
}
struct ReferenceProject: Hashable {
var name: ReferenceProjectName
var customDirectoryName: String? = nil
var fixtureCodeDirectoryName: String { customDirectoryName ?? name.fixtureCodeDirectoryName }
var openAPIDocFileName: String { name.openAPIDocFileName }
}
func _test(
referenceProject project: ReferenceProject,
featureFlags: FeatureFlags = [],
ignoredDiagnosticMessages: Set<String> = []
) throws {
let modes: [GeneratorMode] = [.types, .client, .server]
for mode in modes {
try performReferenceTest(
.init(
docFilePath: "Docs/\(project.openAPIDocFileName)",
mode: mode,
additionalImports: [],
featureFlags: featureFlags,
referenceOutputDirectory: "ReferenceSources/\(project.fixtureCodeDirectoryName)"
),
ignoredDiagnosticMessages: ignoredDiagnosticMessages
)
}
}
}
extension FileBasedReferenceTests {
private func makeGeneratorPipeline(config: Config, ignoredDiagnosticMessages: Set<String> = []) -> GeneratorPipeline
{
let parser = YamsParser()
let translator = MultiplexTranslator()
let renderer = TextBasedRenderer.default
return _OpenAPIGeneratorCore.makeGeneratorPipeline(
parser: parser,
translator: translator,
renderer: renderer,
config: config,
diagnostics: XCTestDiagnosticCollector(test: self, ignoredDiagnosticMessages: ignoredDiagnosticMessages)
)
}
private func temporaryDirectory() throws -> URL {
let directoryURL = FileManager.default.temporaryDirectory.appendingPathComponent(
UUID().uuidString,
isDirectory: true
)
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true)
addTeardownBlock {
do {
if FileManager.default.fileExists(atPath: directoryURL.path) {
try FileManager.default.removeItem(at: directoryURL)
XCTAssertFalse(FileManager.default.fileExists(atPath: directoryURL.path))
}
} catch {
// Treat any errors during file deletion as a test failure.
XCTFail("Error while deleting temporary directory: \(error)")
}
}
return directoryURL
}
private func assert(
contentsOf generatedFile: URL,
equalsContentsOf referenceFile: URL,
message: @autoclosure () -> String = "",
runDiffWhenContentsDiffer: Bool = true,
file: StaticString = #filePath,
line: UInt = #line
) {
if FileManager.default.contentsEqual(atPath: generatedFile.path, andPath: referenceFile.path) { return }
let diffOutput: String?
if runDiffWhenContentsDiffer {
do { diffOutput = try runDiff(reference: referenceFile, actual: generatedFile) } catch {
diffOutput = "failed: \(error)"
}
} else {
diffOutput = nil
}
XCTFail(
"""
\(message())
Directory contents not equal:
File under test: \(generatedFile.relativePath)
Reference file: \(referenceFile.relativePath)
Diff output: \(diffOutput == nil ? "[disabled]" : "(see below)")
\(h2("begin diff output"))
\(diffOutput ?? "[diff disabled]")
\(h2("end diff output"))
""",
file: file,
line: line
)
}
private func runDiff(reference: URL, actual: URL) throws -> String {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.currentDirectoryURL = self.referenceTestResourcesDirectory
process.arguments = [
"git", "diff", "--no-index", "-U5",
// The following arguments are useful for development.
// "--ignore-space-change",
// "--ignore-all-space",
// "--ignore-blank-lines",
// "--ignore-space-at-eol",
reference.relativePath, actual.path,
]
let pipe = Pipe()
process.standardOutput = pipe
try process.run()
let stdoutData = try pipe.fileHandleForReading.readToEnd()
process.waitUntilExit()
let pipeData = try XCTUnwrap(
stdoutData,
"""
No output from command:
\(process.executableURL!.path) \(process.arguments!.joined(separator: " "))
"""
)
return String(decoding: pipeData, as: UTF8.self)
}
func heading(_ message: String, paddingCharacter: Character, lineLength: Int) -> String {
let prefix = String(repeating: paddingCharacter, count: 3)
return "\(prefix) \(message.trimmingCharacters(in: .whitespaces)) "
.padding(toLength: lineLength, withPad: "\(paddingCharacter)", startingAt: 0)
}
func h1(_ message: String, lineLength: Int = 60) -> String {
heading(message, paddingCharacter: "=", lineLength: lineLength)
}
func h2(_ message: String, lineLength: Int = 60) -> String {
heading(message, paddingCharacter: "-", lineLength: lineLength)
}
}