Skip to content

Commit 403e58d

Browse files
committed
Check for LeafCacheKey in more HashEncoder functions
Follow-up to #97 with an added test case.
1 parent 58337d6 commit 403e58d

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

Sources/GeneratorEngine/Cache/CacheKeyProtocol.swift renamed to Sources/GeneratorEngine/Cache/CacheKey.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

Sources/GeneratorEngine/Query.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -124,9 +124,15 @@ extension HashEncoder: SingleValueEncodingContainer {
124124
}
125125

126126
func encode<T>(_ value: T) throws where T: Encodable {
127+
if let leaf = value as? LeafCacheKey {
128+
leaf.hash(with: &self.hashFunction)
129+
return
130+
}
131+
127132
guard value is CacheKey else {
128133
throw Error.noCacheKeyConformance(T.self)
129134
}
135+
130136
try String(describing: T.self).encode(to: self)
131137
try value.encode(to: self)
132138
}

Tests/GeneratorEngineTests/EngineTests.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -98,6 +98,21 @@ struct Expression: CachingQuery {
9898
}
9999

100100
final class EngineTests: XCTestCase {
101+
func testFilePathHashing() throws {
102+
let path = "/root"
103+
104+
let hashEncoder1 = HashEncoder<SHA256>()
105+
try hashEncoder1.encode(FilePath(path))
106+
let digest1 = hashEncoder1.finalize()
107+
108+
let hashEncoder2 = HashEncoder<SHA256>()
109+
try hashEncoder2.encode(String(reflecting: FilePath.self))
110+
try hashEncoder2.encode(path)
111+
let digest2 = hashEncoder2.finalize()
112+
113+
XCTAssertEqual(digest1, digest2)
114+
}
115+
101116
func testSimpleCaching() async throws {
102117
let engine = Engine(
103118
VirtualFileSystem(),

0 commit comments

Comments
 (0)