snapshot/lib/assets/SnapshotHelper.swift in fastlane-2.82.0.beta.20180219010003 vs snapshot/lib/assets/SnapshotHelper.swift in fastlane-2.82.0.beta.20180220010002

- old
+ new

@@ -41,10 +41,11 @@ enum SnapshotError: Error, CustomDebugStringConvertible { case cannotDetectUser case cannotFindHomeDirectory case cannotFindSimulatorHomeDirectory case cannotAccessSimulatorHomeDirectory(String) + case cannotRunOnPhysicalDevice var debugDescription: String { switch self { case .cannotDetectUser: return "Couldn't find Snapshot configuration files - can't detect current user " @@ -52,36 +53,45 @@ return "Couldn't find Snapshot configuration files - can't detect `Users` dir" case .cannotFindSimulatorHomeDirectory: return "Couldn't find simulator home location. Please, check SIMULATOR_HOST_HOME env variable." case .cannotAccessSimulatorHomeDirectory(let simulatorHostHome): return "Can't prepare environment. Simulator home location is inaccessible. Does \(simulatorHostHome) exist?" + case .cannotRunOnPhysicalDevice: + return "Can't use Snapshot on a physical device." } } } @objcMembers open class Snapshot: NSObject { - static var app: XCUIApplication! - static var cacheDirectory: URL! + static var app: XCUIApplication? + static var cacheDirectory: URL? static var screenshotsDirectory: URL? { - return cacheDirectory.appendingPathComponent("screenshots", isDirectory: true) + return cacheDirectory?.appendingPathComponent("screenshots", isDirectory: true) } open class func setupSnapshot(_ app: XCUIApplication) { + + Snapshot.app = app + do { let cacheDir = try pathPrefix() Snapshot.cacheDirectory = cacheDir - Snapshot.app = app setLanguage(app) setLocale(app) setLaunchArguments(app) } catch let error { print(error) } } class func setLanguage(_ app: XCUIApplication) { + guard let cacheDirectory = self.cacheDirectory else { + print("CacheDirectory is not set - probably running on a physical device?") + return + } + let path = cacheDirectory.appendingPathComponent("language.txt") do { let trimCharacterSet = CharacterSet.whitespacesAndNewlines deviceLanguage = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet) @@ -90,10 +100,15 @@ print("Couldn't detect/set language...") } } class func setLocale(_ app: XCUIApplication) { + guard let cacheDirectory = self.cacheDirectory else { + print("CacheDirectory is not set - probably running on a physical device?") + return + } + let path = cacheDirectory.appendingPathComponent("locale.txt") do { let trimCharacterSet = CharacterSet.whitespacesAndNewlines locale = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet) @@ -105,10 +120,15 @@ } app.launchArguments += ["-AppleLocale", "\"\(locale)\""] } class func setLaunchArguments(_ app: XCUIApplication) { + guard let cacheDirectory = self.cacheDirectory else { + print("CacheDirectory is not set - probably running on a physical device?") + return + } + let path = cacheDirectory.appendingPathComponent("snapshot-launch_arguments.txt") app.launchArguments += ["-FASTLANE_SNAPSHOT", "YES", "-ui_testing"] do { let launchArguments = try String(contentsOf: path, encoding: String.Encoding.utf8) @@ -133,10 +153,16 @@ sleep(1) // Waiting for the animation to be finished (kind of) #if os(OSX) XCUIApplication().typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: []) #else + + guard let app = self.app else { + print("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().") + return + } + let screenshot = app.windows.firstMatch.screenshot() guard let simulator = ProcessInfo().environment["SIMULATOR_DEVICE_NAME"], let screenshotsDir = screenshotsDirectory else { return } let path = screenshotsDir.appendingPathComponent("\(simulator)-\(name).png") do { try screenshot.pngRepresentation.write(to: path) @@ -170,17 +196,21 @@ throw SnapshotError.cannotFindHomeDirectory } homeDir = usersDir.appendingPathComponent(user) #else - guard let simulatorHostHome = ProcessInfo().environment["SIMULATOR_HOST_HOME"] else { - throw SnapshotError.cannotFindSimulatorHomeDirectory - } - guard let homeDirUrl = URL(string: simulatorHostHome) else { - throw SnapshotError.cannotAccessSimulatorHomeDirectory(simulatorHostHome) - } - homeDir = URL(fileURLWithPath: homeDirUrl.path) + #if arch(i386) || arch(x86_64) + guard let simulatorHostHome = ProcessInfo().environment["SIMULATOR_HOST_HOME"] else { + throw SnapshotError.cannotFindSimulatorHomeDirectory + } + guard let homeDirUrl = URL(string: simulatorHostHome) else { + throw SnapshotError.cannotAccessSimulatorHomeDirectory(simulatorHostHome) + } + homeDir = URL(fileURLWithPath: homeDirUrl.path) + #else + throw SnapshotError.cannotRunOnPhysicalDevice + #endif #endif return homeDir.appendingPathComponent("Library/Caches/tools.fastlane") } } @@ -241,6 +271,6 @@ } } // Please don't remove the lines below // They are used to detect outdated configuration files -// SnapshotHelperVersion [1.8] +// SnapshotHelperVersion [1.9]