github.com/getgauge/gauge@v1.6.9/filter/specsFilter.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package filter 8 9 import ( 10 "strings" 11 12 "github.com/getgauge/gauge/gauge" 13 "github.com/getgauge/gauge/logger" 14 ) 15 16 type specsFilter interface { 17 filter([]*gauge.Specification) []*gauge.Specification 18 } 19 20 type tagsFilter struct { 21 tagExp string 22 } 23 24 type tagFilterForParallelRun struct { 25 tagExp string 26 } 27 28 type specsGroupFilter struct { 29 group int 30 execStreams int 31 } 32 33 type scenariosFilter struct { 34 scenarios []string 35 } 36 37 func (tf *tagFilterForParallelRun) filter(specs []*gauge.Specification) ([]*gauge.Specification, []*gauge.Specification) { 38 return filterByTags(tf.tagExp, specs) 39 } 40 41 func (tagsFilter *tagsFilter) filter(specs []*gauge.Specification) []*gauge.Specification { 42 specs, _ = filterByTags(tagsFilter.tagExp, specs) 43 return specs 44 } 45 46 func filterByTags(tagExpression string, specs []*gauge.Specification) ([]*gauge.Specification, []*gauge.Specification) { 47 if tagExpression != "" { 48 logger.Debugf(true, "Applying tags filter: %s", tagExpression) 49 validateTagExpression(tagExpression) 50 return filterSpecsByTags(specs, tagExpression) 51 } 52 return specs, specs 53 } 54 55 func (groupFilter *specsGroupFilter) filter(specs []*gauge.Specification) []*gauge.Specification { 56 if groupFilter.group == -1 { 57 return specs 58 } 59 logger.Infof(true, "Using the -g flag will make the distribution strategy 'eager'. The --strategy setting will be overridden.") 60 if groupFilter.group < 1 || groupFilter.group > groupFilter.execStreams { 61 return make([]*gauge.Specification, 0) 62 } 63 logger.Debugf(true, "Applying group filter: %d", groupFilter.group) 64 group := DistributeSpecs(specs, groupFilter.execStreams)[groupFilter.group-1] 65 if group == nil { 66 return make([]*gauge.Specification, 0) 67 } 68 return group.Specs() 69 } 70 71 func (scenarioFilter *scenariosFilter) filter(specs []*gauge.Specification) []*gauge.Specification { 72 if len(scenarioFilter.scenarios) != 0 { 73 logger.Debugf(true, "Applying scenarios filter: %s", strings.Join(scenarioFilter.scenarios, ", ")) 74 specs = filterSpecsByScenarioName(specs, scenarioFilter.scenarios) 75 } 76 return specs 77 } 78 79 func DistributeSpecs(specifications []*gauge.Specification, distributions int) []*gauge.SpecCollection { 80 s := make([]*gauge.SpecCollection, distributions) 81 for i := 0; i < len(specifications); i++ { 82 mod := i % distributions 83 if s[mod] == nil { 84 s[mod] = gauge.NewSpecCollection(make([]*gauge.Specification, 0), false) 85 } 86 s[mod].Add(specifications[i]) 87 } 88 return s 89 }