Skip to content

Commit 4a790ba

Browse files
committed
Add node scope to compiler. Fix tests; the stats count was not being tested properly leading to false negatives.
1 parent 99a8dd2 commit 4a790ba

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

internal/pkg/engine/engine.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ func compileRuleTree(cf compiler.RuntimeI, tree *parser.TreeT) (compiler.ObjsT,
8080

8181
opts := []compiler.CompilerOptT{
8282
compiler.WithRuntime(cf),
83+
compiler.WithPlugin(schema.ScopeNode, compiler.NewDefaultPlugin()),
8384
}
8485

85-
if nodeObjs, err = compiler.CompileTree(tree, schema.ScopeDefault, opts...); err != nil {
86+
if nodeObjs, err = compiler.CompileTree(tree, schema.ScopeNode, opts...); err != nil {
8687
return nil, err
8788
}
8889

internal/pkg/ux/eval.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ LOOP:
9494
}
9595

9696
return StatsT{
97-
"rules": u.Rules,
98-
"problems": u.Problems,
97+
"rules": int64(u.Rules),
98+
"problems": int64(u.Problems),
9999
"lines": u.Lines.Load(),
100100
"bytes": u.Bytes.Value(),
101101
}, nil

internal/pkg/ux/eval_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,31 @@ func TestUxEvalT_FinalStats(t *testing.T) {
148148
}
149149

150150
// Convert map values to the correct type for comparison
151-
rules, ok := stats["rules"].(uint32)
151+
rules, ok := stats["rules"]
152152
if !ok {
153153
t.Fatal("Expected rules to be uint32")
154154
}
155155
if rules != 5 {
156156
t.Errorf("Expected rules to be 5, got %d", rules)
157157
}
158158

159-
problems, ok := stats["problems"].(uint32)
159+
problems, ok := stats["problems"]
160160
if !ok {
161161
t.Fatal("Expected problems to be uint32")
162162
}
163163
if problems != 2 {
164164
t.Errorf("Expected problems to be 2, got %d", problems)
165165
}
166166

167-
lines, ok := stats["lines"].(int64)
167+
lines, ok := stats["lines"]
168168
if !ok {
169169
t.Fatal("Expected lines to be int64")
170170
}
171171
if lines != 1000 {
172172
t.Errorf("Expected lines to be 1000, got %d", lines)
173173
}
174174

175-
bytes, ok := stats["bytes"].(int64)
175+
bytes, ok := stats["bytes"]
176176
if !ok {
177177
t.Fatal("Expected bytes to be int64")
178178
}
@@ -190,16 +190,16 @@ func TestUxEvalT_FinalStats(t *testing.T) {
190190
t.Errorf("Expected no error, got %v", err)
191191
}
192192

193-
if rules, ok := stats["rules"].(uint32); !ok || rules != 0 {
193+
if rules, ok := stats["rules"]; !ok || rules != 0 {
194194
t.Errorf("Expected rules to be 0, got %v", rules)
195195
}
196-
if problems, ok := stats["problems"].(uint32); !ok || problems != 0 {
196+
if problems, ok := stats["problems"]; !ok || problems != 0 {
197197
t.Errorf("Expected problems to be 0, got %v", problems)
198198
}
199-
if lines, ok := stats["lines"].(int64); !ok || lines != 0 {
199+
if lines, ok := stats["lines"]; !ok || lines != 0 {
200200
t.Errorf("Expected lines to be 0, got %v", lines)
201201
}
202-
if bytes, ok := stats["bytes"].(int64); !ok || bytes != 0 {
202+
if bytes, ok := stats["bytes"]; !ok || bytes != 0 {
203203
t.Errorf("Expected bytes to be 0, got %v", bytes)
204204
}
205205
})

internal/pkg/ux/ux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ var (
103103
HelpAcceptUpdates = "Accept updates to rules or new release"
104104
)
105105

106-
type StatsT map[string]any
106+
type StatsT map[string]int64
107107

108108
type UxFactoryI interface {
109109
NewBytesTracker(src string) (*progress.Tracker, error)

test/preq_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestSuccessExamples(t *testing.T) {
2828
var tests = map[string]struct {
2929
rulePath string
3030
dataPath string
31+
negative bool
3132
}{
3233
"Example00": {
3334
rulePath: "../examples/00-rules-document-example.yaml",
@@ -44,10 +45,12 @@ func TestSuccessExamples(t *testing.T) {
4445
"Example03": {
4546
rulePath: "../examples/03-set-negative-example.yaml",
4647
dataPath: "../examples/03-example.log",
48+
negative: true,
4749
},
4850
"Example04": {
4951
rulePath: "../examples/04-set-1x1-example.yaml",
5052
dataPath: "../examples/04-example.log",
53+
negative: true,
5154
},
5255
"Example08": {
5356
rulePath: "../examples/08-sequence-example-good-window.yaml",
@@ -56,6 +59,7 @@ func TestSuccessExamples(t *testing.T) {
5659
"Example09": {
5760
rulePath: "../examples/09-sequence-negate-example.yaml",
5861
dataPath: "../examples/09-example.log",
62+
negative: true,
5963
},
6064
"Example13": {
6165
rulePath: "../examples/13-string-example.yaml",
@@ -84,6 +88,7 @@ func TestSuccessExamples(t *testing.T) {
8488
"Example21": {
8589
rulePath: "../examples/21-negative-example.yaml",
8690
dataPath: "../examples/21-example.log",
91+
negative: true,
8792
},
8893
"Example22": {
8994
rulePath: "../examples/21-negative-example.yaml",
@@ -144,8 +149,15 @@ func TestSuccessExamples(t *testing.T) {
144149
t.Fatalf("Error running detection: %v", err)
145150
}
146151

147-
if stats["problems"] == 0 {
148-
t.Fatalf("Expected problems, got %d", stats["problems"])
152+
switch stats["problems"] {
153+
case 0:
154+
if !test.negative {
155+
t.Fatalf("Expected problems, got %d", stats["problems"])
156+
}
157+
default:
158+
if test.negative {
159+
t.Fatalf("Expected no problems, got %d", stats["problems"])
160+
}
149161
}
150162
})
151163
}
@@ -232,7 +244,7 @@ func TestMissExamples(t *testing.T) {
232244
t.Fatalf("Error running detection: %v", err)
233245
}
234246

235-
if stats["problems"] != uint32(0) {
247+
if stats["problems"] != 0 {
236248
t.Fatalf("Expected no problems, got %d", stats["problems"])
237249
}
238250
})

0 commit comments

Comments
 (0)