-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathconsole_redirect_test.go
More file actions
54 lines (43 loc) · 1014 Bytes
/
console_redirect_test.go
File metadata and controls
54 lines (43 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package console
import (
"io"
"os"
"testing"
)
func TestStdinRedirectionDetection(t *testing.T) {
// Save original stdin
origStdin := os.Stdin
defer func() { os.Stdin = origStdin }()
// Create a pipe
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Couldn't create pipe: %v", err)
}
defer r.Close()
defer w.Close()
// Replace stdin with our pipe
os.Stdin = r
// Test if stdin is properly detected as redirected
if !isStdinRedirected() {
t.Errorf("Pipe input should be detected as redirected")
}
// Write some test input
go func() {
_, _ = io.WriteString(w, "test input\n")
w.Close()
}()
// Create console with redirected stdin
console := NewConsole("")
// Test readline
line, err := console.Readline()
if err != nil {
t.Fatalf("Failed to read from redirected stdin: %v", err)
}
if line != "test input" {
t.Errorf("Expected 'test input', got '%s'", line)
}
// Clean up
console.Close()
}