Skip to content

Add swift-testing support to swift build --build-tests #7377

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

Merged
merged 4 commits into from
Feb 29, 2024

Conversation

grynspan
Copy link
Contributor

This PR generalizes the enable/disable XCTest/swift-testing options from swift test and swift package init and adds them to swift build --build-tests so that it is possible to build all test content without actually running the tests. If neither flag is specified, the default test content is built (always XCTest, and swift-testing if it's a dependency, same as swift test.)

I don't have unit tests for this work because SwiftPM cannot currently reference or link to swift-testing in CI. I'm open to ideas here.

Resolves #7375.

This PR generalizes the enable/disable XCTest/swift-testing options from
`swift test` and `swift package init` and adds them to `swift build --build-tests`
so that it is possible to build all test content without actually running the
tests. If neither flag is specified, the default test content is built (always
XCTest, and swift-testing if it's a dependency, same as `swift test`.)

I don't have unit tests for this work because SwiftPM cannot currently reference
or link to swift-testing in CI. I'm open to ideas here.

Resolves #7375.
@grynspan grynspan requested a review from weissi February 28, 2024 16:02
@grynspan grynspan self-assigned this Feb 28, 2024
@grynspan grynspan added bug swift build Changes impacting `swift build` swift test Changes impacting `swift test` tool labels Feb 28, 2024
Copy link
Contributor

@weissi weissi left a comment

Choose a reason for hiding this comment

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

I can't claim to fully understand the code but it does look good to me. Thank you!!

@MaxDesiatov MaxDesiatov changed the title Add swift-testing support to swift build --build-tests. Add swift-testing support to swift build --build-tests Feb 28, 2024
@@ -132,6 +148,20 @@ struct SharedOptions: ParsableArguments {
// Default to disabled since swift-testing is experimental (opt-in.)
return false
}

/// Get the set of enabled testing libraries.
func enabledTestingLibraries(swiftCommandState: SwiftCommandState) throws -> Set<BuildParameters.Testing.Library> {
Copy link
Contributor

@MaxDesiatov MaxDesiatov Feb 28, 2024

Choose a reason for hiding this comment

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

Formatting nit: we avoid long lines for better readability in diff views and on devices that don't have huge screens.

Suggested change
func enabledTestingLibraries(swiftCommandState: SwiftCommandState) throws -> Set<BuildParameters.Testing.Library> {
func enabledTestingLibraries(
swiftCommandState: SwiftCommandState
) throws -> Set<BuildParameters.Testing.Library> {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the cutoff you generally prefer? I can set my gutter to that.

Copy link
Contributor

Choose a reason for hiding this comment

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

We have all of the formatting rules specified in the formatter config https://github.com/apple/swift-package-manager/blob/main/.swiftformat. Just run ./Utilities/soundness.sh before committing and that will reformat changed files for you with that config.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That script did not produce any diagnostics nor make any changes to my branch.

@grynspan
Copy link
Contributor Author

@swift-ci please test

@MaxDesiatov MaxDesiatov added the needs tests This change needs test coverage label Feb 28, 2024
Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Would you mind adding a test case to prevent future regressions?

@grynspan
Copy link
Contributor Author

Would you mind adding a test case to prevent future regressions?

See the PR description. How would you implement it?

@grynspan grynspan requested a review from MaxDesiatov February 28, 2024 16:56
@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor Author

@swift-ci please test Windows

@MaxDesiatov
Copy link
Contributor

MaxDesiatov commented Feb 28, 2024

See the PR description. How would you implement it?

I think we need a new BuildSystemProvider that can create a mock build system. Then we can initialize SwiftCommandState with desired options in the test and verify that the mock build system is passed correct build parameters for correct build subsets.

@grynspan
Copy link
Contributor Author

See the PR description. How would you implement it?

I think we need a new BuildSystemProvider that can create a mock build system. Then we can initialize SwiftCommandState with desired options in the test and verify that the mock build system is passed correct build parameters for correct build subsets.

It looks like --build-tests doesn't really have any test coverage right now. I filed #7378 for test coverage for that option.

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor Author

@swift-ci please test Windows

@MaxDesiatov
Copy link
Contributor

It looks like --build-tests doesn't really have any test coverage right now.

I understand, this shouldn't be the reason to reduce our overall test coverage though.

@grynspan
Copy link
Contributor Author

It looks like --build-tests doesn't really have any test coverage right now.

I understand, this shouldn't be the reason to reduce our overall test coverage though.

I don't believe I'm reducing it as this feature already does not have any test coverage, but maybe I'm misunderstanding? 🙂

@weissi
Copy link
Contributor

weissi commented Feb 28, 2024

It looks like --build-tests doesn't really have any test coverage right now.

I understand, this shouldn't be the reason to reduce our overall test coverage though.

I'd argue that right now the feature is untested (bad) but also not working (worse). Why don't we make the situation better by fixing it? Once this is merged (and therefore fixed) we can maybe team up to write at least some basic tests?

@grynspan
Copy link
Contributor Author

It looks like --build-tests doesn't really have any test coverage right now.

I understand, this shouldn't be the reason to reduce our overall test coverage though.

I'd argue that right now the feature is untested (bad) but also not working (worse). Why don't we make the situation better by fixing it? Once this is merged (and therefore fixed) we can maybe team up to write at least some basic tests?

That sounds like an excellent plan!

@grynspan grynspan requested a review from MaxDesiatov February 28, 2024 21:34
@MaxDesiatov MaxDesiatov merged commit 64adc92 into main Feb 29, 2024
@MaxDesiatov MaxDesiatov deleted the jgrynspan/7375-swift-build-swift-testing branch February 29, 2024 08:53
@finagolfin
Copy link
Member

It appears the lack of tests hurt us here: this pull broke swift build --build-tests registering the --enable-test-discovery flag also. My Android CI builds swift-numerics trunk daily with the latest Swift trunk snapshot builds and those flags: it now errors with the March 1 snapshot.

I can reproduce the crash on linux x86_64 with the following commands, which work with the last Feb. 29 trunk snapshot before this pull:

> ../swift-DEVELOPMENT-SNAPSHOT-2024-03-01-a-ubi9/usr/bin/swift build --build-tests --enable-test-discovery
> .build/x86_64-unknown-linux-gnu/debug/swift-numericsPackageTests.xctest

furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
)

This PR generalizes the enable/disable XCTest/swift-testing options from
`swift test` and `swift package init` and adds them to `swift build
--build-tests` so that it is possible to build all test content without
actually running the tests. If neither flag is specified, the default
test content is built (always XCTest, and swift-testing if it's a
dependency, same as `swift test`.)

Resolves swiftlang#7375.
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
)

This PR generalizes the enable/disable XCTest/swift-testing options from
`swift test` and `swift package init` and adds them to `swift build
--build-tests` so that it is possible to build all test content without
actually running the tests. If neither flag is specified, the default
test content is built (always XCTest, and swift-testing if it's a
dependency, same as `swift test`.)

Resolves swiftlang#7375.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs tests This change needs test coverage swift build Changes impacting `swift build` swift test Changes impacting `swift test` tool
Projects
None yet
Development

Successfully merging this pull request may close these issues.

swift build --build-tests doesn't build swift-testing tests
4 participants