Skip to content

[PackageBuilder] Fix test target ref in manifest for external packages #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/PackageGraph/PackageGraphLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public struct PackageGraphLoader {
public init() { }

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

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

Expand Down
12 changes: 4 additions & 8 deletions Sources/PackageLoading/PackageBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,11 @@ public struct PackageBuilder {
/// - includingTestModules: Whether the package's test modules should be loaded.
public func construct(includingTestModules: Bool) throws -> Package {
let modules = try constructModules()
let testModules: [Module]
if includingTestModules {
testModules = try constructTestModules(modules: modules)
} else {
testModules = []
}
let testModules = try constructTestModules(modules: modules)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is ok that we construct test modules here, the performance cost should be negligible and I think it is actually better to not have the graph structure change depending on the use case.

try fillDependencies(modules: modules + testModules)
let products = try constructProducts(modules, testModules: testModules)
return Package(manifest: manifest, path: packagePath, modules: modules, testModules: testModules, products: products)
// FIXME: Lift includingTestModules into a higher module.
let products = try constructProducts(modules, testModules: includingTestModules ? testModules : [])
return Package(manifest: manifest, path: packagePath, modules: modules, testModules: includingTestModules ? testModules : [], products: products)
Copy link
Contributor

@ddunbar ddunbar Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this won't cause us to start building all tests for all packages by default?

Err... I take that back I guess we only ever derive that information from the individual package. It would be nice to hoist this conditional out of the construction and into the build module, perhaps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it seems weird here, I'll add a FIXME

}

// MARK: Utility Predicates
Expand Down
96 changes: 93 additions & 3 deletions Tests/PackageGraphTests/PackageGraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,104 @@

import XCTest

import Basic
import PackageGraph
import PackageDescription
import PackageLoading
import PackageModel

private let v1: Version = "1.0.0"

class PackageGraphTests: XCTestCase {
func testBasics() {
// Implement.

func testBasic() throws {
let fs = InMemoryFileSystem(emptyFiles:
"/Foo/source.swift",
"/Foo/Tests/FooTests/source.swift",
"/Bar/source.swift",
"/Bar/Tests/BarTests/source.swift"
)

let g = try loadMockPackageGraph([
"/Foo": Package(name: "Foo"),
"/Bar": Package(name: "Bar", dependencies: [.Package(url: "/Foo", majorVersion: 1)]),
], root: "/Bar", in: fs)

PackageGraphTester(g) { result in
result.check(packages: "Bar", "Foo")
result.check(modules: "Bar", "Foo")
result.check(testModules: "BarTests")
}
}

// Make sure there is no error when we reference Test targets in a package and then
// use it as a dependency to another package. SR-2353
func testTestTargetDeclInExternalPackage() throws {
let fs = InMemoryFileSystem(emptyFiles:
"/Foo/source.swift",
"/Foo/Tests/SomeTests/source.swift",
"/Bar/source.swift",
"/Bar/Tests/BarTests/source.swift"
)

let g = try loadMockPackageGraph([
"/Foo": Package(name: "Foo", targets: [Target(name: "SomeTests", dependencies: ["Foo"])]),
"/Bar": Package(name: "Bar", dependencies: [.Package(url: "/Foo", majorVersion: 1)]),
], root: "/Bar", in: fs)

PackageGraphTester(g) { result in
result.check(packages: "Bar", "Foo")
result.check(modules: "Bar", "Foo")
result.check(testModules: "BarTests")
}
}

static var allTests = [
("testBasics", testBasics),
("testBasic", testBasic),
("testTestTargetDeclInExternalPackage", testTestTargetDeclInExternalPackage),
]
}

private func PackageGraphTester(_ graph: PackageGraph, _ result: (PackageGraphResult) -> Void) {
result(PackageGraphResult(graph))
}

private class PackageGraphResult {
let graph: PackageGraph

init(_ graph: PackageGraph) {
self.graph = graph
}

func check(packages: String..., file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(graph.packages.map {$0.name}, packages, file: file, line: line)
}

func check(modules: String..., file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(graph.packages.flatMap {$0.modules.map {$0.name} }, modules, file: file, line: line)
}

func check(testModules: String..., file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(graph.packages.flatMap {$0.testModules.map {$0.name} }, testModules, file: file, line: line)
}
}

private func loadMockPackageGraph(_ packageMap: [String: PackageDescription.Package], root: String, in fs: FileSystem) throws -> PackageGraph {
var externalManifests = [Manifest]()
var rootManifest: Manifest!
for (url, package) in packageMap {
let manifest = Manifest(
path: AbsolutePath(url).appending(component: Manifest.filename),
url: url,
package: package,
products: [],
version: v1
)
if url == root {
rootManifest = manifest
} else {
externalManifests.append(manifest)
}
}
return try PackageGraphLoader().load(rootManifest: rootManifest, externalManifests: externalManifests, fileSystem: fs)
}