FIRFixtureLoader.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2021 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 "FirebaseAppCheck/Tests/Unit/Utils/FIRFixtureLoader.h"
  15. @implementation FIRFixtureLoader
  16. + (NSData *)loadFixtureNamed:(NSString *)fileName {
  17. NSURL *fileURL;
  18. __auto_type possibleResourceBundles = [self possibleResourceBundles];
  19. for (NSBundle *bundle in possibleResourceBundles) {
  20. fileURL = [bundle URLForResource:fileName withExtension:nil];
  21. if (fileURL != nil) {
  22. NSLog(@"Fixture named: %@ was found at bundle %@", fileName, [bundle bundleIdentifier]);
  23. break;
  24. }
  25. }
  26. NSError *error;
  27. NSData *data = [NSData dataWithContentsOfURL:fileURL options:0 error:&error];
  28. return data;
  29. }
  30. + (NSArray<NSBundle *> *)possibleResourceBundles {
  31. NSBundle *bundleForClass = [NSBundle bundleForClass:[self class]];
  32. // Swift Package Manager packages resources into separate bundles inside the test bundle.
  33. NSArray<NSURL *> *enclosedBundleURLs = [bundleForClass URLsForResourcesWithExtension:@"bundle"
  34. subdirectory:nil];
  35. NSMutableArray<NSBundle *> *bundlesForResources = [@[ bundleForClass ] mutableCopy];
  36. for (NSURL *bundleURL in enclosedBundleURLs) {
  37. NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
  38. if (bundle) {
  39. [bundlesForResources addObject:bundle];
  40. }
  41. }
  42. return [bundlesForResources copy];
  43. }
  44. @end