Add main.tempdirectory .

This commit is contained in:
Kare Morstol
2015-10-16 02:10:21 +02:00
parent 796af16d1a
commit 1d07e1db41
2 changed files with 22 additions and 0 deletions
+17
View File
@@ -71,6 +71,18 @@ extension ShellContext: ShellRunnable {
}
private func createTempdirectory () -> String {
let tempdirectory = NSURL(fileURLWithPath:NSTemporaryDirectory()) / ("SwiftShell-" + NSProcessInfo.processInfo().globallyUniqueString)
do {
try Files.createDirectoryAtPath(tempdirectory.path!, withIntermediateDirectories: true, attributes: nil)
return tempdirectory.path!
} catch let error as NSError {
exit(errormessage: "Could not create new temporary directory '\(tempdirectory)':\n\(error.localizedDescription)")
} catch {
exit(errormessage: "Unexpected error: \(error)")
}
}
public final class MainShellContext: ShellContextType {
// TODO: get encoding from environmental variable LC_CTYPE
@@ -98,6 +110,11 @@ public final class MainShellContext: ShellContextType {
}
}
/**
The tempdirectory is unique each time a script is run and is created the first time it is used.
It lies in the user's temporary directory and will be automatically deleted at some point.
*/
public lazy var tempdirectory: String = createTempdirectory()
public lazy var arguments: [String] = Process.arguments.count <= 1 ? [] : Array(Process.arguments.dropFirst())
public lazy var name: String = Process.arguments.first.map(NSURL.init)?.lastPathComponent ?? ""
+5
View File
@@ -32,6 +32,11 @@ class MainContext_Tests: XCTestCase {
XCTAssertEqual(NSURL(fileURLWithPath: "file").baseURL, NSURL(fileURLWithPath: "/private") )
main.currentdirectory = originalcurrentdirectory
}
func testTempDirectory () {
XCTAssertEqual( main.tempdirectory, main.tempdirectory )
XCTAssert( Files.fileExistsAtPath(main.tempdirectory), "Temporary directory \(main.tempdirectory) does not exist" )
}
}
class CopiedShellContext_Tests: XCTestCase {