Skip to content

Commit 067c46c

Browse files
John ButeJohn Bute
John Bute
authored and
John Bute
committed
added more test coverage and implemented sam's suggestion
1 parent c4bc77e commit 067c46c

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

Sources/PackageLoading/ManifestLoader+Validation.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ extension Basics.Diagnostic {
309309
return .error("\(messagePrefix) in dependencies of target '\(targetName)'; valid packages are: \(validPackages.map{ "\($0.descriptionForValidation)" }.joined(separator: ", "))")
310310
}
311311

312-
static func invalidDependencyOnTestTarget(dependency: String, targetName: String) -> Self {
312+
static func invalidDependencyOnTestTarget(dependency: Module.Dependency, targetName: String) -> Self {
313313
.error(
314-
"Invalid dependency: '\(targetName)' cannot depend on test target dependency '\(dependency)'. Only test targets can depend on other test targets"
314+
"Invalid dependency: '\(targetName)' cannot depend on test target dependency '\(dependency.name)'. Only test targets can depend on other test targets"
315315
)
316316
}
317317

Sources/PackageLoading/PackageBuilder.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ public final class PackageBuilder {
955955
for dependency in dependencies {
956956
if let depTarget = dependency.module, depTarget.type == .test {
957957
self.observabilityScope.emit(.invalidDependencyOnTestTarget(
958-
dependency: dependency.name,
958+
dependency: dependency,
959959
targetName: potentialModule.name
960960
))
961961
}

Tests/PackageGraphTests/ModulesGraphTests.swift

+109
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,115 @@ final class ModulesGraphTests: XCTestCase {
446446
}
447447
}
448448

449+
func testExecutableInvalidDependencyOnTestTarget() throws {
450+
let fs = InMemoryFileSystem(
451+
emptyFiles:
452+
"/Foo/Sources/Foo/main.swift",
453+
"/Foo/Tests/FooTest/FooTest.swift"
454+
)
455+
456+
let observability = ObservabilitySystem.makeForTesting()
457+
458+
let _ = try loadModulesGraph(
459+
fileSystem: fs,
460+
manifests: [
461+
Manifest.createRootManifest(
462+
displayName: "Foo",
463+
path: "/Foo",
464+
toolsVersion: .v6_0,
465+
targets: [
466+
TargetDescription(name: "Foo", dependencies: ["FooTest"], type: .executable),
467+
TargetDescription(name: "FooTest", type: .test),
468+
]
469+
),
470+
],
471+
observabilityScope: observability.topScope
472+
)
473+
474+
testDiagnostics(observability.diagnostics) { result in
475+
result.check(
476+
diagnostic: "Invalid dependency: 'Foo' cannot depend on test target dependency 'FooTest'. Only test targets can depend on other test targets",
477+
severity: .error
478+
)
479+
}
480+
}
481+
482+
func testPluginInvalidDependencyOnTestTarget() throws {
483+
let fs = InMemoryFileSystem(
484+
emptyFiles:
485+
"/Foo/Plugins/Foo/main.swift",
486+
"/Foo/Tests/FooTest/FooTest.swift"
487+
)
488+
489+
let observability = ObservabilitySystem.makeForTesting()
490+
491+
let _ = try loadModulesGraph(
492+
fileSystem: fs,
493+
manifests: [
494+
Manifest.createRootManifest(
495+
displayName: "Foo",
496+
path: "/Foo",
497+
toolsVersion: .v6_0,
498+
targets: [
499+
TargetDescription(
500+
name: "Foo",
501+
dependencies: ["FooTest"],
502+
type: .plugin,
503+
pluginCapability: .buildTool
504+
),
505+
TargetDescription(name: "FooTest", type: .test),
506+
]
507+
),
508+
],
509+
observabilityScope: observability.topScope
510+
)
511+
512+
testDiagnostics(observability.diagnostics) { result in
513+
result.check(
514+
diagnostic: "Invalid dependency: 'Foo' cannot depend on test target dependency 'FooTest'. Only test targets can depend on other test targets",
515+
severity: .error
516+
)
517+
}
518+
}
519+
520+
func testMacroInvalidDependencyOnTestTarget() throws {
521+
let fs = InMemoryFileSystem(
522+
emptyFiles:
523+
"/Foo/Sources/Foo/main.swift",
524+
"/Foo/Tests/FooTest/FooTest.swift"
525+
)
526+
527+
let observability = ObservabilitySystem.makeForTesting()
528+
529+
let _ = try loadModulesGraph(
530+
fileSystem: fs,
531+
manifests: [
532+
Manifest.createRootManifest(
533+
displayName: "Foo",
534+
path: "/Foo",
535+
toolsVersion: .v6_0,
536+
targets: [
537+
TargetDescription(
538+
name: "Foo",
539+
dependencies: ["FooTest"],
540+
type: .macro
541+
),
542+
TargetDescription(name: "FooTest", type: .test),
543+
]
544+
),
545+
],
546+
observabilityScope: observability.topScope
547+
)
548+
549+
testDiagnostics(observability.diagnostics) { result in
550+
result.check(
551+
diagnostic: "Invalid dependency: 'Foo' cannot depend on test target dependency 'FooTest'. Only test targets can depend on other test targets",
552+
severity: .error
553+
)
554+
}
555+
}
556+
557+
449558
func testValidDependencyOnTestTarget() throws {
450559
let fs = InMemoryFileSystem(
451560
emptyFiles:

0 commit comments

Comments
 (0)