Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions cmd/numbat/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ func runRulesList(args []string, stdout, stderr io.Writer) int {

// runRulesTest evaluates the compiled rules against a fixture of NDJSON
// events (one model.Event per line) and prints each match as "rule_id\tevent_id".
// It is the deterministic, offline check that rules fire as intended.
// It is the deterministic, offline check that rules fire as intended. With
// --json the same evaluation emits a machine-readable NDJSON result contract
// on stdout instead: one event_result per successfully evaluated fixture
// line plus a terminal summary. See docs/schema/rules-test-result.v1.md.
func runRulesTest(args []string, stdout, stderr io.Writer) int {
fs := flag.NewFlagSet("rules test", flag.ContinueOnError)
fs.SetOutput(stderr)
Expand All @@ -315,11 +318,12 @@ func runRulesTest(args []string, stdout, stderr io.Writer) int {
expectNone := fs.Bool("expect-none", false, "exit non-zero if any rule matches (for negative fixtures)")
var expect multiFlag
fs.Var(&expect, "expect", "rule ID expected to match at least once (repeatable)")
jsonOut := fs.Bool("json", false, "emit a machine-readable NDJSON result stream (schema rules-test-result.v1) instead of tab-separated matches")
var rf ruleFlags
rf.register(fs)
fs.Usage = func() {
fmt.Fprintln(stderr, "usage: numbat rules test --fixture FILE [--require-match] [--expect RULE_ID ...] [--expect-none] [--rules-dir DIR ...] [--no-builtin-rules]")
fmt.Fprintln(stderr, "\nEvaluate fixture events and print rule_id<TAB>event_id for each match.")
fmt.Fprintln(stderr, "usage: numbat rules test --fixture FILE [--json] [--require-match] [--expect RULE_ID ...] [--expect-none] [--rules-dir DIR ...] [--no-builtin-rules]")
fmt.Fprintln(stderr, "\nEvaluate fixture events and print rule_id<TAB>event_id for each match, or a JSON result stream with --json.")
fs.PrintDefaults()
}
if err := fs.Parse(args); err != nil {
Expand Down Expand Up @@ -356,6 +360,10 @@ func runRulesTest(args []string, stdout, stderr io.Writer) int {
return 1
}

if *jsonOut {
return runRulesTestJSON(eng, f, stdout, stderr, *requireMatch, *expectNone, expect)
}

matched, matchedRules, evalErr := evalFixture(eng, f, stdout)
if evalErr != nil {
fmt.Fprintln(stderr, evalErr.Error())
Expand Down
Loading