Skip to content

Commit

Permalink
added integration test for good measure
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Nov 3, 2021
1 parent 5adb327 commit 33272b3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ func run(c *cli.Context) error {
LinkNames: c.Bool("link-names"),
},
}

if plugin.Build.Commit == "" {
plugin.Build.Commit = "0000000000000000000000000000000000000000"
}
if plugin.Config.Webhook == "" {
return errors.New("Missing webhook")
}
Expand Down
12 changes: 2 additions & 10 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,6 @@ func (p Plugin) Exec() error {
return client.PostMessage(&payload)
}

func detectRef(build Build) string {
if build.Commit != "" {
return build.Commit[:8]
}

return build.Tag
}

func templateMessage(t string, plugin Plugin) (string, error) {
return template.RenderTrim(t, plugin)
}
Expand All @@ -155,7 +147,7 @@ func message(repo Repo, build Build) string {
build.Link,
repo.Owner,
repo.Name,
detectRef(build),
build.Commit[:8],
build.Branch,
build.Author,
)
Expand All @@ -166,7 +158,7 @@ func fallback(repo Repo, build Build) string {
build.Status,
repo.Owner,
repo.Name,
detectRef(build),
build.Commit[:8],
build.Branch,
build.Author,
)
Expand Down
31 changes: 31 additions & 0 deletions plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
package main

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"gotest.tools/assert"
)

func TestExec(t *testing.T) {
plugin := Plugin{
Repo: getTestRepo(),
Build: getTestBuild(),
Job: getTestJob(),
Config: getTestConfig(),
}

handler := func(w http.ResponseWriter, r *http.Request) {
out, _ := ioutil.ReadAll(r.Body)
got := string(out)
want := `{"attachments":[{"color":"good","fallback":"Message Template Fallback:\nInitial commit\nmaster\nsuccess","text":"Message Template:\nInitial commit\n\nMessage body\nInitial commit\nMessage body","mrkdwn_in":["text","fallback"]}]}`
assert.Equal(t, got, want)
}

server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()

plugin.Config.Webhook = server.URL
plugin.Exec()
}

func TestNewCommitMessage(t *testing.T) {
testCases := map[string]struct {
Msg string
Expand Down Expand Up @@ -135,6 +160,12 @@ func getTestBuild() Build {
}
}

func getTestJob() Job {
return Job{
Started: 1546340400,
}
}

func getTestConfig() Config {
t := `Message Template:
{{build.message}}
Expand Down

0 comments on commit 33272b3

Please sign in to comment.