From 7ec913dde681498ab559319f8163a502be8a6f91 Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Thu, 29 Aug 2024 14:13:54 +0200 Subject: [PATCH] Allow tests to be in 'test' package And allow a test file to be named `test.rego` This is a Styra DAS convention, and while I'd say `_test` is preferred, I think it's OK to allow this single exception. Also updated the test-outside-test-package rule to report end location. Signed-off-by: Anders Eknert --- .../file_missing_test_suffix.rego | 7 +++++- .../file_missing_test_suffix_test.rego | 23 ++++++++++++++++++- .../test_outside_test_package.rego | 9 ++++++-- .../test_outside_test_package_test.rego | 8 ++++++- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/bundle/regal/rules/testing/file-missing-test-suffix/file_missing_test_suffix.rego b/bundle/regal/rules/testing/file-missing-test-suffix/file_missing_test_suffix.rego index 076484d0..87f5f8c7 100644 --- a/bundle/regal/rules/testing/file-missing-test-suffix/file_missing_test_suffix.rego +++ b/bundle/regal/rules/testing/file-missing-test-suffix/file_missing_test_suffix.rego @@ -10,7 +10,12 @@ import data.regal.result report contains violation if { count(ast.tests) > 0 - not endswith(input.regal.file.name, "_test.rego") + not _valid_test_file_name(input.regal.file.name) violation := result.fail(rego.metadata.chain(), {"location": {"file": input.regal.file.name}}) } + +_valid_test_file_name(filename) if endswith(filename, "_test.rego") + +# Styra DAS convention considered OK +_valid_test_file_name("test.rego") diff --git a/bundle/regal/rules/testing/file-missing-test-suffix/file_missing_test_suffix_test.rego b/bundle/regal/rules/testing/file-missing-test-suffix/file_missing_test_suffix_test.rego index 728bee6a..0f0d6f3c 100644 --- a/bundle/regal/rules/testing/file-missing-test-suffix/file_missing_test_suffix_test.rego +++ b/bundle/regal/rules/testing/file-missing-test-suffix/file_missing_test_suffix_test.rego @@ -3,6 +3,7 @@ package regal.rules.testing["file-missing-test-suffix_test"] import rego.v1 import data.regal.config + import data.regal.rules.testing["file-missing-test-suffix"] as rule test_fail_test_in_file_without_test_suffix if { @@ -11,7 +12,7 @@ test_fail_test_in_file_without_test_suffix if { test_foo { false } `) - r := rule.report with input as ast with config.for_rule as {"level": "error"} + r := rule.report with input as ast r == {{ "category": "testing", "description": "Files containing tests should have a _test.rego suffix", @@ -24,3 +25,23 @@ test_fail_test_in_file_without_test_suffix if { "level": "error", }} } + +test_success_test_in_file_with_test_suffix if { + ast := regal.parse_module("policy_test.rego", `package policy_test + + test_foo { false } + `) + + r := rule.report with input as ast + r == set() +} + +test_success_test_in_file_named_test if { + ast := regal.parse_module("test.rego", `package test + + test_foo { false } + `) + + r := rule.report with input as ast + r == set() +} diff --git a/bundle/regal/rules/testing/test-outside-test-package/test_outside_test_package.rego b/bundle/regal/rules/testing/test-outside-test-package/test_outside_test_package.rego index 4c948694..58e360a9 100644 --- a/bundle/regal/rules/testing/test-outside-test-package/test_outside_test_package.rego +++ b/bundle/regal/rules/testing/test-outside-test-package/test_outside_test_package.rego @@ -8,9 +8,14 @@ import data.regal.ast import data.regal.result report contains violation if { - not endswith(ast.package_name, "_test") + not _is_test_package(ast.package_name) some rule in ast.tests - violation := result.fail(rego.metadata.chain(), result.location(rule.head)) + violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(rule.head)) } + +_is_test_package(package_name) if endswith(package_name, "_test") + +# Styra DAS convention considered OK +_is_test_package("test") diff --git a/bundle/regal/rules/testing/test-outside-test-package/test_outside_test_package_test.rego b/bundle/regal/rules/testing/test-outside-test-package/test_outside_test_package_test.rego index 8653de82..825e18a2 100644 --- a/bundle/regal/rules/testing/test-outside-test-package/test_outside_test_package_test.rego +++ b/bundle/regal/rules/testing/test-outside-test-package/test_outside_test_package_test.rego @@ -18,7 +18,13 @@ test_fail_test_outside_test_package if { "ref": config.docs.resolve_url("$baseUrl/$category/test-outside-test-package", "testing"), }], "title": "test-outside-test-package", - "location": {"col": 1, "file": "p_test.rego", "row": 5, "text": `test_foo if { false }`}, + "location": { + "col": 1, + "file": "p_test.rego", + "row": 5, + "end": {"col": 9, "row": 5}, + "text": `test_foo if { false }`, + }, "level": "error", }} }