ENABLE_TESTABILITY on package target

Hello,

I'm having issues porting one of my Cocoapods to SwiftPM. The package contains two products "MyPackage" and "MyPackageTestSupport".

The key thing here is that "MyPackageTestSupport" imports "MyPackage" using @testability and relies on the ENABLE_TESTABILITY=YES build setting. The target "MyPackageTests" then imports "MyPackageTestSupport". This allows test targets to import the test support package and use the helpers and test factory methods it provides which can use the internal initialisers of the types defined in "MyPackage". Other 3rd party consumers of "MyPackage" can also use "MyPackageTestSupport" on the test targets to create test instances of types they wouldn't have access to otherwise.

This was doable on Cocoapods just be adding setting s.pod_target_xcconfig = { 'ENABLE_TESTABILITY' => 'YES' }.

Is there a way to ENABLE_TESTABILITY for products created via SwiftPM, an example package file is shown below.

// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyPackage",
    platforms: [
        .iOS(.v11),
        .macOS(.v10_14)
    ],    
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyPackage"]),
        .library(
            name: "MyPackageTestSupport",
            targets: ["MyPackageTestSupport"]),
    ],
    targets: [
        .target(
            name: "MyPackage",
            dependencies: []),
        .target(
            name: "MyPackageTestSupport",
            dependencies: ["iOS-Networking"]),
        .testTarget(
            name: "MyPackageTests",
            dependencies: [
                "MyPackage",
                "MyPackageTestSupport"]),
    ]
)

Currently when building for archive an Xcode project that has "MyPackage" as a dependency I'm getting the error "Module 'MyPackage' was not compiled for testing" in the TestSupport files.

4 Likes

There is currently no way to do this, but you can configure your Xcode scheme such that any modules needing testability are not built for archive by unchecking that checkbox in the Build tab.

Thanks for your reply! Having just checked this out SwiftPM modules don't seem to appear in the Scheme editor under the Build tab unfortunately in Xcode projects. It's worth noting my packages don't have xcodeproj files and even if they did, they wouldn't be used by Xcode 11 projects that import those SwiftPM dependencies.

I find it slightly odd that Xcode 11 gives you no way of specifying build settings for SwiftPM dependencies and neither can you define them in the SwiftPM package manifest. This is going to be a big limitation to using and porting more complex iOS/macOS library packages to Xcode.

Odd, could you create a small example of your exact configuration and file a radar so that we can investigate in detail?

In terms of build settings, there is no support for arbitrary ones (except when using unsafeFlags), but we did add a set of specific settings in this proposal. As said in the future directions section of that proposal, we do have plans to have a richer build settings model in the future.