Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimum required structure to compile? #8195

Closed
jsumners opened this issue Dec 31, 2024 · 8 comments
Closed

Minimum required structure to compile? #8195

jsumners opened this issue Dec 31, 2024 · 8 comments

Comments

@jsumners
Copy link

What is the minimum required structure to successfully issue swift test? If I have the following setup, when I issue that command I get: "error: 'json': target 'JsonExperimentTests' has overlapping sources:"

./Package.swift:

// swift-tools-version:6.0

import PackageDescription

let package = Package(
  name: "JsonExperiment",

  products: [
    .library(name: "JsonExperiment", targets: ["JsonExperiment"])
  ],

  targets: [
    .target(name: "JsonExperiment", dependencies: []),
    .testTarget(name: "JsonExperimentTests", dependencies: ["JsonExperiment"])
  ]
)

./Sources/company.swift:

import Foundation

struct Company : Decodable {
  let name: String
  let catchPhrase: String
  let moto: String
}

./Sources/company_test.swift:

import Testing
import Foundation

@Test
func decodeCompany() {
  let input = """
    {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  """

  let decoded = try JSONDecoder().decode(Company.self, from: input)
}
@AnthonyLatsis
Copy link

AnthonyLatsis commented Jan 2, 2025

Your test file should be in a separate directory named Tests. Try running

swift package init --name MyPackage

inside a new empty directory. The generated files should demonstrate the expected structure of a basic package.

@hamishknight Could you transfer this please?

@hamishknight hamishknight transferred this issue from swiftlang/swift Jan 2, 2025
@jsumners
Copy link
Author

jsumners commented Jan 2, 2025

I think I'm getting close to it with https://github.com/jsumners/swift-experiments/tree/ebeb073deddda038183975caeea9c586eae1d633/json, but now I get an error about the Company type not being in scope.

@jsumners
Copy link
Author

jsumners commented Jan 3, 2025

Managed to trial and error into a working setup -- https://github.com/jsumners/swift-experiments/tree/57c28883c76aeed46151163c2d7dd68ffd461724/json

It would be very helpful if this were documented in a single place.

@plemarquand
Copy link
Contributor

plemarquand commented Jan 3, 2025

@AnthonyLatsis is correct swift package init is the fastest way to generate the minimal required structure of a package, with tests. There is also some more documentation available, though I think it could be improved to call out that tests are expected to live in the Tests folder.

It sounds like you've got something working now, so I'll close this issue out. If you have specific improvements or suggestions please feel free to open a new issue.

@AnthonyLatsis
Copy link

but now I get an error about the Company type not being in scope

Yeah, Company is not a module, so you cannot import it like that. You can either import the parent module like you already have, or use an import specifier like this: import struct Foo.Company. The module qualifier is always required. And we are not doing a great job at diagnosing this, but just import Foo is not enough as you see because Company is internal (the default access level).

@jsumners
Copy link
Author

jsumners commented Jan 3, 2025

I think what ended up making things work is @testable import Foo. Is that doing something to make Foo's symbols available to the external testing package?

@AnthonyLatsis
Copy link

AnthonyLatsis commented Jan 3, 2025

Yes, basically, @testable gives access to internal stuff.

@jsumners
Copy link
Author

jsumners commented Jan 4, 2025

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants