|
10 | 10 |
|
11 | 11 | import XCTest
|
12 | 12 |
|
| 13 | +import Basic |
13 | 14 | import PackageGraph
|
| 15 | +import PackageDescription |
| 16 | +import PackageLoading |
| 17 | +import PackageModel |
| 18 | + |
| 19 | +private let v1: Version = "1.0.0" |
14 | 20 |
|
15 | 21 | 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 | + // Make sure there is no error when we reference Test targets in a package and then |
| 44 | + // use it as a dependency to another package. SR-2353 |
| 45 | + func testTestTargetDeclInExternalPackage() throws { |
| 46 | + let fs = InMemoryFileSystem(emptyFiles: |
| 47 | + "/Foo/source.swift", |
| 48 | + "/Foo/Tests/SomeTests/source.swift", |
| 49 | + "/Bar/source.swift", |
| 50 | + "/Bar/Tests/BarTests/source.swift" |
| 51 | + ) |
| 52 | + |
| 53 | + let g = try loadMockPackageGraph([ |
| 54 | + "/Foo": Package(name: "Foo", targets: [Target(name: "SomeTests", dependencies: ["Foo"])]), |
| 55 | + "/Bar": Package(name: "Bar", dependencies: [.Package(url: "/Foo", majorVersion: 1)]), |
| 56 | + ], root: "/Bar", in: fs) |
| 57 | + |
| 58 | + PackageGraphTester(g) { result in |
| 59 | + result.check(packages: "Bar", "Foo") |
| 60 | + result.check(modules: "Bar", "Foo") |
| 61 | + result.check(testModules: "BarTests") |
| 62 | + } |
18 | 63 | }
|
19 | 64 |
|
20 | 65 | static var allTests = [
|
21 |
| - ("testBasics", testBasics), |
| 66 | + ("testBasic", testBasic), |
| 67 | + ("testTestTargetDeclInExternalPackage", testTestTargetDeclInExternalPackage), |
22 | 68 | ]
|
23 | 69 | }
|
| 70 | + |
| 71 | +private func PackageGraphTester(_ graph: PackageGraph, _ result: (PackageGraphResult) -> Void) { |
| 72 | + result(PackageGraphResult(graph)) |
| 73 | +} |
| 74 | + |
| 75 | +private class PackageGraphResult { |
| 76 | + let graph: PackageGraph |
| 77 | + |
| 78 | + init(_ graph: PackageGraph) { |
| 79 | + self.graph = graph |
| 80 | + } |
| 81 | + |
| 82 | + func check(packages: String..., file: StaticString = #file, line: UInt = #line) { |
| 83 | + XCTAssertEqual(graph.packages.map {$0.name}, packages, file: file, line: line) |
| 84 | + } |
| 85 | + |
| 86 | + func check(modules: String..., file: StaticString = #file, line: UInt = #line) { |
| 87 | + XCTAssertEqual(graph.packages.flatMap {$0.modules.map {$0.name} }, modules, file: file, line: line) |
| 88 | + } |
| 89 | + |
| 90 | + func check(testModules: String..., file: StaticString = #file, line: UInt = #line) { |
| 91 | + XCTAssertEqual(graph.packages.flatMap {$0.testModules.map {$0.name} }, testModules, file: file, line: line) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +private func loadMockPackageGraph(_ packageMap: [String: PackageDescription.Package], root: String, in fs: FileSystem) throws -> PackageGraph { |
| 96 | + var externalManifests = [Manifest]() |
| 97 | + var rootManifest: Manifest! |
| 98 | + for (url, package) in packageMap { |
| 99 | + let manifest = Manifest( |
| 100 | + path: AbsolutePath(url).appending(component: Manifest.filename), |
| 101 | + url: url, |
| 102 | + package: package, |
| 103 | + products: [], |
| 104 | + version: v1 |
| 105 | + ) |
| 106 | + if url == root { |
| 107 | + rootManifest = manifest |
| 108 | + } else { |
| 109 | + externalManifests.append(manifest) |
| 110 | + } |
| 111 | + } |
| 112 | + return try PackageGraphLoader().load(rootManifest: rootManifest, externalManifests: externalManifests, fileSystem: fs) |
| 113 | +} |
0 commit comments