mirror of
https://github.com/xtool-org/xtool.git
synced 2026-11-15 03:54:55 +03:00
3e0c11d967
## What does this PR do? Adds `Dependency`s for console IO. Also fixes #268 by explicitly handling non-pipe input. ## How was it tested? Ran on macOS + Linux in Docker. I have a future PR that overrides the dependencies in certain cases, which also appears to work correctly. ## AI tool usage How much of this PR was AI-assisted? (check one) - [ ] **0** - No AI was used to write code - [x] **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 Did not scrutinize the tests too much tbh but they look reasonable. Easy enough to update/remove them in the future if needed. <!-- 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** * Added support for reading command-line input, including asynchronous prompts, line-based responses, and graceful handling of end-of-input. * Added coordinated cleanup when commands finish. * **Improvements** * Command messages across authentication, device management, installation, SDK, and other workflows now use consistent output handling; displayed messages and command behavior remain unchanged. * Improved handling of complete output when the system temporarily cannot accept more data. * **Tests** * Added coverage for command input and output, including stream behavior, cancellation, and shutdown. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
104 lines
3.8 KiB
Swift
104 lines
3.8 KiB
Swift
@testable import XCLIUtils
|
|
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)
|
|
}
|
|
}
|
|
}
|