From 7012bab59d6568757bf5e1e06103c80b3f70bcdd Mon Sep 17 00:00:00 2001 From: Ondrej Rafaj Date: Wed, 21 Aug 2019 22:55:21 +0100 Subject: [PATCH] wip --- Package.swift | 4 + Tests/RefRepoKitRealTests/Tests.swift | 143 ++++++++++++++++++++++++++ Tests/RefRepoKitTests/Tests.swift | 20 ++-- 3 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 Tests/RefRepoKitRealTests/Tests.swift diff --git a/Package.swift b/Package.swift index a492822..2621140 100644 --- a/Package.swift +++ b/Package.swift @@ -27,6 +27,10 @@ let package = Package( ), .testTarget( name: "RefRepoKitTests", + dependencies: ["RefRepoKit", "NIO", "CommandKit", "ExecutorMocks"] + ), + .testTarget( + name: "RefRepoKitRealTests", dependencies: ["RefRepoKit", "NIO", "CommandKit"] ) ] diff --git a/Tests/RefRepoKitRealTests/Tests.swift b/Tests/RefRepoKitRealTests/Tests.swift new file mode 100644 index 0000000..60d76c9 --- /dev/null +++ b/Tests/RefRepoKitRealTests/Tests.swift @@ -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) + ] + +} + diff --git a/Tests/RefRepoKitTests/Tests.swift b/Tests/RefRepoKitTests/Tests.swift index 60d76c9..a55f959 100644 --- a/Tests/RefRepoKitTests/Tests.swift +++ b/Tests/RefRepoKitTests/Tests.swift @@ -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 { @@ -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 = "" @@ -25,8 +20,10 @@ final class RefRepoTests: XCTestCase { let eventLoop = EmbeddedEventLoop() + let shell = ExecutorMock() + ref = try! RefRepo( - .local, + shell, temp: "/tmp/test-refrepo/", on: eventLoop) { text in @@ -34,9 +31,6 @@ final class RefRepoTests: XCTestCase { 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() } @@ -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() @@ -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()