FIRCLSMockFileManager.m 3.0 KB

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