Skip to content

Updates to support Swift 6 language version #650

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 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions Sources/ArgumentParser/Utilities/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ extension CommandLine {
/// Accesses the command line arguments in a concurrency-safe way.
///
/// Workaround for https://github.com/apple/swift/issues/66213
static let _staticArguments: [String] =
UnsafeBufferPointer(start: unsafeArgv, count: Int(argc))
.compactMap { String(validatingUTF8: $0!)
}
static let _staticArguments: [String] = Self.arguments
}

#if canImport(Glibc)
import Glibc
@preconcurrency import Glibc
#elseif canImport(Musl)
import Musl
@preconcurrency import Musl
#elseif canImport(Darwin)
import Darwin
#elseif canImport(CRT)
import CRT
@preconcurrency import CRT
#elseif canImport(WASILibc)
import WASILibc
@preconcurrency import WASILibc
#endif

enum Platform {}
Expand Down
28 changes: 14 additions & 14 deletions Sources/ArgumentParserTestHelpers/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extension XCTestExpectation {
public func AssertResultFailure<T, U: Error>(
_ expression: @autoclosure () -> Result<T, U>,
_ message: @autoclosure () -> String = "",
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line)
{
switch expression() {
Expand All @@ -89,7 +89,7 @@ public func AssertResultFailure<T, U: Error>(
}
}

public func AssertErrorMessage<A>(_ type: A.Type, _ arguments: [String], _ errorMessage: String, file: StaticString = #file, line: UInt = #line) where A: ParsableArguments {
public func AssertErrorMessage<A>(_ type: A.Type, _ arguments: [String], _ errorMessage: String, file: StaticString = #filePath, line: UInt = #line) where A: ParsableArguments {
do {
_ = try A.parse(arguments)
XCTFail("Parsing should have failed.", file: file, line: line)
Expand All @@ -99,7 +99,7 @@ public func AssertErrorMessage<A>(_ type: A.Type, _ arguments: [String], _ error
}
}

public func AssertFullErrorMessage<A>(_ type: A.Type, _ arguments: [String], _ errorMessage: String, file: StaticString = #file, line: UInt = #line) where A: ParsableArguments {
public func AssertFullErrorMessage<A>(_ type: A.Type, _ arguments: [String], _ errorMessage: String, file: StaticString = #filePath, line: UInt = #line) where A: ParsableArguments {
do {
_ = try A.parse(arguments)
XCTFail("Parsing should have failed.", file: (file), line: line)
Expand All @@ -109,7 +109,7 @@ public func AssertFullErrorMessage<A>(_ type: A.Type, _ arguments: [String], _ e
}
}

public func AssertParse<A>(_ type: A.Type, _ arguments: [String], file: StaticString = #file, line: UInt = #line, closure: (A) throws -> Void) where A: ParsableArguments {
public func AssertParse<A>(_ type: A.Type, _ arguments: [String], file: StaticString = #filePath, line: UInt = #line, closure: (A) throws -> Void) where A: ParsableArguments {
do {
let parsed = try type.parse(arguments)
try closure(parsed)
Expand All @@ -119,7 +119,7 @@ public func AssertParse<A>(_ type: A.Type, _ arguments: [String], file: StaticSt
}
}

public func AssertParseCommand<A: ParsableCommand>(_ rootCommand: ParsableCommand.Type, _ type: A.Type, _ arguments: [String], file: StaticString = #file, line: UInt = #line, closure: (A) throws -> Void) {
public func AssertParseCommand<A: ParsableCommand>(_ rootCommand: ParsableCommand.Type, _ type: A.Type, _ arguments: [String], file: StaticString = #filePath, line: UInt = #line, closure: (A) throws -> Void) {
do {
let command = try rootCommand.parseAsRoot(arguments)
guard let aCommand = command as? A else {
Expand All @@ -136,7 +136,7 @@ public func AssertParseCommand<A: ParsableCommand>(_ rootCommand: ParsableComman
public func AssertEqualStrings(
actual: String,
expected: String,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) {
// If the input strings are not equal, create a simple diff for debugging...
Expand Down Expand Up @@ -207,7 +207,7 @@ public func AssertHelp<T: ParsableArguments>(
for _: T.Type,
columns: Int? = 80,
equals expected: String,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) {
let flag: String
Expand Down Expand Up @@ -246,7 +246,7 @@ public func AssertHelp<T: ParsableCommand, U: ParsableCommand>(
root _: U.Type,
columns: Int? = 80,
equals expected: String,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) {
let includeHidden: Bool
Expand All @@ -271,7 +271,7 @@ public func AssertHelp<T: ParsableCommand, U: ParsableCommand>(

public func AssertDump<T: ParsableArguments>(
for _: T.Type, equals expected: String,
file: StaticString = #file, line: UInt = #line
file: StaticString = #filePath, line: UInt = #line
) throws {
do {
_ = try T.parse(["--experimental-dump-help"])
Expand All @@ -284,7 +284,7 @@ public func AssertDump<T: ParsableArguments>(
try AssertJSONEqualFromString(actual: T._dumpHelp(), expected: expected, for: ToolInfoV0.self, file: file, line: line)
}

public func AssertJSONEqualFromString<T: Codable & Equatable>(actual: String, expected: String, for type: T.Type, file: StaticString = #file, line: UInt = #line) throws {
public func AssertJSONEqualFromString<T: Codable & Equatable>(actual: String, expected: String, for type: T.Type, file: StaticString = #filePath, line: UInt = #line) throws {
if #available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) {
AssertEqualStrings(actual: actual, expected: expected, file: file, line: line)
}
Expand All @@ -309,7 +309,7 @@ extension XCTest {
command: String,
expected: String? = nil,
exitCode: ExitCode = .success,
file: StaticString = #file, line: UInt = #line) throws
file: StaticString = #filePath, line: UInt = #line) throws
{
try AssertExecuteCommand(
command: command.split(separator: " ").map(String.init),
Expand All @@ -323,7 +323,7 @@ extension XCTest {
command: [String],
expected: String? = nil,
exitCode: ExitCode = .success,
file: StaticString = #file, line: UInt = #line) throws
file: StaticString = #filePath, line: UInt = #line) throws
{
#if os(Windows)
throw XCTSkip("Unsupported on this platform")
Expand Down Expand Up @@ -385,7 +385,7 @@ extension XCTest {
public func AssertJSONOutputEqual(
command: String,
expected: String,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) throws {
#if os(Windows)
Expand Down Expand Up @@ -439,7 +439,7 @@ extension XCTest {
multiPage: Bool,
command: String,
expected: String,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) throws {
#if os(Windows)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ actor AsyncStatusCheck {
}
}

@MainActor
var statusCheck = AsyncStatusCheck()

// MARK: AsyncParsableCommand.main() testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fileprivate struct Foo: ParsableCommand {
}
}

fileprivate func AssertParseFooCommand<A>(_ type: A.Type, _ arguments: [String], file: StaticString = #file, line: UInt = #line, closure: (A) throws -> Void) where A: ParsableCommand {
fileprivate func AssertParseFooCommand<A>(_ type: A.Type, _ arguments: [String], file: StaticString = #filePath, line: UInt = #line, closure: (A) throws -> Void) where A: ParsableCommand {
AssertParseCommand(Foo.self, type, arguments, file: file, line: line, closure: closure)
}

Expand Down
14 changes: 7 additions & 7 deletions Tests/ArgumentParserEndToEndTests/SubcommandEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ extension SubcommandEndToEndTests {
}
}

fileprivate var mathDidRun = false

fileprivate struct Math: ParsableCommand {
enum Operation: String, ExpressibleByArgument {
case add
Expand All @@ -127,20 +125,22 @@ fileprivate struct Math: ParsableCommand {
@Argument(help: "The first operand")
var operands: [Int] = []

var didRun = false

mutating func run() {
XCTAssertEqual(operation, .multiply)
XCTAssertTrue(verbose)
XCTAssertEqual(operands, [5, 11])
mathDidRun = true
didRun = true
}
}

extension SubcommandEndToEndTests {
func testParsing_SingleCommand() throws {
var mathCommand = try Math.parseAsRoot(["--operation", "multiply", "-v", "5", "11"])
XCTAssertFalse(mathDidRun)
try mathCommand.run()
XCTAssertTrue(mathDidRun)
var mathCommand = try Math.parseAsRoot(["--operation", "multiply", "-v", "5", "11"]) as! Math
XCTAssertFalse(mathCommand.didRun)
mathCommand.run()
XCTAssertTrue(mathCommand.didRun)
}
}

Expand Down
16 changes: 8 additions & 8 deletions Tests/ArgumentParserEndToEndTests/TransformEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ extension Convert {

fileprivate struct FooOption: Convert, ParsableArguments {

static var usageString: String = """
static let usageString: String = """
Usage: foo_option --string <int_str>
See 'foo_option --help' for more information.
"""
static var help: String = "Help: --string <int_str> Convert string to integer\n"
static let help: String = "Help: --string <int_str> Convert string to integer\n"

@Option(help: ArgumentHelp("Convert string to integer", valueName: "int_str"),
transform: { try convert($0) })
Expand All @@ -49,11 +49,11 @@ fileprivate struct FooOption: Convert, ParsableArguments {

fileprivate struct BarOption: Convert, ParsableCommand {

static var usageString: String = """
static let usageString: String = """
Usage: bar-option [--strings <int_str> ...]
See 'bar-option --help' for more information.
"""
static var help: String = "Help: --strings <int_str> Convert a list of strings to an array of integers\n"
static let help: String = "Help: --strings <int_str> Convert a list of strings to an array of integers\n"

@Option(help: ArgumentHelp("Convert a list of strings to an array of integers", valueName: "int_str"),
transform: { try convert($0) })
Expand Down Expand Up @@ -99,11 +99,11 @@ extension TransformEndToEndTests {

fileprivate struct FooArgument: Convert, ParsableArguments {

static var usageString: String = """
static let usageString: String = """
Usage: foo_argument <int_str>
See 'foo_argument --help' for more information.
"""
static var help: String = "Help: <int_str> Convert string to integer\n"
static let help: String = "Help: <int_str> Convert string to integer\n"

enum FooError: Error {
case outOfBounds
Expand All @@ -116,11 +116,11 @@ fileprivate struct FooArgument: Convert, ParsableArguments {

fileprivate struct BarArgument: Convert, ParsableCommand {

static var usageString: String = """
static let usageString: String = """
Usage: bar-argument [<int_str> ...]
See 'bar-argument --help' for more information.
"""
static var help: String = "Help: <int_str> Convert a list of strings to an array of integers\n"
static let help: String = "Help: <int_str> Convert a list of strings to an array of integers\n"

@Argument(help: ArgumentHelp("Convert a list of strings to an array of integers", valueName: "int_str"),
transform: { try convert($0) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ fileprivate enum UserValidationError: LocalizedError {
}

fileprivate struct Foo: ParsableArguments {
static var usageString: String = """
static let usageString: String = """
Usage: foo [--count <count>] [<names> ...] [--version] [--throw]
See 'foo --help' for more information.
"""

static var helpString: String = """
static let helpString: String = """
USAGE: foo [--count <count>] [<names> ...] [--version] [--throw]

ARGUMENTS:
Expand Down
2 changes: 1 addition & 1 deletion Tests/ArgumentParserPackageManagerTests/HelpTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct Simple: ParsableArguments {
@Option() var min: Int?
@Argument() var max: Int

static var helpText = """
static let helpText = """
USAGE: simple [--verbose] [--min <min>] <max>

ARGUMENTS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension Package {
}

extension Package.Config {
public static var configuration = CommandConfiguration(
public static let configuration = CommandConfiguration(
subcommands: [GetMirror.self, SetMirror.self, UnsetMirror.self])

/// Print mirror configuration for the given package dependency
Expand Down
2 changes: 1 addition & 1 deletion Tests/ArgumentParserUnitTests/CompletionScriptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ extension CompletionScriptTests {
func verifyCustomOutput(
_ arg: String,
expectedOutput: String,
file: StaticString = #file, line: UInt = #line
file: StaticString = #filePath, line: UInt = #line
) throws {
do {
_ = try Custom.parse(["---completion", "--", arg])
Expand Down
7 changes: 1 addition & 6 deletions Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ import XCTest
import ArgumentParserTestHelpers
@testable import ArgumentParser

final class DumpHelpGenerationTests: XCTestCase {
public static let allTests = [
("testDumpExampleCommands", testDumpExampleCommands),
("testDumpA", testDumpA)
]
}
final class DumpHelpGenerationTests: XCTestCase {}

extension DumpHelpGenerationTests {
struct A: ParsableCommand {
Expand Down
8 changes: 4 additions & 4 deletions Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ extension HelpGenerationTests {
struct H: ParsableCommand {
struct CommandWithVeryLongName: ParsableCommand {}
struct ShortCommand: ParsableCommand {
static var configuration: CommandConfiguration = CommandConfiguration(abstract: "Test short command name.")
static let configuration: CommandConfiguration = CommandConfiguration(abstract: "Test short command name.")
}
struct AnotherCommandWithVeryLongName: ParsableCommand {
static var configuration: CommandConfiguration = CommandConfiguration(abstract: "Test long command name.")
static let configuration: CommandConfiguration = CommandConfiguration(abstract: "Test long command name.")
}
struct AnotherCommand: ParsableCommand {
@Option()
Expand Down Expand Up @@ -474,7 +474,7 @@ extension HelpGenerationTests {
}

struct Foo: ParsableCommand {
public static var configuration = CommandConfiguration(
public static let configuration = CommandConfiguration(
commandName: "foo",
abstract: "Perform some foo",
subcommands: [
Expand Down Expand Up @@ -681,7 +681,7 @@ extension HelpGenerationTests {
struct AllValues: ParsableCommand {
enum Manual: Int, ExpressibleByArgument {
case foo
static var allValueStrings = ["bar"]
static let allValueStrings = ["bar"]
}

enum UnspecializedSynthesized: Int, CaseIterable, ExpressibleByArgument {
Expand Down
4 changes: 2 additions & 2 deletions Tests/ArgumentParserUnitTests/NameSpecificationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ extension NameSpecificationTests {
}
}

fileprivate func Assert(nameSpecification: NameSpecification, key: String, parent: InputKey? = nil, makeNames expected: [Name], file: StaticString = #file, line: UInt = #line) {
fileprivate func Assert(nameSpecification: NameSpecification, key: String, parent: InputKey? = nil, makeNames expected: [Name], file: StaticString = #filePath, line: UInt = #line) {
let names = nameSpecification.makeNames(InputKey(name: key, parent: parent))
Assert(names: names, expected: expected, file: file, line: line)
}

fileprivate func Assert<N>(names: [N], expected: [N], file: StaticString = #file, line: UInt = #line) where N: Equatable {
fileprivate func Assert<N>(names: [N], expected: [N], file: StaticString = #filePath, line: UInt = #line) where N: Equatable {
names.forEach {
XCTAssert(expected.contains($0), "Unexpected name '\($0)'.", file: (file), line: line)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/ArgumentParserUnitTests/SplitArgumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension ArgumentParser.SplitArguments.InputIndex: Swift.ExpressibleByIntegerLi
}
}

private func AssertIndexEqual(_ sut: SplitArguments, at index: Int, inputIndex: Int, subIndex: SplitArguments.SubIndex, file: StaticString = #file, line: UInt = #line) {
private func AssertIndexEqual(_ sut: SplitArguments, at index: Int, inputIndex: Int, subIndex: SplitArguments.SubIndex, file: StaticString = #filePath, line: UInt = #line) {
guard index < sut.elements.endIndex else {
XCTFail("Element index \(index) is out of range. sur only has \(sut.elements.count) elements.", file: (file), line: line)
return
Expand All @@ -34,7 +34,7 @@ private func AssertIndexEqual(_ sut: SplitArguments, at index: Int, inputIndex:
}
}

private func AssertElementEqual(_ sut: SplitArguments, at index: Int, _ element: SplitArguments.Element.Value, file: StaticString = #file, line: UInt = #line) {
private func AssertElementEqual(_ sut: SplitArguments, at index: Int, _ element: SplitArguments.Element.Value, file: StaticString = #filePath, line: UInt = #line) {
guard index < sut.elements.endIndex else {
XCTFail("Element index \(index) is out of range. sur only has \(sut.elements.count) elements.", file: (file), line: line)
return
Expand Down
Loading