Skip to content

Commit adb8438

Browse files
committed
[PackageGraph] Add unit test for mock package graphs
1 parent 2bd21e3 commit adb8438

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

Sources/PackageGraph/PackageGraphLoader.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public struct PackageGraphLoader {
4949
public init() { }
5050

5151
/// Load the package graph for the given package path.
52-
public func load(rootManifest: Manifest, externalManifests: [Manifest]) throws -> PackageGraph {
52+
public func load(rootManifest: Manifest, externalManifests: [Manifest], fileSystem: FileSystem = localFileSystem) throws -> PackageGraph {
5353
let allManifests = externalManifests + [rootManifest]
5454

5555
// Create the packages and convert to modules.
@@ -68,7 +68,7 @@ public struct PackageGraphLoader {
6868
// FIXME: We should always load the tests, but just change which
6969
// tests we build based on higher-level logic. This would make it
7070
// easier to allow testing of external package tests.
71-
let builder = PackageBuilder(manifest: manifest, path: packagePath)
71+
let builder = PackageBuilder(manifest: manifest, path: packagePath, fileSystem: fileSystem)
7272
let package = try builder.construct(includingTestModules: isRootPackage)
7373
packages.append(package)
7474

Tests/PackageGraphTests/PackageGraphTests.swift

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,102 @@
1010

1111
import XCTest
1212

13+
import Basic
1314
import PackageGraph
15+
import PackageDescription
16+
import PackageLoading
17+
import PackageModel
18+
19+
private let v1: Version = "1.0.0"
1420

1521
class PackageGraphTests: XCTestCase {
16-
func testBasics() {
17-
// Implement.
22+
23+
func testBasic() throws {
24+
let fs = InMemoryFileSystem(emptyFiles:
25+
"/Foo/source.swift",
26+
"/Foo/Tests/FooTests/source.swift",
27+
"/Bar/source.swift",
28+
"/Bar/Tests/BarTests/source.swift"
29+
)
30+
31+
let g = try loadMockPackageGraph([
32+
"/Foo": Package(name: "Foo"),
33+
"/Bar": Package(name: "Bar", dependencies: [.Package(url: "/Foo", majorVersion: 1)]),
34+
], root: "/Bar", in: fs)
35+
36+
PackageGraphTester(g) { result in
37+
result.check(packages: "Bar", "Foo")
38+
result.check(modules: "Bar", "Foo")
39+
result.check(testModules: "BarTests")
40+
}
41+
}
42+
43+
func testTestTargetDeclInExternalPackage() throws {
44+
let fs = InMemoryFileSystem(emptyFiles:
45+
"/Foo/source.swift",
46+
"/Foo/Tests/SomeTests/source.swift",
47+
"/Bar/source.swift",
48+
"/Bar/Tests/BarTests/source.swift"
49+
)
50+
51+
let g = try loadMockPackageGraph([
52+
"/Foo": Package(name: "Foo", targets: [Target(name: "SomeTests", dependencies: ["Foo"])]),
53+
"/Bar": Package(name: "Bar", dependencies: [.Package(url: "/Foo", majorVersion: 1)]),
54+
], root: "/Bar", in: fs)
55+
56+
PackageGraphTester(g) { result in
57+
result.check(packages: "Bar", "Foo")
58+
result.check(modules: "Bar", "Foo")
59+
result.check(testModules: "BarTests")
60+
}
1861
}
1962

2063
static var allTests = [
21-
("testBasics", testBasics),
64+
("testBasic", testBasic),
65+
("testTestTargetDeclInExternalPackage", testTestTargetDeclInExternalPackage),
2266
]
2367
}
68+
69+
private func PackageGraphTester(_ graph: PackageGraph, _ result: (PackageGraphResult) -> Void) {
70+
result(PackageGraphResult(graph))
71+
}
72+
73+
private class PackageGraphResult {
74+
let graph: PackageGraph
75+
76+
init(_ graph: PackageGraph) {
77+
self.graph = graph
78+
}
79+
80+
func check(packages: String..., file: StaticString = #file, line: UInt = #line) {
81+
XCTAssertEqual(graph.packages.map {$0.name}, packages, file: file, line: line)
82+
}
83+
84+
func check(modules: String..., file: StaticString = #file, line: UInt = #line) {
85+
XCTAssertEqual(graph.packages.flatMap {$0.modules.map {$0.name} }, modules, file: file, line: line)
86+
}
87+
88+
func check(testModules: String..., file: StaticString = #file, line: UInt = #line) {
89+
XCTAssertEqual(graph.packages.flatMap {$0.testModules.map {$0.name} }, testModules, file: file, line: line)
90+
}
91+
}
92+
93+
private func loadMockPackageGraph(_ packageMap: [String: PackageDescription.Package], root: String, in fs: FileSystem) throws -> PackageGraph {
94+
var externalManifests = [Manifest]()
95+
var rootManifest: Manifest!
96+
for (url, package) in packageMap {
97+
let manifest = Manifest(
98+
path: AbsolutePath(url).appending(component: Manifest.filename),
99+
url: url,
100+
package: package,
101+
products: [],
102+
version: v1
103+
)
104+
if url == root {
105+
rootManifest = manifest
106+
} else {
107+
externalManifests.append(manifest)
108+
}
109+
}
110+
return try PackageGraphLoader().load(rootManifest: rootManifest, externalManifests: externalManifests, fileSystem: fs)
111+
}

0 commit comments

Comments
 (0)