forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot_test.go
More file actions
143 lines (118 loc) · 3.05 KB
/
root_test.go
File metadata and controls
143 lines (118 loc) · 3.05 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cmd
import (
"bytes"
"io"
"os"
"testing"
)
func TestCheckStdinPipe(t *testing.T) {
// Save original stdin
origStdin := os.Stdin
// Restore original stdin when test completes
defer func() {
os.Stdin = origStdin
}()
// Test case 1: Data is piped in
t.Run("WithPipedData", func(t *testing.T) {
// Create a pipe
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe: %v", err)
}
// Replace stdin with our pipe
os.Stdin = r
// Write test data to the pipe
testData := "test piped input"
go func() {
defer w.Close()
w.Write([]byte(testData))
}()
// Call the function
data, hasPiped := checkStdinPipe()
// Check results
if !hasPiped {
t.Error("Expected hasPiped to be true, got false")
}
if data != testData {
t.Errorf("Expected data to be %q, got %q", testData, data)
}
})
// Test case 2: No data is piped in (simulated terminal)
t.Run("WithoutPipedData", func(t *testing.T) {
// Create a temporary file to simulate a terminal
tmpFile, err := os.CreateTemp("", "terminal-sim")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
// Open the file for reading
f, err := os.Open(tmpFile.Name())
if err != nil {
t.Fatalf("Failed to open temp file: %v", err)
}
defer f.Close()
// Replace stdin with our file
os.Stdin = f
// Call the function
data, hasPiped := checkStdinPipe()
// Check results
if hasPiped {
t.Error("Expected hasPiped to be false, got true")
}
if data != "" {
t.Errorf("Expected data to be empty, got %q", data)
}
})
}
// This is a mock implementation for testing since we can't easily mock os.Stdin.Stat()
// in a way that would return the correct Mode() for our test cases
func mockCheckStdinPipe(reader io.Reader, isPipe bool) (string, bool) {
if !isPipe {
return "", false
}
data, err := io.ReadAll(reader)
if err != nil {
return "", false
}
if len(data) > 0 {
return string(data), true
}
return "", false
}
func TestMockCheckStdinPipe(t *testing.T) {
// Test with data
t.Run("WithData", func(t *testing.T) {
testData := "test data"
reader := bytes.NewBufferString(testData)
data, hasPiped := mockCheckStdinPipe(reader, true)
if !hasPiped {
t.Error("Expected hasPiped to be true, got false")
}
if data != testData {
t.Errorf("Expected data to be %q, got %q", testData, data)
}
})
// Test without data
t.Run("WithoutData", func(t *testing.T) {
reader := bytes.NewBufferString("")
data, hasPiped := mockCheckStdinPipe(reader, true)
if hasPiped {
t.Error("Expected hasPiped to be false, got true")
}
if data != "" {
t.Errorf("Expected data to be empty, got %q", data)
}
})
// Test not a pipe
t.Run("NotAPipe", func(t *testing.T) {
reader := bytes.NewBufferString("data that should be ignored")
data, hasPiped := mockCheckStdinPipe(reader, false)
if hasPiped {
t.Error("Expected hasPiped to be false, got true")
}
if data != "" {
t.Errorf("Expected data to be empty, got %q", data)
}
})
}