Skip to content

Commit 0ae2e28

Browse files
saiHemakparkera
authored andcommitted
Implementing preflight(),loadWithErrorin NSBundle (#421)
* Implementing preflight(),loadWithErrorin NSBundle Also includes the fix for segmentation fault issue when the NSBundle is created with an invalid path * Done the changes as per the comments
1 parent 7fb3d00 commit 0ae2e28

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

Foundation/NSBundle.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class Bundle: NSObject {
3737

3838
let url = URL(fileURLWithPath: resolvedPath)
3939
_bundle = CFBundleCreate(kCFAllocatorSystemDefault, unsafeBitCast(url, to: CFURL.self))
40+
if (_bundle == nil) {
41+
return nil
42+
}
4043
}
4144

4245
public convenience init?(url: URL) {
@@ -73,8 +76,27 @@ public class Bundle: NSObject {
7376
}
7477
public func unload() -> Bool { NSUnimplemented() }
7578

76-
public func preflight() throws { NSUnimplemented() }
77-
public func loadAndReturnError() throws { NSUnimplemented() }
79+
public func preflight() throws {
80+
var unmanagedError:Unmanaged<CFError>? = nil
81+
try withUnsafeMutablePointer(&unmanagedError) { (unmanagedCFError: UnsafeMutablePointer<Unmanaged<CFError>?>) in
82+
CFBundlePreflightExecutable(_bundle, unmanagedCFError)
83+
if let error = unmanagedCFError.pointee {
84+
throw error.takeRetainedValue()._nsObject
85+
}
86+
}
87+
}
88+
89+
public func loadAndReturnError() throws {
90+
var unmanagedError:Unmanaged<CFError>? = nil
91+
try withUnsafeMutablePointer(&unmanagedError) { (unmanagedCFError: UnsafeMutablePointer<Unmanaged<CFError>?>) in
92+
CFBundleLoadExecutableAndReturnError(_bundle, unmanagedCFError)
93+
if let error = unmanagedCFError.pointee {
94+
let retainedValue = error.takeRetainedValue()
95+
throw retainedValue._nsObject
96+
}
97+
}
98+
}
99+
78100

79101
/* Methods for locating various components of a bundle. */
80102
public var bundleURL: URL {

TestFoundation/TestNSBundle.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class TestNSBundle : XCTestCase {
2929
("test_localizations", test_localizations),
3030
("test_URLsForResourcesWithExtension", test_URLsForResourcesWithExtension),
3131
("test_bundleLoad", test_bundleLoad),
32+
("test_bundleLoadWithError", test_bundleLoadWithError),
33+
("test_bundleWithInvalidPath", test_bundleWithInvalidPath),
34+
("test_bundlePreflight", test_bundlePreflight),
3235
]
3336
}
3437

@@ -153,10 +156,46 @@ class TestNSBundle : XCTestCase {
153156

154157
_cleanupPlayground(playground)
155158
}
159+
156160
func test_bundleLoad(){
157161
let bundle = Bundle.main()
158162
let _ = bundle.load()
159163
XCTAssertTrue(bundle.isLoaded)
160164
}
165+
166+
func test_bundleLoadWithError(){
167+
let bundleValid = Bundle.main()
168+
//test valid load using loadAndReturnError
169+
do{
170+
try bundleValid.loadAndReturnError()
171+
}catch{
172+
XCTFail("should not fail to load")
173+
}
174+
// executable cannot be located
175+
guard let playground = _setupPlayground() else { XCTFail("Unable to create playground"); return }
176+
let bundle = Bundle(path: playground + _bundleName)
177+
XCTAssertThrowsError(try bundle!.loadAndReturnError())
178+
_cleanupPlayground(playground)
179+
}
180+
181+
func test_bundleWithInvalidPath(){
182+
let bundleInvalid = Bundle(path: "/tmp/test.playground")
183+
XCTAssertNil(bundleInvalid)
184+
}
185+
186+
func test_bundlePreflight(){
187+
let bundleValid = Bundle.main()
188+
do{
189+
try bundleValid.preflight()
190+
}catch{
191+
XCTFail("should not fail to load")
192+
}
193+
// executable cannot be located ..preflight should report error
194+
guard let playground = _setupPlayground() else { XCTFail("Unable to create playground"); return }
195+
let bundle = Bundle(path: playground + _bundleName)
196+
XCTAssertThrowsError(try bundle!.preflight())
197+
_cleanupPlayground(playground)
198+
}
199+
161200

162201
}

0 commit comments

Comments
 (0)