Skip to content
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,11 @@ func testSoftwareVersion() async throws {
}
```

Apply it to a whole suite to cover every test in the type:
Apply it to a whole suite to cover every test in the type (all tests inherit the suite level shared `@TaskLock`):

```swift
@Suite(.subprocess)
// Using `.subprocess` at the suite level you will almost certainly need to also use `.serialized`
@Suite(.serialized, .subprocess)
struct MyCommandTests {
@Test
func testGrep() async throws {
Expand Down
33 changes: 23 additions & 10 deletions Tests/SwiftTesting/SubprocessSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SubprocessTesting

@Suite
struct SubprocessSwiftTests: ~Copyable {
@Test(.subprocessTesting, arguments: 0..<100)
@Test(.subprocess, arguments: 0..<100)
func `mocks can handle parallel testing`(_ count: Int) async throws {
let testFileURL = URL(fileURLWithPath: "/tmp/\(Self.self)-\(UUID().uuidString).txt")

Expand Down Expand Up @@ -35,7 +35,7 @@ struct SubprocessSwiftTests: ~Copyable {
}

@Test(arguments: 0..<5)
func `other testing still works`(_ count: Int) async throws {
func `direct test of command still works`(_ count: Int) async throws {
let testFileURL: URL = {
let url = URL(fileURLWithPath: "/tmp/\(Self.self)-\(UUID().uuidString).txt")

Expand All @@ -49,8 +49,8 @@ struct SubprocessSwiftTests: ~Copyable {
try FileManager.default.removeItem(at: testFileURL)
}

@Test(.subprocessTesting, arguments: ["foo", "bar", "baz"])
func testEcho(_ word: String) async throws {
@Test(.subprocess, arguments: ["foo", "bar", "baz"])
func echo(_ word: String) async throws {
Subprocess.expect(["/bin/echo", word], standardOutput: "\(word)\n".data(using: .utf8))

let output = try await Subprocess.string(for: ["/bin/echo", word])
Expand All @@ -59,8 +59,8 @@ struct SubprocessSwiftTests: ~Copyable {
try Subprocess.verify()
}

@Test(.subprocessTesting)
func testSoftwareVersion() async throws {
@Test(.subprocess)
func `software version`() async throws {
Subprocess.expect(["/usr/bin/sw_vers", "-productVersion"], standardOutput: "15.0\n".data(using: .utf8))

let version = try await Subprocess.string(for: ["/usr/bin/sw_vers", "-productVersion"])
Expand All @@ -70,10 +70,11 @@ struct SubprocessSwiftTests: ~Copyable {
}
}

@Suite(.subprocessTesting)
@Suite(.subprocess)
struct MyCommandTests: ~Copyable {
@Test
func testGrep() async throws {
// must be serialized otherwise concurrent usage will race against the suite shared mock storage
@Test(.serialized, arguments: 0..<10)
func grep(count: Int) async throws {
Subprocess.expect(["/usr/bin/grep", "foo"], standardOutput: "foo bar\n".data(using: .utf8))

let result = try await Subprocess.string(for: ["/usr/bin/grep", "foo"])
Expand All @@ -83,14 +84,26 @@ struct MyCommandTests: ~Copyable {
}

@Test
func testMissingFile() async throws {
func `missing file`() async throws {
let error = NSError(domain: NSPOSIXErrorDomain, code: Int(ENOENT))
Subprocess.expect(["/bin/cat", "/no/such/file"], error: error)

await #expect(throws: (any Error).self) {
try await Subprocess.data(for: ["/bin/cat", "/no/such/file"])
}
}

// this test creates its own isolated mocks separate from the other tests
@Test(.subprocess, arguments: 0..<10)
func `grep returns an error`(count: Int) async throws {
let error = NSError(domain: NSPOSIXErrorDomain, code: Int(ENOENT))
Subprocess.expect(["/usr/bin/grep", "foo"], error: error)

await #expect(throws: (any Error).self) {
try await Subprocess.string(for: ["/usr/bin/grep", "foo"])
}
try Subprocess.verify()
}
}

private extension SubprocessSwiftTests {
Expand Down