From 5f7a594f3a994a7c7579be4ae64bcd748c34bedd Mon Sep 17 00:00:00 2001 From: Sean Cunningham Date: Wed, 14 Jan 2026 11:52:55 -0500 Subject: [PATCH] Add node scope to compiler. Fix tests; the stats count was not being tested properly leading to false negatives. --- internal/pkg/engine/engine.go | 3 ++- internal/pkg/ux/eval.go | 4 ++-- internal/pkg/ux/eval_test.go | 16 ++++++++-------- internal/pkg/ux/ux.go | 2 +- test/preq_test.go | 18 +++++++++++++++--- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/internal/pkg/engine/engine.go b/internal/pkg/engine/engine.go index 7de52e7..13997b1 100644 --- a/internal/pkg/engine/engine.go +++ b/internal/pkg/engine/engine.go @@ -80,9 +80,10 @@ func compileRuleTree(cf compiler.RuntimeI, tree *parser.TreeT) (compiler.ObjsT, opts := []compiler.CompilerOptT{ compiler.WithRuntime(cf), + compiler.WithPlugin(schema.ScopeNode, compiler.NewDefaultPlugin()), } - if nodeObjs, err = compiler.CompileTree(tree, schema.ScopeDefault, opts...); err != nil { + if nodeObjs, err = compiler.CompileTree(tree, schema.ScopeNode, opts...); err != nil { return nil, err } diff --git a/internal/pkg/ux/eval.go b/internal/pkg/ux/eval.go index 1356921..4849556 100644 --- a/internal/pkg/ux/eval.go +++ b/internal/pkg/ux/eval.go @@ -94,8 +94,8 @@ LOOP: } return StatsT{ - "rules": u.Rules, - "problems": u.Problems, + "rules": int64(u.Rules), + "problems": int64(u.Problems), "lines": u.Lines.Load(), "bytes": u.Bytes.Value(), }, nil diff --git a/internal/pkg/ux/eval_test.go b/internal/pkg/ux/eval_test.go index 23c2d4e..28b63d4 100644 --- a/internal/pkg/ux/eval_test.go +++ b/internal/pkg/ux/eval_test.go @@ -148,7 +148,7 @@ func TestUxEvalT_FinalStats(t *testing.T) { } // Convert map values to the correct type for comparison - rules, ok := stats["rules"].(uint32) + rules, ok := stats["rules"] if !ok { t.Fatal("Expected rules to be uint32") } @@ -156,7 +156,7 @@ func TestUxEvalT_FinalStats(t *testing.T) { t.Errorf("Expected rules to be 5, got %d", rules) } - problems, ok := stats["problems"].(uint32) + problems, ok := stats["problems"] if !ok { t.Fatal("Expected problems to be uint32") } @@ -164,7 +164,7 @@ func TestUxEvalT_FinalStats(t *testing.T) { t.Errorf("Expected problems to be 2, got %d", problems) } - lines, ok := stats["lines"].(int64) + lines, ok := stats["lines"] if !ok { t.Fatal("Expected lines to be int64") } @@ -172,7 +172,7 @@ func TestUxEvalT_FinalStats(t *testing.T) { t.Errorf("Expected lines to be 1000, got %d", lines) } - bytes, ok := stats["bytes"].(int64) + bytes, ok := stats["bytes"] if !ok { t.Fatal("Expected bytes to be int64") } @@ -190,16 +190,16 @@ func TestUxEvalT_FinalStats(t *testing.T) { t.Errorf("Expected no error, got %v", err) } - if rules, ok := stats["rules"].(uint32); !ok || rules != 0 { + if rules, ok := stats["rules"]; !ok || rules != 0 { t.Errorf("Expected rules to be 0, got %v", rules) } - if problems, ok := stats["problems"].(uint32); !ok || problems != 0 { + if problems, ok := stats["problems"]; !ok || problems != 0 { t.Errorf("Expected problems to be 0, got %v", problems) } - if lines, ok := stats["lines"].(int64); !ok || lines != 0 { + if lines, ok := stats["lines"]; !ok || lines != 0 { t.Errorf("Expected lines to be 0, got %v", lines) } - if bytes, ok := stats["bytes"].(int64); !ok || bytes != 0 { + if bytes, ok := stats["bytes"]; !ok || bytes != 0 { t.Errorf("Expected bytes to be 0, got %v", bytes) } }) diff --git a/internal/pkg/ux/ux.go b/internal/pkg/ux/ux.go index 6e0e73e..dfe2a0b 100644 --- a/internal/pkg/ux/ux.go +++ b/internal/pkg/ux/ux.go @@ -103,7 +103,7 @@ var ( HelpAcceptUpdates = "Accept updates to rules or new release" ) -type StatsT map[string]any +type StatsT map[string]int64 type UxFactoryI interface { NewBytesTracker(src string) (*progress.Tracker, error) diff --git a/test/preq_test.go b/test/preq_test.go index 2643840..c091a11 100644 --- a/test/preq_test.go +++ b/test/preq_test.go @@ -28,6 +28,7 @@ func TestSuccessExamples(t *testing.T) { var tests = map[string]struct { rulePath string dataPath string + negative bool }{ "Example00": { rulePath: "../examples/00-rules-document-example.yaml", @@ -44,10 +45,12 @@ func TestSuccessExamples(t *testing.T) { "Example03": { rulePath: "../examples/03-set-negative-example.yaml", dataPath: "../examples/03-example.log", + negative: true, }, "Example04": { rulePath: "../examples/04-set-1x1-example.yaml", dataPath: "../examples/04-example.log", + negative: true, }, "Example08": { rulePath: "../examples/08-sequence-example-good-window.yaml", @@ -56,6 +59,7 @@ func TestSuccessExamples(t *testing.T) { "Example09": { rulePath: "../examples/09-sequence-negate-example.yaml", dataPath: "../examples/09-example.log", + negative: true, }, "Example13": { rulePath: "../examples/13-string-example.yaml", @@ -84,6 +88,7 @@ func TestSuccessExamples(t *testing.T) { "Example21": { rulePath: "../examples/21-negative-example.yaml", dataPath: "../examples/21-example.log", + negative: true, }, "Example22": { rulePath: "../examples/21-negative-example.yaml", @@ -144,8 +149,15 @@ func TestSuccessExamples(t *testing.T) { t.Fatalf("Error running detection: %v", err) } - if stats["problems"] == 0 { - t.Fatalf("Expected problems, got %d", stats["problems"]) + switch stats["problems"] { + case 0: + if !test.negative { + t.Fatalf("Expected problems, got %d", stats["problems"]) + } + default: + if test.negative { + t.Fatalf("Expected no problems, got %d", stats["problems"]) + } } }) } @@ -232,7 +244,7 @@ func TestMissExamples(t *testing.T) { t.Fatalf("Error running detection: %v", err) } - if stats["problems"] != uint32(0) { + if stats["problems"] != 0 { t.Fatalf("Expected no problems, got %d", stats["problems"]) } })