Skip to content

Fix: Generate tests for executable/tool with --enable-swift-testing #9032

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JeffESchmitz
Copy link

Updated InitPackage to handle .executable and .tool types when generating test files with SwiftTesting. Added corresponding tests to verify correct test file content for these package types.

Motivation:

The swift package init command did not generate a Tests directory for executable or tool packages when the --enable-swift-testing flag was used. This was caused by logic in Sources/Workspace/InitPackage.swift that incorrectly skipped test generation for these package types. This change fixes that behavior, ensuring that tests are created as expected.

Modifications:

  • In Sources/Workspace/InitPackage.swift, the writeTests() function was modified to proceed with test generation for .executable and .tool package types.
  • In the same file, the writeTestFileStubs() function was updated to include .executable and .tool types, allowing the creation of standard library-style test files for them.

Result:

After this change, running swift package init --type executable --enable-swift-testing or swift package init --type tool --enable-swift-testing correctly generates a Tests directory with sample test files, aligning the tool's behavior with its intended functionality.

Reproduction and Verification:

Reproducing the Issue (on main branch):

  1. Create a temporary directory: mkdir -p /tmp/test-repro && cd /tmp/test-repro
  2. Run swift package init --type executable --enable-swift-testing.
  3. Verify that no Tests directory is created by running ls -F.

Verifying the Fix (on this branch):

  1. Build the package manager: swift build
  2. Find the built executable: find .build -name swift-package
  3. Create a temporary directory: mkdir -p /tmp/test-fix && cd /tmp/test-fix
  4. Run the newly built package manager to init a project: ../.build/arm64-apple-macosx/debug/swift-package init --type executable --enable-swift-testing (adjust path as needed).
  5. Verify that a Tests directory is now created by running ls -F.

Updated InitPackage to handle .executable and .tool types when generating test files with SwiftTesting. Added corresponding tests to verify correct test file content for these package types.
@MaxDesiatov
Copy link
Contributor

@swift-ci test

@plemarquand
Copy link
Contributor

This patch does the same thing: #8995

I was waiting for CI to get back to green before progressing on it, but if you'd like to take over working on this I'm happy to pass it along.

@JeffESchmitz
Copy link
Author

JeffESchmitz commented Aug 16, 2025

This patch does the same thing: #8995

I was waiting for CI to get back to green before progressing on it, but if you'd like to take over working on this I'm happy to pass it along.

Thanks, @plemarquand — I didn’t realize #8995 was already in progress. After looking closely, your PR already includes the same functional changes I made (allowing tests to be generated for .executable and .tool) and it adds the swift-testing modernization. Sorry for the duplicate effort.

The only delta I see from my side is a small style tweak where I used an exhaustive switch (no default:) to force future case handling.

I’m happy to help land #8995 and can open a PR against your branch for any cleanups you want, or port your test changes into #9032 if you’d prefer consolidating here. I’ll follow your lead, 👍🏻.

@JeffESchmitz JeffESchmitz marked this pull request as draft August 16, 2025 14:21
@plemarquand
Copy link
Contributor

@JeffESchmitz I say you push forward with this PR. I'll give it a review when you unmark it as draft, and once it's merged I'll extract my work to convert the suite to Swift testing in to its own PR. Probably makes sense to break up that work anyway. Thanks!

@@ -660,10 +660,16 @@ public final class InitPackage {
return
}

// Intentionally exhaustive switch without `default` case - forces compilation failure when new
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the long explanatory comment is necessary - this pattern is used dozens of times throughout any decent-sized Swift codebase, and this would be best mentioned once in a style guide vs applied to every one of those locations.

case .library:
case .empty, .buildToolPlugin, .commandPlugin:
break
case .library, .executable, .tool: // Added .executable and .tool here
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, this kind of comment isn't adding any useful information, and could become out of date over time.

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

Successfully merging this pull request may close these issues.

4 participants