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

Operation Manifest Generation Updates #3128

Merged
merged 3 commits into from
Jul 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,23 @@ struct OperationManifestFileGenerator {
func generate(fileManager: ApolloFileManager = .default) throws {
let rendered: String = try template.render(operations: operationManifest)

var manifestPath = config.output.operationManifest.unsafelyUnwrapped.path
let relativePrefix = "./"

// if path begins with './' the path should be relative to the config.rootURL
if manifestPath.hasPrefix(relativePrefix) {
let fileURL = URL(fileURLWithPath: String(manifestPath.dropFirst(relativePrefix.count)), relativeTo: config.rootURL)
manifestPath = fileURL
.resolvingSymlinksInPath()
.path
}

if !manifestPath.hasSuffix(".json") {
manifestPath.append(".json")
}

try fileManager.createFile(
atPath: config.output.operationManifest.unsafelyUnwrapped.path,
atPath: manifestPath,
data: rendered.data(using: .utf8),
overwrite: true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class OperationManifestFileGeneratorTests: XCTestCase {

// MARK: Generate Tests

func test__generate__givenOperation_shouldWriteToPath() throws {
func test__generate__givenOperation_shouldWriteToAbsolutePath() throws {
// given
let filePath = "path/to/match"
try buildSubject(path: filePath)
Expand All @@ -95,7 +95,120 @@ class OperationManifestFileGeneratorTests: XCTestCase {
}))

fileManager.mock(closure: .createFile({ path, data, attributes in
expect(path).to(equal(filePath))
expect(path).to(equal("\(filePath).json"))

return true
}))

// when
try subject.generate(fileManager: fileManager)

expect(self.fileManager.allClosuresCalled).to(beTrue())
}

func test__generate__givenOperation_withPathExtension_shouldWriteToAbsolutePathWithSinglePathExtension() throws {
// given
let filePath = "path/to/match"
try buildSubject(path: "\(filePath).json")

subject.collectOperationIdentifier(.mock(
name: "TestQuery",
type: .query,
source: """
query TestQuery {
test
}
"""
))

fileManager.mock(closure: .fileExists({ path, isDirectory in
return false
}))

fileManager.mock(closure: .createDirectory({ path, intermediateDirectories, attributes in
// no-op
}))

fileManager.mock(closure: .createFile({ path, data, attributes in
expect(path).to(equal("\(filePath).json"))

return true
}))

// when
try subject.generate(fileManager: fileManager)

expect(self.fileManager.allClosuresCalled).to(beTrue())
}

func test__generate__givenOperation_shouldWriteToRelativePath() throws {
// given
let filePath = "./path/to/match"
try buildSubject(path: filePath)

subject.collectOperationIdentifier(.mock(
name: "TestQuery",
type: .query,
source: """
query TestQuery {
test
}
"""
))

fileManager.mock(closure: .fileExists({ path, isDirectory in
return false
}))

fileManager.mock(closure: .createDirectory({ path, intermediateDirectories, attributes in
// no-op
}))

fileManager.mock(closure: .createFile({ path, data, attributes in
let expectedPath = URL(fileURLWithPath: String(filePath.dropFirst(2)), relativeTo: self.subject.config.rootURL)
.resolvingSymlinksInPath()
.appendingPathExtension("json")
.path
expect(path).to(equal(expectedPath))

return true
}))

// when
try subject.generate(fileManager: fileManager)

expect(self.fileManager.allClosuresCalled).to(beTrue())
}

func test__generate__givenOperation_withPathExtension_shouldWriteToRelativePathWithSinglePathExtension() throws {
// given
let filePath = "./path/to/match"
try buildSubject(path: "\(filePath).json")

subject.collectOperationIdentifier(.mock(
name: "TestQuery",
type: .query,
source: """
query TestQuery {
test
}
"""
))

fileManager.mock(closure: .fileExists({ path, isDirectory in
return false
}))

fileManager.mock(closure: .createDirectory({ path, intermediateDirectories, attributes in
// no-op
}))

fileManager.mock(closure: .createFile({ path, data, attributes in
let expectedPath = URL(fileURLWithPath: String(filePath.dropFirst(2)), relativeTo: self.subject.config.rootURL)
.resolvingSymlinksInPath()
.appendingPathExtension("json")
.path
expect(path).to(equal(expectedPath))

return true
}))
Expand Down Expand Up @@ -130,7 +243,7 @@ class OperationManifestFileGeneratorTests: XCTestCase {
}))

fileManager.mock(closure: .createFile({ path, data, attributes in
expect(path).to(equal(filePath))
expect(path).to(equal("\(filePath).json"))

expect(String(data: data!, encoding: .utf8)).to(equal(
"""
Expand Down