66 "testing"
77
88 "github.com/bitrise-io/go-utils/pathutil"
9+ "github.com/bitrise-io/stepman/models"
910 "github.com/bitrise-io/stepman/stepid"
11+ "github.com/stretchr/testify/require"
1012)
1113
1214func Test_activateStepLibStep (t * testing.T ) {
@@ -99,7 +101,7 @@ func Test_activateStepLibStep(t *testing.T) {
99101 t .Errorf ("failed to create dir for step.yml: %s" , err )
100102 }
101103
102- got , _ , err := prepareStepLibForActivation (TestLogger {t }, tt .stepIDData , false , false )
104+ got , _ , err := prepareStepLibForActivation (TestLogger [ * testing. T ] {t }, tt .stepIDData , false , false )
103105 if (err != nil ) != tt .wantErr {
104106 t .Errorf ("activateStepLibStep() error = %v, wantErr %v" , err , tt .wantErr )
105107 return
@@ -111,19 +113,105 @@ func Test_activateStepLibStep(t *testing.T) {
111113 }
112114}
113115
114- type TestLogger struct {
115- t * testing. T
116+ type genericLogger interface {
117+ Logf ( format string , v ... any )
116118}
117119
118- func ( t TestLogger ) Debugf ( format string , v ... interface {}) {
119- t . t . Logf ( format , v ... )
120+ type TestLogger [ t genericLogger ] struct {
121+ l genericLogger
120122}
121- func (t TestLogger ) Errorf (format string , v ... interface {}) {
122- t .t .Logf (format , v ... )
123+
124+ func (t TestLogger [l ]) Debugf (format string , v ... any ) {
125+ t .l .Logf (format , v ... )
126+ }
127+ func (t TestLogger [l ]) Errorf (format string , v ... any ) {
128+ t .l .Logf (format , v ... )
123129}
124- func (t TestLogger ) Warnf (format string , v ... interface {} ) {
125- t .t .Logf (format , v ... )
130+ func (t TestLogger [ l ] ) Warnf (format string , v ... any ) {
131+ t .l .Logf (format , v ... )
126132}
127- func (t TestLogger ) Infof (format string , v ... interface {}) {
128- t .t .Logf (format , v ... )
133+ func (t TestLogger [l ]) Infof (format string , v ... any ) {
134+ t .l .Logf (format , v ... )
135+ }
136+
137+ func BenchmarkActivateSteplibRefStep (b * testing.B ) {
138+ logger := TestLogger [* testing.B ]{b }
139+ tests := []struct {
140+ name string
141+ id stepid.CanonicalID
142+ isOfflineMode bool
143+ didStepLibUpdateInWorkflow bool
144+ shouldCleanSteplib bool
145+ wantErr bool
146+ }{
147+ {
148+ name : "No steplib update, major versiom" ,
149+ id : stepid.CanonicalID {
150+ SteplibSource : "https://github.com/bitrise-io/bitrise-steplib.git" ,
151+ IDorURI : "xcode-archive" ,
152+ Version : "1" ,
153+ },
154+ didStepLibUpdateInWorkflow : true ,
155+ wantErr : false ,
156+ },
157+ {
158+ name : "Steplib update, major versiom" ,
159+ id : stepid.CanonicalID {
160+ SteplibSource : "https://github.com/bitrise-io/bitrise-steplib.git" ,
161+ IDorURI : "xcode-archive" ,
162+ Version : "1" ,
163+ },
164+ didStepLibUpdateInWorkflow : false ,
165+ wantErr : false ,
166+ },
167+ {
168+ name : "Clean steplib every time" ,
169+ id : stepid.CanonicalID {
170+ SteplibSource : "https://github.com/bitrise-io/bitrise-steplib.git" ,
171+ IDorURI : "xcode-archive" ,
172+ Version : "1" ,
173+ },
174+ didStepLibUpdateInWorkflow : false ,
175+ shouldCleanSteplib : true ,
176+ wantErr : false ,
177+ },
178+ }
179+ for _ , tt := range tests {
180+ b .Run (tt .name , func (b * testing.B ) {
181+ for b .Loop () {
182+ if tt .shouldCleanSteplib {
183+ err := os .RemoveAll ("~/.stepman" )
184+ require .NoError (b , err )
185+ }
186+ tmpDir , err := pathutil .NormalizedOSTempDirPath ("activateStepLibStep" )
187+ if err != nil {
188+ b .Errorf ("failed to create tmp dir: %s" , err )
189+ }
190+ stepYMLCopyPth := filepath .Join (tmpDir , "step-yml" , "step.yml" )
191+
192+ if err := os .MkdirAll (filepath .Dir (stepYMLCopyPth ), 0777 ); err != nil {
193+ b .Errorf ("failed to create dir for step.yml: %s" , err )
194+ }
195+
196+ stepInfoPtr := new (models.StepInfoModel )
197+ got , gotErr := ActivateSteplibRefStep (logger , tt .id , stepYMLCopyPth , tmpDir , tt .didStepLibUpdateInWorkflow , tt .isOfflineMode , stepInfoPtr )
198+ if gotErr != nil {
199+ if ! tt .wantErr {
200+ b .Errorf ("ActivateSteplibRefStep() failed: %v" , gotErr )
201+ }
202+ return
203+ }
204+ if tt .wantErr {
205+ b .Fatal ("ActivateSteplibRefStep() succeeded unexpectedly" )
206+ }
207+
208+ want := ActivatedStep {
209+ StepYMLPath : tmpDir + "/current_step.yml" ,
210+ ActivationType : ActivationTypeSteplibSource ,
211+ DidStepLibUpdate : ! tt .didStepLibUpdateInWorkflow ,
212+ }
213+ require .Equal (b , want , got )
214+ }
215+ })
216+ }
129217}
0 commit comments