Skip to content

Commit 2f08879

Browse files
author
David Ungar
committed
Fix tests to prepare for immutable map
1 parent ab1db13 commit 2f08879

File tree

5 files changed

+128
-69
lines changed

5 files changed

+128
-69
lines changed

Sources/SwiftDriver/IncrementalCompilation/DirectAndTransitiveCollections.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct Directly {}
1919
public struct InvalidatedSet<ClosureLevel, Element: Hashable>: Sequence {
2020
var contents: Set<Element>
2121

22-
init(_ s: Set<Element> = Set()) {
22+
@_spi(Testing) public init(_ s: Set<Element> = Set()) {
2323
self.contents = s
2424
}
2525
init<Elements: Sequence>(_ elements: Elements)

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraphParts/InputDependencySourceMap.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ extension InputDependencySourceMap {
4545
_ eachFn: (TypedVirtualPath, DependencySource) -> Void
4646
) {
4747
biMap.forEach(eachFn)
48+
49+
extension OutputFileMap {
50+
@_spi(Testing) public func getDependencySource(
51+
for sourceFile: TypedVirtualPath
52+
) -> DependencySource? {
53+
assert(sourceFile.type == FileType.swift)
54+
guard let swiftDepsPath = existingOutput(inputFile: sourceFile.fileHandle,
55+
outputType: .swiftDeps)
56+
else {
57+
return nil
58+
}
59+
assert(VirtualPath.lookup(swiftDepsPath).extension == FileType.swiftDeps.rawValue)
60+
let typedSwiftDepsFile = TypedVirtualPath(file: swiftDepsPath, type: .swiftDeps)
61+
return DependencySource(typedSwiftDepsFile)
62+
}
63+
}
64+
65+
extension OutputFileMap {
66+
@_spi(Testing) public func getDependencySource(
67+
for sourceFile: TypedVirtualPath
68+
) -> DependencySource? {
69+
assert(sourceFile.type == FileType.swift)
70+
guard let swiftDepsPath = existingOutput(inputFile: sourceFile.fileHandle,
71+
outputType: .swiftDeps)
72+
else {
73+
return nil
74+
}
75+
assert(VirtualPath.lookup(swiftDepsPath).extension == FileType.swiftDeps.rawValue)
76+
let typedSwiftDepsFile = TypedVirtualPath(file: swiftDepsPath, type: .swiftDeps)
77+
return DependencySource(typedSwiftDepsFile)
4878
}
4979
}
5080

Tests/SwiftDriverTests/DependencyGraphSerializationTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import XCTest
1414
@_spi(Testing) import SwiftDriver
1515
import TSCBasic
1616

17-
class DependencyGraphSerializationTests: XCTestCase {
17+
class DependencyGraphSerializationTests: XCTestCase, ModuleDependencyGraphMocker {
18+
static let mockGraphCreator = MockModuleDependencyGraphCreator(maxIndex: 12)
19+
1820
func roundTrip(_ graph: ModuleDependencyGraph) throws {
1921
let mockPath = VirtualPath.absolute(AbsolutePath("/module-dependency-graph"))
2022
let fs = InMemoryFileSystem()
@@ -216,8 +218,7 @@ class DependencyGraphSerializationTests: XCTestCase {
216218
]
217219

218220
for fixture in fixtures {
219-
let de = DiagnosticsEngine()
220-
let graph = ModuleDependencyGraph(mock: de)
221+
let graph = Self.mockGraphCreator.mockUpAGraph()
221222
for loadCommand in fixture.commands {
222223
switch loadCommand {
223224
case .load(index: let index, nodes: let nodes, fingerprint: let fingerprint):

Tests/SwiftDriverTests/Helpers/MockingIncrementalCompilation.swift

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
@_spi(Testing) import SwiftDriver
1414
import TSCBasic
1515
import Foundation
16+
import XCTest
1617

1718
// MARK: - utilities for unit testing
1819
extension ModuleDependencyGraph {
@@ -119,13 +120,15 @@ extension BuildRecordInfo {
119120
extension IncrementalCompilationState.IncrementalDependencyAndInputSetup {
120121
static func mock(
121122
options: IncrementalCompilationState.Options = [.verifyDependencyGraphAfterEveryImport],
122-
diagnosticEngine: DiagnosticsEngine = DiagnosticsEngine(),
123-
fileSystem: FileSystem = localFileSystem) -> Self {
123+
outputFileMap: OutputFileMap = OutputFileMap(),
124+
fileSystem: FileSystem = localFileSystem,
125+
diagnosticEngine: DiagnosticsEngine = DiagnosticsEngine()
126+
) -> Self {
124127
let diagnosticsEngine = DiagnosticsEngine()
125-
let outputFileMap = OutputFileMap()
126128
return Self(options, outputFileMap,
127129
BuildRecordInfo.mock(diagnosticsEngine, outputFileMap),
128-
nil, nil, [], fileSystem, diagnosticsEngine)
130+
nil, nil, [], fileSystem,
131+
diagnosticsEngine)
129132
}
130133
}
131134

@@ -139,3 +142,38 @@ extension Collection where Element: StringProtocol {
139142
sorted(by: `is`(dotFileName:lessThan:))
140143
}
141144
}
145+
146+
// MARK: - Mocking up a ModuleDependencyGraph
147+
protocol ModuleDependencyGraphMocker {
148+
static var mockGraphCreator: MockModuleDependencyGraphCreator {get}
149+
}
150+
151+
struct MockModuleDependencyGraphCreator {
152+
let maxIndex: Int
153+
let info: IncrementalCompilationState.IncrementalDependencyAndInputSetup
154+
155+
/// maxIndex must be larger than any index used
156+
init(maxIndex: Int) {
157+
let outputFileMap = OutputFileMap.mock(maxIndex: maxIndex)
158+
self.info = IncrementalCompilationState.IncrementalDependencyAndInputSetup
159+
.mock(outputFileMap: outputFileMap)
160+
self.maxIndex = maxIndex
161+
}
162+
163+
func mockUpAGraph() -> ModuleDependencyGraph {
164+
ModuleDependencyGraph(info, .buildingWithoutAPrior)
165+
}
166+
}
167+
168+
169+
extension OutputFileMap {
170+
fileprivate static func mock(maxIndex: Int) -> Self {
171+
OutputFileMap( entries: (0...maxIndex) .reduce(into: [:]) {
172+
entries, index in
173+
let inputHandle = TypedVirtualPath(mockInput: index).file.intern()
174+
let swiftDepsHandle = DependencySource(mock: index).file.intern()
175+
entries[inputHandle] = [.swiftDeps: swiftDepsHandle]
176+
}
177+
)
178+
}
179+
}

0 commit comments

Comments
 (0)