Closed orta closed 8 years ago
Idea: It could setup a fake version of the user support dir etc in /tmp
Real FS access is slow, though, and as this is about forging the NSFileManager API being used, and not necessarily all other FS APIs, I think it can all be done in memory, akin to https://github.com/defunkt/fakefs.
E.g.
describe(@"NSFileManager", ^{
__block NSFileManager *fileManager = nil;
before(@"", ^{
fileManager = [ForgeriesFileManager new];
[fileManager createDirectoryAtPath:@"/what/a/beautiful/directory" withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:@"/what/a/beautiful/directory/file" contents:@"zomg" attributes:nil];
});
it(@"reads from the in-memory FS", ^{
expect([fileManager contentsAtPath:@"/what/a/beautiful/directory/file"]).to.equal(@"zomg");
});
});
For clarity sake I wrote this as if the APIs take/return strings, where in fact they work with data.
I have to add, having written this example, I think it would be quite an undertaking, and so
I have a small use case right now ( https://github.com/artsy/Aerodramus/pull/1 ) - I can start with it in-memory and see how it goes from there if/when we start integrating it into ours apps.
👍
This is hard to cover a lot of use cases, a key API for file writing functions exists on NSData:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically; // the atomically flag is ignored if the url is not of a type the supports atomic writes
- (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)writeOptionsMask error:(NSError **)errorPtr;
- (BOOL)writeToURL:(NSURL *)url options:(NSDataWritingOptions)writeOptionsMask error:(NSError **)errorPtr;
Yeah, that’s the thing, you have to then do all the FS access through that API. Although I don’t think that it’s that much of a problem to use -[NSFileManager createFileAtPath:contents:attributes:]
if you’re already DI'ing a NSFileManager, no?
If the point is to stub any and all FS access, than this ticket is not related to NSFileManager per se.
It's possible however createFileAtPath:contents:attributes
has different behaviour ( won't overwrite unlike the NSData ones )
Highly unlikely, the NSFileManager API will be lower-level than such convenience methods.
I think we could do something around the file manager, it's one of the other big non-DI'd singleton in the Foundation space. Eigen could definitely do with this, there's some ugly thing going on in ARFileUtilTests.