Skip to content
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

Feature/swift testing #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ jobs:
fetch-depth: 0 # until https://github.com/actions/checkout/issues/1471 fixed
- uses: swift-actions/setup-swift@v2
with:
swift-version: '5.10'
swift-version: '6.0'
# https://www.swift.org/download/
- name: Build
run: swift build -v
working-directory: getargv-swift
Expand Down
2 changes: 1 addition & 1 deletion .spi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ builder:
configs:
- documentation_targets: [ SwiftGetargv, Cgetargv ]
platform: macosSpm
swift_version: '5.10'
swift_version: '6.0'
20 changes: 19 additions & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "962efd74b56b59bb8cdeb4ecfc2c8b4570ec41f2408fd1ffbb8a6c271e5c3e90",
"originHash" : "b1e697511022260581e726112f485afd54217c065d6e3a59d58cab2ce29762fc",
"pins" : [
{
"identity" : "swift-argument-parser",
Expand Down Expand Up @@ -27,6 +27,24 @@
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-syntax.git",
"state" : {
"revision" : "06b5cdc432e93b60e3bdf53aff2857c6b312991a",
"version" : "600.0.0-prerelease-2024-07-30"
}
},
{
"identity" : "swift-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-testing",
"state" : {
"revision" : "6155e83dbaa2696d13bcd0079665b6a6daaf6dc8",
"version" : "0.11.0"
}
}
],
"version" : 3
Expand Down
87 changes: 61 additions & 26 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,67 @@
// swift-tools-version:5.10
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Cgetargv",
platforms: [.macOS(.v10_13)],
products: [
.library(name: "SwiftGetargv", targets: ["SwiftGetargv"]),
.library(name: "Cgetargv", targets: ["Cgetargv"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
],
targets: [
.executableTarget(name: "getargv2", dependencies: [ "SwiftGetargv", .product(
name: "ArgumentParser",
package: "swift-argument-parser"
)]),
.executableTarget(name: "getargv", dependencies: [ "Cgetargv", .product(
name: "ArgumentParser",
package: "swift-argument-parser"
)]),
.target(name: "SwiftGetargv", dependencies: [ "Cgetargv" ]),
.systemLibrary(name: "Cgetargv", pkgConfig: "getargv", providers: [ .brew(["libgetagv"]) ]),
.testTarget(name: "SwiftGetargvTests", dependencies: [ "SwiftGetargv" ]),
.testTarget(name: "CgetargvTests", dependencies: [ "Cgetargv" ])
],
cLanguageStandard: .c99 // https://developer.apple.com/documentation/packagedescription/clanguagestandard
name: "Cgetargv",
platforms: [.macOS(.v10_15)],
products: [
.library(name: "SwiftGetargv", targets: ["SwiftGetargv"]),
.library(name: "Cgetargv", targets: ["Cgetargv"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
.package(url: "https://github.com/swiftlang/swift-testing", from: "0.11.0"),
],
targets: [
.executableTarget(
name: "getargv2",
dependencies: [
"SwiftGetargv",
.product(
name: "ArgumentParser",
package: "swift-argument-parser"
)
]
),
.executableTarget(
name: "getargv",
dependencies: [
"Cgetargv",
.product(
name: "ArgumentParser",
package: "swift-argument-parser"
)
]
),
.target(name: "SwiftGetargv", dependencies: [ "Cgetargv" ]),
.systemLibrary(
name: "Cgetargv",
pkgConfig: "getargv",
providers: [ .brew(["libgetagv"]) ]
),
.testTarget(
name: "SwiftGetargvTests",
dependencies: [
"SwiftGetargv",
.product(
name: "Testing",
package: "swift-testing"
)
]
),
.testTarget(
name: "CgetargvTests",
dependencies: [
"Cgetargv",
.product(
name: "Testing",
package: "swift-testing"
)
]
)
],
cLanguageStandard: .c99 // https://developer.apple.com/documentation/packagedescription/clanguagestandard
)
39 changes: 17 additions & 22 deletions Tests/CgetargvTests/Test.swift
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
import Foundation
import XCTest
import Testing

@testable import Cgetargv

final class CgetargvTests: XCTestCase {
func testGetArgvOfPid() {
let options = GetArgvOptions(skip: 0, pid: getpid(), nuls: true)
var res = ArgvResult()
XCTAssert(withUnsafePointer(to: options, { get_argv_of_pid($0, &res) }))

let expectedOutput = ProcessInfo.processInfo.arguments.joined(separator: " ")
let actualOutput = String(cString: res.start_pointer)

XCTAssertEqual(actualOutput, expectedOutput, "The Args are not correct.")
}
@Suite()
struct CgetargvTests {

func testGetArgvOfPidWithNuls() {
let options = GetArgvOptions(skip: 0, pid: getpid(), nuls: false)
@Test("test getArgvOfPid NUL replacement", arguments: zip([true, false], [" ", "\0"]))
func GetArgvOfPidNuls(nuls: Bool, separator: String) throws {
let options = GetArgvOptions(skip: 0, pid: getpid(), nuls: nuls)
var res = ArgvResult()
XCTAssert(withUnsafePointer(to: options, { get_argv_of_pid($0, &res) }))
try #require(withUnsafePointer(to: options, { get_argv_of_pid($0, &res) }))

let expectedOutput = ProcessInfo.processInfo.arguments.flatMap { $0.utf8CString }
let actualOutput = Array(
UnsafeBufferPointer<CChar>(start: res.start_pointer!, count: res.end_pointer - res.start_pointer + 1))
let expectedOutput = try #require(ProcessInfo.processInfo.arguments.joined(separator: separator).data(using: .nonLossyASCII))

let actualOutput = Data(
buffer: UnsafeBufferPointer<CChar>(start: res.start_pointer!, count: res.end_pointer - res.start_pointer + 1)
)

XCTAssertEqual(actualOutput, expectedOutput, "The Args are not correct.")
#expect(actualOutput == expectedOutput)
}

func testGetArgvAndArgcOfPid() {
@Test("test getArgvAndArgcOfPid")
func testGetArgvAndArgcOfPid() throws {
let pid = getpid()
var res = ArgvArgcResult()
XCTAssert(get_argv_and_argc_of_pid(pid, &res))
try #require(get_argv_and_argc_of_pid(pid, &res))

let expectedOutput = ProcessInfo.processInfo.arguments
let actualOutput = Array(
UnsafeBufferPointer<UnsafeMutablePointer<CChar>?>(start: res.argv, count: Int(res.argc))
).map { String(cString: $0!) }

XCTAssertEqual(actualOutput, expectedOutput, "The Args are not correct.")
#expect(actualOutput == expectedOutput)
}
}
44 changes: 16 additions & 28 deletions Tests/SwiftGetargvTests/Test.swift
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
import Foundation
import XCTest
import Testing

@testable import SwiftGetargv

final class SwiftGetargvTests: XCTestCase {
func testGetArgvOfPid() {
let expectedOutput = CommandLine.arguments.joined(separator: " ").utf8CString
@Suite()
struct SwiftGetargvTests {

switch getArgvOfPid(pid: getpid(), nuls: true) {
case .success(let actualOutput):
XCTAssertEqual(actualOutput.array, Array(expectedOutput), "The Args are not correct.")
case .failure(let error):
XCTFail("getArgvOfPid failed with \(error)")
}
}

func testGetArgvOfPidWithNuls() {
let expectedOutput = CommandLine.arguments.flatMap { $0.utf8CString }
@available(macOS 11.0, *)
@Test("test getArgvOfPid NUL replacement", arguments: zip([true, false], [" ", "\0"]))
func GetArgvOfPidNuls(nuls: Bool, separator: String) throws {
let expectedOutput = CommandLine.arguments.joined(separator: separator).utf8CString
//let expectedOutput = CommandLine.arguments.flatMap { $0.utf8CString }

switch getArgvOfPid(pid: getpid()) {
case .success(let actualOutput):
XCTAssertEqual(actualOutput.array, expectedOutput, "The Args are not correct.")
case .failure(let error):
XCTFail("getArgvOfPid failed with \(error)")
let actualOutput = try #require(try getArgvOfPid(pid: getpid(), nuls: nuls).get())
#expect(actualOutput.array == Array(expectedOutput))
}
}

func testGetArgvAndArgcOfPid() {
let expectedOutput = CommandLine.arguments
@available(macOS 11.0, *)
@Test("test getArgvAndArgcOfPid")
func testGetArgvAndArgcOfPid() throws {
let expectedOutput = CommandLine.arguments

switch getArgvAndArgcOfPid(pid: getpid(), encoding: String.Encoding.nonLossyASCII) {
case .success(let actualOutput):
XCTAssertEqual(actualOutput, expectedOutput, "The Args are not correct.")
case .failure(let error):
XCTFail("getArgvAndArgcOfPid failed with \(error)")
let actualOutput = try #require(try getArgvAndArgcOfPid(pid: getpid(), encoding: String.Encoding.nonLossyASCII).get())
#expect(actualOutput == expectedOutput)
}
}
}
Loading