FIRCLSMockFileManager.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. dispatch_async(dispatch_get_main_queue(), ^{
  34. [self.removeExpectation fulfill];
  35. });
  36. }
  37. return YES;
  38. }
  39. - (BOOL)fileExistsAtPath:(NSString *)path {
  40. return self.fileSystemDict[path] != nil;
  41. }
  42. - (BOOL)createFileAtPath:(NSString *)path
  43. contents:(NSData *)data
  44. attributes:(NSDictionary<NSFileAttributeKey, id> *)attr {
  45. self.fileSystemDict[path] = data;
  46. return YES;
  47. }
  48. - (NSArray *)activePathContents {
  49. NSMutableArray *pathsWithActive = [[NSMutableArray alloc] init];
  50. for (NSString *path in [_fileSystemDict allKeys]) {
  51. if ([path containsString:@"v5/reports/active"]) {
  52. [pathsWithActive addObject:path];
  53. }
  54. }
  55. return pathsWithActive;
  56. }
  57. - (NSData *)dataWithContentsOfFile:(NSString *)path {
  58. return self.fileSystemDict[path];
  59. }
  60. - (void)enumerateFilesInDirectory:(NSString *)directory
  61. usingBlock:(void (^)(NSString *filePath, NSString *extension))block {
  62. NSArray<NSString *> *filteredPaths = [self.fileSystemDict.allKeys
  63. filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *path,
  64. NSDictionary *bindings) {
  65. return [path hasPrefix:directory];
  66. }]];
  67. for (NSString *path in filteredPaths) {
  68. NSString *extension;
  69. NSString *fullPath;
  70. // Skip files that start with a dot. This is important, because if you try to move a .DS_Store
  71. // file, it will fail if the target directory also has a .DS_Store file in it. Plus, its
  72. // wasteful, because we don't care about dot files.
  73. if ([path hasPrefix:@"."]) {
  74. continue;
  75. }
  76. extension = [path pathExtension];
  77. fullPath = [directory stringByAppendingPathComponent:path];
  78. if (block) {
  79. block(fullPath, extension);
  80. }
  81. }
  82. }
  83. @end