Skip to content

Commit

Permalink
Operation Manifest Generation Updates (#3128)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobaFetters authored Jul 19, 2023
1 parent c338ee9 commit 7515d46
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 4 deletions.
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

0 comments on commit 7515d46

Please sign in to comment.