|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | import Basics
|
14 |
| -import PackageGraph |
| 14 | +@testable import PackageGraph |
15 | 15 | import PackageLoading
|
16 | 16 | import PackageModel
|
17 | 17 | @testable import SPMBuildCore
|
@@ -584,6 +584,183 @@ class PluginInvocationTests: XCTestCase {
|
584 | 584 | }
|
585 | 585 | }
|
586 | 586 |
|
| 587 | + func testUnsupportedDependencyProduct() throws { |
| 588 | + // Only run the test if the environment in which we're running actually supports Swift concurrency (which the plugin APIs require). |
| 589 | + try XCTSkipIf(!UserToolchain.default.supportsSwiftConcurrency(), "skipping because test environment doesn't support concurrency") |
| 590 | + |
| 591 | + try testWithTemporaryDirectory { tmpPath in |
| 592 | + // Create a sample package with a library product and a plugin. |
| 593 | + let packageDir = tmpPath.appending(components: "MyPackage") |
| 594 | + try localFileSystem.createDirectory(packageDir, recursive: true) |
| 595 | + try localFileSystem.writeFileContents(packageDir.appending(component: "Package.swift"), string: """ |
| 596 | + // swift-tools-version: 5.7 |
| 597 | + import PackageDescription |
| 598 | + let package = Package( |
| 599 | + name: "MyPackage", |
| 600 | + dependencies: [ |
| 601 | + .package(path: "../FooPackage"), |
| 602 | + ], |
| 603 | + targets: [ |
| 604 | + .plugin( |
| 605 | + name: "MyPlugin", |
| 606 | + capability: .buildTool(), |
| 607 | + dependencies: [ |
| 608 | + .product(name: "FooLib", package: "FooPackage"), |
| 609 | + ] |
| 610 | + ), |
| 611 | + ] |
| 612 | + ) |
| 613 | + """) |
| 614 | + |
| 615 | + let myPluginTargetDir = packageDir.appending(components: "Plugins", "MyPlugin") |
| 616 | + try localFileSystem.createDirectory(myPluginTargetDir, recursive: true) |
| 617 | + try localFileSystem.writeFileContents(myPluginTargetDir.appending(component: "plugin.swift"), string: """ |
| 618 | + import PackagePlugin |
| 619 | + import Foo |
| 620 | + @main struct MyBuildToolPlugin: BuildToolPlugin { |
| 621 | + func createBuildCommands( |
| 622 | + context: PluginContext, |
| 623 | + target: Target |
| 624 | + ) throws -> [Command] { } |
| 625 | + } |
| 626 | + """) |
| 627 | + |
| 628 | + let fooPkgDir = tmpPath.appending(components: "FooPackage") |
| 629 | + try localFileSystem.createDirectory(fooPkgDir, recursive: true) |
| 630 | + try localFileSystem.writeFileContents(fooPkgDir.appending(component: "Package.swift"), string: """ |
| 631 | + // swift-tools-version: 5.7 |
| 632 | + import PackageDescription |
| 633 | + let package = Package( |
| 634 | + name: "FooPackage", |
| 635 | + products: [ |
| 636 | + .library(name: "FooLib", |
| 637 | + targets: ["Foo"]), |
| 638 | + ], |
| 639 | + targets: [ |
| 640 | + .target( |
| 641 | + name: "Foo", |
| 642 | + dependencies: [] |
| 643 | + ), |
| 644 | + ] |
| 645 | + ) |
| 646 | + """) |
| 647 | + let fooTargetDir = fooPkgDir.appending(components: "Sources", "Foo") |
| 648 | + try localFileSystem.createDirectory(fooTargetDir, recursive: true) |
| 649 | + try localFileSystem.writeFileContents(fooTargetDir.appending(component: "file.swift"), string: """ |
| 650 | + public func foo() { } |
| 651 | + """) |
| 652 | + |
| 653 | + // Load a workspace from the package. |
| 654 | + let observability = ObservabilitySystem.makeForTesting() |
| 655 | + let workspace = try Workspace( |
| 656 | + fileSystem: localFileSystem, |
| 657 | + forRootPackage: packageDir, |
| 658 | + customManifestLoader: ManifestLoader(toolchain: UserToolchain.default), |
| 659 | + delegate: MockWorkspaceDelegate() |
| 660 | + ) |
| 661 | + |
| 662 | + // Load the root manifest. |
| 663 | + let rootInput = PackageGraphRootInput(packages: [packageDir], dependencies: []) |
| 664 | + let rootManifests = try tsc_await { |
| 665 | + workspace.loadRootManifests( |
| 666 | + packages: rootInput.packages, |
| 667 | + observabilityScope: observability.topScope, |
| 668 | + completion: $0 |
| 669 | + ) |
| 670 | + } |
| 671 | + XCTAssert(rootManifests.count == 1, "\(rootManifests)") |
| 672 | + |
| 673 | + // Load the package graph. |
| 674 | + XCTAssertThrowsError(try workspace.loadPackageGraph(rootInput: rootInput, observabilityScope: observability.topScope)) { error in |
| 675 | + var diagnosed = false |
| 676 | + if let realError = error as? PackageGraphError, |
| 677 | + realError.description == "plugin 'MyPlugin' cannot depend on 'FooLib' of type 'library' from package 'foopackage'; this dependency is unsupported" { |
| 678 | + diagnosed = true |
| 679 | + } |
| 680 | + XCTAssertTrue(diagnosed) |
| 681 | + } |
| 682 | + } |
| 683 | + } |
| 684 | + |
| 685 | + func testUnsupportedDependencyTarget() throws { |
| 686 | + // Only run the test if the environment in which we're running actually supports Swift concurrency (which the plugin APIs require). |
| 687 | + try XCTSkipIf(!UserToolchain.default.supportsSwiftConcurrency(), "skipping because test environment doesn't support concurrency") |
| 688 | + |
| 689 | + try testWithTemporaryDirectory { tmpPath in |
| 690 | + // Create a sample package with a library target and a plugin. |
| 691 | + let packageDir = tmpPath.appending(components: "MyPackage") |
| 692 | + try localFileSystem.createDirectory(packageDir, recursive: true) |
| 693 | + try localFileSystem.writeFileContents(packageDir.appending(component: "Package.swift"), string: """ |
| 694 | + // swift-tools-version: 5.7 |
| 695 | + import PackageDescription |
| 696 | + let package = Package( |
| 697 | + name: "MyPackage", |
| 698 | + targets: [ |
| 699 | + .target( |
| 700 | + name: "MyLibrary", |
| 701 | + dependencies: [] |
| 702 | + ), |
| 703 | + .plugin( |
| 704 | + name: "MyPlugin", |
| 705 | + capability: .buildTool(), |
| 706 | + dependencies: [ |
| 707 | + "MyLibrary" |
| 708 | + ] |
| 709 | + ), |
| 710 | + ] |
| 711 | + ) |
| 712 | + """) |
| 713 | + |
| 714 | + let myLibraryTargetDir = packageDir.appending(components: "Sources", "MyLibrary") |
| 715 | + try localFileSystem.createDirectory(myLibraryTargetDir, recursive: true) |
| 716 | + try localFileSystem.writeFileContents(myLibraryTargetDir.appending(component: "library.swift"), string: """ |
| 717 | + public func hello() { } |
| 718 | + """) |
| 719 | + let myPluginTargetDir = packageDir.appending(components: "Plugins", "MyPlugin") |
| 720 | + try localFileSystem.createDirectory(myPluginTargetDir, recursive: true) |
| 721 | + try localFileSystem.writeFileContents(myPluginTargetDir.appending(component: "plugin.swift"), string: """ |
| 722 | + import PackagePlugin |
| 723 | + import MyLibrary |
| 724 | + @main struct MyBuildToolPlugin: BuildToolPlugin { |
| 725 | + func createBuildCommands( |
| 726 | + context: PluginContext, |
| 727 | + target: Target |
| 728 | + ) throws -> [Command] { } |
| 729 | + } |
| 730 | + """) |
| 731 | + |
| 732 | + // Load a workspace from the package. |
| 733 | + let observability = ObservabilitySystem.makeForTesting() |
| 734 | + let workspace = try Workspace( |
| 735 | + fileSystem: localFileSystem, |
| 736 | + forRootPackage: packageDir, |
| 737 | + customManifestLoader: ManifestLoader(toolchain: UserToolchain.default), |
| 738 | + delegate: MockWorkspaceDelegate() |
| 739 | + ) |
| 740 | + |
| 741 | + // Load the root manifest. |
| 742 | + let rootInput = PackageGraphRootInput(packages: [packageDir], dependencies: []) |
| 743 | + let rootManifests = try tsc_await { |
| 744 | + workspace.loadRootManifests( |
| 745 | + packages: rootInput.packages, |
| 746 | + observabilityScope: observability.topScope, |
| 747 | + completion: $0 |
| 748 | + ) |
| 749 | + } |
| 750 | + XCTAssert(rootManifests.count == 1, "\(rootManifests)") |
| 751 | + |
| 752 | + // Load the package graph. |
| 753 | + XCTAssertThrowsError(try workspace.loadPackageGraph(rootInput: rootInput, observabilityScope: observability.topScope)) { error in |
| 754 | + var diagnosed = false |
| 755 | + if let realError = error as? PackageGraphError, |
| 756 | + realError.description == "plugin 'MyPlugin' cannot depend on 'MyLibrary' of type 'library'; this dependency is unsupported" { |
| 757 | + diagnosed = true |
| 758 | + } |
| 759 | + XCTAssertTrue(diagnosed) |
| 760 | + } |
| 761 | + } |
| 762 | + } |
| 763 | + |
587 | 764 | func testPrebuildPluginShouldNotUseExecTarget() throws {
|
588 | 765 | // Only run the test if the environment in which we're running actually supports Swift concurrency (which the plugin APIs require).
|
589 | 766 | try XCTSkipIf(!UserToolchain.default.supportsSwiftConcurrency(), "skipping because test environment doesn't support concurrency")
|
@@ -659,7 +836,6 @@ class PluginInvocationTests: XCTestCase {
|
659 | 836 | )
|
660 | 837 | ]
|
661 | 838 | }
|
662 |
| -
|
663 | 839 | }
|
664 | 840 | """)
|
665 | 841 |
|
|
0 commit comments