Skip to content

Commit

Permalink
fix: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nickybondarenko committed Apr 15, 2024
1 parent 2e68d46 commit 99903f3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
25 changes: 19 additions & 6 deletions Sources/Confidence/EventStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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 }
}
}

Expand All @@ -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
} ?? []
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/ConfidenceProvider/Cache/DefaultStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
40 changes: 32 additions & 8 deletions Tests/ConfidenceTests/EventStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])
Expand All @@ -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)
Expand Down

0 comments on commit 99903f3

Please sign in to comment.