Skip to content

Commit c4062a8

Browse files
authored
Merge pull request #58 from zaquestion/tests-cmd-2
(tests) add unittests around pkg cmd functions to increase coverage
2 parents 6cc5b81 + 5dd2085 commit c4062a8

File tree

9 files changed

+108
-23
lines changed

9 files changed

+108
-23
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ test:
22
bash -c "trap 'trap - SIGINT SIGTERM ERR; mv testdata/.git testdata/test.git; exit 1' SIGINT SIGTERM ERR; $(MAKE) internal-test"
33

44
internal-test:
5+
rm coverage-* 2>&1 > /dev/null || true
56
mv testdata/test.git testdata/.git
67
go test -coverprofile=coverage-git.out -covermode=count github.com/zaquestion/lab/internal/git
78
go test -coverprofile=coverage-gitlab.out -covermode=count github.com/zaquestion/lab/internal/gitlab
8-
go test -coverprofile=coverage-cmd.out -covermode=count github.com/zaquestion/lab/cmd
9+
go test -coverprofile=coverage-cmd.out -covermode=count -coverpkg ./... github.com/zaquestion/lab/cmd
910
mv testdata/.git testdata/test.git
1011
go get github.com/wadey/gocovmerge
1112
gocovmerge coverage-*.out > coverage.txt && rm coverage-*.out

cmd/issueCreate_test.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,44 @@ func Test_issueCreate(t *testing.T) {
2424
}
2525

