forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_details.go
232 lines (205 loc) · 8.32 KB
/
spec_details.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"github.com/getgauge/common"
"github.com/getgauge/gauge/config"
"github.com/getgauge/gauge/gauge_messages"
"github.com/getgauge/gauge/logger"
"github.com/getgauge/gauge/util"
"github.com/golang/protobuf/proto"
"path/filepath"
"strconv"
"sync"
"time"
)
type specInfoGatherer struct {
availableSpecs []*specification
availableStepsMap map[string]*stepValue
stepsFromRunner []string
specStepMapCache map[string][]*step
conceptInfos []*gauge_messages.ConceptInfo
mutex sync.Mutex
projectRoot string
}
func (specInfoGatherer *specInfoGatherer) makeListOfAvailableSteps(runner *testRunner) {
specInfoGatherer.availableStepsMap = make(map[string]*stepValue)
specInfoGatherer.specStepMapCache = make(map[string][]*step)
specInfoGatherer.stepsFromRunner = specInfoGatherer.getStepsFromRunner(runner)
specInfoGatherer.addStepValuesToAvailableSteps(specInfoGatherer.stepsFromRunner)
newSpecStepMap, conceptInfos := specInfoGatherer.getAllStepsFromSpecs()
specInfoGatherer.conceptInfos = conceptInfos
specInfoGatherer.addStepsToAvailableSteps(newSpecStepMap)
conceptStepsMap := specInfoGatherer.getAllStepsFromConcepts()
specInfoGatherer.addStepsToAvailableSteps(conceptStepsMap)
go specInfoGatherer.refreshSteps(config.ApiRefreshInterval())
}
func (specInfoGatherer *specInfoGatherer) getAllStepsFromSpecs() (map[string][]*step, []*gauge_messages.ConceptInfo) {
specFiles := util.FindSpecFilesIn(specInfoGatherer.projectRoot + fmt.Sprintf("%c", filepath.Separator) + common.SpecsDirectoryName)
dictionary, _ := createConceptsDictionary(true)
availableSpecs, parseResults := parseSpecFiles(specFiles, dictionary)
specInfoGatherer.handleParseFailures(parseResults)
specInfoGatherer.availableSpecs = availableSpecs
return specInfoGatherer.findAvailableStepsInSpecs(specInfoGatherer.availableSpecs), specInfoGatherer.createConceptInfos(dictionary)
}
func (specInfoGatherer *specInfoGatherer) handleParseFailures(parseResults []*parseResult) {
for _, result := range parseResults {
if !result.ok {
logger.ApiLog.Error("Spec Parse failure: %s", result.Error())
}
}
}
func (specInfoGatherer *specInfoGatherer) getAllTags() []string {
specFiles := util.FindSpecFilesIn(specInfoGatherer.projectRoot + fmt.Sprintf("%c", filepath.Separator) + common.SpecsDirectoryName)
dictionary, _ := createConceptsDictionary(true)
availableSpecs, parseResults := parseSpecFiles(specFiles, dictionary)
specInfoGatherer.handleParseFailures(parseResults)
specInfoGatherer.availableSpecs = availableSpecs
allTags := make(map[string]bool, 0)
for _, spec := range specInfoGatherer.availableSpecs {
for _, value := range spec.tags.values {
allTags[value] = true
}
for _, scenario := range spec.scenarios {
for _, value := range scenario.tags.values {
allTags[value] = true
}
}
}
tags := make([]string, 0)
for key, _ := range allTags {
tags = append(tags, key)
}
return tags
}
func (specInfoGatherer *specInfoGatherer) getAllStepsFromConcepts() map[string][]*step {
allStepsInConcepts := make(map[string][]*step, 0)
conceptFiles := util.FindConceptFilesIn(specInfoGatherer.projectRoot + fmt.Sprintf("%c", filepath.Separator) + common.SpecsDirectoryName)
for _, conceptFile := range conceptFiles {
fileText, fileReadErr := common.ReadFileContents(conceptFile)
if fileReadErr != nil {
logger.ApiLog.Error("failed to read concept file %s", conceptFile)
continue
}
concepts, err := new(conceptParser).parse(fileText)
if err != nil {
logger.ApiLog.Error("Concept Parse failure: %s: line no: %s, %s", conceptFile, strconv.Itoa(err.lineNo), err.message)
continue
}
conceptSteps := make([]*step, 0)
for _, concept := range concepts {
for _, conceptStep := range concept.conceptSteps {
conceptSteps = append(conceptSteps, conceptStep)
}
}
allStepsInConcepts[conceptFile] = conceptSteps
}
return allStepsInConcepts
}
func (specInfoGatherer *specInfoGatherer) createConceptInfos(dictionary *conceptDictionary) []*gauge_messages.ConceptInfo {
conceptInfos := make([]*gauge_messages.ConceptInfo, 0)
for _, concept := range dictionary.conceptsMap {
stepValue := createStepValue(concept.conceptStep)
conceptInfos = append(conceptInfos, &gauge_messages.ConceptInfo{StepValue: convertToProtoStepValue(&stepValue), Filepath: proto.String(concept.fileName), LineNumber: proto.Int(concept.conceptStep.lineNo)})
}
return conceptInfos
}
func (specInfoGatherer *specInfoGatherer) refreshSteps(seconds time.Duration) {
for {
time.Sleep(seconds)
specInfoGatherer.mutex.Lock()
specInfoGatherer.availableStepsMap = make(map[string]*stepValue, 0)
specInfoGatherer.addStepValuesToAvailableSteps(specInfoGatherer.stepsFromRunner)
newSpecStepMap, conceptInfos := specInfoGatherer.getAllStepsFromSpecs()
specInfoGatherer.conceptInfos = conceptInfos
specInfoGatherer.addStepsToAvailableSteps(newSpecStepMap)
conceptStepsMap := specInfoGatherer.getAllStepsFromConcepts()
specInfoGatherer.addStepsToAvailableSteps(conceptStepsMap)
specInfoGatherer.mutex.Unlock()
}
}
func (specInfoGatherer *specInfoGatherer) getStepsFromRunner(runner *testRunner) []string {
steps := make([]string, 0)
if runner == nil {
runner, connErr := startRunnerAndMakeConnection(getProjectManifest(getCurrentExecutionLogger()), getCurrentExecutionLogger())
if connErr == nil {
steps = append(steps, requestForSteps(runner)...)
logger.ApiLog.Debug("Steps got from runner: %v", steps)
runner.kill(getCurrentExecutionLogger())
}
if connErr != nil {
logger.ApiLog.Error("Runner connection failed: %s", connErr)
}
} else {
steps = append(steps, requestForSteps(runner)...)
logger.ApiLog.Debug("Steps got from runner: %v", steps)
}
return steps
}
func (specInfoGatherer *specInfoGatherer) findAvailableStepsInSpecs(specs []*specification) map[string][]*step {
specStepsMap := make(map[string][]*step)
for _, spec := range specs {
stepsInSpec := make([]*step, 0)
stepsInSpec = append(stepsInSpec, spec.contexts...)
for _, scenario := range spec.scenarios {
stepsInSpec = append(stepsInSpec, scenario.steps...)
}
specStepsMap[spec.fileName] = stepsInSpec
}
return specStepsMap
}
func (specInfoGatherer *specInfoGatherer) addStepsToAvailableSteps(newSpecStepsMap map[string][]*step) {
specInfoGatherer.updateCache(newSpecStepsMap)
for _, steps := range specInfoGatherer.specStepMapCache {
for _, step := range steps {
if step.isConcept {
continue
}
stepValue := createStepValue(step)
if _, ok := specInfoGatherer.availableStepsMap[stepValue.stepValue]; !ok {
specInfoGatherer.availableStepsMap[stepValue.stepValue] = &stepValue
}
}
}
}
func (specInfoGatherer *specInfoGatherer) updateCache(newSpecStepsMap map[string][]*step) {
for fileName, specsteps := range newSpecStepsMap {
specInfoGatherer.specStepMapCache[fileName] = specsteps
}
}
func (specInfoGatherer *specInfoGatherer) addStepValuesToAvailableSteps(stepValues []string) {
for _, step := range stepValues {
specInfoGatherer.addToAvailableSteps(step)
}
}
func (specInfoGatherer *specInfoGatherer) addToAvailableSteps(stepText string) {
stepValue, err := extractStepValueAndParams(stepText, false)
if err == nil {
if _, ok := specInfoGatherer.availableStepsMap[stepValue.stepValue]; !ok {
specInfoGatherer.availableStepsMap[stepValue.stepValue] = stepValue
}
}
}
func (specInfoGatherer *specInfoGatherer) getAvailableSteps() []*stepValue {
if specInfoGatherer.availableStepsMap == nil {
specInfoGatherer.makeListOfAvailableSteps(nil)
}
specInfoGatherer.mutex.Lock()
steps := make([]*stepValue, 0)
for _, stepValue := range specInfoGatherer.availableStepsMap {
steps = append(steps, stepValue)
}
specInfoGatherer.mutex.Unlock()
return steps
}