Created
September 15, 2023 19:10
-
-
Save iamleot/2017b3c27ddb44572c32ea90630dfcee to your computer and use it in GitHub Desktop.
Mechanically try to solve test-outside-test-package Regal violation for conftest policies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
# | |
# Mechanically rewrite OPA policy in order to satisfy requirements of Regal | |
# test-outside-test-package check. Limited to conftest by only checking and | |
# adjusting `deny', `warning', `violation' rules. | |
# | |
# <https://docs.styra.com/regal/rules/testing/test-outside-test-package> | |
# | |
/^package/ { | |
# Sanity check that full_package and package are not defined multiple | |
# times | |
if (full_package) { | |
printf "error: full_package already defined as %s\n", | |
full_package > "/dev/stderr" | |
exit 1 | |
} | |
if (package) { | |
printf "error: package already defined as %s\n", | |
package > "/dev/stderr" | |
exit 1 | |
} | |
# Extract last part of the package name, if any | |
full_package = $2 | |
n = split($2, a, "\.") | |
if (n > 0) { | |
package = a[n] | |
} else { | |
package = full_package | |
} | |
# Rewrite the package by adding the `_test' suffix as required and | |
# import the actual package. | |
printf "package %s_test\n", full_package | |
printf "\n" | |
printf "import data.%s\n", full_package | |
next | |
} | |
/^test_.*{/ { | |
# ignore any possible `deny' or similar in the middle of the test name | |
next | |
} | |
{ | |
# Rewrite all `deny', `violation', `warn' rules by prefixing according | |
# the corresponding imported package. | |
gsub("deny", package ".deny") | |
gsub("violation", package ".violation") | |
gsub("warn", package ".warn") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment