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
19 changes: 19 additions & 0 deletions Sources/ShellKit/IO/InputSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ public struct InputSource: Sendable {
await cursor.readLine()
}

/// True when this source is the canonical ``empty`` instance —
/// the marker a shell binds when no pipe or redirect feeds a
/// command.
///
/// Commands that emulate tools with "read stdin OR walk the cwd"
/// behaviour (ripgrep's no-path default) need to know whether
/// the embedder attached real input. The host process's fd 0
/// can't answer that for an embedded shell; the binding can:
/// `.empty` means "nothing attached", anything else was bound
/// on purpose.
///
/// Identity-based: only the shared ``empty`` instance (and its
/// copies) answer true. A manually built zero-byte stream is
/// still "attached input that happens to be empty" — matching
/// bash, where `true | cmd` hands `cmd` a real (empty) pipe.
public var isCanonicalEmpty: Bool {
cursor === Self.empty.cursor
}

// MARK: Factories

/// An already-finished stream with no data.
Expand Down
29 changes: 29 additions & 0 deletions Tests/ShellKitTests/InputSourceCanonicalEmptyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation
import Testing
@testable import ShellKit

/// ``InputSource/isCanonicalEmpty`` — the "did the shell attach real
/// input?" probe used by commands with read-stdin-or-walk-cwd
/// behaviour (rg's no-path default).
@Suite struct InputSourceCanonicalEmptyTests {

@Test func canonicalEmptyAnswersTrue() {
#expect(InputSource.empty.isCanonicalEmpty)
// Copies share the cursor, so the marker survives passing
// the source around by value.
let copy = InputSource.empty
#expect(copy.isCanonicalEmpty)
}

@Test func attachedSourcesAnswerFalse() {
#expect(!InputSource.string("x").isCanonicalEmpty)
// Deliberately attached zero-byte input is NOT canonical —
// bash's `true | cmd` hands cmd a real (empty) pipe, and a
// command must read it (and see EOF), not walk the cwd.
#expect(!InputSource.string("").isCanonicalEmpty)
#expect(!InputSource.data(Data()).isCanonicalEmpty)
let (stream, continuation) = AsyncStream<Data>.makeStream()
continuation.finish()
#expect(!InputSource(bytes: stream).isCanonicalEmpty)
}
}
Loading