Skip to content

Commit 65aced4

Browse files
committed
test(commit): fix commit test
body test 1 body test 2 bodytest 3
1 parent a2623aa commit 65aced4

File tree

3 files changed

+21
-37
lines changed

3 files changed

+21
-37
lines changed

cmd/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func makeCommitMsg(
142142

143143
// ContainsNewline check if string contains newline chars
144144
func ContainsNewline(s string) bool {
145-
return strings.Contains(s, "\n") || strings.Contains(s, "\r\n")
145+
return strings.Contains(s, fmt.Sprintln())
146146
}
147147

148148
// ContainsWhiteSpace check if string contains whitesapces

cmd/commit_test.go

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@ package cmd
22

33
import (
44
"fmt"
5-
"runtime"
65
"strings"
76
"testing"
87
)
98

10-
// NL is newline represented in os file
11-
var NL string = DefineNewline()
12-
13-
func DefineNewline() string {
14-
if runtime.GOOS == "windows" {
15-
return "\r\n"
16-
}
17-
return "\n"
18-
}
9+
var NL = fmt.Sprintln()
1910

2011
func TestContainsNewline(t *testing.T) {
21-
if !ContainsNewline(NL) {
12+
if !ContainsNewline(fmt.Sprintln()) {
2213
t.Error("ContainsNewline check failed")
2314
}
2415
}
@@ -53,15 +44,15 @@ func TestCommitMsgHeader(t *testing.T) {
5344
noDescMsg := makeCommitMsg("fix", "", false, "", "", []string{})
5445

5546
if ok, msg := emptyMsg.Validate(); ok || msg != RequiredType {
56-
t.Errorf(`Required commit type check failed, expected: %s, got: %s`, RequiredType, msg)
47+
t.Errorf("Required commit type check failed, expected: %s, got: %s", RequiredType, msg)
5748
}
5849

5950
if ok, msg := invalidScopeMsg.Validate(); ok || msg != InvalidScope {
60-
t.Errorf(`No linebreak in scope check failed, expected: %s, got: %s`, InvalidScope, msg)
51+
t.Errorf("No linebreak in scope check failed, expected: %s, got: %s", InvalidScope, msg)
6152
}
6253

6354
if ok, msg := noDescMsg.Validate(); ok || msg != RequiredDesc {
64-
t.Errorf(`Required commit description check failed, expected: %s, got: %s`, RequiredDesc, msg)
55+
t.Errorf("Required commit description check failed, expected: %s, got: %s", RequiredDesc, msg)
6556
}
6657

6758
// test toString format
@@ -71,30 +62,31 @@ func TestCommitMsgHeader(t *testing.T) {
7162
msg4 := makeCommitMsg("fix", "lib", true, "fix bug", "", []string{})
7263

7364
if got, expected := msg1.ToString(), fmt.Sprintln("docs: fix typo"); got != expected {
74-
t.Errorf(`expected: %s, got: %s`, expected, got)
65+
t.Errorf("expected: %s, got: %s", expected, got)
7566
}
7667

7768
if got, expected := msg2.ToString(), fmt.Sprintln("docs(READ ME.md): fix typo"); got != expected {
78-
t.Errorf(`expected: %s, got: %s`, expected, got)
69+
t.Errorf("expected: %s, got: %s", expected, got)
7970
}
8071

8172
if got, expected := msg3.ToString(), fmt.Sprintln("docs!: fix typo"); got != expected {
82-
t.Errorf(`expected: %s, got: %s`, expected, got)
73+
t.Errorf("expected: %s, got: %s", expected, got)
8374
}
8475

8576
if got, expected := msg4.ToString(), fmt.Sprintln("fix(lib)!: fix bug"); got != expected {
86-
t.Errorf(`expected: %s, got: %s`, expected, got)
77+
t.Errorf("expected: %s, got: %s", expected, got)
8778
}
8879
}
8980

9081
// TestCommitMsgBody
9182
// @spec conventional commits v1.0.0
9283
// 6. body MUST begin one blank line after the description
9384
func TestCommitMsgBody(t *testing.T) {
94-
msg1 := makeCommitMsg("docs", "", false, "fix typo", "msg body\nbody line2", []string{})
85+
body := fmt.Sprintln("msg body") + NL + "body line2"
86+
msg1 := makeCommitMsg("docs", "", false, "fix typo", body, []string{})
9587

96-
if s := msg1.ToString(); s != fmt.Sprintf("docs: fix typo%s%smsg body\nbody line2%s", NL, NL, NL) {
97-
t.Errorf(`expected: %s, got: %s`, `docs: fix typo\n\nmsg body\nbody line2\n`, s)
88+
if got, expected := msg1.ToString(), fmt.Sprintln("docs: fix typo")+NL+fmt.Sprintln(body); got != expected {
89+
t.Errorf("expected: %s%%, got: %s%%", expected, got)
9890
}
9991
}
10092

@@ -104,7 +96,7 @@ func TestParseFooter(t *testing.T) {
10496
}
10597

10698
// footer's value can have newlines
107-
if token, sep, val := ParseFooter("token: value1\nvalue2"); token != "token" || sep != FSepColonSpace || val != "value1\nvalue2" {
99+
if token, sep, val := ParseFooter(fmt.Sprintf("token: value1%svalue2", NL)); token != "token" || sep != FSepColonSpace || val != fmt.Sprintf("value1%svalue2", NL) {
108100
t.Error("ParseFooter check failed")
109101
}
110102

@@ -129,8 +121,8 @@ func TestParseFooter(t *testing.T) {
129121
func TestCommitMsgFooter(t *testing.T) {
130122
// test validation
131123
validFts := []string{
132-
fmt.Sprintf("%s: some\nchange\nof lines", FTokenBrkChange),
133-
fmt.Sprintf("%s: some\nchange\nof lines", FTokenBrkChangeAlias),
124+
fmt.Sprintf("%s: some%schange%sof lines", FTokenBrkChange, NL, NL),
125+
fmt.Sprintf("%s: some%schange%sof lines", FTokenBrkChangeAlias, NL, NL),
134126
"Acked-by: RT",
135127
"Reviewed: ", // separator space should not be trimed if no footer value
136128
"fix #1",
@@ -164,13 +156,13 @@ func TestCommitMsgFooter(t *testing.T) {
164156
}
165157

166158
footerAfterBodyMsg := makeCommitMsg("test", "spec 8a", false, "check newline after body", "body", []string{"Acked-by: RT"})
167-
if s := footerAfterBodyMsg.ToString(); s != strings.ReplaceAll("test(spec 8a): check newline after body%s%sbody%s%sAcked-by: RT%s", "%s", NL) {
168-
t.Errorf(`Spec rule 8a check failed, expected: %s, got: %s`, `test(spec 8a): check newline after body\n\nbody\n\nAcked-by: RT\n`, s)
159+
if got, expected := footerAfterBodyMsg.ToString(), strings.ReplaceAll("test(spec 8a): check newline after body%s%sbody%s%sAcked-by: RT%s", "%s", NL); got != expected {
160+
t.Errorf(`Spec rule 8a check failed, expected: %s%%, got: %s%%`, expected, got)
169161
}
170162

171163
footerAfterHeaderMsg := makeCommitMsg("test", "spec 8a", false, "check newline after header", "", []string{"Acked-by: RT"})
172-
if s := footerAfterHeaderMsg.ToString(); s != strings.ReplaceAll("test(spec 8a): check newline after header%s%sAcked-by: RT%s", "%s", NL) {
173-
t.Errorf(`Spec rule 8a check failed, expected: %s, got: %s`, `test(spec 8a): check newline after header\n\nAcked-by: RT\n`, s)
164+
if got, expected := footerAfterHeaderMsg.ToString(), strings.ReplaceAll("test(spec 8a): check newline after header%s%sAcked-by: RT%s", "%s", NL); got != expected {
165+
t.Errorf(`Spec rule 8a check failed, expected: %s%%, got: %s%%`, expected, got)
174166
}
175167
}
176168

templates/commitmsg.tmpl

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)