2626
func Test_issueMsg(t *testing.T) {
27-
title, body, err := issueMsg([]string{"issue title", "issue body", "issue body 2"})
28-
if err != nil {
29-
t.Fatal(err)
27+
tests := []struct {
28+
Name string
29+
Msgs []string
30+
ExpectedTitle string
31+
ExpectedBody string
32+
}{
33+
{
34+
Name: "Using messages",
35+
Msgs: []string{"issue title", "issue body", "issue body 2"},
36+
ExpectedTitle: "issue title",
37+
ExpectedBody: "issue body\n\nissue body 2",
38+
},
39+
{
40+
Name: "From Editor",
41+
Msgs: nil,
42+
ExpectedTitle: "I am the issue tmpl",
43+
ExpectedBody: "",
44+
},
45+
}
46+
for _, test := range tests {
47+
t.Run(test.Name, func(t *testing.T) {
48+
test := test
49+
t.Parallel()
50+
title, body, err := issueMsg(test.Msgs)
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
assert.Equal(t, test.ExpectedTitle, title)
55+
assert.Equal(t, test.ExpectedBody, body)
56+
})
3057
}
31-
assert.Equal(t, "issue title", title)
32-
assert.Equal(t, "issue body\n\nissue body 2", body)
33-
3458
}
3559

3660
func Test_issueText(t *testing.T) {
3761
text, err := issueText()
3862
if err != nil {
3963
t.Fatal(err)
4064
}
41-
// Normally we we expect the issue template to prefix this. However
42-
// since `issueText()` is being called from the `cmd` directory the
43-
// underlying LoadGitLabTmpl call doesn't find a template.
44-
// This is fine since we have other tests to test loading the template
4565
require.Equal(t, `
4666
4767
I am the issue tmpl

cmd/mrCreate_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ func Test_mrText(t *testing.T) {
3535
t.Log(text)
3636
t.Fatal(err)
3737
}
38-
// Normally we we expect the issue template to prefix this. However
39-
// since `issueText()` is being called from the `cmd` directory the
40-
// underlying LoadGitLabTmpl call doesn't find a template.
41-
// This is fine since we have other tests to test loading the template
4238
require.Contains(t, text, `Added additional commit for LastCommitMessage and meeting requirements for Log test (>1 commit)
4339
4440
I am the mr tmpl

cmd/snippetCreate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func snipCode(path string) (string, error) {
159159
}
160160
}
161161

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

cmd/snippetCreate_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cmd
22

33
import (
4+
"io/ioutil"
45
"os/exec"
56
"testing"
67

8+
"github.com/stretchr/testify/assert"
79
"github.com/stretchr/testify/require"
810
)
911

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

6769
require.Contains(t, string(b), "https://gitlab.com/snippets/")
6870
}
71+
72+
func Test_snipMsg(t *testing.T) {
73+
title, desc, err := snipMsg(nil, "snip title\nthis should be dropped")
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
// This title was defaulted from the snippet contents/code because no
78+
// msgs -m title was provided
79+
assert.Equal(t, "snip title", title)
80+
// This is the body created in during editing or with provided msgs -m
81+
assert.Equal(t, "", desc)
82+
}
83+
84+
func Test_snipCode(t *testing.T) {
85+
err := ioutil.WriteFile("./testfile", []byte("test file contents"), 0644)
86+
if err != nil {
87+
t.Fatal(err)
88+
}
89+
90+
tests := []struct {
91+
Name string
92+
Path string
93+
ExpectedCode string
94+
}{
95+
{
96+
Name: "From File",
97+
Path: "./testfile",
98+
ExpectedCode: "test file contents",
99+
},
100+
{
101+
Name: "From Editor",
102+
Path: "",
103+
ExpectedCode: "\n\n",
104+
},
105+
}
106+
for _, test := range tests {
107+
t.Run(test.Name, func(t *testing.T) {
108+
test := test
109+
t.Parallel()
110+
code, err := snipCode(test.Path)
111+
if err != nil {
112+
t.Fatal(err)
113+
}
114+
require.Equal(t, test.ExpectedCode, code)
115+
})
116+
}
117+
}
118+
119+
func Test_snipText(t *testing.T) {
120+
var tmpl = "foo" + `
121+
{{.CommentChar}} In this mode you are writing a snippet from scratch
122+
{{.CommentChar}} The first block is the title and the rest is the contents.`
123+
text, err := snipText(tmpl)
124+
if err != nil {
125+
t.Fatal(err)
126+
}
127+
require.Equal(t, `foo
128+
# In this mode you are writing a snippet from scratch
129+
# The first block is the title and the rest is the contents.`, text)
130+
131+
}

internal/git/edit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ func editorPath() (string, error) {
6161
}
6262

6363
func editorCMD(editorPath, filePath string) *exec.Cmd {
64+
parts := strings.Split(editorPath, " ")
6465
r := regexp.MustCompile("[nmg]?vi[m]?$")
6566
args := make([]string, 0, 3)
6667
if r.MatchString(editorPath) {
6768
args = append(args, "--cmd", "set ft=gitcommit tw=0 wrap lbr")
6869
}
70+
args = append(args, parts[1:]...)
6971
args = append(args, filePath)
70-
cmd := exec.Command(editorPath, args...)
72+
cmd := exec.Command(parts[0], args...)
7173
cmd.Stdin = os.Stdin
7274
cmd.Stdout = os.Stdout
7375
cmd.Stderr = os.Stderr

internal/git/edit_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func Test_parseTitleBody(t *testing.T) {
12-
cases := []struct {
12+
tests := []struct {
1313
Name string
1414
Message string
1515
ExpectedTitle string
@@ -47,16 +47,17 @@ func Test_parseTitleBody(t *testing.T) {
4747
},
4848
}
4949

50-
for _, c := range cases {
51-
t.Run(c.Name, func(t *testing.T) {
50+
for _, test := range tests {
51+
t.Run(test.Name, func(t *testing.T) {
52+
test := test
5253
t.Parallel()
53-
title, body, err := parseTitleBody(c.Message)
54+
title, body, err := parseTitleBody(test.Message)
5455
if err != nil {
5556
t.Fatal(err)
5657
}
5758

58-
assert.Equal(t, title, c.ExpectedTitle)
59-
assert.Equal(t, body, c.ExpectedBody)
59+
assert.Equal(t, title, test.ExpectedTitle)
60+
assert.Equal(t, body, test.ExpectedBody)
6061
})
6162
}
6263
}

testdata/test.git/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
filemode = true
44
bare = false
55
logallrefupdates = true
6+
editor = test -f
67
[remote "origin"]
78
url = git@gitlab.com:zaquestion/test.git
89
fetch = +refs/heads/*:refs/remotes/origin/*

testdata/testfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test file contents

0 commit comments

Comments
 (0)