Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add validation check for conflicting configuration values #2677

Merged
merged 2 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,35 @@ public class ApolloCodegen {
case schemaNameConflict(name: String)
case cannotLoadSchema
case cannotLoadOperations
case invalidConfiguration(message: String)

public var errorDescription: String? {
switch self {
case let .graphQLSourceValidationFailure(lines):
return "An error occured during validation of the GraphQL schema or operations! Check \(lines)"
return """
An error occured during validation of the GraphQL schema or operations! Check \(lines)
"""
case .testMocksInvalidSwiftPackageConfiguration:
return "Schema Types must be generated with module type 'swiftPackageManager' to generate a swift package for test mocks."
return """
Schema Types must be generated with module type 'swiftPackageManager' to generate a \
swift package for test mocks.
"""
case let .inputSearchPathInvalid(path):
return "Input search path '\(path)' is invalid. Input search paths must include a file extension component. (eg. '.graphql')"
return """
Input search path '\(path)' is invalid. Input search paths must include a file \
extension component. (eg. '.graphql')
"""
case let .schemaNameConflict(name):
return "Schema name \(name) conflicts with name of a type in your GraphQL schema. Please choose a different schema name. Suggestions: \(name)Schema, \(name)GraphQL, \(name)API"
return """
Schema name \(name) conflicts with name of a type in your GraphQL schema. Please \
choose a different schema name. Suggestions: \(name)Schema, \(name)GraphQL, \(name)API
"""
case .cannotLoadSchema:
return "A GraphQL schema could not be found. Please verify the schema search paths."
case .cannotLoadOperations:
return "No GraphQL operations could be found. Please verify the operation search paths."
case let .invalidConfiguration(message):
return "The codegen configuration has conflicting values: \(message)"
}
}
}
Expand Down Expand Up @@ -126,6 +140,15 @@ public class ApolloCodegen {
throw Error.testMocksInvalidSwiftPackageConfiguration
}

if case .swiftPackageManager = config.output.schemaTypes.moduleType,
config.options.cocoapodsCompatibleImportStatements == true {
throw Error.invalidConfiguration(message: """
cocoapodsCompatibleImportStatements cannot be set to 'true' when the output schema types \
module type is Swift Package Manager. Change the cocoapodsCompatibleImportStatements \
value to 'false', or choose a different module type, to resolve the conflict.
""")
}

for searchPath in config.input.schemaSearchPaths {
try validate(inputSearchPath: searchPath)
}
Expand Down
57 changes: 51 additions & 6 deletions Tests/ApolloCodegenTests/ApolloCodegenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@ class ApolloCodegenTests: XCTestCase {
expect(ApolloFileManager.default.doesFileExist(atPath: testInTestMocksFolderUserFile)).to(beTrue())
}

// MARK: Validation Tests (test mock module type)
// MARK: Validation Tests

func test_validation_givenTestMockConfiguration_asSwiftPackage_withSchemaTypesModule_asEmbeddedInTarget_shouldThrow() throws {
// given
Expand Down Expand Up @@ -1988,8 +1988,6 @@ class ApolloCodegenTests: XCTestCase {
.notTo(throwError())
}

// MARK: Validation Tests (search paths)

func test_validation_givenOperationSearchPathWithoutFileExtensionComponent_shouldThrow() throws {
// given
let configContext = ApolloCodegen.ConfigurationContext(config: .mock(
Expand Down Expand Up @@ -2034,8 +2032,6 @@ class ApolloCodegenTests: XCTestCase {
.to(throwError(ApolloCodegen.Error.inputSearchPathInvalid(path: "schema/*.")))
}

// MARK: Validation Tests (conflicting schema name)

let conflictingSchemaNames = ["rocket", "Rocket"]

func test__validation__givenSchemaName_matchingObjectName_shouldThrow() throws {
Expand Down Expand Up @@ -2214,7 +2210,56 @@ class ApolloCodegenTests: XCTestCase {
.notTo(throwError())
}

// Special-folder Exclusion Tests
func test__validation__givenSchemaTypesModule_swiftPackageManager_withCocoapodsCompatibleImportStatements_true_shouldThrow() throws {
// given
let configContext = ApolloCodegen.ConfigurationContext(config: .mock(
.swiftPackageManager,
options: .init(cocoapodsCompatibleImportStatements: true)
))

// then
expect(try ApolloCodegen.validate(config: configContext))
.to(throwError(ApolloCodegen.Error.invalidConfiguration(message: """
cocoapodsCompatibleImportStatements cannot be set to 'true' when the output schema types \
module type is Swift Package Manager. Change the cocoapodsCompatibleImportStatements \
value to 'false' to resolve the conflict.
""")))
}

func test__validation__givenSchemaTypesModule_swiftPackageManager_withCocoapodsCompatibleImportStatements_false_shouldNotThrow() throws {
// given
let configContext = ApolloCodegen.ConfigurationContext(config: .mock(
.swiftPackageManager,
options: .init(cocoapodsCompatibleImportStatements: false)
))

// then
expect(try ApolloCodegen.validate(config: configContext)).notTo(throwError())
}

func test__validation__givenSchemaTypesModule_embeddedInTarget_withCocoapodsCompatibleImportStatements_true_shouldNotThrow() throws {
// given
let configContext = ApolloCodegen.ConfigurationContext(config: .mock(
.embeddedInTarget(name: "TestTarget"),
options: .init(cocoapodsCompatibleImportStatements: true)
))

// then
expect(try ApolloCodegen.validate(config: configContext)).notTo(throwError())
}

func test__validation__givenSchemaTypesModule_other_withCocoapodsCompatibleImportStatements_true_shouldNotThrow() throws {
// given
let configContext = ApolloCodegen.ConfigurationContext(config: .mock(
.other,
options: .init(cocoapodsCompatibleImportStatements: true)
))

// then
expect(try ApolloCodegen.validate(config: configContext)).notTo(throwError())
}

// MARK: Path Match Exclusion Tests

func test__match__givenFilesInSpecialExcludedPaths_shouldNotReturnExcludedPaths() throws {
// given
Expand Down