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

Verify Git version >= 2.3 #41

Merged
merged 2 commits into from
May 13, 2015
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
28 changes: 28 additions & 0 deletions BuildaUtils/SSHKeyVerification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,36 @@ import Foundation

public class SSHKeyVerification {

public class func getGitVersion() -> Script.ScriptResponse {

let response = Script.run("/usr/bin/git", arguments: ["--version"])
return response
}

public class func verifyGitVersion(response: Script.ScriptResponse) -> (Bool, String) {

let versionString = response.standardOutput.stripTrailingNewline()
let comps = versionString.componentsSeparatedByString(" ")

if comps.count >= 3 {
let version = comps[2]
if version >= "2.3.0" {
return (true, "")
}
return (false, "Git version >= 2.3 required, found \(version)")
}
return (false, "Couldn't verify git version")
}

public class func verifyKeys(path: String, repoSSHUrl: String) -> Script.ScriptResponse {

let git = self.getGitVersion()
let (gitVersionSuccess, gitVersionErrorString) = self.verifyGitVersion(git)
if !gitVersionSuccess {
Log.error("Insufficient git version. Found \(git.standardOutput.stripTrailingNewline()), we require at least 2.3")
return (1, "", gitVersionErrorString)
}

//create a temp script, because NSTask is being difficult and doesn't play nice with environment variables,
//which we need for forcing SSH keys from a specific path to verify they are valid for your repo. sigh.
let uuid = NSUUID().UUIDString
Expand Down
37 changes: 23 additions & 14 deletions BuildaUtilsTests/ScriptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,35 @@ import BuildaUtils

class ScriptTests: XCTestCase {

func testGitVersion() {
func testRealGitVersion() {

//here we have to specify the exact path, unfortunately `which git` leads to the prebundled git in xcode
//whereas when you run Buildasaur normally (under your user), `which git` will do what you'd expect, choose
//your active git binary (probably in /usr/bin/git or /usr/local/bin/git

let response = Script.run("/usr/bin/git", arguments: ["--version"])
let response = SSHKeyVerification.getGitVersion()
XCTAssertEqual(response.terminationStatus, 0)
XCTAssertEqual(response.standardError, "")

let versionString = response.standardOutput
XCTAssert(count(versionString) > 0, "Git version must not be an empty string")
let (success, errorString) = SSHKeyVerification.verifyGitVersion(response)
XCTAssertTrue(success)
XCTAssertEqual(errorString, "")
}

func testFakeRightGitVersion() {

//test we pass higher versions...
let response = (0, "git version 2.3.1 bla bla bla", "")

let (success, errorString) = SSHKeyVerification.verifyGitVersion(response)
XCTAssertTrue(success)
XCTAssertEqual(errorString, "")
}

func testFakeWrongGitVersion() {

let comps = versionString.componentsSeparatedByString(" ")
XCTAssertGreaterThanOrEqual(comps.count, 3)
//test we catch lower versions...
let response = (0, "git version 2.1.0 bla bla bla", "")

if comps.count >= 3 {
let version = comps[2]
XCTAssertGreaterThanOrEqual(version, "2.3.0", "Git version must be at least 2.3")
}
let (success, errorString) = SSHKeyVerification.verifyGitVersion(response)
XCTAssertFalse(success)
XCTAssertNotEqual(errorString, "")
}

func testWhich() {
Expand Down