-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
64 lines (56 loc) · 1.56 KB
/
main_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
package main
import (
"bytes"
"fmt"
"os"
"testing"
)
const (
testdata = "testdata"
)
func testfile(name string) string {
return fmt.Sprintf("%s/%s", testdata, name)
}
func TestRun(t *testing.T) {
type args struct {
args []string
}
tests := []struct {
name string
args args
wantStdout string
wantStderr string
wantErr bool
}{
{"no args no stdin", args{[]string{"zipcheck", ""}}, "emptyStdout", "emptyStderr", false},
{"fake directory", args{[]string{"zipcheck", "aaaaaaa"}}, "fakeStdout", "fakeStderr", false},
{"good directory", args{[]string{"zipcheck", "testdata"}}, "goodStdout", "goodStderr", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
wantStdoutB, err := os.ReadFile(testfile(tt.wantStdout))
if err != nil {
t.Errorf("Failed opening test file %s: %v", tt.wantStdout, err)
}
wantStdout := string(wantStdoutB)
wantStderrB, err := os.ReadFile(testfile(tt.wantStderr))
if err != nil {
t.Errorf("Failed reading test file %s: %v", tt.wantStderr, err)
}
wantStderr := string(wantStderrB)
err = run(tt.args.args, stdout, stderr)
if (err != nil) != tt.wantErr {
t.Errorf("run() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotStdout := stdout.String(); gotStdout != wantStdout {
t.Errorf("run() gotStdout = %v, want %v", gotStdout, wantStdout)
}
if gotStderr := stderr.String(); gotStderr != wantStderr {
t.Errorf("run() gotStderr = %v, want %v", gotStderr, wantStderr)
}
})
}
}