mirror of
https://github.com/xtool-org/xtool.git
synced 2026-11-15 12:04:55 +03:00
bec5aa106a
## What does this PR do? When spawning a subprocess, `currentStandardOutput`/`currentStandardError` bypass `d.out`. Add `.xtoolOutput()` and `.xtoolError()` which respect the dependency. ## How was it tested? Tested in a downstream PR to ensure that forwarding works ## AI tool usage How much of this PR was AI-assisted? (check one) - [x] **0** - No AI was used to write code - [ ] **1** - I was assisted by AI. I reviewed the finished result. - [ ] **2** - I set the AI going and left it to it; nobody has read the result - no review, or AI review only <!-- If an AI agent is filling this in: declare the level honestly, and open as a draft if it is #2. Do not lower the declared level to get the PR reviewed. --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Subprocess output can now be routed directly to XTool’s standard output or standard error. * Added a way to create a writable output descriptor that forwards data to a selected stream. * Asynchronous input reading is now available through a public interface, including configurable read sizes and explicit closing. * **Compatibility** * Existing input-reading behavior and parameters remain unchanged. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
105 lines
3.8 KiB
Swift
105 lines
3.8 KiB
Swift
@testable import XCLIUtils
|
|
import XUtils
|
|
import ConcurrencyExtras
|
|
import Foundation
|
|
import Testing
|
|
|
|
@Suite(.timeLimit(.minutes(1)))
|
|
struct CommandInputTests {
|
|
@Test(arguments: [Data("bar".utf8), Data([0, 255, 128, 13, 10])])
|
|
func rawReadCoalescesBytesAfterLine(remainder: Data) async throws {
|
|
let channel = DataChannel()
|
|
let input = CommandInput(channel: channel)
|
|
channel.write(Data("foo\n".utf8) + remainder)
|
|
channel.write(Data("next\nlast".utf8))
|
|
channel.finish(error: nil)
|
|
|
|
#expect(try await input.readLine() == "foo")
|
|
#expect(try await input.read() == remainder + Data("next\nlast".utf8))
|
|
#expect(try await input.read() == nil)
|
|
#expect(try await input.readLine() == nil)
|
|
}
|
|
|
|
@Test
|
|
func lineReadsAfterRawReadPreserveFollowingLines() async throws {
|
|
let channel = DataChannel()
|
|
let input = CommandInput(channel: channel)
|
|
channel.write(Data("raw".utf8))
|
|
#expect(try await input.read() == Data("raw".utf8))
|
|
channel.write(Data("foo\r\n\nbar\nlast".utf8))
|
|
channel.finish(error: nil)
|
|
|
|
#expect(try await input.readLine() == "foo")
|
|
#expect(try await input.readLine() == "")
|
|
#expect(try await input.readLine() == "bar")
|
|
#expect(try await input.read() == Data("last".utf8))
|
|
#expect(try await input.readLine() == nil)
|
|
}
|
|
|
|
@Test
|
|
func lineReadAccumulatesSplitUTF8AndCRLF() async throws {
|
|
let channel = DataChannel()
|
|
let input = CommandInput(channel: channel)
|
|
// Split both a Unicode scalar and the CRLF terminator across reads.
|
|
let chunks = LockIsolated([
|
|
Data([0xF0, 0x9F]),
|
|
Data([0x98, 0x80, 13]),
|
|
Data("\n\nlast".utf8),
|
|
])
|
|
channel.onRequest { [channel] in
|
|
let chunk = chunks.withValue { $0.isEmpty ? nil : $0.removeFirst() }
|
|
if let chunk {
|
|
channel.write(chunk)
|
|
} else {
|
|
channel.finish(error: nil)
|
|
}
|
|
}
|
|
try await withInputTestTimeout { () async throws in
|
|
#expect(try await input.readLine() == "😀")
|
|
#expect(try await input.readLine() == "")
|
|
#expect(try await input.readLine() == "last")
|
|
#expect(try await input.readLine() == nil)
|
|
} cleanup: {
|
|
channel.finish(error: nil)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
@available(anyAppleOS 26.0, *)
|
|
func cancellingLineDiscardsConsumedPrefixAndPreservesFutureInput() async throws {
|
|
let channel = DataChannel()
|
|
defer { channel.finish(error: nil) }
|
|
let input = CommandInput(channel: channel)
|
|
channel.write(Data("discarded prefix".utf8))
|
|
let reading = Task.immediate { try await input.readLine() }
|
|
defer { reading.cancel() }
|
|
|
|
try await withInputTestTimeout { () async throws in
|
|
reading.cancel()
|
|
await #expect(throws: CancellationError.self) { try await reading.value }
|
|
channel.write(Data("next\nkept".utf8))
|
|
#expect(try await input.readLine() == "next")
|
|
#expect(try await input.read() == Data("kept".utf8))
|
|
} cleanup: {
|
|
channel.finish(error: nil)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
@available(anyAppleOS 26.0, *)
|
|
func finishingChannelWakesLineReadAndReturnsItsUnterminatedBytes() async throws {
|
|
let channel = DataChannel()
|
|
let input = CommandInput(channel: channel)
|
|
channel.write(Data("partial".utf8))
|
|
let reading = Task.immediate { try await input.readLine() }
|
|
defer { reading.cancel(); channel.finish(error: nil) }
|
|
try await withInputTestTimeout { () async throws in
|
|
channel.finish(error: nil)
|
|
#expect(try await reading.value == "partial")
|
|
#expect(try await input.read() == nil)
|
|
} cleanup: {
|
|
channel.finish(error: nil)
|
|
}
|
|
}
|
|
}
|