From ce9bb9601b8cdb686448b12ca70f290a98f82e1f Mon Sep 17 00:00:00 2001 From: Sam Hofius Date: Sun, 17 May 2020 00:38:23 -0400 Subject: [PATCH] Add tests --- chat.go | 3 +-- chat_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ keybase.go | 5 +++- keybase_test.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 chat_test.go create mode 100644 keybase_test.go diff --git a/chat.go b/chat.go index 701354c..a85bfc9 100644 --- a/chat.go +++ b/chat.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "os/exec" "time" "samhofi.us/x/keybase/v2/types/chat1" @@ -45,7 +44,7 @@ func getNewMessages(k *Keybase, subs *subscriptionChannels, execOptions []string cmd = append(cmd, execString...) - execCmd := exec.Command(k.ExePath, cmd...) + execCmd := execCommand(k.ExePath, cmd...) stdOut, _ := execCmd.StdoutPipe() execCmd.Start() scanner := bufio.NewScanner(stdOut) diff --git a/chat_test.go b/chat_test.go new file mode 100644 index 0000000..3f3cf83 --- /dev/null +++ b/chat_test.go @@ -0,0 +1,70 @@ +package keybase + +import ( + "os/exec" + "testing" + + "samhofi.us/x/keybase/v2/types/chat1" +) + +func TestListConvsOnName(t *testing.T) { + execCommand = createFakeExecCommand("listconvsonname") + defer func() { execCommand = exec.Command }() + + channel := chat1.ChatChannel{ + Name: "mkbot", + MembersType: TEAM, + } + k := New() + res, err := k.ListConvsOnName(channel) + if err != nil { + t.Errorf("Expected nil error, got %#v", err) + } + + channelcount := 10 + if len(*res) != channelcount { + t.Errorf("Expected %d channels, got %d channels", channelcount, len(*res)) + } +} + +func TestSendMessageByChannel(t *testing.T) { + execCommand = createFakeExecCommand("send") + defer func() { execCommand = exec.Command }() + + channel := chat1.ChatChannel{ + Name: "user1,user2", + MembersType: USER, + } + k := New() + res, err := k.SendMessageByChannel(channel, "Hello!") + if err != nil { + t.Errorf("Expected nil error, got %#v", err) + } + + if expected := "message sent"; res.Message != expected { + t.Errorf(`res.Message: expected "%s", got "%v"`, expected, res.Message) + } + + if expected := uint(894); uint(*res.MessageID) != expected { + t.Errorf(`res.MessageID: expected %d, got %d`, expected, uint(*res.MessageID)) + } +} + +func TestSendMessageByConvID(t *testing.T) { + execCommand = createFakeExecCommand("send") + defer func() { execCommand = exec.Command }() + + k := New() + res, err := k.SendMessageByConvID(chat1.ConvIDStr("000049d2395435dff0c865c18832d9645eb69fd74a2814ef55310b294092ba6d"), "Hello!") + if err != nil { + t.Errorf("Expected nil error, got %#v", err) + } + + if expected := "message sent"; res.Message != expected { + t.Errorf(`res.Message: expected "%s", got "%v"`, expected, res.Message) + } + + if expected := uint(894); uint(*res.MessageID) != expected { + t.Errorf(`res.MessageID: expected %d, got %d`, expected, uint(*res.MessageID)) + } +} diff --git a/keybase.go b/keybase.go index 5b90868..ea22a29 100644 --- a/keybase.go +++ b/keybase.go @@ -9,6 +9,9 @@ import ( "samhofi.us/x/keybase/v2/types/chat1" ) +// Used for testing +var execCommand = exec.Command + // Possible MemberTypes const ( TEAM string = "team" @@ -70,7 +73,7 @@ func (k *Keybase) Exec(command ...string) ([]byte, error) { cmd = append(cmd, command...) - out, err := exec.Command(k.ExePath, cmd...).Output() + out, err := execCommand(k.ExePath, cmd...).Output() if err != nil { return []byte{}, err } diff --git a/keybase_test.go b/keybase_test.go new file mode 100644 index 0000000..3e90d1a --- /dev/null +++ b/keybase_test.go @@ -0,0 +1,68 @@ +package keybase + +import ( + "fmt" + "os" + "os/exec" + "testing" +) + +func createFakeExecCommand(caller string) func(command string, args ...string) *exec.Cmd { + return func(command string, args ...string) *exec.Cmd { + cs := []string{"-test.run=ExecHelper", "--"} + cs = append(cs, args...) + cmd := exec.Command(os.Args[0], cs...) + cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1", "GO_WANT_HELPER_CALLER=" + caller} + return cmd + } +} + +func TestNew(t *testing.T) { + execCommand = createFakeExecCommand("new") + defer func() { execCommand = exec.Command }() + + k := New() + + if expected := "keybase"; k.ExePath != expected { + t.Errorf(`k.ExePath: expected "%s", got "%s"`, expected, k.ExePath) + } + if expected := ""; k.HomePath != expected { + t.Errorf(`k.HomePath: expected "%s", got "%s"`, expected, k.HomePath) + } + + k = New( + SetExePath("/path/to/exepath"), + SetHomePath("/path/to/homepath"), + ) + + if expected := "/path/to/exepath"; k.ExePath != expected { + t.Errorf(`k.ExePath: expected "%s", got "%s"`, expected, k.ExePath) + } + if expected := "/path/to/homepath"; k.HomePath != expected { + t.Errorf(`k.HomePath: expected "%s", got "%s"`, expected, k.HomePath) + } +} + +func TestExecHelper(t *testing.T) { + if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { + return + } + + var ( + listconvsonname = `{"result":{"conversations":[{"id":"0000b9d9be8586029d4876af2192b94d6705603cf6859fc27c746de94befd45c","channel":{"name":"mkbot","members_type":"team","topic_type":"chat"},"is_default_conv":false,"unread":true,"active_at":1589468132,"active_at_ms":1589468132734,"member_status":"active","creator_info":{"ctime":1551848940888,"username":"dxb"}},{"id":"0000d2c4d915aa04c093a25b9496cd885ff510f4eeeacac5a7249f65d82ed0ad","channel":{"name":"mkbot","members_type":"team","topic_type":"chat"},"is_default_conv":false,"unread":false,"active_at":1589633805,"active_at_ms":1589633805970,"member_status":"active","creator_info":{"ctime":1551848919282,"username":"dxb"}},{"id":"0000e967261971be5aae47d1cfd7d77e695d4a2f90e2ee35236ef3472b2884d4","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"gameroom"},"is_default_conv":false,"unread":true,"active_at":1589468113,"active_at_ms":1589468113173,"member_status":"active","creator_info":{"ctime":1566244683161,"username":"dxb"}},{"id":"0000d5ae3da566307f6c9906881e5bd08dc9a0bf8c341b5769240026e367c478","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"general"},"is_default_conv":true,"unread":false,"active_at":1589348381,"active_at_ms":1589348381358,"member_status":"active","creator_info":{"ctime":1551840458201,"username":"dxb"}},{"id":"0000d7cf1e6f51d75f9a354c2cb7c3bd30415f184bbb9eba0c57aa50827b7663","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test1"},"is_default_conv":false,"unread":true,"active_at":1589468203,"active_at_ms":1589468203686,"member_status":"active","creator_info":{"ctime":1551849049656,"username":"dxb"}},{"id":"0000d0cf70804671490e7f8f21c207a1ac6a8bc2ee05db804fb4531ab6c06f05","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test2"},"is_default_conv":false,"unread":true,"active_at":1589468118,"active_at_ms":1589468118254,"member_status":"active","creator_info":{"ctime":1551849050007,"username":"dxb"}},{"id":"000044e620fef1e84b623350faff06ebef7a0cd7e403ba81a1b35d311976b9f6","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test3"},"is_default_conv":false,"unread":true,"active_at":1589468117,"active_at_ms":1589468117094,"member_status":"active","creator_info":{"ctime":1551849050351,"username":"dxb"}},{"id":"0000a8dd5969f6bb414562278a5abf8f3bd80b39d7cdcf0d3df5045f05fbac77","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test4"},"is_default_conv":false,"unread":true,"active_at":1589468197,"active_at_ms":1589468197735,"member_status":"active","creator_info":{"ctime":1551849050729,"username":"dxb"}},{"id":"00004380e20bf4d56cf5e80a7435d594e07ebe043da93468c93c9bf0080f9ef5","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test5"},"is_default_conv":false,"unread":true,"active_at":1589468203,"active_at_ms":1589468203980,"member_status":"active","creator_info":{"ctime":1551849051084,"username":"dxb"}},{"id":"00003bd0aa429c33eee546f20efb76c9e0a9854b0ca18278300bcf6fc4c4fb93","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"trivia"},"is_default_conv":false,"unread":false,"active_at":1589468113,"active_at_ms":1589468113074,"member_status":"active","creator_info":{"ctime":1580428008401,"username":"dxb"}}],"offline":false}}` + send = `{"result":{"message":"message sent","id":894,"ratelimits":[{"tank":"chat","capacity":9000,"reset":155,"gas":8991}]}}` + ) + + var jsonOut string + switch os.Getenv("GO_WANT_HELPER_CALLER") { + case "listconvsonname": + jsonOut = listconvsonname + case "send": + jsonOut = send + default: + jsonOut = "" + } + + fmt.Fprintf(os.Stdout, jsonOut) + os.Exit(0) +}