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

Teach swift package add-target --type test about swift-testing #7481

Merged
merged 3 commits into from May 8, 2024

Conversation

DougGregor
Copy link
Member

Introduce a command-line argument --testing-library to the add-target
command to specify which test library to generate the test for. This
can be 'xctest' (the prior XCTest behavior), 'swift-testing' (to use the
new swift-testing library), or 'none' (for no test library at all).

For the new swift-testing generation, also add the appropriate package
and test target dependency, along with a stub testsuite to start from.

Fixes #7478

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

Copy link
Contributor

@grynspan grynspan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this! I understand your motivation in wanting to use a single option here, however splitting it into two flags is important for a few reasons I listed in comments.

Thanks again!

@@ -60,6 +60,9 @@ extension SwiftPackageCommand {
@Option(help: "The checksum for a remote binary target")
var checksum: String?

@Option(help: "The testing library to use when generating test targets, which can be one of 'xctest', 'swift-testing', or 'none'")
var testingLibrary: PackageModelSyntax.AddTarget.TestHarness = .default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please break this up into two flags, --enable/disable-xctest and --enable/disable-experimental-swift-testing:

  1. It would be consistent with the flags we've added to other commands;
  2. swift-testing is experimental only;
  3. The default behaviour if neither is specified should be to add support for XCTest only;
  4. In the future, we can always change the default if needed; and
  5. It is valid to specify both libraries as dependencies for a test target.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you are proposing is a confusing design for functionality that is adding a brand-new test target. Roughly nobody is going to create a new test target that uses both of these testing libraries: they'll pick one and stick with it.

Other commands need to deal with one or both libraries being present, because existing test targets likely use XCTest and will want to pick up Swift Testing. This functionality is not for those use cases.

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

)
}

private static var defaultSwiftTestingVersionForTestTemplate: Version {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not have this at all and solely use config.json, swiftSyntaxVersionForMacroTemplate should also be removed but we can at least not add another.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this was driven by a failure in Linux CI in prior iterations of this patch, where we were getting a config.json from an older toolchain. It seemed like a reasonable policy that we would support at least slightly-older toolchains, so the default bridges that gap. If we want to be strict about this, we'll need to track down the configuration that produced this failure.

)
""",
expectedManifest: """
// swift-tools-version: 5.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does 5.5 actually come from 🤔?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5.5 introduced some tweaks to the package manifest format (e.g., some things moved to labels), such as introducing new package dependencies and deprecating some old ones. We can either write a bunch of code to deal with all of the differences, or we can limit the functionality to 5.5 or newer. The latter was easier.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swift-testing is going to need a manifest version of 6.0 or later at some point in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm not sure if that affects its clients—I assume not?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if we end up changing the manifest format in some way for swift-testing, we'll need make changes here as well. We can enforce a 6.0 manifest or help with updating the manifest.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clients of a package are not affected by the package manifest version of the package, other than the fact that all consumers of the client will need new-enough tools.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sounds good. :)

@Suite
struct \(raw: target.name)Tests {
@Test("\(raw: target.name) tests")
func tests() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistic suggestion: I recommend naming the func something not prefixed by the word "test", like example(). The @Test attribute already marks it as a test so having that prefix can feel redundant. (I realize this was just adapted from the prior XCTest template, of course!)

(If you take this suggestion, I think a unit test below will need to be adjusted)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll drop the test prefix for swift-testing.

{"version":1,"swiftSyntaxVersionForMacroTemplate":{"major":600,"minor":0,"patch":0, "prereleaseIdentifier":"latest"}}
{"version":1,
"swiftSyntaxVersionForMacroTemplate":{"major":600,"minor":0,"patch":0, "prereleaseIdentifier":"latest"},
"swiftTestingVersionForTestTemplate":{"major":0,"minor":7,"patch":0}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We recently released tag 0.8.0 so you can bump this, as well as another place I saw in this PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll bump in both places, thanks!

Copy link
Contributor

@stmontgomery stmontgomery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple comments but this largely looks good to me from the perspective of a swift-testing maintainer! Thank you

Introduce a command-line argument `--testing-library` to the add-target
command to specify which test library to generate the test for. This
can be 'xctest' (the prior XCTest behavior), 'swift-testing' (to use the
new swift-testing library), or 'none' (for no test library at all).

For the new swift-testing generation, also add the appropriate package
and test target dependency, along with a stub testsuite to start from.

Fixes apple#7478
Make the conformance to Codable explicit so we can swap in the
default swift-testing package version when it wasn't present in the
config file.
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor DougGregor dismissed grynspan’s stale review May 8, 2024 05:33

Discussed offline

@DougGregor DougGregor enabled auto-merge (squash) May 8, 2024 05:33
@DougGregor
Copy link
Member Author

@swift-ci please test Linux

@DougGregor
Copy link
Member Author

apple/swift#73507

@swift-ci please test Linux

@DougGregor DougGregor merged commit a37631a into apple:main May 8, 2024
5 checks passed
@DougGregor DougGregor deleted the add-target-swift-testing branch May 8, 2024 13:36
DougGregor added a commit to DougGregor/swift-package-manager that referenced this pull request May 8, 2024
…#7481)

Introduce a command-line argument `--testing-library` to the add-target
command to specify which test library to generate the test for. This
can be 'xctest' (the prior XCTest behavior), 'swift-testing' (to use the
new swift-testing library), or 'none' (for no test library at all).

For the new swift-testing generation, also add the appropriate package
and test target dependency, along with a stub testsuite to start from.

Fixes apple#7478

(cherry picked from commit a37631a)
DougGregor added a commit that referenced this pull request May 8, 2024
…7481) (#7537)

**Explanation**: Introduce a command-line argument `--testing-library`
to the new add-target command to specify which test library to generate
the test for. This can be 'xctest' (the prior XCTest behavior),
'swift-testing' (to use the new swift-testing library), or 'none' (for
no test library at all).
**Original PR**:
#7481
**Risk**: Low. Extends new functionality in a new package command.
**Testing**: New tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AddTarget incorrectly assumes an XCTest dependency.
5 participants