{"record":{"id":"f6b36260950304f3","repo":"crowdsecurity/crowdsec","slug":"unable-to-run-assert-s-w","errorCode":null,"errorMessage":"unable to run assert '%s': %w","messagePattern":"unable to run assert '(.+?)': %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/hubtest/parser_assert.go","lineNumber":97,"sourceCode":"\tif err := p.LoadTest(testFile); err != nil {\n\t\treturn fmt.Errorf(\"unable to load parser dump file '%s': %w\", testFile, err)\n\t}\n\n\tscanner := bufio.NewScanner(file)\n\tscanner.Split(bufio.ScanLines)\n\n\tnbLine := 0\n\n\tfor scanner.Scan() {\n\t\tnbLine++\n\n\t\tif scanner.Text() == \"\" {\n\t\t\tcontinue\n\t\t}\n\n\t\tok, err := p.Run(scanner.Text())\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to run assert '%s': %w\", scanner.Text(), err)\n\t\t}\n\n\t\tp.NbAssert++\n\n\t\tif !ok {\n\t\t\tlog.Debugf(\"%s is FALSE\", scanner.Text())\n\t\t\tfailedAssert := &AssertFail{\n\t\t\t\tFile:       p.File,\n\t\t\t\tLine:       nbLine,\n\t\t\t\tExpression: scanner.Text(),\n\t\t\t\tDebug:      make(map[string]string),\n\t\t\t}\n\n\t\t\tmatch := variableRE.FindStringSubmatch(scanner.Text())\n\n\t\t\tvar variable string\n\n\t\t\tif len(match) == 0 {","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/hubtest/parser_assert.go#L79-L115","documentation":"AssertFile in the hubtest package evaluates each line of a .assert file as an expression. If ParserAssert.Run (which compiles/evaluates the expression via expr) returns an error, the error is wrapped with the offending assertion text so the developer can see which line failed. It means the assertion expression itself could not be executed (syntax error, unknown field/function, runtime evaluation error), not that the assertion was false.","triggerScenarios":"Calling AssertFile (directly or via RunWithLogFile) on an assert file containing a line that expr fails to compile or evaluate: malformed expression, reference to a field absent from the event map, or a runtime type error inside the expression.","commonSituations":"Hand-edited .assert files in hub tests with typos; assertions referencing parser fields that were renamed or no longer produced; expr syntax unsupported by the expr-lang version in use; empty/whitespace-only lines are skipped, but stray characters (trailing commas, unmatched quotes) are not.","solutions":["Run the failing assertion text through EvalExpression directly to get the underlying expr compile/runtime error and fix the expression syntax","Verify every field referenced in the assertion exists in the event map produced by the parser (add debug dump of the evt map)","Regenerate the assertions with cscli hubtest auto-generate if the parser output changed","Check the expr-lang/library version for syntax incompatibilities"],"exampleFix":"// before (assert file)\nAlert.GetScope() == 'ip' and Alert.GetValue == '1.2.3.4'\n// after\nAlert.GetScope() == 'ip' and Alert.GetValue() == '1.2.3.4'","handlingStrategy":"try-catch","validationCode":"// validate assertions before running\nlines := strings.Split(string(assertContent), \"\\n\")\nfor _, l := range lines {\n    l = strings.TrimSpace(l)\n    if l == \"\" { continue }\n    if _, err := p.EvalExpression(l); err != nil {\n        return fmt.Errorf(\"invalid assertion %q: %w\", l, err)\n    }\n}","typeGuard":"func isBoolOutput(out interface{}) bool {\n    _, ok := out.(bool)\n    return ok\n}","tryCatchPattern":"if err := p.AssertFile(assertFile, snapFilePath, testFile); err != nil {\n    var exprErr interface{ Unwrap() error }\n    if errors.As(err, &wrapped) {\n        log.Errorf(\"assertion failed: %v (cause: %v)\", err, errors.Unwrap(err))\n    }\n    return err\n}","preventionTips":["Lint every .assert line through EvalExpression before committing hub tests","Keep assertions as explicit boolean comparisons","Regenerate assertions whenever parser output fields change","Test with `cscli hubtest run <test>` before submitting"],"tags":["expr","hubtest","assertion"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}