From 30dafc092d7e6b73abdd5c90c48bccf3db969567 Mon Sep 17 00:00:00 2001 From: Ilya Puchka Date: Wed, 31 Oct 2018 17:45:44 +0000 Subject: [PATCH] Add configuration to allow recording empty tracks (#88) * add configuration to allow recording empty tracks * always write into file without checking for recorded tracks * fixed tests * adjust expectation * do not enable recording when recording path is nil --- Vinyl/Recorder.swift | 15 ++----- Vinyl/Turntable.swift | 8 +--- Vinyl/TurntableConfiguration.swift | 13 +++--- VinylTests/TurntableTests.swift | 65 +++++++++++++++++++----------- 4 files changed, 55 insertions(+), 46 deletions(-) diff --git a/Vinyl/Recorder.swift b/Vinyl/Recorder.swift index a9ac4b1..7076e2a 100644 --- a/Vinyl/Recorder.swift +++ b/Vinyl/Recorder.swift @@ -11,8 +11,7 @@ import Foundation final class Recorder { var wax: Wax let recordingPath: String? - var somethingRecorded = false - + init(wax: Wax, recordingPath: String?) { self.wax = wax self.recordingPath = recordingPath @@ -22,7 +21,6 @@ final class Recorder { extension Recorder { func saveTrack(with request: Request, response: Response) { wax.add(track: Track(request: request, response: response)) - somethingRecorded = true } func saveTrack(with request: Request, urlResponse: HTTPURLResponse?, body: Data? = nil, error: Error? = nil) { @@ -34,14 +32,10 @@ extension Recorder { extension Recorder { func persist() throws { - guard let recordingPath = recordingPath, somethingRecorded else { - if somethingRecorded { - throw TurntableError.noRecordingPath - } else { - throw TurntableError.nothingToRecord - } + guard let recordingPath = recordingPath else { + throw TurntableError.noRecordingPath } - + let fileManager = FileManager.default guard fileManager.createFile(atPath: recordingPath, contents: nil, attributes: nil) == true, let file = FileHandle(forWritingAtPath: recordingPath) else { @@ -57,6 +51,5 @@ extension Recorder { file.synchronizeFile() print("Vinyl recorded to: \(recordingPath)") - somethingRecorded = false } } diff --git a/Vinyl/Turntable.swift b/Vinyl/Turntable.swift index e0e0c16..56b1bbf 100644 --- a/Vinyl/Turntable.swift +++ b/Vinyl/Turntable.swift @@ -12,7 +12,6 @@ enum TurntableError: Error { case trackNotFound case noRecordingPath - case nothingToRecord } public typealias Plastic = [[String: Any]] @@ -39,9 +38,9 @@ public final class Turntable: URLSession { if configuration.recodingEnabled { recorder = Recorder(wax: Wax(tracks: []), recordingPath: configuration.recordingPath) - recordingSession = urlSession ?? URLSession.shared } - + recordingSession = urlSession ?? URLSession.shared + super.init() } @@ -81,9 +80,6 @@ public final class Turntable: URLSession { do { try recorder.persist() } - catch TurntableError.nothingToRecord { - print("Nothing to record.") - } catch TurntableError.noRecordingPath { fatalError("💣 no path was configured for saving the recording.") } diff --git a/Vinyl/TurntableConfiguration.swift b/Vinyl/TurntableConfiguration.swift index 2390281..4c06d9c 100644 --- a/Vinyl/TurntableConfiguration.swift +++ b/Vinyl/TurntableConfiguration.swift @@ -23,7 +23,7 @@ public struct TurntableConfiguration { public let matchingStrategy: MatchingStrategy public let recordingMode: RecordingMode - + var playTracksUniquely: Bool { get { switch matchingStrategy { @@ -36,10 +36,10 @@ public struct TurntableConfiguration { var recodingEnabled: Bool { get { switch recordingMode { - case .none: - return false + case let .missingTracks(recordingPath), let .missingVinyl(recordingPath): + return recordingPath != nil default: - return true + return false } } } @@ -57,7 +57,10 @@ public struct TurntableConfiguration { } } - public init(matchingStrategy: MatchingStrategy = .requestAttributes(types: [.method, .url], playTracksUniquely: true), recordingMode: RecordingMode = .missingVinyl(recordingPath: nil)) { + public init( + matchingStrategy: MatchingStrategy = .requestAttributes(types: [.method, .url], playTracksUniquely: true), + recordingMode: RecordingMode = .missingVinyl(recordingPath: nil) + ) { self.matchingStrategy = matchingStrategy self.recordingMode = recordingMode } diff --git a/VinylTests/TurntableTests.swift b/VinylTests/TurntableTests.swift index 38cf900..64576ab 100644 --- a/VinylTests/TurntableTests.swift +++ b/VinylTests/TurntableTests.swift @@ -238,11 +238,11 @@ class TurntableTests: XCTestCase { XCTAssertEqual(httpResponse.url!.absoluteString, urlString) XCTAssertEqual(httpResponse.statusCode, 200) - XCTAssertTrue(data! == body) + XCTAssertTrue(data == body) XCTAssertNotNil(httpResponse.allHeaderFields) expectation.fulfill() - }) .resume() + }).resume() } fileprivate func multipleCallTest(_ turntable: Turntable) { @@ -358,7 +358,7 @@ class TurntableTests: XCTestCase { XCTAssertFalse(Thread.isMainThread) expectation.fulfill() - }) .resume() + }).resume() } func test_Vinyl_mainQueue() { @@ -374,7 +374,7 @@ class TurntableTests: XCTestCase { XCTAssertTrue(Thread.isMainThread) expectation.fulfill() - }) .resume() + }).resume() } func test_Vinyl_Delegate() { @@ -382,51 +382,51 @@ class TurntableTests: XCTestCase { let turntable = Turntable(vinylName: "vinyl_single", delegateQueue: OperationQueue.main) XCTAssertNil(turntable.delegate) } - + func test_Vinyl_recording_missingVinyl_vinylMissing() { let expectation = self.expectation(description: "Expected callback to be called on background thread") defer { self.waitForExpectations(timeout: 4, handler: nil) } - + let recordingVinylName = "vinyl_recording" let path = prepPathForRecording(recordingVinylName) - + let dogFood = Turntable(vinylName: "vinyl_single") let turntable = Turntable(vinylName: recordingVinylName, turntableConfiguration: TurntableConfiguration(recordingMode: .missingVinyl(recordingPath: nil)), urlSession: dogFood) - + let urlString = "http://api.test.com" - + turntable.dataTask(with: URL(string: urlString)!, completionHandler: { (data, response, anError) in turntable.stopRecording() - + XCTAssertTrue(FileManager.default.fileExists(atPath: path)) - + expectation.fulfill() - }) .resume() + }).resume() } func test_Vinyl_recording_missingVinyl_vinylPresent() { let expectation = self.expectation(description: "Expected callback to be called on background thread") defer { self.waitForExpectations(timeout: 4, handler: nil) } - + let recordingVinylName = "vinyl_recording" let path = prepPathForRecording(recordingVinylName) - + let dogFood = Turntable(vinylName: "vinyl_single") let turntable = Turntable(vinylName: "vinyl_single", turntableConfiguration: TurntableConfiguration(recordingMode: .missingVinyl(recordingPath: nil)), urlSession: dogFood) - + let urlString = "http://api.test.com" - + turntable.dataTask(with: URL(string: urlString)!, completionHandler: { (data, response, anError) in turntable.stopRecording() - + XCTAssertFalse(FileManager.default.fileExists(atPath: path)) - + expectation.fulfill() - }) .resume() + }).resume() } func test_Vinyl_recording_missingTracks_missingTrack() { @@ -449,7 +449,7 @@ class TurntableTests: XCTestCase { XCTAssertTrue(FileManager.default.fileExists(atPath: path)) expectation.fulfill() - }) .resume() + }).resume() } func test_Vinyl_recording_missingTracks_existingTrack() { @@ -469,10 +469,24 @@ class TurntableTests: XCTestCase { turntable.dataTask(with: URL(string: urlString)!, completionHandler: { (data, response, anError) in turntable.stopRecording() - XCTAssertFalse(FileManager.default.fileExists(atPath: path)) + XCTAssertTrue(FileManager.default.fileExists(atPath: path)) expectation.fulfill() - }) .resume() + }).resume() + } + + func test_Vynil_recording_empty() { + let recordingVinylName = "vinyl_recording" + let path = prepPathForRecording(recordingVinylName) + + let dogFood = Turntable(vinylName: "vinyl_single") + let turntable = Turntable(vinylName: "vinyl_single", + turntableConfiguration: TurntableConfiguration(recordingMode: .missingTracks(recordingPath: path)), + urlSession: dogFood) + + turntable.stopRecording() + + XCTAssertTrue(FileManager.default.fileExists(atPath: path)) } func test_default_URLSession_configuration() { @@ -487,12 +501,15 @@ class TurntableTests: XCTestCase { let configuration = URLSessionConfiguration.default configuration.httpAdditionalHeaders = ["X-UniqueHeader": expectedHeaderValue] + let recordingVinylName = "vinyl_recording" + let path = prepPathForRecording(recordingVinylName) + let urlSession = URLSession(configuration: configuration) let turntable = Turntable(vinylName: "vinyl_single", - turntableConfiguration: TurntableConfiguration(recordingMode: .missingTracks(recordingPath: nil)), + turntableConfiguration: TurntableConfiguration(recordingMode: .missingTracks(recordingPath: path)), urlSession: urlSession) - let headerValue = turntable.configuration.httpAdditionalHeaders!["X-UniqueHeader"] as! String + let headerValue = turntable.configuration.httpAdditionalHeaders?["X-UniqueHeader"] as? String // Can't compare `configuration` instances since they return copies XCTAssertEqual(headerValue, expectedHeaderValue)