Skip to content

Commit

Permalink
Merge pull request #58 from zaquestion/tests-cmd-2
Browse files Browse the repository at this point in the history
(tests) add unittests around pkg cmd functions to increase coverage
  • Loading branch information
zaquestion authored Dec 15, 2017
2 parents 6cc5b81 + 5dd2085 commit c4062a8
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ test:
bash -c "trap 'trap - SIGINT SIGTERM ERR; mv testdata/.git testdata/test.git; exit 1' SIGINT SIGTERM ERR; $(MAKE) internal-test"

internal-test:
rm coverage-* 2>&1 > /dev/null || true
mv testdata/test.git testdata/.git
go test -coverprofile=coverage-git.out -covermode=count github.com/zaquestion/lab/internal/git
go test -coverprofile=coverage-gitlab.out -covermode=count github.com/zaquestion/lab/internal/gitlab
go test -coverprofile=coverage-cmd.out -covermode=count github.com/zaquestion/lab/cmd
go test -coverprofile=coverage-cmd.out -covermode=count -coverpkg ./... github.com/zaquestion/lab/cmd
mv testdata/.git testdata/test.git
go get github.com/wadey/gocovmerge
gocovmerge coverage-*.out > coverage.txt && rm coverage-*.out
40 changes: 30 additions & 10 deletions cmd/issueCreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,44 @@ func Test_issueCreate(t *testing.T) {
}

func Test_issueMsg(t *testing.T) {
title, body, err := issueMsg([]string{"issue title", "issue body", "issue body 2"})
if err != nil {
t.Fatal(err)
tests := []struct {
Name string
Msgs []string
ExpectedTitle string
ExpectedBody string
}{
{
Name: "Using messages",
Msgs: []string{"issue title", "issue body", "issue body 2"},
ExpectedTitle: "issue title",
ExpectedBody: "issue body\n\nissue body 2",
},
{
Name: "From Editor",
Msgs: nil,
ExpectedTitle: "I am the issue tmpl",
ExpectedBody: "",
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test := test
t.Parallel()
title, body, err := issueMsg(test.Msgs)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, test.ExpectedTitle, title)
assert.Equal(t, test.ExpectedBody, body)
})
}
assert.Equal(t, "issue title", title)
assert.Equal(t, "issue body\n\nissue body 2", body)

}

func Test_issueText(t *testing.T) {
text, err := issueText()
if err != nil {
t.Fatal(err)
}
// Normally we we expect the issue template to prefix this. However
// since `issueText()` is being called from the `cmd` directory the
// underlying LoadGitLabTmpl call doesn't find a template.
// This is fine since we have other tests to test loading the template
require.Equal(t, `
I am the issue tmpl
Expand Down
4 changes: 0 additions & 4 deletions cmd/mrCreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ func Test_mrText(t *testing.T) {
t.Log(text)
t.Fatal(err)
}
// Normally we we expect the issue template to prefix this. However
// since `issueText()` is being called from the `cmd` directory the
// underlying LoadGitLabTmpl call doesn't find a template.
// This is fine since we have other tests to test loading the template
require.Contains(t, text, `Added additional commit for LastCommitMessage and meeting requirements for Log test (>1 commit)
I am the mr tmpl
Expand Down
2 changes: 1 addition & 1 deletion cmd/snippetCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func snipCode(path string) (string, error) {
}
}

var tmpl = string(b) + `
var tmpl = `
{{.CommentChar}} In this mode you are writing a snippet from scratch
{{.CommentChar}} The first block is the title and the rest is the contents.`

Expand Down
63 changes: 63 additions & 0 deletions cmd/snippetCreate_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"io/ioutil"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -66,3 +68,64 @@ func Test_snippetCreate_Global(t *testing.T) {

require.Contains(t, string(b), "https://gitlab.com/snippets/")
}

func Test_snipMsg(t *testing.T) {
title, desc, err := snipMsg(nil, "snip title\nthis should be dropped")
if err != nil {
t.Fatal(err)
}
// This title was defaulted from the snippet contents/code because no
// msgs -m title was provided
assert.Equal(t, "snip title", title)
// This is the body created in during editing or with provided msgs -m
assert.Equal(t, "", desc)
}

func Test_snipCode(t *testing.T) {
err := ioutil.WriteFile("./testfile", []byte("test file contents"), 0644)
if err != nil {
t.Fatal(err)
}

tests := []struct {
Name string
Path string
ExpectedCode string
}{
{
Name: "From File",
Path: "./testfile",
ExpectedCode: "test file contents",
},
{
Name: "From Editor",
Path: "",
ExpectedCode: "\n\n",
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test := test
t.Parallel()
code, err := snipCode(test.Path)
if err != nil {
t.Fatal(err)
}
require.Equal(t, test.ExpectedCode, code)
})
}
}

func Test_snipText(t *testing.T) {
var tmpl = "foo" + `
{{.CommentChar}} In this mode you are writing a snippet from scratch
{{.CommentChar}} The first block is the title and the rest is the contents.`
text, err := snipText(tmpl)
if err != nil {
t.Fatal(err)
}
require.Equal(t, `foo
# In this mode you are writing a snippet from scratch
# The first block is the title and the rest is the contents.`, text)

}
4 changes: 3 additions & 1 deletion internal/git/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ func editorPath() (string, error) {
}

func editorCMD(editorPath, filePath string) *exec.Cmd {
parts := strings.Split(editorPath, " ")
r := regexp.MustCompile("[nmg]?vi[m]?$")
args := make([]string, 0, 3)
if r.MatchString(editorPath) {
args = append(args, "--cmd", "set ft=gitcommit tw=0 wrap lbr")
}
args = append(args, parts[1:]...)
args = append(args, filePath)
cmd := exec.Command(editorPath, args...)
cmd := exec.Command(parts[0], args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
13 changes: 7 additions & 6 deletions internal/git/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func Test_parseTitleBody(t *testing.T) {
cases := []struct {
tests := []struct {
Name string
Message string
ExpectedTitle string
Expand Down Expand Up @@ -47,16 +47,17 @@ func Test_parseTitleBody(t *testing.T) {
},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test := test
t.Parallel()
title, body, err := parseTitleBody(c.Message)
title, body, err := parseTitleBody(test.Message)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, title, c.ExpectedTitle)
assert.Equal(t, body, c.ExpectedBody)
assert.Equal(t, title, test.ExpectedTitle)
assert.Equal(t, body, test.ExpectedBody)
})
}
}
Expand Down
1 change: 1 addition & 0 deletions testdata/test.git/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
filemode = true
bare = false
logallrefupdates = true
editor = test -f
[remote "origin"]
url = git@gitlab.com:zaquestion/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
Expand Down
1 change: 1 addition & 0 deletions testdata/testfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test file contents

0 comments on commit c4062a8

Please sign in to comment.