@@ -2,23 +2,14 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
- "runtime"
6
5
"strings"
7
6
"testing"
8
7
)
9
8
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 ()
19
10
20
11
func TestContainsNewline (t * testing.T ) {
21
- if ! ContainsNewline (NL ) {
12
+ if ! ContainsNewline (fmt . Sprintln () ) {
22
13
t .Error ("ContainsNewline check failed" )
23
14
}
24
15
}
@@ -53,15 +44,15 @@ func TestCommitMsgHeader(t *testing.T) {
53
44
noDescMsg := makeCommitMsg ("fix" , "" , false , "" , "" , []string {})
54
45
55
46
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 )
57
48
}
58
49
59
50
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 )
61
52
}
62
53
63
54
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 )
65
56
}
66
57
67
58
// test toString format
@@ -71,30 +62,31 @@ func TestCommitMsgHeader(t *testing.T) {
71
62
msg4 := makeCommitMsg ("fix" , "lib" , true , "fix bug" , "" , []string {})
72
63
73
64
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 )
75
66
}
76
67
77
68
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 )
79
70
}
80
71
81
72
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 )
83
74
}
84
75
85
76
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 )
87
78
}
88
79
}
89
80
90
81
// TestCommitMsgBody
91
82
// @spec conventional commits v1.0.0
92
83
// 6. body MUST begin one blank line after the description
93
84
func TestCommitMsgBody (t * testing.T ) {
94
- msg1 := makeCommitMsg ("docs" , "" , false , "fix typo" , "msg body\n body line2" , []string {})
85
+ body := fmt .Sprintln ("msg body" ) + NL + "body line2"
86
+ msg1 := makeCommitMsg ("docs" , "" , false , "fix typo" , body , []string {})
95
87
96
- if s := msg1 .ToString (); s != fmt .Sprintf ("docs: fix typo%s%smsg body \n body 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 )
98
90
}
99
91
}
100
92
@@ -104,7 +96,7 @@ func TestParseFooter(t *testing.T) {
104
96
}
105
97
106
98
// footer's value can have newlines
107
- if token , sep , val := ParseFooter ("token: value1\n value2" ) ; token != "token" || sep != FSepColonSpace || val != "value1\n value2" {
99
+ if token , sep , val := ParseFooter (fmt . Sprintf ( "token: value1%svalue2" , NL )) ; token != "token" || sep != FSepColonSpace || val != fmt . Sprintf ( "value1%svalue2" , NL ) {
108
100
t .Error ("ParseFooter check failed" )
109
101
}
110
102
@@ -129,8 +121,8 @@ func TestParseFooter(t *testing.T) {
129
121
func TestCommitMsgFooter (t * testing.T ) {
130
122
// test validation
131
123
validFts := []string {
132
- fmt .Sprintf ("%s: some\n change \n of lines" , FTokenBrkChange ),
133
- fmt .Sprintf ("%s: some\n change \n of lines" , FTokenBrkChangeAlias ),
124
+ fmt .Sprintf ("%s: some%schange%sof lines" , FTokenBrkChange , NL , NL ),
125
+ fmt .Sprintf ("%s: some%schange%sof lines" , FTokenBrkChangeAlias , NL , NL ),
134
126
"Acked-by: RT" ,
135
127
"Reviewed: " , // separator space should not be trimed if no footer value
136
128
"fix #1" ,
@@ -164,13 +156,13 @@ func TestCommitMsgFooter(t *testing.T) {
164
156
}
165
157
166
158
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 )
169
161
}
170
162
171
163
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 )
174
166
}
175
167
}
176
168
0 commit comments