Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tests to be in 'test' package #1027

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand All @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}}
}
Expand Down