diff --git a/SwiftShell/Files.swift b/SwiftShell/Files.swift index 6b109cc..5825d40 100644 --- a/SwiftShell/Files.swift +++ b/SwiftShell/Files.swift @@ -24,7 +24,7 @@ It lies in the user's temporary directory and will be automatically deleted at s */ public let tempdirectory: String = { var error: NSError? - let tempdirectory = NSTemporaryDirectory().stringByAppendingPathComponent("SwiftShell-" + NSProcessInfo.processInfo().globallyUniqueString) + let tempdirectory = NSTemporaryDirectory() / "SwiftShell-" + NSProcessInfo.processInfo().globallyUniqueString NSFileManager.defaultManager() .createDirectoryAtPath(tempdirectory, withIntermediateDirectories:true, attributes: nil, error: &error) if let error = error { @@ -50,3 +50,9 @@ public var workdirectory: String { } } } + + +/** Allows for `"/directory" / "file.extension"` etc. */ +public func / (leftpath: String, rightpath: String) -> String { + return toURLOrError(leftpath).URLByAppendingPathComponent(rightpath).path! +} diff --git a/SwiftShellTests/Files_Tests.swift b/SwiftShellTests/Files_Tests.swift index 393b673..afc08e8 100644 --- a/SwiftShellTests/Files_Tests.swift +++ b/SwiftShellTests/Files_Tests.swift @@ -25,4 +25,10 @@ class Files_Tests: XCTestCase { XCTAssertEqual( workdirectory, "/private/tmp" ) XCTAssertEqual( $("pwd"), "/private/tmp" ) } + + func testURLConcatenationOperator () { + XCTAssertEqual( "/directory" / "file.extension", "/directory/file.extension" ) + XCTAssertEqual( "/root" / "directory" / "file.extension", "/root/directory/file.extension" ) + XCTAssertEqual( "directory" / "file.extension", workdirectory + "/directory/file.extension" ) + } } diff --git a/SwiftShellTests/Scripts/readandwritefiles.swift b/SwiftShellTests/Scripts/readandwritefiles.swift index c361fd8..03a8982 100755 --- a/SwiftShellTests/Scripts/readandwritefiles.swift +++ b/SwiftShellTests/Scripts/readandwritefiles.swift @@ -2,7 +2,7 @@ import SwiftShell -let filepath = tempdirectory + "/newfile.txt" +let filepath = tempdirectory / "newfile.txt" // File doesn't exist. Create it. let file1 = open(forWriting: filepath )