Skip to content
Draft
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
4 changes: 1 addition & 3 deletions Sources/SPTPersistentCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ - (instancetype)initWithOptions:(SPTPersistentCacheOptions *)options
queue:_workQueue];


if (![_dataCacheFileManager createCacheDirectory]) {
return nil;
}
[_dataCacheFileManager createCacheDirectory];
}
return self;
}
Expand Down
17 changes: 7 additions & 10 deletions Tests/SPTPersistentCacheTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1258,22 +1258,19 @@ - (void)testSerialStoreWithLockDoesntIncrementRefCount
XCTAssertEqual(header.refCount, 0u, @"refCount must match");
}

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

Method originalMethod = class_getClassMethod(NSFileManager.class, @selector(defaultManager));
IMP originalMethodImplementation = method_getImplementation(originalMethod);
IMP fakeMethodImplementation = imp_implementationWithBlock(^ {
return nil;
});
method_setImplementation(originalMethod, fakeMethodImplementation);
BOOL didCreateFileThatWillPreventCacheDirectoryCreation = [[NSFileManager defaultManager] createFileAtPath:filePathBlockingCacheDirectoryCreation contents:nil attributes:nil];
XCTAssertTrue(didCreateFileThatWillPreventCacheDirectoryCreation);
SPTPersistentCache *cache = [[SPTPersistentCache alloc] initWithOptions:options];
method_setImplementation(originalMethod, originalMethodImplementation);
[[NSFileManager defaultManager] removeItemAtPath:filePathBlockingCacheDirectoryCreation error:nil];

XCTAssertNil(cache, @"The cache should be nil if it could not create the directory");
XCTAssertNotNil(cache, @"The cache should initialise even if it could not create the directory");
}

- (void)testFailToLoadDataWhenCallbackAbsent
Expand Down