@@ -2,11 +2,12 @@ package avancement
22
33import (
44 "errors"
5+ "fmt"
56 "path"
67 "path/filepath"
8+ "regexp"
79 "strings"
810 "testing"
9- "regexp"
1011
1112 "github.com/jenkins-x/go-scm/scm"
1213 fakescm "github.com/jenkins-x/go-scm/scm/driver/fake"
@@ -21,14 +22,18 @@ const (
2122)
2223
2324func TestPromoteWithSuccessKeepCacheTrue (t * testing.T ) {
24- promoteWithSuccess (t , true )
25+ promoteWithSuccess (t , true , "" )
2526}
2627
2728func TestPromoteWithSuccessKeepCacheFalse (t * testing.T ) {
28- promoteWithSuccess (t , false )
29+ promoteWithSuccess (t , false , "" )
30+ }
31+
32+ func TestPromoteWithSuccessCustomMsg (t * testing.T ) {
33+ promoteLocalWithSuccess (t , true , "custom commit message here" )
2934}
3035
31- func promoteWithSuccess (t * testing.T , keepCache bool ) {
36+ func promoteWithSuccess (t * testing.T , keepCache bool , msg string ) {
3237 dstBranch := "test-branch"
3338 author := & git.
Author {
Name :
"Testing User" ,
Email :
"[email protected] " ,
Token :
"test-token" }
3439 devRepo , stagingRepo := mock .New ("/dev" , "master" ), mock .New ("/staging" , "master" )
@@ -46,14 +51,20 @@ func promoteWithSuccess(t *testing.T, keepCache bool) {
4651 }
4752 devRepo .AddFiles ("/services/my-service/base/config/myfile.yaml" )
4853
49- err := sm .Promote ("my-service" , dev , staging , dstBranch , keepCache )
54+ err := sm .Promote ("my-service" , dev , staging , dstBranch , msg , keepCache )
5055 if err != nil {
5156 t .Fatal (err )
5257 }
5358
59+ expectedCommitMsg := msg
60+ if msg == "" {
61+ commit := devRepo .GetCommitID ()
62+ expectedCommitMsg = fmt .Sprintf ("Promoting service `my-service` at commit `%s` from branch `master` in `%s`." , commit , dev )
63+ }
64+
5465 stagingRepo .AssertBranchCreated (t , "master" , dstBranch )
5566 stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "/dev/services/my-service/base/config/myfile.yaml" , "/staging/services/my-service/base/config/myfile.yaml" )
56- stagingRepo .AssertCommit (t , dstBranch , defaultCommitMsg , author )
67+ stagingRepo .AssertCommit (t , dstBranch , expectedCommitMsg , author )
5768 stagingRepo .AssertPush (t , dstBranch )
5869
5970 if keepCache {
@@ -66,14 +77,18 @@ func promoteWithSuccess(t *testing.T, keepCache bool) {
6677}
6778
6879func TestPromoteLocalWithSuccessKeepCacheFalse (t * testing.T ) {
69- promoteLocalWithSuccess (t , false )
80+ promoteLocalWithSuccess (t , false , "" )
7081}
7182
7283func TestPromoteLocalWithSuccessKeepCacheTrue (t * testing.T ) {
73- promoteLocalWithSuccess (t , true )
84+ promoteLocalWithSuccess (t , true , "" )
7485}
7586
76- func promoteLocalWithSuccess (t * testing.T , keepCache bool ) {
87+ func TestPromoteLocalWithSuccessCustomMsg (t * testing.T ) {
88+ promoteLocalWithSuccess (t , true , "custom commit message supplied" )
89+ }
90+
91+ func promoteLocalWithSuccess (t * testing.T , keepCache bool , msg string ) {
7792 dstBranch := "test-branch"
7893 author := & git.
Author {
Name :
"Testing User" ,
Email :
"[email protected] " ,
Token :
"test-token" }
7994 stagingRepo := mock .New ("/staging" , "master" )
@@ -93,14 +108,19 @@ func promoteLocalWithSuccess(t *testing.T, keepCache bool) {
93108 sm .debug = true
94109 devRepo .AddFiles ("/config/myfile.yaml" )
95110
96- err := sm .Promote ("my-service" , ldev , staging , dstBranch , keepCache )
111+ err := sm .Promote ("my-service" , ldev , staging , dstBranch , msg , keepCache )
97112 if err != nil {
98113 t .Fatal (err )
99114 }
100115
116+ expectedCommitMsg := msg
117+ if expectedCommitMsg == "" {
118+ expectedCommitMsg = "Promotion of service `my-service` from local filesystem directory `/root/repo`."
119+ }
120+
101121 stagingRepo .AssertBranchCreated (t , "master" , dstBranch )
102122 stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "/dev/config/myfile.yaml" , "/staging/services/my-service/base/config/myfile.yaml" )
103- stagingRepo .AssertCommit (t , dstBranch , defaultCommitMsg , author )
123+ stagingRepo .AssertCommit (t , dstBranch , expectedCommitMsg , author )
104124 stagingRepo .AssertPush (t , dstBranch )
105125
106126 if keepCache {
@@ -161,22 +181,25 @@ func TestPromoteWithCacheDeletionFailure(t *testing.T) {
161181 }
162182 devRepo .AddFiles ("/services/my-service/base/config/myfile.yaml" )
163183
164- err := sm .Promote ("my-service" , dev , staging , dstBranch , false )
184+ err := sm .Promote ("my-service" , dev , staging , dstBranch , "" , false )
165185 if err != nil {
166186 t .Fatal (err )
167187 }
168188
189+ commit := devRepo .GetCommitID ()
190+ expectedCommitMsg := fmt .Sprintf ("Promoting service `my-service` at commit `%s` from branch `master` in `%s`." , commit , dev )
191+
169192 stagingRepo .AssertBranchCreated (t , "master" , dstBranch )
170193 stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "/dev/services/my-service/base/config/myfile.yaml" , "/staging/services/my-service/base/config/myfile.yaml" )
171- stagingRepo .AssertCommit (t , dstBranch , defaultCommitMsg , author )
194+ stagingRepo .AssertCommit (t , dstBranch , expectedCommitMsg , author )
172195 stagingRepo .AssertPush (t , dstBranch )
173196
174197 stagingRepo .AssertNotDeletedFromCache (t )
175198 devRepo .AssertDeletedFromCache (t )
176199}
177200
178201func TestGenerateBranchWithSuccess (t * testing.T ) {
179- repo := mock .New ("/dev" , "master" )
202+ repo := mock .New ("/dev" , "master" )
180203 GenerateBranchWithSuccess (t , repo )
181204}
182205
0 commit comments