From 998fb7a93a3d4db5e2d6078c36b09bcc0319967d Mon Sep 17 00:00:00 2001 From: Brad Slayter Date: Tue, 3 Sep 2024 09:10:54 -0500 Subject: [PATCH] More test fixing --- DuckDuckGo/Application/AppDelegate.swift | 2 +- .../Configuration/ConfigurationManager.swift | 18 +++++++++--------- .../Configuration/ConfigurationStore.swift | 1 - .../ContentBlocker/ContentBlocking.swift | 6 +++--- .../Mocks/ContentBlockingMock.swift | 4 ++-- .../ContentBlocker/ScriptSourceProviding.swift | 2 +- .../RemoteMessagingClient.swift | 2 +- .../AutoconsentMessageProtocolTests.swift | 4 ++-- .../ConfigurationStorageTests.swift | 12 +++++++----- .../ContentBlockingUpdatingTests.swift | 9 +++++---- 10 files changed, 31 insertions(+), 29 deletions(-) diff --git a/DuckDuckGo/Application/AppDelegate.swift b/DuckDuckGo/Application/AppDelegate.swift index e571cbefb2..c3349d0d83 100644 --- a/DuckDuckGo/Application/AppDelegate.swift +++ b/DuckDuckGo/Application/AppDelegate.swift @@ -234,7 +234,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate { appearancePreferences: .shared, pinnedTabsManager: pinnedTabsManager, internalUserDecider: internalUserDecider, - configurationStore: ConfigurationStore.shared, + configurationStore: ConfigurationStore(), remoteMessagingAvailabilityProvider: PrivacyConfigurationRemoteMessagingAvailabilityProvider( privacyConfigurationManager: ContentBlocking.shared.privacyConfigurationManager ) diff --git a/DuckDuckGo/Configuration/ConfigurationManager.swift b/DuckDuckGo/Configuration/ConfigurationManager.swift index 05e8296232..93b7ab5807 100644 --- a/DuckDuckGo/Configuration/ConfigurationManager.swift +++ b/DuckDuckGo/Configuration/ConfigurationManager.swift @@ -98,7 +98,7 @@ final class ConfigurationManager: DefaultConfigurationManager { await updateBloomFilterTask.value await updateBloomFilterExclusionsTask.value - ConfigurationStore.shared.log() + (store as? ConfigurationStore)?.log() Logger.config.info("last update \(String(describing: self.lastUpdateTime), privacy: .public)") Logger.config.info("last refresh check \(String(describing: self.lastRefreshCheckTime), privacy: .public)") @@ -141,18 +141,18 @@ final class ConfigurationManager: DefaultConfigurationManager { private func updateTrackerBlockingDependencies() { lastConfigurationInstallDate = Date() - ContentBlocking.shared.trackerDataManager.reload(etag: ConfigurationStore.shared.loadEtag(for: .trackerDataSet), - data: ConfigurationStore.shared.loadData(for: .trackerDataSet)) - ContentBlocking.shared.privacyConfigurationManager.reload(etag: ConfigurationStore.shared.loadEtag(for: .privacyConfiguration), - data: ConfigurationStore.shared.loadData(for: .privacyConfiguration)) + ContentBlocking.shared.trackerDataManager.reload(etag: store.loadEtag(for: .trackerDataSet), + data: store.loadData(for: .trackerDataSet)) + ContentBlocking.shared.privacyConfigurationManager.reload(etag: store.loadEtag(for: .privacyConfiguration), + data: store.loadData(for: .privacyConfiguration)) ContentBlocking.shared.contentBlockingManager.scheduleCompilation() } private func updateBloomFilter() async throws { - guard let specData = ConfigurationStore.shared.loadData(for: .bloomFilterSpec) else { + guard let specData = store.loadData(for: .bloomFilterSpec) else { throw Error.bloomFilterSpecNotFound } - guard let bloomFilterData = ConfigurationStore.shared.loadData(for: .bloomFilterBinary) else { + guard let bloomFilterData = store.loadData(for: .bloomFilterBinary) else { throw Error.bloomFilterBinaryNotFound } try await Task.detached { @@ -168,7 +168,7 @@ final class ConfigurationManager: DefaultConfigurationManager { } private func updateBloomFilterExclusions() async throws { - guard let bloomFilterExclusions = ConfigurationStore.shared.loadData(for: .bloomFilterExcludedDomains) else { + guard let bloomFilterExclusions = store.loadData(for: .bloomFilterExcludedDomains) else { throw Error.bloomFilterExclusionsNotFound } try await Task.detached { @@ -186,7 +186,7 @@ final class ConfigurationManager: DefaultConfigurationManager { extension ConfigurationManager { override var presentedItemURL: URL? { - ConfigurationStore.shared.fileUrl(for: .privacyConfiguration) + store.fileUrl(for: .privacyConfiguration) } override func presentedItemDidChange() { diff --git a/DuckDuckGo/Configuration/ConfigurationStore.swift b/DuckDuckGo/Configuration/ConfigurationStore.swift index e2a48a23d1..1d0c6bfe3b 100644 --- a/DuckDuckGo/Configuration/ConfigurationStore.swift +++ b/DuckDuckGo/Configuration/ConfigurationStore.swift @@ -48,7 +48,6 @@ final class ConfigurationStore: ConfigurationStoring { static let configStorageRemoteMessagingConfigEtag = "config.storage.remotemessagingconfig.etag" } - static let shared = ConfigurationStore() private let defaults: KeyValueStoring private var trackerRadarEtag: String? { diff --git a/DuckDuckGo/ContentBlocker/ContentBlocking.swift b/DuckDuckGo/ContentBlocker/ContentBlocking.swift index 973622888a..4413bfd2f3 100644 --- a/DuckDuckGo/ContentBlocker/ContentBlocking.swift +++ b/DuckDuckGo/ContentBlocker/ContentBlocking.swift @@ -60,7 +60,7 @@ final class AppContentBlocking { // keeping whole ContentBlocking state initialization in one place to avoid races between updates publishing and rules storing @MainActor init(internalUserDecider: InternalUserDecider) { - let configStorage = ConfigurationStore.shared + let configStorage = ConfigurationStore() privacyConfigurationManager = PrivacyConfigurationManager(fetchedETag: configStorage.loadEtag(for: .privacyConfiguration), fetchedData: configStorage.loadData(for: .privacyConfiguration), embeddedDataProvider: AppPrivacyConfigurationDataProvider(), @@ -68,8 +68,8 @@ final class AppContentBlocking { errorReporting: Self.debugEvents, internalUserDecider: internalUserDecider) - trackerDataManager = TrackerDataManager(etag: ConfigurationStore.shared.loadEtag(for: .trackerDataSet), - data: ConfigurationStore.shared.loadData(for: .trackerDataSet), + trackerDataManager = TrackerDataManager(etag: configStorage.loadEtag(for: .trackerDataSet), + data: configStorage.loadData(for: .trackerDataSet), embeddedDataProvider: AppTrackerDataSetProvider(), errorReporting: Self.debugEvents) diff --git a/DuckDuckGo/ContentBlocker/Mocks/ContentBlockingMock.swift b/DuckDuckGo/ContentBlocker/Mocks/ContentBlockingMock.swift index 6da3d7024c..fb9e5620d5 100644 --- a/DuckDuckGo/ContentBlocker/Mocks/ContentBlockingMock.swift +++ b/DuckDuckGo/ContentBlocker/Mocks/ContentBlockingMock.swift @@ -30,8 +30,8 @@ final class ContentBlockingMock: NSObject, ContentBlockingProtocol, AdClickAttri var embeddedDataEtag: String = "" var embeddedData: Data = .init() } - var trackerDataManager = TrackerDataManager(etag: ConfigurationStore.shared.loadEtag(for: .trackerDataSet), - data: ConfigurationStore.shared.loadData(for: .trackerDataSet), + var trackerDataManager = TrackerDataManager(etag: ConfigurationStore().loadEtag(for: .trackerDataSet), + data: ConfigurationStore().loadData(for: .trackerDataSet), embeddedDataProvider: AppTrackerDataSetProvider(), errorReporting: nil) diff --git a/DuckDuckGo/ContentBlocker/ScriptSourceProviding.swift b/DuckDuckGo/ContentBlocker/ScriptSourceProviding.swift index 8f76e1596c..66a65a040a 100644 --- a/DuckDuckGo/ContentBlocker/ScriptSourceProviding.swift +++ b/DuckDuckGo/ContentBlocker/ScriptSourceProviding.swift @@ -38,7 +38,7 @@ protocol ScriptSourceProviding { // refactor: ScriptSourceProvider to be passed to init methods as `some ScriptSourceProviding`, DefaultScriptSourceProvider to be killed // swiftlint:disable:next identifier_name @MainActor func DefaultScriptSourceProvider() -> ScriptSourceProviding { - ScriptSourceProvider(configStorage: ConfigurationStore.shared, privacyConfigurationManager: ContentBlocking.shared.privacyConfigurationManager, webTrackingProtectionPreferences: WebTrackingProtectionPreferences.shared, contentBlockingManager: ContentBlocking.shared.contentBlockingManager, trackerDataManager: ContentBlocking.shared.trackerDataManager, tld: ContentBlocking.shared.tld) + ScriptSourceProvider(configStorage: ConfigurationStore(), privacyConfigurationManager: ContentBlocking.shared.privacyConfigurationManager, webTrackingProtectionPreferences: WebTrackingProtectionPreferences.shared, contentBlockingManager: ContentBlocking.shared.contentBlockingManager, trackerDataManager: ContentBlocking.shared.trackerDataManager, tld: ContentBlocking.shared.tld) } struct ScriptSourceProvider: ScriptSourceProviding { diff --git a/DuckDuckGo/RemoteMessaging/RemoteMessagingClient.swift b/DuckDuckGo/RemoteMessaging/RemoteMessagingClient.swift index 4151ed5164..a428829526 100644 --- a/DuckDuckGo/RemoteMessaging/RemoteMessagingClient.swift +++ b/DuckDuckGo/RemoteMessaging/RemoteMessagingClient.swift @@ -99,7 +99,7 @@ final class RemoteMessagingClient: RemoteMessagingProcessing { urlSession: .session(), eventMapping: ConfigurationManager.configurationDebugEvents ), - configurationStore: ConfigurationStore.shared + configurationStore: ConfigurationStore() ) self.configMatcherProvider = configMatcherProvider self.remoteMessagingAvailabilityProvider = remoteMessagingAvailabilityProvider diff --git a/UnitTests/Autoconsent/AutoconsentMessageProtocolTests.swift b/UnitTests/Autoconsent/AutoconsentMessageProtocolTests.swift index cdc70e2ca2..e19f21d6b9 100644 --- a/UnitTests/Autoconsent/AutoconsentMessageProtocolTests.swift +++ b/UnitTests/Autoconsent/AutoconsentMessageProtocolTests.swift @@ -29,8 +29,8 @@ class AutoconsentMessageProtocolTests: XCTestCase { privacyConfigurationManager: MockPrivacyConfigurationManager(), webTrackingProtectionPreferences: WebTrackingProtectionPreferences.shared, // mock contentBlockingManager: ContentBlockerRulesManagerMock(), - trackerDataManager: TrackerDataManager(etag: ConfigurationStore.shared.loadEtag(for: .trackerDataSet), - data: ConfigurationStore.shared.loadData(for: .trackerDataSet), + trackerDataManager: TrackerDataManager(etag: ConfigurationStore().loadEtag(for: .trackerDataSet), + data: ConfigurationStore().loadData(for: .trackerDataSet), embeddedDataProvider: AppTrackerDataSetProvider(), errorReporting: nil), tld: TLD()), diff --git a/UnitTests/Configuration/ConfigurationStorageTests.swift b/UnitTests/Configuration/ConfigurationStorageTests.swift index 81d4b7a890..9b2876ddfb 100644 --- a/UnitTests/Configuration/ConfigurationStorageTests.swift +++ b/UnitTests/Configuration/ConfigurationStorageTests.swift @@ -23,10 +23,12 @@ import Configuration final class ConfigurationStorageTests: XCTestCase { + var configurationStore: ConfigurationStore = ConfigurationStore() + override func tearDown() { super.tearDown() for config in Configuration.allCases { - let url = ConfigurationStore.shared.fileUrl(for: config) + let url = configurationStore.fileUrl(for: config) try? FileManager.default.removeItem(at: url) } } @@ -34,16 +36,16 @@ final class ConfigurationStorageTests: XCTestCase { func test_when_data_is_saved_for_config_then_it_can_be_loaded_correctly() { for config in Configuration.allCases { let uuid = UUID().uuidString - try? ConfigurationStore.shared.saveData(uuid.data(using: .utf8)!, for: config) - XCTAssertEqual(uuid, ConfigurationStore.shared.loadData(for: config)?.utf8String()) + try? configurationStore.saveData(uuid.data(using: .utf8)!, for: config) + XCTAssertEqual(uuid, configurationStore.loadData(for: config)?.utf8String()) } } func test_when_etag_is_saved_for_config_then_it_can_be_loaded_correctly() { for config in Configuration.allCases { let etag = UUID().uuidString - try? ConfigurationStore.shared.saveEtag(etag, for: config) - XCTAssertEqual(etag, ConfigurationStore.shared.loadEtag(for: config)) + try? configurationStore.saveEtag(etag, for: config) + XCTAssertEqual(etag, configurationStore.loadEtag(for: config)) } } diff --git a/UnitTests/ContentBlocker/ContentBlockingUpdatingTests.swift b/UnitTests/ContentBlocker/ContentBlockingUpdatingTests.swift index da76ce79d6..2e17ee672f 100644 --- a/UnitTests/ContentBlocker/ContentBlockingUpdatingTests.swift +++ b/UnitTests/ContentBlocker/ContentBlockingUpdatingTests.swift @@ -31,12 +31,13 @@ final class ContentBlockingUpdatingTests: XCTestCase { @MainActor override func setUp() { + let configStore = ConfigurationStore() updating = UserContentUpdating(contentBlockerRulesManager: rulesManager, privacyConfigurationManager: MockPrivacyConfigurationManager(), - trackerDataManager: TrackerDataManager(etag: ConfigurationStore.shared.loadEtag(for: .trackerDataSet), - data: ConfigurationStore.shared.loadData(for: .trackerDataSet), - embeddedDataProvider: AppTrackerDataSetProvider(), - errorReporting: nil), + trackerDataManager: TrackerDataManager(etag: configStore.loadEtag(for: .trackerDataSet), + data: configStore.loadData(for: .trackerDataSet), + embeddedDataProvider: AppTrackerDataSetProvider(), + errorReporting: nil), configStorage: MockConfigurationStore(), webTrackingProtectionPreferences: preferences, tld: TLD())