FIRCLSMockFileManager.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockFileManager.h"
  15. @interface FIRCLSMockFileManager ()
  16. @property(nonatomic) NSMutableDictionary<NSString *, NSData *> *fileSystemDict;
  17. @end
  18. @implementation FIRCLSMockFileManager
  19. - (instancetype)init {
  20. self = [super init];
  21. if (!self) {
  22. return nil;
  23. }
  24. _fileSystemDict = [[NSMutableDictionary<NSString *, NSData *> alloc] init];
  25. return self;
  26. }
  27. - (BOOL)removeItemAtPath:(NSString *)path {
  28. [self.fileSystemDict removeObjectForKey:path];
  29. self.removeCount += 1;
  30. // If we set up the expectation, and we went over the expected count or removes, fulfill the
  31. // expectation
  32. if (self.removeExpectation && self.removeCount >= self.expectedRemoveCount) {
  33. [self.removeExpectation fulfill];
  34. }
  35. return YES;
  36. }
  37. - (BOOL)fileExistsAtPath:(NSString *)path {
  38. return self.fileSystemDict[path] != nil;
  39. }
  40. - (BOOL)createFileAtPath:(NSString *)path
  41. contents:(NSData *)data
  42. attributes:(NSDictionary<NSFileAttributeKey, id> *)attr {
  43. self.fileSystemDict[path] = data;
  44. return YES;
  45. }
  46. - (NSData *)dataWithContentsOfFile:(NSString *)path {
  47. return self.fileSystemDict[path];
  48. }
  49. - (void)enumerateFilesInDirectory:(NSString *)directory
  50. usingBlock:(void (^)(NSString *filePath, NSString *extension))block {
  51. NSArray<NSString *> *filteredPaths = [self.fileSystemDict.allKeys
  52. filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *path,
  53. NSDictionary *bindings) {
  54. return [path hasPrefix:directory];
  55. }]];
  56. for (NSString *path in filteredPaths) {
  57. NSString *extension;
  58. NSString *fullPath;
  59. // Skip files that start with a dot. This is important, because if you try to move a .DS_Store
  60. // file, it will fail if the target directory also has a .DS_Store file in it. Plus, its
  61. // wasteful, because we don't care about dot files.
  62. if ([path hasPrefix:@"."]) {
  63. continue;
  64. }
  65. extension = [path pathExtension];
  66. fullPath = [directory stringByAppendingPathComponent:path];
  67. if (block) {
  68. block(fullPath, extension);
  69. }
  70. }
  71. }
  72. @end