Skip to content

Commit

Permalink
Updated truthy to operate correctly
Browse files Browse the repository at this point in the history
tests were failing

Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Apr 3, 2024
1 parent eb21309 commit 2dfc2ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
33 changes: 17 additions & 16 deletions functions/core/truthy.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,25 @@ func (t *Truthy) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
}

if !utils.IsNodeMap(fieldNode) && !utils.IsNodeArray(fieldNodeValue) && !utils.IsNodeMap(fieldNodeValue) {
origin := context.Index.FindNodeOrigin(node)
if origin.Line > 1 {
nm := context.Index.GetNodeMap()
var keys []int
for k := range nm {
keys = append(keys, k)
}

// Sort the keys slice.
sort.Ints(keys)

np := nm[origin.Line-1][keys[0]]

if np != nil {
node = np
if context.Index != nil {
origin := context.Index.FindNodeOrigin(node)
if origin != nil && origin.Line > 1 {
nm := context.Index.GetNodeMap()
var keys []int
for k := range nm {
keys = append(keys, k)
}

// Sort the keys slice.
sort.Ints(keys)

np := nm[origin.Line-1][keys[0]]

if np != nil {
node = np
}
}
}

results = append(results, model.RuleFunctionResult{
Message: vacuumUtils.SuppliedOrDefault(message,
fmt.Sprintf("%s: `%s` must be set", ruleMessage, context.RuleAction.Field)),
Expand Down
9 changes: 5 additions & 4 deletions motor/rule_applicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,8 @@ func TestApplyRules_Length_Description_BadConfig(t *testing.T) {
}
results := ApplyRulesToRuleSet(rse)
assert.Len(t, results.Errors, 0)
assert.Len(t, results.Results, 0)
assert.Len(t, results.Results, 1)
assert.Equal(t, "'length' needs 'min' or 'max' (or both) properties being set to operate: minimum property number not met (1)", results.Results[0].Message)

}

Expand Down Expand Up @@ -2058,7 +2059,7 @@ custom:

assert.Len(t, results.Results, 1)
assert.Equal(t, "name 'hello' and id '1234' are not 'some_name' or 'some_id'", results.Results[0].Message)
assert.Equal(t, "my-custom-js-rule", results.Results[0].RuleId)
assert.Equal(t, "my-custom-js-rule", results.Results[0].Rule.Id)
assert.Equal(t, 3, results.Results[0].Range.Start.Line)
}

Expand Down Expand Up @@ -2104,7 +2105,7 @@ notCustom: true"

assert.Len(t, results.Results, 2)
assert.Equal(t, "core me up: `custom` must be set", results.Results[0].Message)
assert.Equal(t, "my-custom-js-rule", results.Results[0].RuleId)
assert.Equal(t, "my-custom-js-rule", results.Results[0].Rule.Id)
assert.Equal(t, "this is a message, added after truthy was called", results.Results[1].Message)
assert.Equal(t, 2, results.Results[0].Range.Start.Line)
}
Expand Down Expand Up @@ -2202,7 +2203,7 @@ notCustom: true"

assert.Len(t, results.Results, 1)
assert.Equal(t, "someOption is set to someValue", results.Results[0].Message)
assert.Equal(t, "my-custom-js-rule", results.Results[0].RuleId)
assert.Equal(t, "my-custom-js-rule", results.Results[0].Rule.Id)
assert.Equal(t, 2, results.Results[0].Range.Start.Line)
}

Expand Down

0 comments on commit 2dfc2ed

Please sign in to comment.