-
Notifications
You must be signed in to change notification settings - Fork 406
Migrate ArgumentParserExampleTests to Swift Testing #949
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
bkhouri
merged 1 commit into
main
from
bkhouri/t/main/migrate_tests_to_swift_testing_batch2
Sep 1, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Sources/ArgumentParserTestHelpers/TestHelpers+SwiftTesting+Tags.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift Argument Parser open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import Testing | ||
|
|
||
| /// A Swift Testing trait that disables a test on platforms where launching a | ||
| /// subprocess via `Foundation.Process` is not supported. | ||
| /// | ||
| /// Apply with `@Test(.requiresProcessExecution)` to tests whose bodies | ||
| /// (transitively) call `requireExecuteCommand`, `expectDumpHelp(command:)`, | ||
| /// `expectGenerateManual`, or `expectGeneratedReference` *and* also compare | ||
| /// the output against a fixed expectation (e.g. via `expectSnapshot`). On | ||
| /// unsupported platforms these helpers return an empty string rather than | ||
| /// throwing, so a downstream snapshot comparison would fail spuriously; the | ||
| /// trait suppresses the test entirely instead. | ||
| extension Trait where Self == Testing.ConditionTrait { | ||
| public static var requiresProcessExecution: Self { | ||
| #if os(Windows) || (canImport(Darwin) && !os(macOS)) | ||
| return .disabled( | ||
| "Process execution is not supported on this platform.") | ||
| #else | ||
| return .enabled(if: true) | ||
| #endif | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| // | ||
| // This source file is part of the Swift Argument Parser open source project | ||
| // | ||
| // Copyright (c) 2020 Apple Inc. and the Swift project authors | ||
| // Copyright (c) 2020-2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
|
|
@@ -11,26 +11,28 @@ | |
|
|
||
| #if os(macOS) | ||
|
|
||
| import XCTest | ||
| import ArgumentParserTestHelpers | ||
| import Foundation | ||
| import Testing | ||
|
|
||
| @testable import ArgumentParser | ||
|
|
||
| final class CountLinesExampleTests: XCTestCase { | ||
| override func setUp() { | ||
| @Suite struct CountLinesExampleTests { | ||
| init() { | ||
| Platform.Environment[.columns] = nil | ||
| } | ||
|
|
||
| func testCountLines() throws { | ||
| @Test func countLines() throws { | ||
| guard #available(macOS 12, *) else { return } | ||
| let testFile = try XCTUnwrap( | ||
| let testFile = try #require( | ||
| Bundle.module.url(forResource: "CountLinesTest", withExtension: "txt")) | ||
| try AssertExecuteCommand( | ||
| try requireExecuteCommand( | ||
| command: "count-lines \(testFile.path)", expected: "20\n") | ||
| try AssertExecuteCommand( | ||
| try requireExecuteCommand( | ||
| command: "count-lines \(testFile.path) --prefix al", expected: "4\n") | ||
| } | ||
|
|
||
| func testCountLinesHelp() throws { | ||
| @Test func countLinesHelp() throws { | ||
| guard #available(macOS 12, *) else { return } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Out of curiosity, why do test test only run on macOS? Should the test, and related binary, be updated to run on all platforms os is the intents to verify Swift Argument Parser with the |
||
| let helpText = """ | ||
| USAGE: count-lines [<input-file>] [--prefix <prefix>] [--verbose] | ||
|
|
@@ -46,7 +48,7 @@ final class CountLinesExampleTests: XCTestCase { | |
|
|
||
|
|
||
| """ | ||
| try AssertExecuteCommand(command: "count-lines -h", expected: helpText) | ||
| try requireExecuteCommand(command: "count-lines -h", expected: helpText) | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Out of curiosity, why do test test only run on macOS? Should the test, and related binary, be updated to run on all platforms os is the intents to verify Swift Argument Parser with the
@available(...)API?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all these Qs, I think the gating factor was what I could get working at the time of setting up these executable tests. So the question is more about whether we can get these executable tests to run on Linux/Windows, there isn't a principled reason not to.