Skip to content

Conversation

JeffESchmitz
Copy link
Contributor

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
Contributor 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for pointing that out, @jakepetroules.

My verbose comment was meant to capture my thought process—I probably should have included it in the PR description or removed it altogether.

I’ll go ahead and remove that inline comment and rely on the style guide to document the exhaustive‑switch pattern. Thanks again for catching that.

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.

The exhaustive-switch pattern is self-evident here and common across the codebase.
This comment adds noise rather than clarity, so it's better removed and documented
via a style guide if needed
@plemarquand
Copy link
Contributor

@swift-ci please test

@JeffESchmitz JeffESchmitz marked this pull request as ready for review August 20, 2025 16:38
@JeffESchmitz
Copy link
Contributor Author

JeffESchmitz commented Aug 21, 2025

The Windows Swift Test build errors are puzzling. What is SimpleTests and where is that within this repo?

0:24:49      [5/11] Compiling Simple Simple.swift

10:24:49      [6/12] Compiling SimpleTests SwiftTests.swift

10:24:49      C:\Users\ContainerAdministrator\AppData\Local\Temp\Miscellaneous_TestDiscovery_Simple.hs68qY\Miscellaneous_TestDiscovery_Simple\Tests\SimpleTests\SwiftTests.swift:2:18: 
error: module 'Simple' was not compiled for testing

https://ci-external.swift.org/job/pr-swiftpm-windows-self-hosted/1880/console

@plemarquand, any ideas?

@plemarquand
Copy link
Contributor

@swift-ci test windows

@plemarquand
Copy link
Contributor

@JeffESchmitz odd, that doesn't seem related. I've rekicked a build.

@plemarquand
Copy link
Contributor

@swift-ci test windows

@JeffESchmitz
Copy link
Contributor Author

Thanks @plemarquand for kicking off the Windows build. This PR should be ready for another review.

Does it need anyone else to review and approve before it can be merged to main?

@plemarquand
Copy link
Contributor

@JeffESchmitz Looks good to me, thanks for all your help!

@plemarquand plemarquand merged commit 339afc8 into swiftlang:main Aug 27, 2025
6 checks passed
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