-
Notifications
You must be signed in to change notification settings - Fork 16
/
ninetail_test.go
111 lines (94 loc) · 2.04 KB
/
ninetail_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
110
111
package ninetail
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/mattn/go-colorable"
)
func TestRun(t *testing.T) {
tfs, err := newTestFileSet([]string{
"php.txt",
"吾輩は猫である.txt",
"i_am_a_CAT.txt",
})
if err != nil {
t.Fatal(err)
}
defer tfs.removeAll()
tailers, err := NewTailers(tfs.getFilenames())
if err != nil {
t.Fatal(err)
}
output := new(bytes.Buffer)
target := &NineTail{
output: colorable.NewNonColorable(output),
tailers: tailers,
}
var wg sync.WaitGroup
wg.Add(1)
go func(n *NineTail) {
n.Run()
wg.Done()
}(target)
interval := time.Tick(100 * time.Millisecond)
<-interval
tfs.writeString(0, "PHP(PHP: Hypertext Preprocessor)\n")
<-interval
tfs.writeString(2, "I am a cat. As yet I have no name.\n")
<-interval
tfs.writeString(1, "吾輩は猫である。名前はまだ無い。\n")
<-interval
for _, t := range target.tailers {
t.Stop()
}
wg.Wait()
expect := ` php.txt: PHP(PHP: Hypertext Preprocessor)
i_am_a_CAT.txt: I am a cat. As yet I have no name.
吾輩は猫である.txt: 吾輩は猫である。名前はまだ無い。
`
actual := output.String()
if expect != actual {
t.Fatal("Incorrect align")
}
}
type testFileSet struct {
dir string
files []*os.File
}
func newTestFileSet(basenames []string) (*testFileSet, error) {
dir, err := ioutil.TempDir("", "ninetail")
if err != nil {
return nil, err
}
files := make([]*os.File, len(basenames))
for i, name := range basenames {
filename := filepath.Join(dir, name)
f, err := os.Create(filename)
if err != nil {
return nil, err
}
files[i] = f
}
return &testFileSet{
dir: dir,
files: files,
}, nil
}
func (tfs *testFileSet) getFilenames() []string {
filenames := make([]string, len(tfs.files))
for i, file := range tfs.files {
filenames[i] = file.Name()
}
return filenames
}
func (tfs *testFileSet) writeString(index int, text string) {
fmt.Fprintf(tfs.files[index], text)
}
func (tfs *testFileSet) removeAll() {
os.RemoveAll(tfs.dir)
}