-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
61 lines (48 loc) · 1.28 KB
/
main_test.go
File metadata and controls
61 lines (48 loc) · 1.28 KB
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
package main
import (
"encoding/json"
"github.com/fuwalab/tools/conf"
"github.com/fuwalab/tools/db"
"os"
"os/exec"
"testing"
)
var config *conf.AppConf
func TestMain(m *testing.M) {
conf.SetEnv("test")
db.NewRepo(db.Conn()).InitDB()
code := m.Run()
os.Exit(code)
}
func TestUsage(t *testing.T) {
config = conf.GetAppConf()
// no argument
cmd := exec.Command("go",
"run", config.ProjectRoot+"/main.go",
)
result, _ := cmd.CombinedOutput()
actual := string(result)
expected := usage()
if actual != expected {
t.Error("Got different sentences.")
t.Errorf("actual\n%v", actual)
t.Errorf("expected\n%v", expected)
}
// incorrect argument
cmd = exec.Command("go",
"run", "main.go", "DoSomething",
)
result, _ = cmd.CombinedOutput()
expected = `{"time":"2018-08-23T02:21:03.812357433+09:00","level":"INFO","prefix":"-","file":"main.go","line":"40","message":"subcommand DoSomething is not exist"}`
type LogInfo struct {
Message string `json:"message"`
}
var actualJSON, expectedJSON LogInfo
json.Unmarshal(result, &actualJSON)
json.Unmarshal([]byte(expected), &expectedJSON)
if actualJSON.Message != expectedJSON.Message {
t.Error("Got different sentences.")
t.Errorf("actual\n%v", actualJSON.Message)
t.Errorf("expected\n%v", expectedJSON.Message)
}
}