Can I specify a platform target when running swift test from the CLI?

自古美人都是妖i 提交于 2021-01-12 05:09:32

问题


My Package.swift looks something like

let package = Package(
  name: "MyPackage",
  platforms: [
    .iOS(.v13)
  ],
  products: [
    .library(
      name: "MyPackage",
      targets: ["MyPackage"])
  ],
  dependencies: [
    .package(url: "https://github.com/SnapKit/SnapKit.git", from: "5.0.0"),
  ],
  targets: [
    .target(
      name: "MyPackage",
      dependencies: [
        "SnapKit",
      ]),
    .testTarget(
      name: "MyPackageTests",
      dependencies: ["MyPackage"])
  ]
)

When I run swift test I get

error: the library 'MyPackage' requires macos 10.10, but depends on the product 'SnapKit' \
which requires macos 10.12; consider changing the library 'SurfUIKit' to require macos 10.12 \
or later, or the product 'SnapKit' to require macos 10.10 or earlier.

Why is swift running tests for macos that is not listed as a supported platform? Can I get swift to run the tests for iOS, ideally specifying some version target? What alternative do I have using xcode in the CLI?


回答1:


The trick is:

  • you think that .iOS(...) is here to restrict the compilation to only one platform

  • while it actually is a line used to precised what minimum version is going to be supported by your product for this platform

It does not say: only compile for .iOS X.y, but .iOS min version is X.y

SPM is a tool for Swift first, thus wants to build for all~ platforms, and has currently no way of using a ~system only parameter~ (I know me sad too).


Now if you want to have an iOS only Package it's still possible but you'll have to compil through xcodebuild commands (and you don't need a xcodeproj file for that).

// Compile and test on iOS simulator

xcodebuild -list

xcodebuild -scheme <scheme> -destination 'generic/platform=iOS'

xcodebuild -scheme <scheme> -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11'

xcodebuild -scheme <scheme> test -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11'


SPM documentation usage about "Depending on Apple modules"


From my experience I would also say that you could have a folder hierarchy as follow (with xCode 12):

| Root Project folder/

  • Source/ // folder of your sources
  • Example/ // create an xcodeproj in it
  • Tests/ // Your tests files ~
  • YourPackage.xcworkspace
    • add the root folder in it to be able to access your Package targets
    • add your example project (and add your Package to its dependencies)
    • finally create a new scheme in which you select the Test target of your package

Now you're all setup to develop your Package in parallele with your example and tests.

And remember that Swift Package Manager currently (12/2020) has no parameter to only build on one platform.

Hope it's clear enough.



来源:https://stackoverflow.com/questions/64758546/can-i-specify-a-platform-target-when-running-swift-test-from-the-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!