Skip to content

Commit

Permalink
Add test for runGenerator function
Browse files Browse the repository at this point in the history
  • Loading branch information
PARAIPAN9 committed Aug 9, 2024
1 parent d71f2cf commit 3d37c20
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ let package = Package(
swiftSettings: swiftSettings
),

// New Test Target for swift-openapi-generator
.testTarget(
name: "OpenAPIGeneratorTests",
dependencies: ["swift-openapi-generator"],
resources: [.copy("Resources")],
swiftSettings: swiftSettings
),

// Generator CLI
.executableTarget(
name: "swift-openapi-generator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension TestConfig {
}

/// Tests that the generator produces Swift files that match a reference.
class FileBasedReferenceTests: XCTestCase {
final class FileBasedReferenceTests: XCTestCase {

/// Setup method called before the invocation of each test method in the class.
override func setUp() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# openapi: '3.1.0' -> Missing openapi version
info:
title: MalformedDocument
version: 1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
generate:
- types
- server
accessModifier: internal
53 changes: 53 additions & 0 deletions Tests/OpenAPIGeneratorTests/Test_GenerateOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// 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 _OpenAPIGeneratorCore
import OpenAPIKit
import ArgumentParser
@testable import swift_openapi_generator

final class Test_GenerateOptions: XCTestCase {

var resourcesDirectory: URL! = nil

/// Setup method called before the invocation of each test method in the class.
override func setUpWithError() throws {
resourcesDirectory = try XCTUnwrap(
Bundle.module.url(forResource: "Resources", withExtension: nil),
"Could not find reference test resources directory."
)
}

func testRunGeneratorThrowsErrorDiagnostic() async throws {
let outputDirectory = URL(fileURLWithPath: "/invalid/path")
let docsDirectory = resourcesDirectory.appendingPathComponent("Docs")
let docPath = docsDirectory.appendingPathComponent("malformed-openapi.yaml")
let configPath = docsDirectory.appendingPathComponent("openapi-generator-config.yaml")

let arguments = [docPath.path(), "--config", configPath.path]

let originalArguments = CommandLine.arguments
defer { CommandLine.arguments = originalArguments }
CommandLine.arguments = arguments

let generator = try _GenerateOptions.parse(arguments)

do {
try await generator.runGenerator(outputDirectory: outputDirectory, pluginSource: nil, isDryRun: false)
XCTFail("Expected to throw an error, but it did not throw")
} catch let diagnostic as Diagnostic {
XCTAssertEqual(diagnostic.severity, .error, "Expected diagnostic severity to be `.error`")
} catch { XCTFail("Expected to throw a Diagnostic `.error`, but threw a different error: \(error)") }
}
}

0 comments on commit 3d37c20

Please sign in to comment.