From 99903f3cd1169b0e4cf9df99b8d7dba08216eb69 Mon Sep 17 00:00:00 2001 From: Nicky Bondarenko Date: Mon, 15 Apr 2024 15:55:38 +0200 Subject: [PATCH] fix: fix lint issues --- Sources/Confidence/EventStorage.swift | 25 +++++++++--- .../Cache/DefaultStorage.swift | 4 +- Tests/ConfidenceTests/EventStorageTests.swift | 40 +++++++++++++++---- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/Sources/Confidence/EventStorage.swift b/Sources/Confidence/EventStorage.swift index 5d241d75..c17e26cd 100644 --- a/Sources/Confidence/EventStorage.swift +++ b/Sources/Confidence/EventStorage.swift @@ -30,7 +30,10 @@ internal class EventStorageImpl: EventStorage { return } try currentFileHandle?.close() - try FileManager.default.moveItem(at: currentFileName, to: currentFileName.appendingPathExtension(READYTOSENDEXTENSION)) + try FileManager.default.moveItem( + at: currentFileName, + to: currentFileName.appendingPathExtension(READYTOSENDEXTENSION) + ) try resetCurrentFile() } } @@ -55,8 +58,13 @@ internal class EventStorageImpl: EventStorage { func batchReadyIds() throws -> [String] { try storageQueue.sync { - let fileUrls = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil) - return fileUrls.filter({ url in url.pathExtension == READYTOSENDEXTENSION }).map({ url in url.lastPathComponent }) + let fileUrls = try FileManager.default.contentsOfDirectory( + at: folderURL, + includingPropertiesForKeys: nil + ) + return fileUrls + .filter { url in url.pathExtension == READYTOSENDEXTENSION } + .map { url in url.lastPathComponent } } } @@ -66,9 +74,14 @@ internal class EventStorageImpl: EventStorage { let fileUrl = folderURL.appendingPathComponent(id) let data = try Data(contentsOf: fileUrl) let dataString = String(data: data, encoding: .utf8) - return try dataString?.components(separatedBy: "\n") - .filter({ events in !events.isEmpty }) - .map({ eventString in try decoder.decode(Event.self, from: eventString.data(using: .utf8)!) }) ?? [] + return try dataString? + .components(separatedBy: "\n") + .filter { events in !events.isEmpty } + .map{ eventString in + // swiftlint:disable force_unwrapping + try decoder.decode(Event.self, from: eventString.data(using: .utf8)!) + // swiftlint:enable force_unwrapping + } ?? [] } } diff --git a/Sources/ConfidenceProvider/Cache/DefaultStorage.swift b/Sources/ConfidenceProvider/Cache/DefaultStorage.swift index 2482a41c..4afa5ada 100644 --- a/Sources/ConfidenceProvider/Cache/DefaultStorage.swift +++ b/Sources/ConfidenceProvider/Cache/DefaultStorage.swift @@ -84,7 +84,9 @@ public class DefaultStorage: Storage { func getConfigUrl() throws -> URL { guard - let applicationSupportUrl: URL = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask) + let applicationSupportUrl = FileManager + .default + .urls(for: .applicationSupportDirectory, in: .userDomainMask) .last else { throw ConfidenceError.cacheError(message: "Could not get URL for application directory") diff --git a/Tests/ConfidenceTests/EventStorageTests.swift b/Tests/ConfidenceTests/EventStorageTests.swift index 8a07a0e9..f4bacf25 100644 --- a/Tests/ConfidenceTests/EventStorageTests.swift +++ b/Tests/ConfidenceTests/EventStorageTests.swift @@ -5,16 +5,24 @@ import XCTest class EventStorageTest: XCTestCase { override func setUp() async throws { - let folderURL = try! EventStorageImpl.getFolderURL() + let folderURL = try EventStorageImpl.getFolderURL() if FileManager.default.fileExists(atPath: folderURL.path) { - try! FileManager.default.removeItem(at: folderURL) + try FileManager.default.removeItem(at: folderURL) } } func testCreateNewBatch() throws { let eventStorage = try EventStorageImpl() - try eventStorage.writeEvent(event: Event(name: "some event", payload: ["pants": ConfidenceValue(string: "green")], eventTime: Date().self)) - try eventStorage.writeEvent(event: Event(name: "some event 2", payload: ["pants": ConfidenceValue(string: "red")], eventTime: Date().self)) + try eventStorage.writeEvent(event: Event( + name: "some event", + payload: ["pants": ConfidenceValue(string: "green")], + eventTime: Date().self + )) + try eventStorage.writeEvent(event: Event( + name: "some event 2", + payload: ["pants": ConfidenceValue(string: "red")], + eventTime: Date().self + )) try eventStorage.startNewBatch() try XCTAssertEqual(eventStorage.batchReadyIds().count, 1) let events = try eventStorage.eventsFrom(id: try eventStorage.batchReadyIds()[0]) @@ -24,10 +32,18 @@ class EventStorageTest: XCTestCase { func testContinueWritingToOldBatch() throws { let eventStorage = try EventStorageImpl() - try eventStorage.writeEvent(event: Event(name: "some event", payload: ["pants": ConfidenceValue(string: "green")], eventTime: Date().self)) + try eventStorage.writeEvent(event: Event( + name: "some event", + payload: ["pants": ConfidenceValue(string: "green")], + eventTime: Date().self + )) // user stops using app, new session after this let eventStorageNew = try EventStorageImpl() - try eventStorageNew.writeEvent(event: Event(name: "some event 2", payload: ["pants": ConfidenceValue(string: "red")], eventTime: Date().self)) + try eventStorageNew.writeEvent(event: Event( + name: "some event 2", + payload: ["pants": ConfidenceValue(string: "red")], + eventTime: Date().self + )) try eventStorageNew.startNewBatch() try XCTAssertEqual(eventStorageNew.batchReadyIds().count, 1) let events = try eventStorageNew.eventsFrom(id: try eventStorageNew.batchReadyIds()[0]) @@ -37,8 +53,16 @@ class EventStorageTest: XCTestCase { func testRemoveFile() throws { let eventStorage = try EventStorageImpl() - try eventStorage.writeEvent(event: Event(name: "some event", payload: ["pants": ConfidenceValue(string: "green")], eventTime: Date().self)) - try eventStorage.writeEvent(event: Event(name: "some event 2", payload: ["pants": ConfidenceValue(string: "red")], eventTime: Date().self)) + try eventStorage.writeEvent(event: Event( + name: "some event", + payload: ["pants": ConfidenceValue(string: "green")], + eventTime: Date().self + )) + try eventStorage.writeEvent(event: Event( + name: "some event 2", + payload: ["pants": ConfidenceValue(string: "red")], + eventTime: Date().self + )) try eventStorage.startNewBatch() try eventStorage.remove(id: eventStorage.batchReadyIds()[0]) try XCTAssertEqual(eventStorage.batchReadyIds().count, 0)