Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Aug 21, 2019
1 parent 9a411d6 commit 7012bab
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ let package = Package(
),
.testTarget(
name: "RefRepoKitTests",
dependencies: ["RefRepoKit", "NIO", "CommandKit", "ExecutorMocks"]
),
.testTarget(
name: "RefRepoKitRealTests",
dependencies: ["RefRepoKit", "NIO", "CommandKit"]
)
]
Expand Down
143 changes: 143 additions & 0 deletions Tests/RefRepoKitRealTests/Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
@testable import RefRepoKit
import XCTest
import NIO
import CommandKit


/*
Warning: The below tests rely on an external service which in a testing environment should be a NO-GO but for the convenience and as this package needs github to work we have decided to test the actual real functionality without mocks ond stubs !!!!!!!!
Also! Tests should not be run in parallel
*/


final class RefRepoTests: XCTestCase {

let repoSSH = "git@github.com:Einstore/Einstore.git"
let repoHTTPS = "https://github.com/Einstore/ShellKit.git"

var ref: RefRepo!
var shell: Shell!

var output: String = ""

override func setUp() {
super.setUp()

let eventLoop = EmbeddedEventLoop()

ref = try! RefRepo(
.local,
temp: "/tmp/test-refrepo/",
on: eventLoop)
{ text in
print(text)
self.output += text
}

shell = try! Shell(.local(dir: "/tmp"), on: eventLoop)
shell.outputCommands = false

_ = try! shell.cmd.rm(path: ref.tmp(for: repoSSH), flags: "-rf").wait()
_ = try! shell.cmd.rm(path: ref.tmp(for: repoHTTPS), flags: "-rf").wait()
}

override func tearDown() {
super.tearDown()

try! ref.clean(for: "test-stuff").wait()
}

func testShell() {
var count = 0
_ = try! shell.run(bash: "pwd") { s in
XCTAssertEqual(s.trimmingCharacters(in: .newlines), "/tmp")
count += 1
}.future.wait()
XCTAssertEqual(count, 1)
}

func testLongShell() {
var count = 0
let command = """
for ((i=1;i<=3;i++));
do
echo $i
echo "\n"
sleep 0.2
done
"""
_ = try! shell.run(bash: command) { s in
let s = s.trimmingCharacters(in: .newlines)
if !s.isEmpty {
count += 1
XCTAssertEqual(s, String(count))
}
}.future.wait()
XCTAssertEqual(count, 3)
}

func testCloneSSH() {
ref.sshKeys.append("id_rsa.0")
let string = try! ref.clone(repo: repoSSH, checkout: "master", target: "/tmp/test-refrepo/clones/test-stuff", workspace: "/tmp/test-refrepo/clones/").wait()

let content = try! FileManager.default.contentsOfDirectory(atPath: string)
XCTAssertTrue(content.count > 3)
XCTAssertTrue(content.contains(".git"))

XCTAssertTrue(output.contains(repoSSH), "Text not present in: \(output)")

XCTAssertEqual(string, "/tmp/test-refrepo/clones/test-stuff")
}

func testCloneHTTPS() {
let string = try! ref.clone(repo: repoHTTPS, checkout: "master", target: "/tmp/test-refrepo/clones/test-stuff", workspace: "/tmp/test-refrepo/clones/").wait()

let content = try! FileManager.default.contentsOfDirectory(atPath: string)
XCTAssertTrue(content.count > 3)
XCTAssertTrue(content.contains(".git"))

XCTAssertTrue(output.contains(repoHTTPS), "Text not present in: \(output)")

XCTAssertEqual(string, "/tmp/test-refrepo/clones/test-stuff")
}

func testFetchSSH() {
ref.sshKeys.append("id_rsa.0")
_ = try! shell.run(bash: "git clone \(repoSSH) \(ref.tmp(for: repoSSH))").future.wait()

let string = try! ref.clone(repo: repoSSH, checkout: "master", target: "/tmp/test-refrepo/clones/test-stuff", workspace: "/tmp/test-refrepo/clones/").wait()

let content = try! FileManager.default.contentsOfDirectory(atPath: string)
XCTAssertTrue(content.count > 3)
XCTAssertTrue(content.contains(".git"))

XCTAssertTrue(output.contains("Your branch is up to date with 'origin/master'"), "Text not present in: \(output)")

XCTAssertEqual(string, "/tmp/test-refrepo/clones/test-stuff")
}

func testFetchHTTPS() {
_ = try! shell.run(bash: "git clone \(repoHTTPS) \(ref.tmp(for: repoSSH))").future.wait()

let string = try! ref.clone(repo: repoHTTPS, checkout: "master", target: "/tmp/test-refrepo/clones/test-stuff", workspace: "/tmp/test-refrepo/clones/").wait()

let content = try! FileManager.default.contentsOfDirectory(atPath: string)
XCTAssertTrue(content.count > 3)
XCTAssertTrue(content.contains(".git"))

XCTAssertTrue(output.contains("Your branch is up to date with 'origin/master'"), "Text not present in: \(output)")

XCTAssertEqual(string, "/tmp/test-refrepo/clones/test-stuff")
}

static let allTests = [
("testShell", testShell),
("testLongShell", testLongShell),
("testCloneSSH", testCloneSSH),
("testCloneHTTPS", testCloneHTTPS),
("testFetchSSH", testFetchSSH),
("testFetchHTTPS", testFetchHTTPS)
]

}

