Skip to content

Implementing preflight(),loadWithErrorin NSBundle #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions Foundation/NSBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class Bundle: NSObject {

let url = URL(fileURLWithPath: resolvedPath)
_bundle = CFBundleCreate(kCFAllocatorSystemDefault, unsafeBitCast(url, to: CFURL.self))
if (_bundle == nil) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parentheses?

return nil
}
}

public convenience init?(url: URL) {
Expand Down Expand Up @@ -73,8 +76,27 @@ public class Bundle: NSObject {
}
public func unload() -> Bool { NSUnimplemented() }

public func preflight() throws { NSUnimplemented() }
public func loadAndReturnError() throws { NSUnimplemented() }
public func preflight() throws {
var unmanagedError:Unmanaged<CFError>? = nil
try withUnsafeMutablePointer(&unmanagedError) { (unmanagedCFError: UnsafeMutablePointer<Unmanaged<CFError>?>) in
CFBundlePreflightExecutable(_bundle, unmanagedCFError)
if let error = unmanagedCFError.pointee {
throw error.takeRetainedValue()._nsObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space.

}
}
}

public func loadAndReturnError() throws {
var unmanagedError:Unmanaged<CFError>? = nil
try withUnsafeMutablePointer(&unmanagedError) { (unmanagedCFError: UnsafeMutablePointer<Unmanaged<CFError>?>) in
CFBundleLoadExecutableAndReturnError(_bundle, unmanagedCFError)
if let error = unmanagedCFError.pointee {
let retainedValue = error.takeRetainedValue()
throw retainedValue._nsObject
}
}
}


/* Methods for locating various components of a bundle. */
public var bundleURL: URL {
Expand Down
39 changes: 39 additions & 0 deletions TestFoundation/TestNSBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class TestNSBundle : XCTestCase {
("test_localizations", test_localizations),
("test_URLsForResourcesWithExtension", test_URLsForResourcesWithExtension),
("test_bundleLoad", test_bundleLoad),
("test_bundleLoadWithError", test_bundleLoadWithError),
("test_bundleWithInvalidPath", test_bundleWithInvalidPath),
("test_bundlePreflight", test_bundlePreflight),
]
}

Expand Down Expand Up @@ -153,10 +156,46 @@ class TestNSBundle : XCTestCase {

_cleanupPlayground(playground)
}

func test_bundleLoad(){
let bundle = Bundle.main()
let _ = bundle.load()
XCTAssertTrue(bundle.isLoaded)
}

func test_bundleLoadWithError(){
let bundleValid = Bundle.main()
//test valid load using loadAndReturnError
do{
try bundleValid.loadAndReturnError()
}catch{
XCTFail("should not fail to load")
}
// executable cannot be located
guard let playground = _setupPlayground() else { XCTFail("Unable to create playground"); return }
let bundle = Bundle(path: playground + _bundleName)
XCTAssertThrowsError(try bundle!.loadAndReturnError())
_cleanupPlayground(playground)
}

func test_bundleWithInvalidPath(){
let bundleInvalid = Bundle(path: "/tmp/test.playground")
XCTAssertNil(bundleInvalid)
}

func test_bundlePreflight(){
let bundleValid = Bundle.main()
do{
try bundleValid.preflight()
}catch{
XCTFail("should not fail to load")
}
// executable cannot be located ..preflight should report error
guard let playground = _setupPlayground() else { XCTFail("Unable to create playground"); return }
let bundle = Bundle(path: playground + _bundleName)
XCTAssertThrowsError(try bundle!.preflight())
_cleanupPlayground(playground)
}


}