From 8f7a7d3a52bc59f682aaf87289d562ac5bb886ff Mon Sep 17 00:00:00 2001 From: Vyacheslav Khorkov Date: Wed, 29 Nov 2023 22:16:27 +0500 Subject: [PATCH] Add tests for ProductHasher --- .../Core/Common/Hashers/ProductHasher.swift | 14 ++-- .../Core/Common/Hashers/TargetsHasher.swift | 4 +- Sources/RugbyFoundation/Vault/Vault.swift | 2 +- .../Common/Hashers/ProductHasherTests.swift | 84 +++++++++++++++++++ Tests/RugbyTests/Utils/Regex/RegexTests.swift | 1 - 5 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 Tests/FoundationTests/Core/Common/Hashers/ProductHasherTests.swift diff --git a/Sources/RugbyFoundation/Core/Common/Hashers/ProductHasher.swift b/Sources/RugbyFoundation/Core/Common/Hashers/ProductHasher.swift index b79599a7..b79aee0b 100644 --- a/Sources/RugbyFoundation/Core/Common/Hashers/ProductHasher.swift +++ b/Sources/RugbyFoundation/Core/Common/Hashers/ProductHasher.swift @@ -1,10 +1,14 @@ -final class ProductHasher { - private let foundationHasher: FoundationHasher +// MARK: - Interface - init(foundationHasher: FoundationHasher) { - self.foundationHasher = foundationHasher - } +protocol IProductHasher: AnyObject { + func hashContext(_ product: Product) -> [String: String?] +} + +// MARK: - Implementation + +final class ProductHasher {} +extension ProductHasher: IProductHasher { func hashContext(_ product: Product) -> [String: String?] { ["name": product.name, "type": product.type.rawValue, diff --git a/Sources/RugbyFoundation/Core/Common/Hashers/TargetsHasher.swift b/Sources/RugbyFoundation/Core/Common/Hashers/TargetsHasher.swift index 1cd1bf97..79b7a433 100644 --- a/Sources/RugbyFoundation/Core/Common/Hashers/TargetsHasher.swift +++ b/Sources/RugbyFoundation/Core/Common/Hashers/TargetsHasher.swift @@ -25,7 +25,7 @@ final class TargetsHasher { private let buildPhaseHasher: IBuildPhaseHasher private let cocoaPodsScriptsHasher: CocoaPodsScriptsHasher private let configurationsHasher: ConfigurationsHasher - private let productHasher: ProductHasher + private let productHasher: IProductHasher private let buildRulesHasher: BuildRulesHasher init(foundationHasher: FoundationHasher, @@ -33,7 +33,7 @@ final class TargetsHasher { buildPhaseHasher: IBuildPhaseHasher, cocoaPodsScriptsHasher: CocoaPodsScriptsHasher, configurationsHasher: ConfigurationsHasher, - productHasher: ProductHasher, + productHasher: IProductHasher, buildRulesHasher: BuildRulesHasher) { self.foundationHasher = foundationHasher self.swiftVersionProvider = swiftVersionProvider diff --git a/Sources/RugbyFoundation/Vault/Vault.swift b/Sources/RugbyFoundation/Vault/Vault.swift index 1c4536ec..6823773e 100644 --- a/Sources/RugbyFoundation/Vault/Vault.swift +++ b/Sources/RugbyFoundation/Vault/Vault.swift @@ -121,7 +121,7 @@ public final class Vault { cocoaPodsScriptsHasher: CocoaPodsScriptsHasher(fileContentHasher: fileContentHasher), configurationsHasher: ConfigurationsHasher(foundationHasher: foundationHasher, excludeKeys: [settings.hasBackupKey]), - productHasher: ProductHasher(foundationHasher: foundationHasher), + productHasher: ProductHasher(), buildRulesHasher: BuildRulesHasher(foundationHasher: foundationHasher, fileContentHasher: fileContentHasher) ) diff --git a/Tests/FoundationTests/Core/Common/Hashers/ProductHasherTests.swift b/Tests/FoundationTests/Core/Common/Hashers/ProductHasherTests.swift new file mode 100644 index 00000000..dae1ccbf --- /dev/null +++ b/Tests/FoundationTests/Core/Common/Hashers/ProductHasherTests.swift @@ -0,0 +1,84 @@ +@testable import RugbyFoundation +import XCTest + +final class ProductHasherTests: XCTestCase { + private var sut: IProductHasher! + + override func setUp() { + super.setUp() + sut = ProductHasher() + } + + override func tearDown() { + super.tearDown() + sut = nil + } +} + +extension ProductHasherTests { + func test_framework() { + let product = Product( + name: "KeyboardLayoutGuide", + moduleName: "KeyboardLayoutGuide", + type: .framework, + parentFolderName: "Keyboard+LayoutGuide-framework" + ) + + // Act + let hashContext = sut.hashContext(product) + + // Assert + XCTAssertEqual( + hashContext, + [ + "parentFolderName": "Keyboard+LayoutGuide-framework", + "name": "KeyboardLayoutGuide", + "type": "com.apple.product-type.framework" + ] + ) + } + + func test_library() { + let product = Product( + name: "Keyboard+LayoutGuide-library", + moduleName: "KeyboardLayoutGuide", + type: .staticLibrary, + parentFolderName: "Keyboard+LayoutGuide-library" + ) + + // Act + let hashContext = sut.hashContext(product) + + // Assert + XCTAssertEqual( + hashContext, + [ + "parentFolderName": "Keyboard+LayoutGuide-library", + "name": "Keyboard+LayoutGuide-library", + "type": "com.apple.product-type.library.static" + ] + ) + } + + func test_bundle() { + let product = Product( + name: "LocalPodResources", + moduleName: nil, + type: .bundle, + parentFolderName: "LocalPod-framework" + ) + + // Act + let hashContext = sut.hashContext(product) + + // Assert + XCTAssertEqual( + hashContext, + [ + "parentFolderName": "LocalPod-framework", + "name": "LocalPodResources", + "type": "com.apple.product-type.bundle" + ] + ) + } +} diff --git a/Tests/RugbyTests/Utils/Regex/RegexTests.swift b/Tests/RugbyTests/Utils/Regex/RegexTests.swift index fdb7ee4b..d34eaf74 100644 --- a/Tests/RugbyTests/Utils/Regex/RegexTests.swift +++ b/Tests/RugbyTests/Utils/Regex/RegexTests.swift @@ -1,5 +1,4 @@ @testable import Rugby -import RugbyFoundation import XCTest final class RegexTests: XCTestCase {