-
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.
Move diagnostics collector logic into prepare function
- Loading branch information
Showing
6 changed files
with
97 additions
and
57 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
49 changes: 49 additions & 0 deletions
49
Sources/_OpenAPIGeneratorCore/DiagnosticsCollectorProvider.swift
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,49 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// A feature that can be explicitly enabled before being released. | ||
/// | ||
/// Commonly used to get early feedback on breaking changes, before | ||
/// they are enabled by default, which can only be done in a major version. | ||
/// | ||
/// Once a feature is enabled unconditionally in the next major version, | ||
/// the corresponding feature flag should be removed at the same time. | ||
/// | ||
/// For example: a breaking feature is being built while version 0.1 is out, | ||
/// and is hidden behind a feature flag. Once ready, the feature is | ||
/// enabled unconditionally on main and the feature flag removed, and version | ||
/// 0.2 is tagged. (This is for pre-1.0 versioning, would be 1.0 and 2.0 after | ||
/// 1.0 is released.) | ||
import Foundation | ||
|
||
/// Prepares a diagnostics collector. | ||
/// - Parameter outputPath: A file path where to persist the YAML file. If `nil`, diagnostics will be printed to stderr. | ||
/// - Returns: A tuple containing: | ||
/// - An instance of `DiagnosticCollector` conforming to `Sendable`. | ||
/// - A closure to finalize the diagnostics collection | ||
public func preparedDiagnosticsCollector(outputPath: URL?) -> (any DiagnosticCollector & Sendable, () throws -> Void) { | ||
let innerDiagnostics: any DiagnosticCollector & Sendable | ||
let finalizeDiagnostics: () throws -> Void | ||
|
||
if let outputPath { | ||
let _diagnostics = _YamlFileDiagnosticsCollector(url: outputPath) | ||
finalizeDiagnostics = _diagnostics.finalize | ||
innerDiagnostics = _diagnostics | ||
} else { | ||
innerDiagnostics = StdErrPrintingDiagnosticCollector() | ||
finalizeDiagnostics = {} | ||
} | ||
let diagnostics = ErrorThrowingDiagnosticCollector(upstream: innerDiagnostics) | ||
return (diagnostics, finalizeDiagnostics) | ||
} |
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
42 changes: 42 additions & 0 deletions
42
Tests/OpenAPIGeneratorCoreTests/Test_DiagnosticsCollectorProvider.swift
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,42 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
@testable import _OpenAPIGeneratorCore | ||
|
||
final class Test_DiagnosticsCollectorProvider: XCTestCase { | ||
|
||
func testPreparedDiagnosticsCollectorWithOutputPath() throws { | ||
let outputPath = URL(fileURLWithPath: "/path/to/diagnostics.yaml") | ||
let (diagnostics, _) = preparedDiagnosticsCollector(outputPath: outputPath) | ||
XCTAssertTrue(diagnostics is ErrorThrowingDiagnosticCollector) | ||
|
||
if let errorThrowingCollector = diagnostics as? ErrorThrowingDiagnosticCollector { | ||
XCTAssertTrue(errorThrowingCollector.upstream is _YamlFileDiagnosticsCollector) | ||
} else { | ||
XCTFail("Expected diagnostics to be `ErrorThrowingDiagnosticCollector`") | ||
} | ||
} | ||
|
||
func testPreparedDiagnosticsCollectorWithoutOutputPath() throws { | ||
let outputPath: URL? = nil | ||
let (diagnostics, _) = preparedDiagnosticsCollector(outputPath: outputPath) | ||
XCTAssertTrue(diagnostics is ErrorThrowingDiagnosticCollector) | ||
if let errorThrowingCollector = diagnostics as? ErrorThrowingDiagnosticCollector { | ||
XCTAssertTrue(errorThrowingCollector.upstream is StdErrPrintingDiagnosticCollector) | ||
} else { | ||
XCTFail("Expected diagnostics to be `ErrorThrowingDiagnosticCollector`") | ||
} | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
Tests/OpenAPIGeneratorCoreTests/Test_preparedDiagnosticsCollector.swift
This file was deleted.
Oops, something went wrong.