-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiohelpers_test.go
55 lines (46 loc) · 1.09 KB
/
iohelpers_test.go
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
package main
// Tests for the iohelpers.go file
import (
"io/ioutil"
"log"
"os"
"strings"
_ "syscall"
"testing"
color "github.com/TwiN/go-color"
)
func Test_printWarning(t *testing.T) {
r, w, _ := os.Pipe()
// restore stdout right after the test
defer func(v *os.File) { os.Stderr = v }(os.Stderr)
os.Stderr = w
logger := log.New(os.Stderr, "", log.Lmsgprefix)
printWarning(logger)
_ = w.Close()
out, _ := ioutil.ReadAll(r)
strOut := string(out)
if strings.Contains(strOut, color.InBold(color.InYellowOverBlack("WARNING"))) {
t.Errorf("printWarning() did not print the warning message!")
}
}
func Test_readFromPipe(t *testing.T) {
input := []byte("Test input\nfrom pipe")
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_, err = w.Write(input)
if err != nil {
t.Error(err)
}
w.Close()
// Restore stdin right after the test
defer func(v *os.File) { os.Stdin = v }(os.Stdin)
os.Stdin = r
text, err := readFromPipe()
if text != string(input)+"\n" {
t.Log([]byte(text))
t.Log(input)
t.Errorf("readFromPipe() = '%v', want '%v'", text, string(input)+"\n")
}
}