# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("@rules_proto//proto:defs.bzl", "proto_descriptor_set")
load("//test:cel_go_test.bzl", "cel_go_test")

package(
    default_visibility = [
        "//policy:__subpackages__",
        "//tools:__subpackages__"
    ],
    licenses = ["notice"],
)

go_library(
    name = "go_default_library",
    srcs = [
        "compiler.go",
        "conformance.go",
        "composer.go",
        "config.go",
        "parser.go",
        "source.go",
        "test_tag_handler_k8s.go",
        "yaml.go",
    ],
    importpath = "github.com/google/cel-go/policy",
    deps = [
        "//cel:go_default_library",
        "//common:go_default_library",
        "//common/ast:go_default_library",
        "//common/containers:go_default_library",
        "//common/decls:go_default_library",
        "//common/env:go_default_library",
        "//common/operators:go_default_library",
        "//common/types:go_default_library",
        "//common/types/ref:go_default_library",
        "//ext:go_default_library",
        "@in_yaml_go_yaml_v3//:go_default_library",
        "@org_golang_google_protobuf//proto:go_default_library",
    ],
)

go_test(
    name = "go_default_test",
    size = "small",
    srcs = [
        "compiler_test.go",
        "composer_test.go",
        "config_test.go",
        "helper_test.go",
        "parser_test.go",
        "yaml_test.go",
    ],
    data = glob(["testdata/**"]),
    embed = [":go_default_library"],
    deps = [
        "//cel:go_default_library",
        "//test:go_default_library",
        "//common/debug:go_default_library",
        "//common/types:go_default_library",
        "//interpreter:go_default_library",
        "//common/types/ref:go_default_library",
        "//test/proto3pb:go_default_library",
        "@in_yaml_go_yaml_v3//:go_default_library",
        "@com_github_google_go_cmp//cmp:go_default_library",
        "@org_golang_google_genproto_googleapis_api//expr/v1alpha1:go_default_library",
        "@org_golang_google_protobuf//encoding/prototext:go_default_library",
    ],
)

filegroup(
    name = "testdata",
    srcs = glob(["testdata/**"]),
)

proto_descriptor_set(
    name = "test_all_types_fds",
    deps = [
        "//test/proto3pb:test_all_types_proto",
    ],
)

cel_go_test(
    name = "context_pb_policy",
    cel_expr = "testdata/context_pb/policy.yaml",
    config = "testdata/context_pb/config.yaml",
    enable_coverage = True,
    file_descriptor_set = ":test_all_types_fds",
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/context_pb/tests.yaml",
)

cel_go_test(
    name = "k8s_policy",
    cel_expr = "testdata/k8s/policy.yaml",
    config = "testdata/k8s/config.yaml",
    deps = ["//policy:go_default_library"],
    enable_coverage = True,
    test_src = "//policy/test:k8s_cel_test_runner.go",
    test_suite = "testdata/k8s/tests.yaml",
)

cel_go_test(
    name = "limits_policy",
    cel_expr = "testdata/limits/policy.yaml",
    config = "testdata/limits/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/limits/tests.yaml",
)

cel_go_test(
    name = "nested_rule_policy",
    cel_expr = "testdata/nested_rule/policy.yaml",
    config = "testdata/nested_rule/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/nested_rule/tests.yaml",
)

cel_go_test(
    name = "nested_rule2_policy",
    cel_expr = "testdata/nested_rule2/policy.yaml",
    config = "testdata/nested_rule2/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/nested_rule2/tests.yaml",
)

cel_go_test(
    name = "nested_rule3_policy",
    cel_expr = "testdata/nested_rule3/policy.yaml",
    config = "testdata/nested_rule3/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/nested_rule3/tests.yaml",
)

cel_go_test(
    name = "nested_rule4_policy",
    cel_expr = "testdata/nested_rule4/policy.yaml",
    config = "testdata/nested_rule4/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/nested_rule4/tests.yaml",
)

cel_go_test(
    name = "nested_rule5_policy",
    cel_expr = "testdata/nested_rule5/policy.yaml",
    config = "testdata/nested_rule5/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/nested_rule5/tests.yaml",
)

cel_go_test(
    name = "nested_rule6_policy",
    cel_expr = "testdata/nested_rule6/policy.yaml",
    config = "testdata/nested_rule6/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/nested_rule6/tests.yaml",
)

cel_go_test(
    name = "nested_rule7_policy",
    cel_expr = "testdata/nested_rule7/policy.yaml",
    config = "testdata/nested_rule7/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/nested_rule7/tests.yaml",
)

cel_go_test(
    name = "pb_policy",
    cel_expr = "testdata/pb/policy.yaml",
    config = "testdata/pb/config.yaml",
    file_descriptor_set = ":test_all_types_fds",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/pb/tests.yaml",
)

cel_go_test(
    name = "required_labels_policy",
    cel_expr = "testdata/required_labels/policy.yaml",
    config = "testdata/required_labels/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/required_labels/tests.yaml",
)

cel_go_test(
    name = "restricted_destinations_policy",
    cel_expr = "testdata/restricted_destinations/policy.yaml",
    config = "testdata/restricted_destinations/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/restricted_destinations/tests.yaml",
)

cel_go_test(
    name = "unnest_policy",
    cel_expr = "testdata/unnest/policy.yaml",
    config = "testdata/unnest/config.yaml",
    enable_coverage = True,
    test_src = "//policy/test:cel_test_runner.go",
    test_suite = "testdata/unnest/tests.yaml",
)
