diff --git a/bazel/bundle.py b/bazel/bundle.py index 8027d20..729189d 100644 --- a/bazel/bundle.py +++ b/bazel/bundle.py @@ -1,7 +1,7 @@ r"""Script to bundle CEL policy conformance tests. Each CEL policy conformance test is a self-contained directory containing an -optional environment, a policy, and a test suite. This script +optional environment (config.yaml), a policy, and a test suite. This script bundles them all together into a single multidocument YAML file to simplify file loading. @@ -22,7 +22,6 @@ Usage: bazel run //third_party/cel/policy/bazel:bundle -- \ - --environment= \ --policy= \ --test= \ --output= @@ -35,16 +34,21 @@ def _read_and_format_section(path: str) -> bytes: + # We're assuming UTF-8 here. Unclear if all of the CEL policy parsers + # correctly handle non-UTF-8 since they directly read some parts of the source + # instead of relying on the yaml parse. with open(path, "rb") as f: content = f.read().rstrip() comment = f"# {os.path.basename(path)}".encode("utf-8") return comment + b"\n" + content -def bundle(environment: str | None, policy: str, test: str) -> bytes: +def bundle(policy: str, test: str) -> bytes: sections = [] - if environment: - sections.append(_read_and_format_section(environment)) + policy_dir = os.path.dirname(policy) + config_path = os.path.join(policy_dir, "config.yaml") + if os.path.exists(config_path): + sections.append(_read_and_format_section(config_path)) sections.append(_read_and_format_section(policy)) sections.append(_read_and_format_section(test)) return b"\n---\n".join(sections) + b"\n" @@ -54,12 +58,6 @@ def main(argv: Sequence[str]) -> None: parser = argparse.ArgumentParser( description="Bundle CEL policy conformance tests." ) - parser.add_argument( - "--environment", - help="Path to the environment file.", - required=False, - default=None, - ) parser.add_argument( "--policy", help="Path to the policy file.", @@ -76,7 +74,7 @@ def main(argv: Sequence[str]) -> None: required=True, ) args = parser.parse_args(argv[1:]) - bundled_content = bundle(args.environment, args.policy, args.test) + bundled_content = bundle(args.policy, args.test) with open(args.output, "wb") as f: f.write(bundled_content) diff --git a/bazel/test_bundle.bzl b/bazel/test_bundle.bzl index 64e0c72..66e77ab 100644 --- a/bazel/test_bundle.bzl +++ b/bazel/test_bundle.bzl @@ -15,18 +15,10 @@ """Defines a bazel rule for bundling CEL policy conformance tests.""" def _cel_policy_test_bundle_impl(ctx): - inputs = [] + inputs = [ctx.file.policy, ctx.file.test] + ctx.files._testdata args = ctx.actions.args() - if ctx.file.environment: - inputs.append(ctx.file.environment) - args.add("--environment", ctx.file.environment.path) - if ctx.file.policy: - inputs.append(ctx.file.policy) - args.add("--policy", ctx.file.policy.path) - if ctx.file.test: - inputs.append(ctx.file.test) - args.add("--test", ctx.file.test.path) - + args.add("--policy", ctx.file.policy.path) + args.add("--test", ctx.file.test.path) args.add("--output", ctx.outputs.out.path) ctx.actions.run( @@ -45,14 +37,20 @@ cel_policy_test_bundle = rule( Output file is a YAML file with three documents: environment, policy, and test. - If there is no environment file, the environment document will be omitted. + If there is no config.yaml in the policy directory, the environment + document will be omitted. """, implementation = _cel_policy_test_bundle_impl, outputs = {"out": "%{name}_bundle.yaml"}, attrs = { - "environment": attr.label(allow_single_file = [".yaml"]), "policy": attr.label(allow_single_file = [".yaml"], mandatory = True), "test": attr.label(allow_single_file = [".yaml"], mandatory = True), + # We include the entire testdata tree as an input so the bundler can lookup optional files + # like config.yaml. May revisit if the bundle format becomes the default instead of just for + # C++. + "_testdata": attr.label( + default = "//conformance:testdata", + ), "_bundle_tool": attr.label( default = "//bazel:bundle", executable = True, diff --git a/conformance/BUILD.bazel b/conformance/BUILD.bazel index 2d10b99..86d37cc 100644 --- a/conformance/BUILD.bazel +++ b/conformance/BUILD.bazel @@ -21,7 +21,13 @@ exports_files( ) _TEST_DIRS = [ + "aggregate", + "aggregate_explicit_list_output", + "aggregate_explicit_optional_none", + "aggregate_nested_explicit_list_double_wrapping", + "aggregate_shadowed_variables", "context_pb", + "first_match_nested_aggregate", "k8s", "limits", "nested_rule", @@ -40,34 +46,29 @@ _TEST_DIRS = [ "variable_type_propagation", ] -_TEST_ERROR_DIRS_WITH_ENV = [ +_TEST_ERROR_DIRS = [ + "aggregate_false_condition", + "aggregate_heterogeneous_outputs", + "aggregate_nested_mixed_semantics", + "aggregate_subrule_heterogeneous_outputs", + "aggregate_unreachable_in_nested_first_match", "compose_conflicting_output", "compose_conflicting_subrule", - "unreachable", -] - -_TEST_ERROR_DIRS = [ "duplicate_variable", "import", "incompatible_outputs", "syntax", "undeclared_reference", + "unreachable", + "unreachable_under_unconditional_aggregate_subrule", ] [cel_policy_test_bundle( name = dir_name, - environment = "testdata/%s/config.yaml" % dir_name, policy = "testdata/%s/policy.yaml" % dir_name, test = "testdata/%s/tests.yaml" % dir_name, ) for dir_name in _TEST_DIRS] -[cel_policy_test_bundle( - name = "compile_error_" + dir_name, - environment = "testdata/compile_errors/%s/config.yaml" % dir_name, - policy = "testdata/compile_errors/%s/policy.yaml" % dir_name, - test = "testdata/compile_errors/%s/tests.yaml" % dir_name, -) for dir_name in _TEST_ERROR_DIRS_WITH_ENV] - [cel_policy_test_bundle( name = "compile_error_" + dir_name, policy = "testdata/compile_errors/%s/policy.yaml" % dir_name, @@ -81,6 +82,6 @@ filegroup( for dir_name in _TEST_DIRS ] + [ ":compile_error_" + dir_name - for dir_name in _TEST_ERROR_DIRS_WITH_ENV + _TEST_ERROR_DIRS + for dir_name in _TEST_ERROR_DIRS ], )