Skip to content

Commit a282915

Browse files
committed
Make persistent cache initializer non-failable
1 parent 85cd408 commit a282915

2 files changed

Lines changed: 8 additions & 13 deletions

File tree

Sources/SPTPersistentCache.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ - (instancetype)initWithOptions:(SPTPersistentCacheOptions *)options
9898
queue:_workQueue];
9999

100100

101-
if (![_dataCacheFileManager createCacheDirectory]) {
102-
return nil;
103-
}
101+
[_dataCacheFileManager createCacheDirectory];
104102
}
105103
return self;
106104
}

Tests/SPTPersistentCacheTests.m

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,22 +1258,19 @@ - (void)testSerialStoreWithLockDoesntIncrementRefCount
12581258
XCTAssertEqual(header.refCount, 0u, @"refCount must match");
12591259
}
12601260

1261-
- (void)testInitNilWhenCannotCreateCacheDirectory
1261+
- (void)testInitSucceedsWhenCannotCreateCacheDirectory
12621262
{
12631263
SPTPersistentCacheOptions *options = [SPTPersistentCacheOptions new];
1264-
options.cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"/com.spotify.temppersistent.image.cache"];
1264+
NSString *filePathBlockingCacheDirectoryCreation = [NSTemporaryDirectory() stringByAppendingPathComponent:@"/com.spotify.temppersistent.image.cache"];
1265+
options.cachePath = filePathBlockingCacheDirectoryCreation;
12651266
options.cacheIdentifier = @"test";
12661267

1267-
Method originalMethod = class_getClassMethod(NSFileManager.class, @selector(defaultManager));
1268-
IMP originalMethodImplementation = method_getImplementation(originalMethod);
1269-
IMP fakeMethodImplementation = imp_implementationWithBlock(^ {
1270-
return nil;
1271-
});
1272-
method_setImplementation(originalMethod, fakeMethodImplementation);
1268+
BOOL didCreateBlockingFile = [[NSFileManager defaultManager] createFileAtPath:filePathBlockingCacheDirectoryCreation contents:nil attributes:nil];
1269+
XCTAssertTrue(didCreateBlockingFile);
12731270
SPTPersistentCache *cache = [[SPTPersistentCache alloc] initWithOptions:options];
1274-
method_setImplementation(originalMethod, originalMethodImplementation);
1271+
[[NSFileManager defaultManager] removeItemAtPath:filePathBlockingCacheDirectoryCreation error:nil];
12751272

1276-
XCTAssertNil(cache, @"The cache should be nil if it could not create the directory");
1273+
XCTAssertNotNil(cache, @"The cache should initialise even if it could not create the directory");
12771274
}
12781275

12791276
- (void)testFailToLoadDataWhenCallbackAbsent

0 commit comments

Comments
 (0)