@@ -2,6 +2,8 @@ package chglog
2
2
3
3
import (
4
4
"fmt"
5
+ "log"
6
+ "os"
5
7
"testing"
6
8
"time"
7
9
@@ -11,9 +13,69 @@ import (
11
13
"github.com/go-git/go-git/v5/plumbing"
12
14
"github.com/go-git/go-git/v5/plumbing/object"
13
15
"github.com/go-git/go-git/v5/storage/memory"
14
- "github.com/smartystreets/goconvey/convey"
16
+ . "github.com/smartystreets/goconvey/convey"
15
17
)
16
18
19
+ type testRepo struct {
20
+ Git * git.Repository
21
+ Source * git.Worktree
22
+ seqno int
23
+ }
24
+
25
+ func newTestRepo () * testRepo {
26
+ var (
27
+ repo * git.Repository
28
+ tree * git.Worktree
29
+ err error
30
+ )
31
+
32
+ fs := memfs .New ()
33
+
34
+ if repo , err = git .Init (memory .NewStorage (), fs ); err != nil {
35
+ log .Fatal (err )
36
+ }
37
+
38
+ if tree , err = repo .Worktree (); err != nil {
39
+ log .Fatal (err )
40
+ }
41
+
42
+ return & testRepo {
43
+ Git : repo ,
44
+ Source : tree ,
45
+ }
46
+ }
47
+
48
+ // modifyAndCommit creates the file if it does not exist, appends a
49
+ // change, commits the file, and returns the hash of the commit.
50
+ func (r * testRepo ) modifyAndCommit (filename string , opts * git.CommitOptions ) plumbing.Hash {
51
+ var (
52
+ hash plumbing.Hash
53
+ err error
54
+ file billy.File
55
+ )
56
+
57
+ if file , err = r .Source .Filesystem .OpenFile (filename , os .O_RDWR | os .O_CREATE | os .O_APPEND , 0666 ); err != nil {
58
+ log .Fatal (err )
59
+ }
60
+ defer file .Close ()
61
+
62
+ if _ , err = file .Write ([]byte (fmt .Sprintf ("commit %d\n " , r .seqno ))); err != nil {
63
+ log .Fatal (err )
64
+ }
65
+
66
+ if _ , err = r .Source .Add (filename ); err != nil {
67
+ log .Fatal (err )
68
+ }
69
+
70
+ if hash , err = r .Source .Commit (fmt .Sprintf ("commit %d" , r .seqno ), opts ); err != nil {
71
+ log .Fatal (err )
72
+ }
73
+
74
+ r .seqno ++
75
+
76
+ return hash
77
+ }
78
+
17
79
func defSignature () * object.Signature {
18
80
tm , err := time .Parse (time .RFC3339 , "2000-01-01T12:00:00+07:00" )
19
81
if err != nil {
@@ -34,64 +96,66 @@ func defCommitOptions() *git.CommitOptions {
34
96
}
35
97
}
36
98
37
- func newTestRepo () (* git.Repository , error ) {
38
- fs := memfs .New ()
39
-
40
- return git .Init (memory .NewStorage (), fs )
41
- }
42
-
43
99
func TestOrderChangelog (t * testing.T ) {
44
- var (
45
- gitRepo * git.Repository
46
- gitTree * git.Worktree
47
- file billy.File
48
- testCLE ChangeLogEntries
49
- )
50
-
51
- goldcle , err := Parse ("./testdata/gold-order-changelog.yml" )
100
+ goldCLE , err := Parse ("./testdata/gold-order-changelog.yml" )
52
101
if err != nil {
53
102
t .Fatal (err )
54
103
}
55
104
56
- if gitRepo , err = newTestRepo (); err != nil {
57
- t .Fatal (err )
58
- }
105
+ repo := newTestRepo ()
59
106
60
- if gitTree , err = gitRepo .Worktree (); err != nil {
61
- t .Fatal (err )
107
+ for i := 0 ; i <= 10 ; i ++ {
108
+ hash := repo .modifyAndCommit ("file" , defCommitOptions ())
109
+
110
+ if _ , err = repo .Git .CreateTag (fmt .Sprintf ("v0.%d.0" , i ), hash , nil ); err != nil {
111
+ t .Fatal (err )
112
+ }
62
113
}
63
114
64
- if file , err = gitTree .Filesystem .Create ("file" ); err != nil {
115
+ testCLE , err := InitChangelog (repo .Git , "" , nil , nil , false )
116
+ if err != nil {
65
117
t .Fatal (err )
66
118
}
67
- defer file .Close ()
68
119
69
- for i := 0 ; i <= 10 ; i ++ {
70
- var hash plumbing.Hash
120
+ Convey ("Generated entry should be the same as the golden entry" , t , func () {
121
+ So (testCLE , ShouldResemble , goldCLE )
122
+ })
71
123
72
- if _ , err = file .Write ([]byte (fmt .Sprintf ("commit %d\n " , i ))); err != nil {
124
+ }
125
+
126
+ func TestSemverTag (t * testing.T ) {
127
+ repo := newTestRepo ()
128
+ tag := "1.0.0"
129
+
130
+ Convey ("Semver tags should be parsed" , t , func () {
131
+ hash := repo .modifyAndCommit ("file" , defCommitOptions ())
132
+
133
+ if _ , err := repo .Git .CreateTag (tag , hash , nil ); err != nil {
73
134
t .Fatal (err )
74
135
}
75
136
76
- if _ , err = gitTree .Add ("file" ); err != nil {
77
- t .Fatal ()
137
+ cle , err := InitChangelog (repo .Git , "" , nil , nil , false )
138
+ if err != nil {
139
+ t .Fatal (err )
78
140
}
79
141
80
- if hash , err = gitTree .Commit (fmt .Sprintf ("commit %d" , i ), defCommitOptions ()); err != nil {
81
- t .Fatal ()
82
- }
142
+ So (cle , ShouldHaveLength , 1 )
143
+ So (cle [0 ].Semver , ShouldEqual , tag )
144
+ })
145
+
146
+ Convey ("Not Semver tags should be ignored" , t , func () {
147
+ hash := repo .modifyAndCommit ("file" , defCommitOptions ())
83
148
84
- if _ , err = gitRepo . CreateTag (fmt . Sprintf ( "v0.%d.0" , i ) , hash , nil ); err != nil {
85
- t .Fatal ()
149
+ if _ , err := repo . Git . CreateTag ("text" , hash , nil ); err != nil {
150
+ t .Fatal (err )
86
151
}
87
- }
88
152
89
- if testCLE , err = InitChangelog (gitRepo , "" , nil , nil , false ); err != nil {
90
- t .Fatal (err )
91
- }
153
+ cle , err := InitChangelog (repo .Git , "" , nil , nil , false )
154
+ if err != nil {
155
+ t .Fatal (err )
156
+ }
92
157
93
- convey . Convey ( "Generated entry should be the same as the golden entry" , t , func () {
94
- convey . So (testCLE , convey . ShouldResemble , goldcle )
158
+ So ( cle , ShouldHaveLength , 1 )
159
+ So (cle [ 0 ]. Semver , ShouldEqual , tag )
95
160
})
96
-
97
161
}
0 commit comments