From 6d38b0970f7ff33c84b03fa19b4f98b52d53cfea Mon Sep 17 00:00:00 2001 From: Scott Talbot Date: Sun, 19 Apr 2020 14:53:26 +1000 Subject: [PATCH] Provide the path to the cassette directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and remove now-superfluous cassette names. --- Sources/DVR/Cassette.swift | 9 +-------- Sources/DVR/Session.swift | 41 ++++++++++++-------------------------- 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/Sources/DVR/Cassette.swift b/Sources/DVR/Cassette.swift index b3e8fb9..0a0e379 100644 --- a/Sources/DVR/Cassette.swift +++ b/Sources/DVR/Cassette.swift @@ -4,14 +4,12 @@ struct Cassette { // MARK: - Properties - let name: String let interactions: [Interaction] // MARK: - Initializers - init(name: String, interactions: [Interaction]) { - self.name = name + init(interactions: [Interaction]) { self.interactions = interactions } @@ -35,16 +33,11 @@ struct Cassette { extension Cassette { var dictionary: [String: Any] { return [ - "name": name as Any, "interactions": interactions.map { $0.dictionary } ] } init?(dictionary: [String: Any]) { - guard let name = dictionary["name"] as? String else { return nil } - - self.name = name - if let array = dictionary["interactions"] as? [[String: Any]] { interactions = array.compactMap { Interaction(dictionary: $0) } } else { diff --git a/Sources/DVR/Session.swift b/Sources/DVR/Session.swift index 69502f9..7811554 100644 --- a/Sources/DVR/Session.swift +++ b/Sources/DVR/Session.swift @@ -1,20 +1,12 @@ import Foundation open class Session: URLSession { - // MARK: - Properties - public static var defaultTestBundle: Bundle? { - return Bundle.allBundles.first { $0.bundlePath.hasSuffix(".xctest") } - } - - open var outputDirectory: String - public let cassetteName: String + public let cassetteURL: URL public let backingSession: URLSession open var recordingEnabled = true - private let testBundle: Bundle - private var recording = false private var needsPersistence = false private var outstandingTasks = [URLSessionTask]() @@ -35,10 +27,8 @@ open class Session: URLSession { // MARK: - Initializers - public init(outputDirectory: String = "~/Desktop/DVR/", cassetteName: String, testBundle: Bundle = Session.defaultTestBundle!, backingSession: URLSession = URLSession.shared) { - self.outputDirectory = outputDirectory - self.cassetteName = cassetteName - self.testBundle = testBundle + public init(cassetteURL: URL, backingSession: URLSession = URLSession.shared) { + self.cassetteURL = cassetteURL self.backingSession = backingSession super.init() } @@ -122,8 +112,7 @@ open class Session: URLSession { // MARK: - Internal var cassette: Cassette? { - guard let path = testBundle.path(forResource: cassetteName, ofType: "json"), - let data = try? Data(contentsOf: URL(fileURLWithPath: path)), + guard let data = try? Data(contentsOf: cassetteURL), let raw = try? JSONSerialization.jsonObject(with: data, options: []), let json = raw as? [String: Any] else { return nil } @@ -201,24 +190,21 @@ open class Session: URLSession { abort() } + let cassetteDirectory = cassetteURL.deletingLastPathComponent() + // Create directory - let outputDirectory = (self.outputDirectory as NSString).expandingTildeInPath - let fileManager = FileManager.default - if !fileManager.fileExists(atPath: outputDirectory) { - do { - try fileManager.createDirectory(atPath: outputDirectory, withIntermediateDirectories: true, attributes: nil) - } catch { - print("[DVR] Failed to create cassettes directory.") - } + do { + try FileManager.default.createDirectory(at: cassetteDirectory, withIntermediateDirectories: true, attributes: nil) + } catch { + print("[DVR] Failed to create cassettes directory.") } - let cassette = Cassette(name: cassetteName, interactions: interactions) + let cassette = Cassette(interactions: interactions) // Persist - do { - let outputPath = ((outputDirectory as NSString).appendingPathComponent(cassetteName) as NSString).appendingPathExtension("json")! + let data = try JSONSerialization.data(withJSONObject: cassette.dictionary, options: [.prettyPrinted]) // Add trailing new line @@ -229,8 +215,7 @@ open class Session: URLSession { string = string.appending("\n") as NSString if let data = string.data(using: String.Encoding.utf8.rawValue) { - try? data.write(to: URL(fileURLWithPath: outputPath), options: [.atomic]) - print("[DVR] Persisted cassette at \(outputPath). Please add this file to your test target") + try? data.write(to: cassetteURL, options: [.atomic]) return }