-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexec_test.go
109 lines (99 loc) · 2.97 KB
/
exec_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
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
// Package mute implements functions to execute other programs muting std streams if required
// license: MIT, see LICENSE for details.
package mute
import (
"bufio"
"bytes"
"testing"
)
func TestExecMute(t *testing.T) {
conf := DefaultConf()
var outBuf bytes.Buffer
var errBuf bytes.Buffer
outWriter := bufio.NewWriter(&outBuf)
errWriter := bufio.NewWriter(&errBuf)
target := Target{Cmd: "go", Args: []string{"version"}, Conf: conf, OutWriter: outWriter, ErrWriter: errWriter, BufPreAlloc: 1024}
code, err := target.Exec()
outWriter.Flush()
errWriter.Flush()
if err != nil {
t.Errorf("Exec returned error")
}
if code != 0 {
t.Errorf("Exec return val. got: %d want: 0", code)
}
outStr := outBuf.String()
errStr := errBuf.String()
if outStr != "" {
t.Errorf("Exec mute should not print output. got: %v", outStr)
}
if errStr != "" {
t.Errorf("Exec mute should not print error. got: %v", errStr)
}
}
func TestExecNoMute(t *testing.T) {
conf := DefaultConf()
var outBuf bytes.Buffer
var errBuf bytes.Buffer
outWriter := bufio.NewWriter(&outBuf)
errWriter := bufio.NewWriter(&errBuf)
target := Target{Cmd: "go", Args: []string{"invalid"}, Conf: conf, OutWriter: outWriter, ErrWriter: errWriter, BufPreAlloc: 1024}
code, err := target.Exec()
outWriter.Flush()
errWriter.Flush()
if err == nil {
t.Errorf("Exec invalid didn't return error")
}
if code == 0 {
t.Errorf("Exec invalid return val is 0")
}
errStr := errBuf.String()
if errStr == "" {
t.Errorf("Exec invalid should print error but didn't")
}
}
func TestCmdCriteriaReturnDefault(t *testing.T) {
c1 := NewCriterion([]int{0}, []string{})
conf := new(Conf)
conf.Default.add(c1)
got := cmdCriteria("testcommand", conf)
if !got.equal(&conf.Default) {
t.Errorf("cmdCriteria should have returned conf default but didn't")
}
}
func TestCmdCriteriaReturnCommandSpecific(t *testing.T) {
var crt1, crt2 Criteria
c1 := NewCriterion([]int{0}, []string{})
c2 := NewCriterion([]int{1}, []string{})
crt1 = append(crt1, c1)
crt2 = append(crt2, c2)
var commandsCriteria map[string]Criteria
commandsCriteria = make(map[string]Criteria)
commandsCriteria["test"] = crt1
commandsCriteria["testcommand"] = crt2
commandsCriteria["somethingelse"] = crt1
conf := new(Conf)
conf.Commands = commandsCriteria
got := cmdCriteria("testcommand", conf)
if !got.equal(&crt2) {
t.Errorf("cmdCriteria should have returned longest matched cmd but didn't")
}
}
func TestMatchesCriteria(t *testing.T) {
conf, _ := ReadConfFile("test/data/simple.toml")
crt := conf.Default
stdout := ""
if !matchesCriteria(&crt, 0, &stdout) {
t.Errorf("matchesCriteria 0 default want 'true' got 'false'")
}
if matchesCriteria(&crt, 3, &stdout) {
t.Errorf("matchesCriteria 3 default want 'false' got 'true'")
}
if matchesCriteria(&crt, 1, &stdout) {
t.Errorf("matchesCriteria 1 empty stdout want 'false' got 'true'")
}
stdout = "OK"
if !matchesCriteria(&crt, 1, &stdout) {
t.Errorf("matchesCriteria 1 matching stdout want 'true' got 'false'")
}
}