mirror of
https://github.com/gosticks/SwiftGit2.git
synced 2025-10-16 11:55:34 +00:00
add specs for Repository.isValid
This commit is contained in:
parent
c5017fc6c9
commit
455ac1143f
@ -934,18 +934,23 @@ final public class Repository {
|
||||
|
||||
// MARK: - Validity/Existence Check
|
||||
|
||||
/// - returns: `.success(true)` iff there is a git repository at `url`, `.success(false)` if there isn't, and a `.failure` if there's been an error.
|
||||
/// - returns: `.success(true)` iff there is a git repository at `url`,
|
||||
/// `.success(false)` if there isn't,
|
||||
/// and a `.failure` if there's been an error.
|
||||
public static func isValid(url: URL) -> Result<Bool, NSError> {
|
||||
var pointer: OpaquePointer? = nil
|
||||
var pointer: OpaquePointer?
|
||||
|
||||
let result = url.withUnsafeFileSystemRepresentation {
|
||||
git_repository_open_ext(&pointer, $0, GIT_REPOSITORY_OPEN_NO_SEARCH.rawValue, nil)
|
||||
}
|
||||
|
||||
switch result {
|
||||
case GIT_ENOTFOUND.rawValue: return .success(false)
|
||||
case GIT_OK.rawValue: return .success(true)
|
||||
default: return .failure(NSError(gitError: result, pointOfFailure: "git_repository_open_ext"))
|
||||
case GIT_ENOTFOUND.rawValue:
|
||||
return .success(false)
|
||||
case GIT_OK.rawValue:
|
||||
return .success(true)
|
||||
default:
|
||||
return .failure(NSError(gitError: result, pointOfFailure: "git_repository_open_ext"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,6 +27,46 @@ class RepositorySpec: QuickSpec {
|
||||
}
|
||||
}
|
||||
|
||||
describe("Repository.Type.isValid(url:)") {
|
||||
it("should return true if the repo exists") {
|
||||
guard let repositoryURL = Fixtures.simpleRepository.directoryURL else {
|
||||
fail("Fixture setup broken: Repository does not exist"); return
|
||||
}
|
||||
|
||||
let result = Repository.isValid(url: repositoryURL)
|
||||
|
||||
expect(result.error).to(beNil())
|
||||
|
||||
if case .success(let isValid) = result {
|
||||
expect(isValid).to(beTruthy())
|
||||
}
|
||||
}
|
||||
|
||||
it("should return false if the directory does not contain a repo") {
|
||||
let tmpURL = URL(fileURLWithPath: "/dev/null")
|
||||
let result = Repository.isValid(url: tmpURL)
|
||||
|
||||
expect(result.error).to(beNil())
|
||||
|
||||
if case .success(let isValid) = result {
|
||||
expect(isValid).to(beFalsy())
|
||||
}
|
||||
}
|
||||
|
||||
it("should return error if .git is not readable") {
|
||||
let localURL = self.temporaryURL(forPurpose: "git-isValid-unreadable").appendingPathComponent(".git")
|
||||
let nonReadablePermissions: [String: Any] = [FileAttributeKey.posixPermissions.rawValue: 0o077]
|
||||
try! FileManager.default.createDirectory(
|
||||
at: localURL,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nonReadablePermissions)
|
||||
let result = Repository.isValid(url: localURL)
|
||||
|
||||
expect(result.value).to(beNil())
|
||||
expect(result.error).notTo(beNil())
|
||||
}
|
||||
}
|
||||
|
||||
describe("Repository.Type.create(at:)") {
|
||||
it("should create a new repo at the specified location") {
|
||||
let localURL = self.temporaryURL(forPurpose: "local-create")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user