diff --git a/bundle/regal/rules/style/line_length.rego b/bundle/regal/rules/style/line_length.rego index 2be95d39..1fab2215 100644 --- a/bundle/regal/rules/style/line_length.rego +++ b/bundle/regal/rules/style/line_length.rego @@ -11,13 +11,17 @@ import data.regal.result cfg := config.for_rule("style", "line-length") +default max_line_length := 120 + +max_line_length := cfg["max-line-length"] + report contains violation if { some i, line in input.regal.file.lines line != "" line_length := count(line) - line_length > cfg["max-line-length"] + line_length > max_line_length not has_word_above_threshold(line, cfg) diff --git a/bundle/regal/rules/style/line_length_test.rego b/bundle/regal/rules/style/line_length_test.rego index eba55ce4..9eda5d21 100644 --- a/bundle/regal/rules/style/line_length_test.rego +++ b/bundle/regal/rules/style/line_length_test.rego @@ -57,7 +57,31 @@ test_fail_line_too_long_but_below_breakable_word_threshold if { }, "related_resources": [{ "description": "documentation", - "ref": "https://docs.styra.com/regal/rules/style/line-length", + "ref": config.docs.resolve_url("$baseUrl/$category/line-length", "style"), + }], + "title": "line-length", + }} +} + +test_fail_line_exceeds_120_characters_even_if_not_in_config if { + # regal ignore:line-length + r := rule.report with input as ast.with_future_keywords(`# Long url: https://www.example.com/this/is/a/very/long/url/that/cannot/be/shortened/and/should/trigger/an/error/anyway/so/that/it/can/be/shortened + allow := true + `) + with config.for_rule as {"level": "error"} + r == {{ + "category": "style", + "description": "Line too long", + "level": "error", + "location": { + "col": 147, + "file": "policy.rego", + # regal ignore:line-length + "row": 8, "text": "# Long url: https://www.example.com/this/is/a/very/long/url/that/cannot/be/shortened/and/should/trigger/an/error/anyway/so/that/it/can/be/shortened", + }, + "related_resources": [{ + "description": "documentation", + "ref": config.docs.resolve_url("$baseUrl/$category/line-length", "style"), }], "title": "line-length", }}