Skip to content

Commit

Permalink
Save full logs on Documents directory
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardocamaratta committed Sep 3, 2024
1 parent 48a457b commit 6700167
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
23 changes: 17 additions & 6 deletions Sources/Netbob/Core/LogFileProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import UIKit

protocol LogFileProviderProtocol {
func createFullLog() throws -> URL
func saveFullLog() throws
func createSingleLog(from connection: HTTPConnection, includeBody: Bool) throws -> URL
}

Expand All @@ -27,16 +28,17 @@ class LogFileProvider: LogFileProviderProtocol {
}

func createFullLog() throws -> URL {
let fullLog = .logHeader +
httpConnectionRepository
.current
.map { $0.toString(includeBody: true) }
.joined(separator: "\n\n\n\(String(repeating: "-", count: 30))\n\n\n")
let logFileUrl = fileManager.temporaryDirectory.appendingPathComponent("session.log")
try writeAction(fullLog, logFileUrl)
try writeAction(fullLogs, logFileUrl)
return logFileUrl
}

func saveFullLog() throws {
guard let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let logFileUrl = documentsUrl.appendingPathComponent("network-logs.txt")
try writeAction(fullLogs, logFileUrl)
}

func createSingleLog(from connection: HTTPConnection, includeBody: Bool) throws -> URL {
let string = .logHeader +
connection.toString(includeBody: includeBody)
Expand All @@ -45,6 +47,15 @@ class LogFileProvider: LogFileProviderProtocol {
try writeAction(string, logFileUrl)
return logFileUrl
}

// MARK: - Private

private var fullLogs: String {
.logHeader + httpConnectionRepository
.current
.map { $0.toString(includeBody: true) }
.joined(separator: "\n\n\n\(String(repeating: "-", count: 30))\n\n\n")
}
}

private func defaultWriteAction(string: String, url: URL) throws {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Netbob/Modules/List/ListViewState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class ListViewState: ListViewStateAbstract {

override func handleSaveAction() {
do {
_ = try logFileProvider.createFullLog()
try logFileProvider.saveFullLog()
} catch {
Netbob.log(String(describing: error))
}
Expand Down
14 changes: 12 additions & 2 deletions Tests/NetbobTests/Core/LogFileProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LogFileProviderTests: XCTestCase {
)
}

func test_singleLog() throws {
func test_createSingleLog() throws {
let url = try sut.createSingleLog(from: .fake(), includeBody: true)

XCTAssertEqual(mockFileManager.calls, [])
Expand All @@ -37,12 +37,22 @@ class LogFileProviderTests: XCTestCase {
XCTAssertEqual(writeActionURLs, ["tmp/single-connection.log"])
}

func test_fullLog() throws {
func test_createFullLog() throws {
let url = try sut.createFullLog()

XCTAssertEqual(mockFileManager.calls, [])
XCTAssertEqual(mockConnectionRepository.calls, [])
XCTAssertEqual(url.absoluteString, "tmp/session.log")
XCTAssertEqual(writeActionURLs, ["tmp/session.log"])
}

func test_saveFullLog() throws {
mockFileManager.urlsReturnValue = [URL(string: "Documents")!]

try sut.saveFullLog()

XCTAssertEqual(mockFileManager.calls, [.urls])
XCTAssertEqual(mockConnectionRepository.calls, [])
XCTAssertEqual(writeActionURLs, ["Documents/network-logs.txt"])
}
}
5 changes: 5 additions & 0 deletions Tests/NetbobTests/Mocks/LogFileProviderMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Foundation
class LogFileProviderMock: LogFileProviderProtocol {
enum Calls: Equatable {
case createFullLog
case saveFullLog
case createSingleLog(connection: HTTPConnection, includeBody: Bool)
}

Expand All @@ -19,6 +20,10 @@ class LogFileProviderMock: LogFileProviderProtocol {
return createFullLogReturnValue
}

func saveFullLog() throws {
calls.append(.saveFullLog)
}

var createSingleLogReturnValue = URL(string: "http://fake.fake")!
func createSingleLog(from connection: HTTPConnection, includeBody: Bool) throws -> URL {
calls.append(.createSingleLog(connection: connection, includeBody: includeBody))
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetbobTests/Modules/List/ListViewStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ListViewStateTests: XCTestCase {
func test_handleSaveAction() {
sut.handleSaveAction()

XCTAssertEqual(mockLogFileProvider.calls, [.createFullLog])
XCTAssertEqual(mockLogFileProvider.calls, [.saveFullLog])
}

func test_createFullLog() {
Expand Down

0 comments on commit 6700167

Please sign in to comment.