Skip to content

Commit 766c93e

Browse files
authored
Merge pull request #6 from wallester/feature/WA-21534-Manageable-randomiser-for-unit-tests
2 parents 6d2b3f0 + 2c675a3 commit 766c93e

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

suite/suite.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import (
55
"fmt"
66
"math/rand"
77
"os"
8+
"path"
89
"reflect"
910
"regexp"
11+
"runtime"
1012
"runtime/debug"
13+
"strings"
1114
"sync"
1215
"testing"
1316
"time"
@@ -122,6 +125,15 @@ func (suite *Suite) Run(name string, subtest func()) bool {
122125
func Run(t *testing.T, suite TestingSuite) {
123126
defer recoverAndFailOnPanic(t)
124127

128+
// getting path to file where the call was made
129+
_, file, _, ok := runtime.Caller(1)
130+
if !ok {
131+
fmt.Println("Could not get caller information")
132+
os.Exit(1)
133+
}
134+
135+
serviceName := cleanPath(file)
136+
125137
suite.SetT(t)
126138
suite.SetS(suite)
127139

@@ -218,9 +230,12 @@ func Run(t *testing.T, suite TestingSuite) {
218230
}()
219231
}
220232

221-
rand.Shuffle(len(tests), func(i, j int) {
222-
tests[i], tests[j] = tests[j], tests[i]
223-
})
233+
// Tests will be shuffled if service name is in servicesToShuffle map
234+
if _, ok := servicesToShuffle[serviceName]; ok {
235+
rand.Shuffle(len(tests), func(i, j int) {
236+
tests[i], tests[j] = tests[j], tests[i]
237+
})
238+
}
224239

225240
runTests(t, tests)
226241
}
@@ -256,3 +271,25 @@ func runTests(t testing.TB, tests []testing.InternalTest) {
256271
type runner interface {
257272
Run(name string, f func(t *testing.T)) bool
258273
}
274+
275+
func cleanPath(path string) string {
276+
replaced := strings.Replace(path, monorepoPath+"/", "", -1)
277+
278+
splittedPath := strings.Split(replaced, "/")
279+
280+
// checking if service is from monorepo/pkg folder
281+
if splittedPath[0] == "pkg" {
282+
return splittedPath[1]
283+
}
284+
285+
return splittedPath[0]
286+
}
287+
288+
var monorepoPath = path.Join(os.Getenv("GOPATH"), "src", "github.com", "wallester", "monorepo")
289+
290+
// services map which are prepared for shuffled tests run
291+
// to be removed after fixing all services tests
292+
var servicesToShuffle = map[string]struct{}{
293+
"automation": {},
294+
"card-reference-numbers-management-service": {},
295+
}

0 commit comments

Comments
 (0)