Support/HockeySDKTests/BITTestHelper.m
#import "BITTestHelper.h"
#import "HockeySDKPrivate.h"

@implementation BITTestHelper

// loads test fixture from json file
// http://blog.roberthoglund.com/2010/12/ios-unit-testing-loading-bundle.html
+ (NSString *)jsonFixture:(NSString *)fixture {
	NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:fixture ofType:@"json"];

	NSError *error = nil;
  NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

  return content;
}

+ (BOOL)createTempDirectory:(NSString *)directory {
  NSFileManager *fm = [[NSFileManager alloc] init];
  
  if (![fm fileExistsAtPath:directory]) {
    NSDictionary *attributes = [NSDictionary dictionaryWithObject: [NSNumber numberWithUnsignedLong: 0755] forKey: NSFilePosixPermissions];
    NSError *theError = NULL;
    
    [fm createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:attributes error:&theError];
    if (theError)
      return NO;
  }
  
  return YES;
}

+ (BOOL)copyFixtureCrashReportWithFileName:(NSString *)filename {
  NSFileManager *fm = [[NSFileManager alloc] init];

  NSString *bundleIdentifierPathString = [[NSBundle mainBundle] bundleIdentifier];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  
  // create the PLCR cache dir
  NSString *plcrRootCrashesDir = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"com.plausiblelabs.crashreporter.data"];
  if (![BITTestHelper createTempDirectory:plcrRootCrashesDir])
    return NO;
  
  NSString *plcrCrashesDir = [plcrRootCrashesDir stringByAppendingPathComponent:bundleIdentifierPathString];
  if (![BITTestHelper createTempDirectory:plcrCrashesDir])
    return NO;
  
	NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:filename ofType:@"plcrash"];
  NSError *theError = NULL;
  
  if (!filePath) return NO;
  NSString *destFile = [plcrCrashesDir stringByAppendingPathComponent:@"live_report.plcrash"];
  if ([[NSFileManager defaultManager] isDeletableFileAtPath:destFile]) {
    [[NSFileManager defaultManager] removeItemAtPath:destFile error:nil];
  }
  [fm copyItemAtPath:filePath toPath:destFile error:&theError];
  
  if (theError)
    return NO;
  else
    return YES;
}

+ (NSData *)dataOfFixtureCrashReportWithFileName:(NSString *)filename {
  // the bundle identifier when running with unit tets is "otest"
  const char *progname = getprogname();
  if (progname == NULL) {
    return nil;
  }

  NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:filename ofType:@"plcrash"];
  
  if (!filePath) return nil;
  NSData *data = [NSData dataWithContentsOfFile:filePath];
  return data;
}

@end
