-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add and internally test convenience methods to access the script dire…
…ctory for supported package managers
- Loading branch information
1 parent
63540aa
commit a1dca7c
Showing
8 changed files
with
269 additions
and
9 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
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
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,19 @@ | ||
// | ||
// PackageManager.swift | ||
// ApolloCodegenLib | ||
// | ||
// Created by Ellen Shapiro on 12/11/19. | ||
// Copyright © 2019 Apollo GraphQL. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// How are you integrating the Apollo library? | ||
public enum PackageManager { | ||
case swiftPackageManager | ||
case cocoaPods | ||
case carthage | ||
|
||
/// - parameter scriptsFolderURL: The direct URL to the checked out `apollo-ios/scripts` folder. | ||
case custom(scriptsFolderURL: URL) | ||
} |
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,101 @@ | ||
// | ||
// ScriptDirectoryFinder.swift | ||
// ApolloCodegenLib | ||
// | ||
// Created by Ellen Shapiro on 12/11/19. | ||
// Copyright © 2019 Apollo GraphQL. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct ScriptDirectoryFinder { | ||
|
||
enum FindError: Error, LocalizedError { | ||
/// SPM-related | ||
case noBuildRootEnvironmentVariable | ||
case couldNotFindSourcePackagesDirectory(buildRoot: String) | ||
|
||
/// CocoaPods-related | ||
case noPodsRootEnvironmentVariable | ||
|
||
/// Carthage-related | ||
case noSourceRootEnvironmentVariable | ||
|
||
var errorDescription: String? { | ||
switch self { | ||
case .noBuildRootEnvironmentVariable: | ||
return "Could not find the `BUILD_ROOT` environment variable. Please ensure this is provided and that you are using Swift Package Manager to install Apollo." | ||
case .couldNotFindSourcePackagesDirectory(let buildRoot): | ||
return "Unable to locate SourcePackages directory from `BUILD_ROOT`: '\(buildRoot)', ensure that the apollo-ios SDK has finished installing with Swift Package Manager." | ||
case .noPodsRootEnvironmentVariable: | ||
return "Could not find the `PODS_ROOT` environment variable. Please ensure this is provided and that you are using CocoaPods to install Apollo." | ||
case .noSourceRootEnvironmentVariable: | ||
return "Could not find the `SOURCE_ROOT` environment variable. Please ensure this is provided and " | ||
} | ||
} | ||
} | ||
|
||
static func findScriptsFolder(for packageManager: PackageManager, | ||
in environment: [String: String]) throws -> URL { | ||
switch packageManager { | ||
case .swiftPackageManager: | ||
return try self.findScriptsFolderForSPM(in: environment) | ||
case .cocoaPods: | ||
return try self.findScriptsFolderForCocoaPods(in: environment) | ||
case .carthage: | ||
return try self.findScriptsFolderForCarthage(in: environment) | ||
case .custom(let urlToScriptsFolder): | ||
return urlToScriptsFolder | ||
} | ||
} | ||
|
||
private static func findScriptsFolderForSPM(in environment: [String: String]) throws -> URL { | ||
guard let buildRootPath = environment["BUILD_ROOT"] else { | ||
throw FindError.noBuildRootEnvironmentVariable | ||
} | ||
|
||
let buildRootURL = URL(fileURLWithPath: buildRootPath) | ||
var currentDirectoryURL = buildRootURL | ||
|
||
// Go to the build root and search up the chain to find the Derived Data Path where the source packages are checked out. | ||
while !FileManager.default.apollo_folderExists(at: currentDirectoryURL.appendingPathComponent("SourcePackages")) { | ||
guard currentDirectoryURL.path != "/" else { | ||
throw FindError.couldNotFindSourcePackagesDirectory(buildRoot: buildRootURL.path) | ||
} | ||
|
||
currentDirectoryURL = buildRootURL.deletingLastPathComponent() | ||
} | ||
|
||
return currentDirectoryURL | ||
.appendingPathComponent("SourcePackages") | ||
.appendingPathComponent("checkouts") | ||
.appendingPathComponent("apollo-ios") | ||
.appendingPathComponent("scripts") | ||
} | ||
|
||
private static func findScriptsFolderForCocoaPods(in environment: [String: String]) throws -> URL { | ||
guard let podsRootPath = environment["PODS_ROOT"] else { | ||
throw FindError.noPodsRootEnvironmentVariable | ||
} | ||
|
||
let podsRootURL = URL(fileURLWithPath: podsRootPath) | ||
|
||
return podsRootURL | ||
.appendingPathComponent("Apollo") | ||
.appendingPathComponent("scripts") | ||
|
||
} | ||
|
||
private static func findScriptsFolderForCarthage(in environment: [String: String]) throws -> URL { | ||
guard let sourceRootPath = environment["SRCROOT"] else { | ||
throw FindError.noSourceRootEnvironmentVariable | ||
} | ||
|
||
let sourceRootURL = URL(fileURLWithPath: sourceRootPath) | ||
return sourceRootURL | ||
.appendingPathComponent("Carthage") | ||
.appendingPathComponent("Checkouts") | ||
.appendingPathComponent("apollo-ios") | ||
.appendingPathComponent("scripts") | ||
} | ||
} |
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
112 changes: 112 additions & 0 deletions
112
Tests/ApolloCodegenTests/ScriptDiectoryFinderTests.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,112 @@ | ||
// | ||
// ScriptDirectoryFinderTests.swift | ||
// ApolloCodegenTests | ||
// | ||
// Created by Ellen Shapiro on 12/11/19. | ||
// Copyright © 2019 Apollo GraphQL. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import ApolloCodegenLib | ||
|
||
class ScriptDirectoryFinderTests: XCTestCase { | ||
|
||
func testMissingPathForSPMBuildRootThrowsCorrectError() { | ||
XCTAssertThrowsError(try ScriptDirectoryFinder.findScriptsFolder(for: .swiftPackageManager, in: [:])) { error in | ||
switch error { | ||
case ScriptDirectoryFinder.FindError.noBuildRootEnvironmentVariable: | ||
// This is what we want | ||
break | ||
default: | ||
XCTFail("Incorrect error: \(error)") | ||
} | ||
} | ||
} | ||
|
||
func testBogusPathForSPMBuildRootThrowsCorrectError() { | ||
let fakeEnvironment = [ | ||
"BUILD_ROOT": "/Applications" | ||
] | ||
|
||
XCTAssertThrowsError(try ScriptDirectoryFinder.findScriptsFolder(for: .swiftPackageManager, in: fakeEnvironment)) { error in | ||
switch error { | ||
case ScriptDirectoryFinder.FindError.couldNotFindSourcePackagesDirectory(let buildRoot): | ||
XCTAssertEqual(buildRoot, "/Applications") | ||
default: | ||
XCTFail("Incorrect error: \(error)") | ||
} | ||
} | ||
} | ||
|
||
func testBuildRootWithSourcePackagesFolderGivesCorrectPath() throws { | ||
let actualSourceRootPath = try XCTUnwrap(ProcessInfo.processInfo.environment["SRCROOT"]) | ||
|
||
let fakeEnvironment = [ | ||
"BUILD_ROOT": "\(actualSourceRootPath)/Tests/ApolloCodegenTests" | ||
] | ||
|
||
let spmScriptFolderURL = try ScriptDirectoryFinder.findScriptsFolder(for: .swiftPackageManager, in: fakeEnvironment) | ||
|
||
XCTAssertEqual(spmScriptFolderURL.path, "\(actualSourceRootPath)/Tests/ApolloCodegenTests/SourcePackages/checkouts/apollo-ios/scripts") | ||
} | ||
|
||
|
||
func testMissingPodsRootPathThrowsCorrectError() { | ||
XCTAssertThrowsError(try ScriptDirectoryFinder.findScriptsFolder(for: .cocoaPods, in: [:])) { error in | ||
switch error { | ||
case ScriptDirectoryFinder.FindError.noPodsRootEnvironmentVariable: | ||
// This is what we want. | ||
break | ||
default: | ||
XCTFail("Incorrect error: \(error)") | ||
} | ||
} | ||
} | ||
|
||
func testPresentPodsRootPathReturnsCorrectURL() throws { | ||
let fakeEnvironment = [ | ||
"PODS_ROOT" : "/Path/to/Pods" | ||
] | ||
|
||
let podsScriptFolderURL = try ScriptDirectoryFinder.findScriptsFolder(for: .cocoaPods, in: fakeEnvironment) | ||
|
||
XCTAssertEqual(podsScriptFolderURL.path, "/Path/to/Pods/Apollo/scripts") | ||
|
||
} | ||
|
||
func testMissingSourceRootPathForCarthageThrowsCorrectError() { | ||
XCTAssertThrowsError(try ScriptDirectoryFinder.findScriptsFolder(for: .carthage, in: [:])) { error in | ||
switch error { | ||
case ScriptDirectoryFinder.FindError.noSourceRootEnvironmentVariable: | ||
// This is what we want. | ||
break | ||
default: | ||
XCTFail("Incorrect error: \(error)") | ||
} | ||
} | ||
} | ||
|
||
func testPresentSourceRootPathReturnsCorrectURL() throws { | ||
let fakeEnvironment = [ | ||
"SRCROOT": "/Path/to/source/root" | ||
] | ||
|
||
let carthageScriptFolderURL = try ScriptDirectoryFinder.findScriptsFolder(for: .carthage, in: fakeEnvironment) | ||
|
||
XCTAssertEqual(carthageScriptFolderURL.path, "/Path/to/source/root/Carthage/Checkouts/apollo-ios/scripts") | ||
} | ||
|
||
func testCustomPackageManagerGivesYouBackWhateverYouHandIt() throws { | ||
let url = URL(string: "https://apollographql.com")! | ||
|
||
let returnedURL = try ScriptDirectoryFinder.findScriptsFolder(for: .custom(scriptsFolderURL: url), in: [:]) | ||
|
||
XCTAssertEqual(returnedURL, url) | ||
|
||
let fileURL = URL(fileURLWithPath: "/Applications") | ||
|
||
let returnedFileURL = try ScriptDirectoryFinder.findScriptsFolder(for: .custom(scriptsFolderURL: fileURL), in: [:]) | ||
|
||
XCTAssertEqual(returnedFileURL, fileURL) | ||
} | ||
} |
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 @@ | ||
This file exists to preserve folder structure in git. |