file proto missing vars in flow & multi-protocol (#5480)

* fix missing template context in file proto

* fix file protocol missing vars

* fix test

* skip example advanced test
This commit is contained in:
Tarun Koyalwar 2024-08-04 18:14:08 +05:30 committed by GitHub
parent ddcc9211be
commit 2df1b2e88e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 41 additions and 13 deletions

View File

@ -62,9 +62,10 @@ jobs:
run: go run . run: go run .
working-directory: examples/simple/ working-directory: examples/simple/
- name: Example SDK Advanced # Temporarily disabled very flaky in github actions
run: go run . # - name: Example SDK Advanced
working-directory: examples/advanced/ # run: go run .
# working-directory: examples/advanced/
- name: Example SDK with speed control - name: Example SDK with speed control
run: go run . run: go run .

View File

@ -51,6 +51,9 @@ func (request *Request) ExecuteWithResults(input *contextargs.Context, metadata,
if err != nil { if err != nil {
return err return err
} }
if input.MetaInput.Input == "" {
return errors.New("input cannot be empty file or folder expected")
}
err = request.getInputPaths(input.MetaInput.Input, func(filePath string) { err = request.getInputPaths(input.MetaInput.Input, func(filePath string) {
wg.Add() wg.Add()
func(filePath string) { func(filePath string) {
@ -250,6 +253,8 @@ func (request *Request) findMatchesWithReader(reader io.Reader, input *contextar
for k, v := range previous { for k, v := range previous {
dslMap[k] = v dslMap[k] = v
} }
// add vars to template context
request.options.AddTemplateVars(input.MetaInput, request.Type(), request.ID, dslMap)
// add template context variables to DSL map // add template context variables to DSL map
if request.options.HasTemplateCtx(input.MetaInput) { if request.options.HasTemplateCtx(input.MetaInput) {
dslMap = generators.MergeMaps(dslMap, request.options.GetTemplateCtx(input.MetaInput).GetAll()) dslMap = generators.MergeMaps(dslMap, request.options.GetTemplateCtx(input.MetaInput).GetAll())
@ -323,7 +328,6 @@ func (request *Request) buildEvent(input, filePath string, fileMatches []FileMat
exprLines[fileMatch.Expr] = append(exprLines[fileMatch.Expr], fileMatch.Line) exprLines[fileMatch.Expr] = append(exprLines[fileMatch.Expr], fileMatch.Line)
exprBytes[fileMatch.Expr] = append(exprBytes[fileMatch.Expr], fileMatch.ByteIndex) exprBytes[fileMatch.Expr] = append(exprBytes[fileMatch.Expr], fileMatch.ByteIndex)
} }
event := eventcreator.CreateEventWithOperatorResults(request, internalEvent, operatorResult) event := eventcreator.CreateEventWithOperatorResults(request, internalEvent, operatorResult)
// Annotate with line numbers if asked by the user // Annotate with line numbers if asked by the user
if request.options.Options.ShowMatchLine { if request.options.Options.ShowMatchLine {

View File

@ -197,6 +197,11 @@ func (e *ExecutorOptions) AddTemplateVars(input *contextargs.MetaInput, reqType
} }
templateCtx := e.GetTemplateCtx(input) templateCtx := e.GetTemplateCtx(input)
for k, v := range vars { for k, v := range vars {
if stringsutil.HasPrefixAny(k, templateTypes.SupportedProtocolsStrings()...) {
// this was inherited from previous protocols no need to modify it we can directly set it or omit
templateCtx.Set(k, v)
continue
}
if !stringsutil.EqualFoldAny(k, "template-id", "template-info", "template-path") { if !stringsutil.EqualFoldAny(k, "template-id", "template-info", "template-path") {
if reqID != "" { if reqID != "" {
k = reqID + "_" + k k = reqID + "_" + k
@ -216,6 +221,11 @@ func (e *ExecutorOptions) AddTemplateVar(input *contextargs.MetaInput, templateT
return return
} }
templateCtx := e.GetTemplateCtx(input) templateCtx := e.GetTemplateCtx(input)
if stringsutil.HasPrefixAny(key, templateTypes.SupportedProtocolsStrings()...) {
// this was inherited from previous protocols no need to modify it we can directly set it or omit
templateCtx.Set(key, value)
return
}
if reqID != "" { if reqID != "" {
key = reqID + "_" + key key = reqID + "_" + key
} else if templateType < templateTypes.InvalidProtocol { } else if templateType < templateTypes.InvalidProtocol {

View File

@ -69,6 +69,18 @@ func GetSupportedProtocolTypes() ProtocolTypes {
return result return result
} }
// SupportedProtocolsStrings returns a slice of strings of supported protocols
func SupportedProtocolsStrings() []string {
var result []string
for _, protocol := range GetSupportedProtocolTypes() {
if protocol.String() == "" {
continue
}
result = append(result, protocol.String())
}
return result
}
func toProtocolType(valueToMap string) (ProtocolType, error) { func toProtocolType(valueToMap string) (ProtocolType, error) {
normalizedValue := normalizeValue(valueToMap) normalizedValue := normalizeValue(valueToMap)
for key, currentValue := range protocolMappings { for key, currentValue := range protocolMappings {

View File

@ -27,15 +27,15 @@ func setup() {
progressImpl, _ := progress.NewStatsTicker(0, false, false, false, 0) progressImpl, _ := progress.NewStatsTicker(0, false, false, false, 0)
executerOpts = protocols.ExecutorOptions{ executerOpts = protocols.ExecutorOptions{
Output: testutils.NewMockOutputWriter(options.OmitTemplate), Output: testutils.NewMockOutputWriter(options.OmitTemplate),
Options: options, Options: options,
Progress: progressImpl, Progress: progressImpl,
ProjectFile: nil, ProjectFile: nil,
IssuesClient: nil, IssuesClient: nil,
Browser: nil, Browser: nil,
Catalog: disk.NewCatalog(config.DefaultConfig.TemplatesDirectory), Catalog: disk.NewCatalog(config.DefaultConfig.TemplatesDirectory),
RateLimiter: ratelimit.New(context.Background(), uint(options.RateLimit), time.Second), RateLimiter: ratelimit.New(context.Background(), uint(options.RateLimit), time.Second),
Parser: templates.NewParser(), Parser: templates.NewParser(),
} }
workflowLoader, err := workflow.NewLoader(&executerOpts) workflowLoader, err := workflow.NewLoader(&executerOpts)
if err != nil { if err != nil {
@ -146,6 +146,7 @@ func TestFlowWithConditionPositive(t *testing.T) {
} }
func TestFlowWithNoMatchers(t *testing.T) { func TestFlowWithNoMatchers(t *testing.T) {
setup()
// when using conditional flow with no matchers at all // when using conditional flow with no matchers at all
// we implicitly assume that request was successful and internally changed the result to true (for scope of condition only) // we implicitly assume that request was successful and internally changed the result to true (for scope of condition only)