Skip to content

Commit

Permalink
(fix): Bugfix for issue #163
Browse files Browse the repository at this point in the history
The end node cannot be calculated if there is no content supplied (i.e. the wrong object type is passed in).

Signed-off-by: Dave Shanley <dave@quobix.com>
  • Loading branch information
daveshanley committed Oct 31, 2022
1 parent 827bdef commit 5d6135c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion functions/core/truthy.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ func (t *Truthy) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
}

if !utils.IsNodeMap(fieldNode) && !utils.IsNodeArray(fieldNodeValue) {
var endNode *yaml.Node
if len(node.Content) > 0 {
endNode = node.Content[len(node.Content)-1]
} else {
endNode = node
}
results = append(results, model.RuleFunctionResult{
Message: fmt.Sprintf("%s: `%s` must be set", context.Rule.Description, context.RuleAction.Field),
StartNode: node,
EndNode: node.Content[len(node.Content)-1],
EndNode: endNode,
Path: pathValue,
Rule: context.Rule,
})
Expand Down
20 changes: 20 additions & 0 deletions functions/core/truthy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ tags:
assert.Len(t, res, 2)
}

func TestTruthy_RunRule_NoContent(t *testing.T) {

sampleYaml := `info: test`

path := "$.info"

nodes, _ := utils.FindNodes([]byte(sampleYaml), path)
assert.Len(t, nodes, 1)

rule := buildCoreTestRule(path, model.SeverityError, "truthy", "info", nil)
ctx := buildCoreTestContext(model.CastToRuleAction(rule.Then), nil)
ctx.Given = path
ctx.Rule = &rule

tru := Truthy{}
res := tru.RunRule(nodes, ctx)

assert.Len(t, res, 1)
}

func TestTruthy_RunRule_ArrayTest(t *testing.T) {

sampleYaml := `- lemons:
Expand Down

0 comments on commit 5d6135c

Please sign in to comment.