73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"log"
|
||
|
"os"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestValidateCommitMsg(t *testing.T) {
|
||
|
tts := []struct {
|
||
|
commitMsg string
|
||
|
expErr error
|
||
|
}{
|
||
|
{commitMsg: "i am wrong", expErr: errCommitMsgSyntax},
|
||
|
{commitMsg: "fix: i am a easy fix", expErr: nil},
|
||
|
{commitMsg: "feat: i am a easy feat", expErr: nil},
|
||
|
{commitMsg: "build: i am a easy build", expErr: nil},
|
||
|
{commitMsg: "chore: i am a easy chore", expErr: nil},
|
||
|
{commitMsg: "ci: i am a easy ci", expErr: nil},
|
||
|
{commitMsg: "docs: i am a easy docs", expErr: nil},
|
||
|
{commitMsg: "style: i am a easy style", expErr: nil},
|
||
|
{commitMsg: "refactor: i am a easy refactor", expErr: nil},
|
||
|
{commitMsg: "perf: i am a easy perf", expErr: nil},
|
||
|
{commitMsg: "test: i am a easy test", expErr: nil},
|
||
|
{commitMsg: "something: i am a easy something", expErr: errCommitMsgInvalideType},
|
||
|
{commitMsg: "fix(main): bla", expErr: nil},
|
||
|
{commitMsg: "fix(main)!: bla", expErr: nil},
|
||
|
{commitMsg: "fix(main)! bla", expErr: errCommitMsgSyntax},
|
||
|
}
|
||
|
|
||
|
for _, tt := range tts {
|
||
|
t.Run(tt.commitMsg, func(t *testing.T) {
|
||
|
err := checkCommitMsg(tt.commitMsg)
|
||
|
assert.Equal(t, tt.expErr, err, "should return right error")
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGetFirstLine(t *testing.T) {
|
||
|
msg := "I am \n a new line"
|
||
|
newmsg, err := getFirstLine(msg)
|
||
|
assert.Nil(t, err, "shouldnt return error")
|
||
|
assert.Equal(t, "I am ", newmsg, "should return just first line of commit msg")
|
||
|
}
|
||
|
func TestGetFirstLineEmpty(t *testing.T) {
|
||
|
msg := ""
|
||
|
newmsg, err := getFirstLine(msg)
|
||
|
assert.Nil(t, err, "shouldnt return error")
|
||
|
assert.Equal(t, "", newmsg, "should return just first line of commit msg")
|
||
|
}
|
||
|
|
||
|
func TestGetCommitMessageFromFile(t *testing.T) {
|
||
|
commitmsg := "some random commit msg"
|
||
|
path := createCommitMessageFile(commitmsg)
|
||
|
defer os.Remove(path)
|
||
|
|
||
|
msg, err := getCommitMessageFromFile(path)
|
||
|
assert.Nil(t, err, "should not return error")
|
||
|
assert.Equal(t, commitmsg, msg, "commit message should be read from file")
|
||
|
}
|
||
|
|
||
|
func createCommitMessageFile(msg string) string {
|
||
|
f, err := os.CreateTemp("", "*")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
f.Write([]byte(msg))
|
||
|
return f.Name()
|
||
|
}
|