Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...