swiftlang / swift-package-manager

The Package Manager for the Swift Programming Language
Apache License 2.0
9.7k stars 1.33k forks source link

Unable to test an executable target on Windows #6083

Open svanimpe opened 1 year ago

svanimpe commented 1 year ago

Steps To Reproduce

In an empty directory named hello:

swift package init --type executable
swift build --build-tests

This outputs:

Building for debugging...
error: emit-module command failed with exit code 1 (use -v to see invocationC:\Users\steve\Developer\hello\Tests\helloTests\helloTests.swift:2:18: error: no such module 'hello'
@testable import hello
                 ^
C:\Users\steve\Developer\hello\Tests\helloTests\helloTests.swift:2:18: error: no such module 'hello'
@testable import hello
                 ^
note: module 'hello' is the main module of an executable, and cannot be imported by tests and other targets
note: module 'hello' is the main module of an executable, and cannot be imported by tests and other targets

This is with Swift 5.7 on Windows 11.

This does work on macOS.

Baneeishaque commented 1 year ago

same issue.

agrozyme commented 1 year ago

I use this way to setting Package.swift

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

import PackageDescription

let package = Package(
  name: "test-swift",
  products: [
      // Products define the executables and libraries a package produces, and make them visible to other packages.
    .library(name: "library-test-swift", targets: ["library-test-swift"]),
  ],
  dependencies: [
    // Dependencies declare other packages that this package depends on.
    // .package(url: /* package url */, from: "1.0.0"),
    .package(url: "https://github.com/apple/swift-crypto.git", from: "2.2.4")
  ],
  targets: [
    // Targets are the basic building blocks of a package. A target can define a module or a test suite.
    // Targets can depend on other targets in this package, and on products in packages this package depends on.
    .target(name: "library-test-swift", dependencies: [.product(name: "Crypto", package: "swift-crypto")]),

    .executableTarget(name: "test-swift", dependencies: ["library-test-swift"]),

    .testTarget(name: "test-swiftTests", dependencies: [
      "library-test-swift",
      // "test-swift"
    ]),
  ]
)
Baneeishaque commented 1 year ago

I use this way to setting Package.swift

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

import PackageDescription

let package = Package(
  name: "test-swift",
  products: [
      // Products define the executables and libraries a package produces, and make them visible to other packages.
    .library(name: "library-test-swift", targets: ["library-test-swift"]),
  ],
  dependencies: [
    // Dependencies declare other packages that this package depends on.
    // .package(url: /* package url */, from: "1.0.0"),
    .package(url: "https://github.com/apple/swift-crypto.git", from: "2.2.4")
  ],
  targets: [
    // Targets are the basic building blocks of a package. A target can define a module or a test suite.
    // Targets can depend on other targets in this package, and on products in packages this package depends on.
    .target(name: "library-test-swift", dependencies: [.product(name: "Crypto", package: "swift-crypto")]),

    .executableTarget(name: "test-swift", dependencies: ["library-test-swift"]),

    .testTarget(name: "test-swiftTests", dependencies: [
      "library-test-swift",
      // "test-swift"
    ]),
  ]
)

is there any working example repository? @agrozyme

agrozyme commented 1 year ago

is there any working example repository? @agrozyme

I don't have Mac device, and I only use Windows 10 for work. You can put all logic function in "library" and write some nomal test case for test "library". in "executableTarget" only put simple flow logic, use this way we can do unit testing, but can't do integration testing.

I have some screen shot but I don't know how to put them in this replay.

Baneeishaque commented 1 year ago

is there any working example repository? @agrozyme

I don't have Mac device, and I only use Windows 10 for work.

Same here...

I have some screen shot but I don't know how to put them in this replay.

Just drag & drop the files to the writing area, it will automatically upload & attached to the comment.

agrozyme commented 1 year ago

2023-03-01 19 33 18