Skip to content

Commit e2b433b

Browse files
committed
Add Bundle.wordPressData and update DataMigratorTests
1 parent 2eacc63 commit e2b433b

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Foundation
2+
3+
extension Bundle {
4+
public static var wordPressData: Bundle {
5+
Bundle(for: BundleToken.self)
6+
}
7+
}
8+
9+
private final class BundleToken {}

Sources/WordPressData/Swift/ContextManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public class ContextManager: NSObject, CoreDataStack, CoreDataStackSwift {
155155

156156
DDLogWarn("Migration required for persistent store.")
157157

158-
guard let modelFileURL = Bundle(for: ContextManager.self).url(forResource: "WordPress", withExtension: "momd") else {
158+
guard let modelFileURL = Bundle.wordPressData.url(forResource: "WordPress", withExtension: "momd") else {
159159
fatalError("Can't find WordPress.momd")
160160
}
161161

@@ -222,7 +222,7 @@ private extension ContextManager {
222222

223223
private extension ContextManager {
224224
static func createPersistentContainer(storeURL: URL, modelName: String) -> NSPersistentContainer {
225-
guard var modelFileURL = Bundle(for: ContextManager.self).url(forResource: "WordPress", withExtension: "momd") else {
225+
guard var modelFileURL = Bundle.wordPressData.url(forResource: "WordPress", withExtension: "momd") else {
226226
fatalError("Can't find WordPress.momd")
227227
}
228228

Sources/WordPressData/Swift/CoreDataHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public extension CoreDataStack {
358358
}
359359

360360
private func migrateDatabaseIfNecessary(at databaseLocation: URL) throws {
361-
guard let modelFileURL = Bundle(for: ContextManager.self).url(forResource: "WordPress", withExtension: "momd"),
361+
guard let modelFileURL = Bundle.wordPressData.url(forResource: "WordPress", withExtension: "momd"),
362362
let objectModel = NSManagedObjectModel(contentsOf: modelFileURL) else {
363363
return
364364
}

Sources/WordPressData/Swift/CoreDataIterativeMigrator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private extension CoreDataIterativeMigrator {
263263
}
264264

265265
static func model(for metadata: [String: Any]) throws -> NSManagedObjectModel? {
266-
let bundle = Bundle(for: ContextManager.self)
266+
let bundle = Bundle.wordPressData
267267
guard let sourceModel = NSManagedObjectModel.mergedModel(from: [bundle], forStoreMetadata: metadata) else {
268268
let description = "Failed to find source model for metadata: \(metadata)"
269269
throw error(with: .noSourceModelForMetadata, description: description)
@@ -287,7 +287,7 @@ private extension CoreDataIterativeMigrator {
287287
}
288288

289289
static func urlForModel(name: String, in directory: String?) -> URL? {
290-
let bundle = Bundle(for: ContextManager.self)
290+
let bundle = Bundle.wordPressData
291291
var url = bundle.url(forResource: name, withExtension: "mom", subdirectory: directory)
292292

293293
if url != nil {

Tests/KeystoneTests/Tests/CoreData/ContextManagerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ class ContextManagerTests: XCTestCase {
445445
}
446446

447447
fileprivate func urlForModelName(_ name: String) -> URL? {
448-
let bundle = Bundle(for: ContextManager.self)
448+
let bundle = Bundle.wordPressData
449449
var url = bundle.url(forResource: name, withExtension: "mom")
450450

451451
if url == nil {

Tests/KeystoneTests/Tests/Jetpack/DataMigratorTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private extension DataMigratorTests {
259259
static let defaultsWrapperKey = "defaults_staging_dictionary"
260260
}
261261

262-
func createContext(for model: NSManagedObjectModel = NSManagedObjectModel.mergedModel(from: [Bundle.main])!,
262+
func createContext(for model: NSManagedObjectModel = NSManagedObjectModel.mergedModel(from: [Bundle.wordPressData])!,
263263
type: String = NSInMemoryStoreType,
264264
at location: URL? = nil) throws -> NSManagedObjectContext {
265265
let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
@@ -288,7 +288,7 @@ private extension DataMigratorTests {
288288
}
289289

290290
func getModelNames() -> [String] {
291-
guard let modelFileURL = Bundle.main.url(forResource: "WordPress", withExtension: "momd"),
291+
guard let modelFileURL = Bundle.wordPressData.url(forResource: "WordPress", withExtension: "momd"),
292292
let versionInfo = NSDictionary(contentsOf: modelFileURL.appendingPathComponent("VersionInfo.plist")),
293293
let modelNames = (versionInfo["NSManagedObjectModel_VersionHashes"] as? [String: AnyObject])?.keys else {
294294
return []
@@ -305,13 +305,13 @@ private extension DataMigratorTests {
305305
}
306306

307307
func urlForModel(name: String) -> URL? {
308-
if let url = Bundle.main.url(forResource: name, withExtension: "mom") {
308+
if let url = Bundle.wordPressData.url(forResource: name, withExtension: "mom") {
309309
return url
310310
}
311311

312-
let momdPaths = Bundle.main.paths(forResourcesOfType: "momd", inDirectory: nil)
312+
let momdPaths = Bundle.wordPressData.paths(forResourcesOfType: "momd", inDirectory: nil)
313313
for path in momdPaths {
314-
if let url = Bundle.main.url(forResource: name, withExtension: "mom", subdirectory: URL(fileURLWithPath: path).lastPathComponent) {
314+
if let url = Bundle.wordPressData.url(forResource: name, withExtension: "mom", subdirectory: URL(fileURLWithPath: path).lastPathComponent) {
315315
return url
316316
}
317317
}

0 commit comments

Comments
 (0)