teat: unit test for new conventional commit

main
hau 2020-11-09 16:29:27 +07:00
parent fc465926e3
commit 818cf7d270
15 changed files with 124 additions and 2 deletions

2
go.mod
View File

@ -5,6 +5,8 @@ go 1.15
require ( require (
github.com/go-git/go-git v4.7.0+incompatible github.com/go-git/go-git v4.7.0+incompatible
github.com/go-git/go-git/v5 v5.2.0 github.com/go-git/go-git/v5 v5.2.0
github.com/google/go-cmp v0.3.0
github.com/sebdah/goldie/v2 v2.5.1
github.com/spf13/cobra v1.1.1 github.com/spf13/cobra v1.1.1
github.com/urfave/cli/v2 v2.3.0 github.com/urfave/cli/v2 v2.3.0
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect

4
go.sum
View File

@ -116,6 +116,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/haunt98/converter v0.0.0-20201106100128-c3708e9bab33 h1:u6Tya29fCvhMiedEMiR4rbX+D+6SUzgL9EasB5wOflY=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg= github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
@ -183,6 +184,9 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sebdah/goldie v1.0.0 h1:9GNhIat69MSlz/ndaBg48vl9dF5fI+NBB6kfOxgfkMc=
github.com/sebdah/goldie/v2 v2.5.1 h1:hh70HvG4n3T3MNRJN2z/baxPR8xutxo7JVxyi2svl+s=
github.com/sebdah/goldie/v2 v2.5.1/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=

13
pkg/comparision/diff.go Normal file
View File

@ -0,0 +1,13 @@
package comparision
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func Diff(t *testing.T, want, got interface{}) {
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}

View File

@ -25,7 +25,8 @@ type Commit struct {
} }
func NewCommit(c git.Commit) (result Commit, err error) { func NewCommit(c git.Commit) (result Commit, err error) {
messages := strings.Split(c.Message, "\n") message := strings.TrimSpace(c.Message)
messages := strings.Split(message, "\n")
if len(messages) == 0 { if len(messages) == 0 {
err = errors.New("empty commit") err = errors.New("empty commit")
return return
@ -36,7 +37,7 @@ func NewCommit(c git.Commit) (result Commit, err error) {
return return
} }
bodyAndFooters := c.Message[len(header):] bodyAndFooters := message[len(header):]
parseBodyAndFooters(bodyAndFooters, &result) parseBodyAndFooters(bodyAndFooters, &result)
return return

View File

@ -0,0 +1,58 @@
package convention
import (
"fmt"
"io/ioutil"
"strings"
"testing"
"github.com/haunt98/changeloguru/pkg/comparision"
"github.com/haunt98/changeloguru/pkg/git"
"github.com/sebdah/goldie/v2"
)
func TestNewCommit(t *testing.T) {
tests := []struct {
name string
wantErr error
}{
{
name: "Commit message with description and breaking change footer",
},
{
name: "Commit message with not character to draw attention to breaking change",
},
{
name: "Commit message with no body",
},
{
name: "Commit message with scope",
},
{
name: "Commit message with multi-paragraph body and multiple footers",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
g := goldie.New(t)
gName := g.GoldenFileName(t, t.Name())
inputName := strings.TrimSuffix(gName, ".golden") + ".txt"
fmt.Println("inputName", inputName)
bytes, err := ioutil.ReadFile(inputName)
if err != nil {
t.Error(err)
}
gotResult, gotErr := NewCommit(git.Commit{
Message: string(bytes),
})
comparision.Diff(t, tc.wantErr, gotErr)
if tc.wantErr != nil {
return
}
g.AssertJson(t, t.Name(), gotResult)
})
}
}

View File

@ -0,0 +1,6 @@
{
"Type": "feat",
"Scope": "",
"Description": "allow provided config object to extend other configs",
"BodyAndFooters": "BREAKING CHANGE: `extends` key in config file is now used for extending other config files"
}

View File

@ -0,0 +1,3 @@
feat: allow provided config object to extend other configs
BREAKING CHANGE: `extends` key in config file is now used for extending other config files

View File

@ -0,0 +1,6 @@
{
"Type": "fix",
"Scope": "",
"Description": "correct minor typos in code",
"BodyAndFooters": "see the issue for details\n\non typos fixed.\n\nReviewed-by: Z\nRefs #133"
}

View File

@ -0,0 +1,8 @@
fix: correct minor typos in code
see the issue for details
on typos fixed.
Reviewed-by: Z
Refs #133

View File

@ -0,0 +1,6 @@
{
"Type": "docs",
"Scope": "",
"Description": "correct spelling of CHANGELOG",
"BodyAndFooters": ""
}

View File

@ -0,0 +1 @@
docs: correct spelling of CHANGELOG

View File

@ -0,0 +1,6 @@
{
"Type": "refactor",
"Scope": "",
"Description": "drop support for Node 6",
"BodyAndFooters": ""
}

View File

@ -0,0 +1 @@
refactor!: drop support for Node 6

View File

@ -0,0 +1,6 @@
{
"Type": "feat",
"Scope": "lang",
"Description": "add polish language",
"BodyAndFooters": ""
}

View File

@ -0,0 +1 @@
feat(lang): add polish language