Skip to content

Commit

Permalink
Provide the path to the cassette directly
Browse files Browse the repository at this point in the history
… and remove now-superfluous cassette names.
  • Loading branch information
cysp committed Apr 19, 2020
1 parent 64c864f commit 6d38b09
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
9 changes: 1 addition & 8 deletions Sources/DVR/Cassette.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
41 changes: 13 additions & 28 deletions Sources/DVR/Session.swift
Original file line number Diff line number Diff line change
@@ -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]()
Expand All @@ -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()
}
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 6d38b09

Please sign in to comment.