Use built-in XCTest error testing.
This commit is contained in:
@@ -71,7 +71,8 @@
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<Testables>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
skipped = "NO"
|
||||
parallelizable = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "GeneralTests"
|
||||
@@ -81,7 +82,8 @@
|
||||
</BuildableReference>
|
||||
</TestableReference>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
skipped = "NO"
|
||||
parallelizable = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "StreamTests"
|
||||
@@ -91,7 +93,8 @@
|
||||
</BuildableReference>
|
||||
</TestableReference>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
skipped = "NO"
|
||||
parallelizable = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "SwiftShellTests"
|
||||
|
||||
@@ -95,34 +95,31 @@ public class Run_Tests: XCTestCase {
|
||||
|
||||
public class RunAsync_Tests: XCTestCase {
|
||||
|
||||
func testReturnsStandardOutput() {
|
||||
func testReturnsStandardOutput() throws {
|
||||
let asynccommand = runAsync("/bin/echo", "one", "two" )
|
||||
AssertDoesNotThrow { try asynccommand.finish() }
|
||||
try asynccommand.finish()
|
||||
|
||||
XCTAssertEqual( asynccommand.stdout.read(), "one two\n" )
|
||||
XCTAssertEqual( asynccommand.stderror.read(), "" )
|
||||
}
|
||||
|
||||
func testReturnsStandardError() {
|
||||
func testReturnsStandardError() throws {
|
||||
let asynccommand = runAsync(bash: "echo one two > /dev/stderr" )
|
||||
AssertDoesNotThrow { try asynccommand.finish() }
|
||||
try asynccommand.finish()
|
||||
|
||||
XCTAssertEqual( asynccommand.stderror.read(), "one two\n" )
|
||||
XCTAssertEqual( asynccommand.stdout.read(), "" )
|
||||
}
|
||||
|
||||
func testArgumentsFromArray() {
|
||||
AssertDoesNotThrow {
|
||||
let output = try runAsync("/bin/echo", ["one", "two"]).finish().stdout.read()
|
||||
XCTAssertEqual( output, "one two\n" )
|
||||
}
|
||||
func testArgumentsFromArray() throws {
|
||||
let output = try runAsync("/bin/echo", ["one", "two"]).finish().stdout.read()
|
||||
XCTAssertEqual( output, "one two\n" )
|
||||
}
|
||||
|
||||
func testFinishThrowsErrorOnExitcodeNotZero() {
|
||||
let asynccommand = runAsync(bash: "echo errormessage > /dev/stderr; exit 1" )
|
||||
|
||||
AssertThrows(CommandError.returnedErrorCode(command: "/bin/bash -c \"echo errormessage > /dev/stderr; exit 1\"", errorcode: 1))
|
||||
{ try asynccommand.finish() }
|
||||
XCTAssertThrowsError(try asynccommand.finish())
|
||||
XCTAssertEqual( asynccommand.stderror.read(), "errormessage\n" )
|
||||
}
|
||||
|
||||
@@ -219,14 +216,14 @@ public class XCTestCase_TestOutput: XCTestCase {
|
||||
}
|
||||
|
||||
public class RunAsyncAndPrint_Tests: XCTestCase_TestOutput {
|
||||
func testReturnsStandardOutput() {
|
||||
AssertDoesNotThrow { try runAsyncAndPrint("/bin/echo", "one", "two" ).finish() }
|
||||
func testReturnsStandardOutput() throws {
|
||||
try runAsyncAndPrint("/bin/echo", "one", "two" ).finish()
|
||||
|
||||
XCTAssertEqual( test_stdout.readSome(encoding: .utf8), "one two\n" )
|
||||
}
|
||||
|
||||
func testReturnsStandardError() {
|
||||
AssertDoesNotThrow { try runAsyncAndPrint(bash: "echo one two > /dev/stderr" ).finish() }
|
||||
func testReturnsStandardError() throws {
|
||||
try runAsyncAndPrint(bash: "echo one two > /dev/stderr" ).finish()
|
||||
|
||||
XCTAssertEqual( test_stderr.readSome(encoding: .utf8), "one two\n" )
|
||||
}
|
||||
@@ -243,32 +240,26 @@ public class RunAsyncAndPrint_Tests: XCTestCase_TestOutput {
|
||||
}
|
||||
|
||||
public class RunAndPrint_Tests: XCTestCase_TestOutput {
|
||||
|
||||
func testReturnsStandardOutput() {
|
||||
AssertDoesNotThrow { try runAndPrint("/bin/echo", "one", "two" ) }
|
||||
|
||||
func testArgumentsFromArray() throws {
|
||||
try runAndPrint("/bin/echo", ["one", "two"] )
|
||||
XCTAssertEqual( test_stdout.readSome(encoding: .utf8), "one two\n" )
|
||||
}
|
||||
|
||||
func testArgumentsFromArray() {
|
||||
AssertDoesNotThrow { try runAndPrint("/bin/echo", ["one", "two"] ) }
|
||||
|
||||
func testReturnsStandardOutput() throws {
|
||||
try runAndPrint("/bin/echo", "one", "two" )
|
||||
XCTAssertEqual( test_stdout.readSome(encoding: .utf8), "one two\n" )
|
||||
}
|
||||
|
||||
func testReturnsStandardError() {
|
||||
AssertDoesNotThrow { try runAndPrint(bash: "echo one two > /dev/stderr" ) }
|
||||
|
||||
func testReturnsStandardError() throws {
|
||||
try runAndPrint(bash: "echo one two > /dev/stderr" )
|
||||
XCTAssertEqual( test_stderr.readSome(encoding: .utf8), "one two\n" )
|
||||
}
|
||||
|
||||
func testThrowsErrorOnExitcodeNotZero() {
|
||||
AssertThrows(CommandError.returnedErrorCode(command: "/bin/bash -c \"exit 1\"", errorcode: 1))
|
||||
{ try runAndPrint("bash", "-c", "exit 1") }
|
||||
XCTAssertThrowsError(try runAndPrint("bash", "-c", "exit 1"))
|
||||
}
|
||||
|
||||
func testThrowsErrorOnInaccessibleExecutable() {
|
||||
AssertThrows(CommandError.inAccessibleExecutable(path: "notachance"))
|
||||
{ try runAndPrint("notachance") }
|
||||
XCTAssertThrowsError(try runAndPrint("notachance"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,18 +83,16 @@ public class BlankCustomContext_Tests: XCTestCase {
|
||||
XCTAssertEqual(process.stdout.read(), "one\n")
|
||||
}
|
||||
|
||||
func testRunAndPrintCommand() {
|
||||
func testRunAndPrintCommand() throws {
|
||||
var context = CustomContext()
|
||||
|
||||
AssertDoesNotThrow {
|
||||
try context.runAndPrint("/bin/echo", "one") // sent to null
|
||||
try context.runAndPrint("/bin/echo", "one") // sent to null
|
||||
|
||||
let outputpipe = Pipe()
|
||||
context.stdout = FileHandleStream(outputpipe.fileHandleForWriting, encoding: .utf8)
|
||||
let output = outputpipe.fileHandleForReading
|
||||
let outputpipe = Pipe()
|
||||
context.stdout = FileHandleStream(outputpipe.fileHandleForWriting, encoding: .utf8)
|
||||
let output = outputpipe.fileHandleForReading
|
||||
|
||||
try context.runAndPrint("/bin/echo", "two")
|
||||
XCTAssertEqual(output.readSome(encoding: .utf8), "two\n")
|
||||
}
|
||||
try context.runAndPrint("/bin/echo", "two")
|
||||
XCTAssertEqual(output.readSome(encoding: .utf8), "two\n")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,88 +13,76 @@ import Foundation
|
||||
public class UrlAppendationOperator: XCTestCase {
|
||||
|
||||
func testUrlPlusString() {
|
||||
XCTAssertEqual( URL(fileURLWithPath: "dir") + "file.txt", URL(fileURLWithPath: "dir/file.txt"))
|
||||
XCTAssertEqual( URL(fileURLWithPath: "dir/") + "/file.txt", URL(fileURLWithPath: "dir/file.txt"))
|
||||
XCTAssertEqual( URL(string: "dir")! + "file.txt", URL(string: "dir/file.txt"))
|
||||
XCTAssertEqual(URL(fileURLWithPath: "dir") + "file.txt", URL(fileURLWithPath: "dir/file.txt"))
|
||||
XCTAssertEqual(URL(fileURLWithPath: "dir/") + "/file.txt", URL(fileURLWithPath: "dir/file.txt"))
|
||||
XCTAssertEqual(URL(string: "dir")! + "file.txt", URL(string: "dir/file.txt"))
|
||||
}
|
||||
}
|
||||
|
||||
public class Open: XCTestCase {
|
||||
|
||||
func testReadFile() {
|
||||
func testReadFile() throws {
|
||||
let path = main.tempdirectory + "testReadFile.txt"
|
||||
let _ = SwiftShell.run(bash: "echo Lorem ipsum dolor > " + path)
|
||||
|
||||
AssertDoesNotThrow {
|
||||
let file = try open(path)
|
||||
XCTAssert(file.read().hasPrefix("Lorem ipsum dolor"))
|
||||
}
|
||||
let file = try open(path)
|
||||
XCTAssert(file.read().hasPrefix("Lorem ipsum dolor"))
|
||||
}
|
||||
|
||||
func testReadFileWhichDoesNotExist() {
|
||||
XCTAssertThrowsError(try open("/nonexistingfile.txt"))
|
||||
}
|
||||
|
||||
func testOpenForWritingFileWhichDoesNotExist() {
|
||||
func testOpenForWritingFileWhichDoesNotExist() throws {
|
||||
let path = main.tempdirectory + "testOpenForWritingFileWhichDoesNotExist.txt"
|
||||
|
||||
AssertDoesNotThrow {
|
||||
let file = try open(forWriting: path)
|
||||
file.print("line 1")
|
||||
file.close()
|
||||
let file = try open(forWriting: path)
|
||||
file.print("line 1")
|
||||
file.close()
|
||||
|
||||
let contents = try String(contentsOfFile: path, encoding: .utf8)
|
||||
XCTAssertEqual( contents, "line 1\n" )
|
||||
}
|
||||
let contents = try String(contentsOfFile: path, encoding: .utf8)
|
||||
XCTAssertEqual( contents, "line 1\n" )
|
||||
}
|
||||
|
||||
func testOpenForOverWritingFileWhichDoesNotExist() {
|
||||
func testOpenForOverWritingFileWhichDoesNotExist() throws {
|
||||
let path = main.tempdirectory + "testOpenForOverWritingFileWhichDoesNotExist.txt"
|
||||
|
||||
AssertDoesNotThrow {
|
||||
let file = try open(forWriting: path, overwrite: true)
|
||||
file.print("line 1")
|
||||
file.close()
|
||||
let file = try open(forWriting: path, overwrite: true)
|
||||
file.print("line 1")
|
||||
file.close()
|
||||
|
||||
let contents = try String(contentsOfFile: path, encoding: .utf8)
|
||||
XCTAssertEqual( contents, "line 1\n" )
|
||||
}
|
||||
let contents = try String(contentsOfFile: path, encoding: .utf8)
|
||||
XCTAssertEqual( contents, "line 1\n" )
|
||||
}
|
||||
|
||||
func testOpenForWritingExistingFile_AppendsFile() {
|
||||
func testOpenForWritingExistingFile_AppendsFile() throws {
|
||||
let path = main.tempdirectory + "testOpenForWritingExistingFile_AppendsFile.txt"
|
||||
let _ = SwiftShell.run(bash: "echo existing line > " + path)
|
||||
|
||||
AssertDoesNotThrow {
|
||||
let file = try open(forWriting: path)
|
||||
file.print("new line")
|
||||
file.close()
|
||||
let file = try open(forWriting: path)
|
||||
file.print("new line")
|
||||
file.close()
|
||||
|
||||
let contents = try String(contentsOfFile: path, encoding: .utf8)
|
||||
XCTAssertEqual( contents, "existing line\nnew line\n" )
|
||||
}
|
||||
let contents = try String(contentsOfFile: path, encoding: .utf8)
|
||||
XCTAssertEqual( contents, "existing line\nnew line\n" )
|
||||
}
|
||||
|
||||
func testOpenForOverWritingExistingFile() {
|
||||
func testOpenForOverWritingExistingFile() throws {
|
||||
let path = main.tempdirectory + "testOpenForOverWritingExistingFile.txt"
|
||||
let _ = SwiftShell.run(bash: "echo existing line > " + path)
|
||||
|
||||
AssertDoesNotThrow {
|
||||
let file = try open(forWriting: path, overwrite: true)
|
||||
file.print("new line")
|
||||
file.close()
|
||||
let file = try open(forWriting: path, overwrite: true)
|
||||
file.print("new line")
|
||||
file.close()
|
||||
|
||||
let contents = try String(contentsOfFile: path, encoding: .utf8)
|
||||
XCTAssertEqual( contents, "new line\n" )
|
||||
}
|
||||
let contents = try String(contentsOfFile: path, encoding: .utf8)
|
||||
XCTAssertEqual( contents, "new line\n" )
|
||||
}
|
||||
|
||||
func testOpenForOverWritingCreatesIntermediateDirectory() {
|
||||
let path = main.tempdirectory + "intermediate/path/testOpenForOverWritingExistingFile.txt"
|
||||
|
||||
AssertDoesNotThrow {
|
||||
_ = try open(forWriting: path, overwrite: false)
|
||||
XCTAssert(Files.fileExists(atPath: path))
|
||||
}
|
||||
}
|
||||
func testOpenForOverWritingCreatesIntermediateDirectory() throws {
|
||||
let path = main.tempdirectory + "intermediate/path/testOpenForOverWritingExistingFile.txt"
|
||||
_ = try open(forWriting: path, overwrite: false)
|
||||
XCTAssert(Files.fileExists(atPath: path))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
//
|
||||
// TestHelpers.swift
|
||||
// SwiftShell
|
||||
//
|
||||
// Created by Kåre Morstøl on 30.01.2017.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
/// Verifies the closure does not throw any errors.
|
||||
///
|
||||
/// AssertDoesNotThrow {
|
||||
/// try something()
|
||||
/// }
|
||||
///
|
||||
func AssertDoesNotThrow(_ closure: () throws -> ()) {
|
||||
do {
|
||||
try closure()
|
||||
} catch {
|
||||
XCTFail(String(describing: error))
|
||||
}
|
||||
}
|
||||
|
||||
// From https://github.com/mrackwitz/CatchingFire/blob/master/src/CatchingFire.swift
|
||||
/// Verifies the closure throws the expected error.
|
||||
///
|
||||
/// AssertThrows(Error.ArgumentMayNotBeNegative) {
|
||||
/// try fib(-1)
|
||||
/// }
|
||||
///
|
||||
/// If the closure does not throw the expected error, the test fails.
|
||||
public func AssertThrows<E>(_ expectedError: E,
|
||||
file: StaticString = #file, line: UInt = #line, _ closure: () throws -> ())
|
||||
where E: Error, E: Equatable {
|
||||
|
||||
do {
|
||||
try closure()
|
||||
XCTFail("Expected to catch <\(expectedError)>, but no error was thrown.", file: file, line: line)
|
||||
} catch let error as E {
|
||||
XCTAssertEqual(error, expectedError,
|
||||
"Caught error <\(error)> is of the expected type <\(E.self)>, "
|
||||
+ "but not the expected case <\(expectedError)>.", file: file, line: line)
|
||||
} catch {
|
||||
XCTFail("Caught error <\(error)>, but not of the expected type and value <\(expectedError)>.", file: file, line: line)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user