20 changes: 7 additions & 13 deletions Tests/RefRepoKitTests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
import XCTest
import NIO
import CommandKit


/*
Warning: The below tests rely on an external service which in a testing environment should be a NO-GO but for the convenience and as this package needs github to work we have decided to test the actual real functionality without mocks ond stubs !!!!!!!!
Also! Tests should not be run in parallel
*/
import ExecutorMocks


final class RefRepoTests: XCTestCase {
Expand All @@ -16,7 +11,7 @@ final class RefRepoTests: XCTestCase {
let repoHTTPS = "https://github.com/Einstore/ShellKit.git"

var ref: RefRepo!
var shell: Shell!
var shell: MasterExecutor!

var output: String = ""

Expand All @@ -25,18 +20,17 @@ final class RefRepoTests: XCTestCase {

let eventLoop = EmbeddedEventLoop()

let shell = ExecutorMock()

ref = try! RefRepo(
.local,
shell,
temp: "/tmp/test-refrepo/",
on: eventLoop)
{ text in
print(text)
self.output += text
}

shell = try! Shell(.local(dir: "/tmp"), on: eventLoop)
shell.outputCommands = false

_ = try! shell.cmd.rm(path: ref.tmp(for: repoSSH), flags: "-rf").wait()
_ = try! shell.cmd.rm(path: ref.tmp(for: repoHTTPS), flags: "-rf").wait()
}
Expand Down Expand Up @@ -103,7 +97,7 @@ final class RefRepoTests: XCTestCase {

func testFetchSSH() {
ref.sshKeys.append("id_rsa.0")
_ = try! shell.run(bash: "git clone \(repoSSH) \(ref.tmp(for: repoSSH))").future.wait()
_ = try! shell.run(bash: "git clone \(repoSSH) \(ref.tmp(for: repoSSH))", output: nil).future.wait()

let string = try! ref.clone(repo: repoSSH, checkout: "master", target: "/tmp/test-refrepo/clones/test-stuff", workspace: "/tmp/test-refrepo/clones/").wait()

Expand All @@ -117,7 +111,7 @@ final class RefRepoTests: XCTestCase {
}

func testFetchHTTPS() {
_ = try! shell.run(bash: "git clone \(repoHTTPS) \(ref.tmp(for: repoSSH))").future.wait()
_ = try! shell.run(bash: "git clone \(repoHTTPS) \(ref.tmp(for: repoSSH))", output: nil).future.wait()

let string = try! ref.clone(repo: repoHTTPS, checkout: "master", target: "/tmp/test-refrepo/clones/test-stuff", workspace: "/tmp/test-refrepo/clones/").wait()

Expand Down

0 comments on commit 7012bab

Please sign in to comment.