From 5aad0cc3e410aeaf735b77b48a1eeadc29d06256 Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Thu, 28 Aug 2014 00:12:01 +0200 Subject: [PATCH] "open" now writes to standardError and quits when opening a file fails. Using assert here was a really bad idea since it may not be evaluated in a release build. --- SwiftShell/File.swift | 8 ++++++-- .../Scripts/exitswhenopeningnon-existentfile.swift | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 SwiftShellTests/Scripts/exitswhenopeningnon-existentfile.swift diff --git a/SwiftShell/File.swift b/SwiftShell/File.swift index 1cc51f0..2df5d96 100644 --- a/SwiftShell/File.swift +++ b/SwiftShell/File.swift @@ -64,8 +64,12 @@ public func open(path: String, mode: FileMode = .Read) -> File { } // file may be nil if for instance path is invalid - // TODO: it physically pains me to write the next line. Proper error handling is forthcoming. - assert( file != nil, "open file \"\(path)\" failed.") + // TODO: it physically pains me to write the next lines. Proper error handling is forthcoming. + if file == nil { + standarderror.write("Error: Opening file \"\(path)\" failed.\n") + exit(EXIT_FAILURE) + } + return file! } diff --git a/SwiftShellTests/Scripts/exitswhenopeningnon-existentfile.swift b/SwiftShellTests/Scripts/exitswhenopeningnon-existentfile.swift new file mode 100644 index 0000000..20481d2 --- /dev/null +++ b/SwiftShellTests/Scripts/exitswhenopeningnon-existentfile.swift @@ -0,0 +1,5 @@ + +import SwiftShell + +let file = open("file which does not exist") +print("this is not printed, the script has exited")