Skip to content

Commit 9436e79

Browse files
committed
add unit test for stdin and stdin err
1 parent 9ada839 commit 9436e79

2 files changed

Lines changed: 62 additions & 9 deletions

File tree

pkg/app/test_command.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ func TestCommand(testPath string, ctx TestCommandContext) error {
6464
return nil
6565
}
6666

67+
func testFile(filePath string, fileName string, filters runtime.Filters) (runtime.Result, error) {
68+
s, err := readFile(filePath, fileName)
69+
if err != nil {
70+
return runtime.Result{}, fmt.Errorf("Error " + err.Error())
71+
}
72+
73+
return execute(s, filters)
74+
}
75+
6776
func testDir(directory string, filters runtime.Filters) (runtime.Result, error) {
6877
result := runtime.Result{}
6978
files, err := ioutil.ReadDir(directory)
@@ -117,15 +126,6 @@ func convergeResults(result runtime.Result, new runtime.Result) runtime.Result {
117126
return result
118127
}
119128

120-
func testFile(filePath string, fileName string, filters runtime.Filters) (runtime.Result, error) {
121-
s, err := readFile(filePath, fileName)
122-
if err != nil {
123-
return runtime.Result{}, fmt.Errorf("Error " + err.Error())
124-
}
125-
126-
return execute(s, filters)
127-
}
128-
129129
func testStdin(filters runtime.Filters) (runtime.Result, error) {
130130
f, err := os.Stdin.Stat()
131131
if err != nil {

pkg/app/test_command_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app
33
import (
44
"bytes"
55
"io"
6+
"io/ioutil"
67
"log"
78
"os"
89
"runtime"
@@ -108,6 +109,58 @@ func Test_TestCommand_Http_Err(t *testing.T) {
108109
}
109110
}
110111

112+
func Test_TestCommand_StdIn(t *testing.T) {
113+
tmpfile, err := ioutil.TempFile("", "test")
114+
if err != nil {
115+
log.Fatal(err)
116+
}
117+
defer os.Remove(tmpfile.Name()) //clean up
118+
119+
content := []byte(`
120+
tests:
121+
echo hello:
122+
exit-code: 0
123+
`)
124+
125+
if _, err := tmpfile.Write(content); err != nil {
126+
log.Fatal(err)
127+
}
128+
129+
if _, err := tmpfile.Seek(0, 0); err != nil {
130+
log.Fatal(err)
131+
}
132+
133+
// set stdin to tempfile
134+
oldStdin := os.Stdin
135+
defer func() { os.Stdin = oldStdin }() // Restore original Stdin
136+
os.Stdin = tmpfile
137+
138+
out := captureOutput(func() {
139+
TestCommand("-", TestCommandContext{Verbose: false})
140+
})
141+
142+
if err := tmpfile.Close(); err != nil {
143+
log.Fatal(err)
144+
}
145+
146+
assert.Contains(t, out, "✓ [local] echo hello")
147+
}
148+
149+
func Test_TestCommand_StdIn_Err(t *testing.T) {
150+
// set stdin to nil
151+
oldStdin := os.Stdin
152+
defer func() { os.Stdin = oldStdin }() // Restore original Stdin
153+
os.Stdin = nil
154+
155+
err := TestCommand("-", TestCommandContext{Verbose: false})
156+
157+
if runtime.GOOS == "windows" {
158+
assert.NotNil(t, err)
159+
} else {
160+
assert.NotNil(t, err)
161+
}
162+
}
163+
111164
func Test_ConvergeResults(t *testing.T) {
112165
duration, _ := time.ParseDuration("5s")
113166

0 commit comments

Comments
 (0)