xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Tests / XToolTests / CommandOutputTests.swift
CommandOutputTests.swift131 lines · 5.3 KiBRaw
| 1 | import ConcurrencyExtras |
| 2 | import Dependencies |
| 3 | import Foundation |
| 4 | import Testing |
| 5 | import XUtils |
| 6 | |
| 7 | struct CommandOutputTests { |
| 8 | @Test |
| 9 | func dependencyOutputSupportsStringAndVariadicPrinting() { |
| 10 | struct Output: Equatable, Sendable { |
| 11 | var message: String |
| 12 | var stream: CommandOutput.Stream |
| 13 | var flush: Bool |
| 14 | } |
| 15 | let received = LockIsolated<[Output]>([]) |
| 16 | withDependencies { |
| 17 | $0.out.write = { data, stream, flush in |
| 18 | received.withValue { |
| 19 | $0.append(Output(message: String(decoding: data, as: UTF8.self), stream: stream, flush: flush)) |
| 20 | } |
| 21 | } |
| 22 | } operation: { |
| 23 | d.out.print() |
| 24 | d.out.print("message") |
| 25 | d.out.print("progress", 42, separator: ": ", terminator: "", stream: .stderr, flush: true) |
| 26 | } |
| 27 | #expect(received.value == [ |
| 28 | Output(message: "\n", stream: .stdout, flush: false), |
| 29 | Output(message: "message\n", stream: .stdout, flush: false), |
| 30 | Output(message: "progress: 42", stream: .stderr, flush: true), |
| 31 | ]) |
| 32 | } |
| 33 | |
| 34 | @Test(.timeLimit(.minutes(1))) |
| 35 | func fullNonblockingPipePreservesAllBytes() async throws { |
| 36 | let pipe = Pipe() |
| 37 | let writer = FileDescriptor(rawValue: pipe.fileHandleForWriting.fileDescriptor) |
| 38 | let reader = FileDescriptor(rawValue: pipe.fileHandleForReading.fileDescriptor) |
| 39 | let flags = fcntl(writer.rawValue, F_GETFL) |
| 40 | try #require(flags != -1) |
| 41 | try #require(fcntl(writer.rawValue, F_SETFL, flags | O_NONBLOCK) == 0) |
| 42 | |
| 43 | // Fill the pipe before invoking the output writer, forcing EAGAIN. |
| 44 | let padding = Data(repeating: 255, count: 4096) |
| 45 | var prefix = Data() |
| 46 | while true { |
| 47 | do { |
| 48 | let count = try padding.withUnsafeBytes { try writer.write($0) } |
| 49 | prefix.append(padding.prefix(count)) |
| 50 | } catch let error as Errno where error.rawValue == EAGAIN || error.rawValue == EWOULDBLOCK { |
| 51 | break |
| 52 | } |
| 53 | } |
| 54 | let message = Data([0, 255, 128]) + Data(String(repeating: "output é 🌍\n", count: 100_000).utf8) |
| 55 | let writing = Task.detached { |
| 56 | defer { try? pipe.fileHandleForWriting.close() } |
| 57 | try message.withUnsafeBytes { try writer.writeAllWaiting($0) } |
| 58 | } |
| 59 | // Let the writer encounter the full pipe before draining it. |
| 60 | try await Task.sleep(for: .milliseconds(20)) |
| 61 | let received = try await Task.detached { |
| 62 | var received = Data() |
| 63 | var buffer = [UInt8](repeating: 0, count: 1024) |
| 64 | while true { |
| 65 | let count = try buffer.withUnsafeMutableBytes { try reader.read(into: $0) } |
| 66 | if count == 0 { return received } |
| 67 | received.append(contentsOf: buffer.prefix(count)) |
| 68 | } |
| 69 | }.value |
| 70 | try await writing.value |
| 71 | #expect(received == prefix + message) |
| 72 | #expect(fcntl(reader.rawValue, F_GETFL) != -1) |
| 73 | } |
| 74 | |
| 75 | @Test(.timeLimit(.minutes(1))) |
| 76 | func fullNonblockingPipeWriteCanBeCancelled() async throws { |
| 77 | let pipe = Pipe() |
| 78 | let writer = FileDescriptor(rawValue: pipe.fileHandleForWriting.fileDescriptor) |
| 79 | let flags = fcntl(writer.rawValue, F_GETFL) |
| 80 | try #require(flags != -1) |
| 81 | try #require(fcntl(writer.rawValue, F_SETFL, flags | O_NONBLOCK) == 0) |
| 82 | |
| 83 | let padding = Data(repeating: 255, count: 4096) |
| 84 | while true { |
| 85 | do { |
| 86 | _ = try padding.withUnsafeBytes { try writer.write($0) } |
| 87 | } catch let error as Errno where error.rawValue == EAGAIN || error.rawValue == EWOULDBLOCK { |
| 88 | break |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | let (started, continuation) = AsyncStream.makeStream(of: Never.self) |
| 93 | let finished = LockIsolated(false) |
| 94 | let writing = Task.detached { |
| 95 | defer { |
| 96 | try? pipe.fileHandleForWriting.close() |
| 97 | finished.setValue(true) |
| 98 | } |
| 99 | continuation.finish() |
| 100 | try padding.withUnsafeBytes { try writer.writeAllWaiting($0) } |
| 101 | } |
| 102 | for await _ in started {} |
| 103 | // Give the writer time to enter poll before cancelling, without draining the pipe. |
| 104 | try await Task.sleep(for: .milliseconds(20)) |
| 105 | writing.cancel() |
| 106 | let deadline = ContinuousClock.now.advanced(by: .seconds(2)) |
| 107 | while !finished.value && ContinuousClock.now < deadline { |
| 108 | try await Task.sleep(for: .milliseconds(10)) |
| 109 | } |
| 110 | if !finished.value { |
| 111 | Issue.record("Cancelled writer did not stop while the pipe remained full") |
| 112 | // Unblock a regressed writer so the test fails instead of hanging. |
| 113 | _ = try pipe.fileHandleForReading.readToEnd() |
| 114 | } |
| 115 | await #expect(throws: CancellationError.self) { |
| 116 | try await writing.value |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | func invalidDescriptorThrowsInsteadOfRaisingAnException() { |
| 122 | let descriptor = FileDescriptor(rawValue: -1) |
| 123 | #expect(throws: Errno.badFileDescriptor) { |
| 124 | try Data("output".utf8).withUnsafeBytes { try descriptor.writeAllWaiting($0) } |
| 125 | } |
| 126 | // Empty output does not attempt a system call. |
| 127 | #expect(throws: Never.self) { |
| 128 | try Data().withUnsafeBytes { try descriptor.writeAllWaiting($0) } |
| 129 | } |
| 130 | } |
| 131 | } |