Add tests for the current cc_library runfiles behavior

Copybara Import from https://github.com/bazelbuild/rules_cc/pull/711

BEGIN_PUBLIC
Add tests for the current cc_library runfiles behavior (#711)

Closes #711
END_PUBLIC

COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_cc/pull/711 from keith:ks/add-tests-for-the-current-cc_library-runfiles-behavior 8bf1eef0ab194356b70ed4c2ee7a9ae37fc2c401
PiperOrigin-RevId: 919555241
Change-Id: I543c300053741ec7d078fa4f1445fbb8bab5261a
diff --git a/tests/cc/common/BUILD b/tests/cc/common/BUILD
index e718caa..ead9080 100644
--- a/tests/cc/common/BUILD
+++ b/tests/cc/common/BUILD
@@ -1,6 +1,7 @@
 load(":cc_binary_configured_target_tests.bzl", "cc_binary_configured_target_tests")
 load(":cc_common_test.bzl", "cc_common_tests")
 load(":cc_import_configured_target_tests.bzl", "cc_import_configured_target_tests")
+load(":cc_library_configured_target_tests.bzl", "cc_library_configured_target_tests")
 load(":cc_objc_library_configured_target_tests.bzl", "cc_objc_library_configured_target_tests")
 
 cc_binary_configured_target_tests(name = "cc_binary_configured_target_tests")
@@ -9,4 +10,6 @@
 
 cc_import_configured_target_tests(name = "cc_import_configured_target_tests")
 
+cc_library_configured_target_tests(name = "cc_library_configured_target_tests")
+
 cc_objc_library_configured_target_tests(name = "cc_objc_library_configured_target_tests")
diff --git a/tests/cc/common/cc_library_configured_target_tests.bzl b/tests/cc/common/cc_library_configured_target_tests.bzl
new file mode 100644
index 0000000..95ad741
--- /dev/null
+++ b/tests/cc/common/cc_library_configured_target_tests.bzl
@@ -0,0 +1,89 @@
+"""Tests for cc_library."""
+
+load("@bazel_features//:features.bzl", "bazel_features")
+load("@rules_testing//lib:analysis_test.bzl", "test_suite")
+load("@rules_testing//lib:truth.bzl", "matching")
+load("@rules_testing//lib:util.bzl", "util")
+load("//cc:cc_library.bzl", "cc_library")
+load("//cc/common:cc_info.bzl", "CcInfo")
+load("//tests/cc/testutil:cc_analysis_test.bzl", "cc_analysis_test")
+
+def _src_with_runfiles_impl(ctx):
+    src = ctx.actions.declare_file(ctx.label.name + ".cc")
+    runfile = ctx.actions.declare_file(ctx.label.name + "_runfile.txt")
+    ctx.actions.write(src, "")
+    ctx.actions.write(runfile, "")
+    return [DefaultInfo(
+        files = depset([src]),
+        default_runfiles = ctx.runfiles(files = [runfile]),
+    )]
+
+_src_with_runfiles = rule(implementation = _src_with_runfiles_impl)
+
+def _cc_dep_with_runfiles_impl(ctx):
+    runfile = ctx.actions.declare_file(ctx.label.name + "_runfile.txt")
+    ctx.actions.write(runfile, "")
+    return [
+        DefaultInfo(default_runfiles = ctx.runfiles(files = [runfile])),
+        CcInfo(),
+    ]
+
+_cc_dep_with_runfiles = rule(implementation = _cc_dep_with_runfiles_impl)
+
+def _test_cc_library_data_in_runfiles(name, **kwargs):
+    srcs_runfiles = name + "_srcs_runfiles"
+    deps_runfiles = name + "_deps_runfiles"
+    implementation_deps_runfiles = name + "_implementation_deps_runfiles"
+
+    util.helper_target(
+        _src_with_runfiles,
+        name = srcs_runfiles,
+    )
+    util.helper_target(
+        _cc_dep_with_runfiles,
+        name = deps_runfiles,
+    )
+    util.helper_target(
+        _cc_dep_with_runfiles,
+        name = implementation_deps_runfiles,
+    )
+    util.helper_target(
+        cc_library,
+        name = name + "_lib_with_data",
+        srcs = [
+            "source.cc",
+            srcs_runfiles,
+        ],
+        hdrs = ["header.h"],
+        data = ["data_file.txt"],
+        deps = [deps_runfiles],
+        implementation_deps = [implementation_deps_runfiles],
+    )
+    cc_analysis_test(
+        name = name,
+        impl = _test_cc_library_data_in_runfiles_impl,
+        target = name + "_lib_with_data",
+        **kwargs
+    )
+
+def _test_cc_library_data_in_runfiles_impl(env, target):
+    target = env.expect.that_target(target)
+    for suffix in [
+        "/data_file.txt",
+        "_srcs_runfiles_runfile.txt",
+        "_deps_runfiles_runfile.txt",
+        "_implementation_deps_runfiles_runfile.txt",
+    ]:
+        target.runfiles().contains_predicate(matching.str_endswith(suffix))
+        target.data_runfiles().contains_predicate(matching.str_endswith(suffix))
+
+    target.runfiles().not_contains_predicate(matching.str_endswith(".a"))
+    target.data_runfiles().not_contains_predicate(matching.str_endswith(".a"))
+
+def cc_library_configured_target_tests(name):
+    test_suite(
+        name = name,
+        tests = [
+            _test_cc_library_data_in_runfiles,
+        ] if bazel_features.cc.cc_common_is_in_rules_cc else [],
+    )