-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NFC] Refactor PetstoreConsumerTests to allow multiple versions (#157)
[NFC] Refactor PetstoreConsumerTests to allow multiple versions ### Motivation In upcoming features that contain breaking changes, but we'll stage them in behind a feature flag, we'd like to have multiple variants of the `PetstoreConsumerTests` test target, allowing us to test both the existing behavior and the new behavior once a feature flag is enabled. These additional variants would be temporary, and would be deleted again (or rather, the main test suite would be updated) once the feature flag is enabled unconditionally. So the steady state number of `PetstoreConsumerTest` variants would continue to be 1, just for short periods of time, it might be higher. ### Modifications This PR makes that possible by refactoring common functionality into a new internal target `PetstoreConsumerTestCore`. Note that all the variants of the test target share the same OpenAPI document, but generate different variants of the reference code. Highlights: - new `PetstoreConsumerTestCore` target, depended on by the existing `PetstoreConsumerTests` - added an ability to _not_ fail the test when specific diagnostics are emitted (through the new `ignoredDiagnosticMessages: Set<String>` parameter), which allows us to test both existing and new behavior on the same OpenAPI document. Other, non-allowlisted diagnostics, still continue to fail the test, so new ones won't sneak through undetected. - many test functions now optionally take `featureFlags` and `ignoredDiagnosticMessages`, again allowing us to test both existing and proposed behavior hidden behind a feature flag ### Result It will be a lot easier to temporarily introduce other variants of the `PetstoreConsumerTests` test target to allow for thoroughly testing features even before they are enabled by default, giving us more confidence in the accuracy of proposals and their associated implementations. ### Test Plan All tests continue to pass, this is an NFC, a pure refactoring, making the next PR much smaller. To see how this all fits together, check out the PR that has all the changes: #146. That said, these partial PRs are easier to review, as they're each focused on one task. Reviewed by: gjcairo, simonjbeaumont Builds: ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (docc test) - Build finished. ✔︎ pull request validation (integration test) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #157
- Loading branch information
Showing
13 changed files
with
209 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 OpenAPIRuntime | ||
import Foundation | ||
|
||
public enum TestError: Swift.Error, LocalizedError, CustomStringConvertible { | ||
case noHandlerFound(method: HTTPMethod, path: [RouterPathComponent]) | ||
case invalidURLString(String) | ||
case unexpectedValue(Any) | ||
case unexpectedMissingRequestBody | ||
|
||
public var description: String { | ||
switch self { | ||
case .noHandlerFound(let method, let path): | ||
return "No handler found for method \(method.name) and path \(path.stringPath)" | ||
case .invalidURLString(let string): | ||
return "Invalid URL string: \(string)" | ||
case .unexpectedValue(let value): | ||
return "Unexpected value: \(value)" | ||
case .unexpectedMissingRequestBody: | ||
return "Unexpected missing request body" | ||
} | ||
} | ||
|
||
public var errorDescription: String? { | ||
description | ||
} | ||
} | ||
|
||
public extension Date { | ||
static var test: Date { | ||
Date(timeIntervalSince1970: 1_674_036_251) | ||
} | ||
|
||
static var testString: String { | ||
"2023-01-18T10:04:11Z" | ||
} | ||
} | ||
|
||
public extension Array where Element == RouterPathComponent { | ||
var stringPath: String { | ||
map(\.description).joined(separator: "/") | ||
} | ||
} | ||
|
||
public extension Response { | ||
init( | ||
statusCode: Int, | ||
headers: [HeaderField] = [], | ||
encodedBody: String | ||
) { | ||
self.init( | ||
statusCode: statusCode, | ||
headerFields: headers, | ||
body: Data(encodedBody.utf8) | ||
) | ||
} | ||
|
||
static var listPetsSuccess: Self { | ||
.init( | ||
statusCode: 200, | ||
headers: [ | ||
.init(name: "content-type", value: "application/json") | ||
], | ||
encodedBody: #""" | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "Fluffz" | ||
} | ||
] | ||
"""# | ||
) | ||
} | ||
} | ||
|
||
public extension Data { | ||
var pretty: String { | ||
String(decoding: self, as: UTF8.self) | ||
} | ||
|
||
static var abcdString: String { | ||
"abcd" | ||
} | ||
|
||
static var abcd: Data { | ||
Data(abcdString.utf8) | ||
} | ||
|
||
static var efghString: String { | ||
"efgh" | ||
} | ||
|
||
static var quotedEfghString: String { | ||
#""efgh""# | ||
} | ||
|
||
static var efgh: Data { | ||
Data(efghString.utf8) | ||
} | ||
} | ||
|
||
public extension Request { | ||
init( | ||
path: String, | ||
query: String? = nil, | ||
method: HTTPMethod, | ||
headerFields: [HeaderField] = [], | ||
encodedBody: String | ||
) throws { | ||
let body = Data(encodedBody.utf8) | ||
self.init( | ||
path: path, | ||
query: query, | ||
method: method, | ||
headerFields: headerFields, | ||
body: body | ||
) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.