1# Copyright 2023 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Tests for DefaultInfoSubject."""
16
17load("//lib:analysis_test.bzl", "analysis_test")
18load("//lib:test_suite.bzl", "test_suite")
19load("//lib:truth.bzl", "matching", "subjects")
20load("//lib:util.bzl", "util")
21load("//tests:test_util.bzl", "test_util")
22
23_tests = []
24
25def _default_info_subject_test(name):
26    util.helper_target(
27        _simple,
28        name = name + "_subject",
29    )
30    analysis_test(
31        name = name,
32        target = name + "_subject",
33        impl = _default_info_subject_test_impl,
34    )
35
36def _default_info_subject_test_impl(env, target):
37    fake_meta = test_util.fake_meta(env)
38    actual = subjects.default_info(
39        target[DefaultInfo],
40        meta = fake_meta,
41    )
42
43    actual.runfiles().contains_predicate(
44        matching.str_matches("default_runfile.txt"),
45    )
46    test_util.expect_no_failures(env, fake_meta, "check default runfiles success")
47
48    actual.runfiles().contains_predicate(
49        matching.str_matches("not-present.txt"),
50    )
51    test_util.expect_failures(
52        env,
53        fake_meta,
54        "check default runfiles failure",
55        "not-present.txt",
56    )
57
58    actual.data_runfiles().contains_predicate(
59        matching.str_matches("data_runfile.txt"),
60    )
61    test_util.expect_no_failures(env, fake_meta, "check data runfiles success")
62
63    actual.data_runfiles().contains_predicate(
64        matching.str_matches("not-present.txt"),
65    )
66    test_util.expect_failures(
67        env,
68        fake_meta,
69        "check data runfiles failure",
70        "not-present.txt",
71    )
72
73    actual.default_outputs().contains_predicate(
74        matching.file_path_matches("default_output.txt"),
75    )
76    test_util.expect_no_failures(env, fake_meta, "check executable success")
77
78    actual.default_outputs().contains_predicate(
79        matching.file_path_matches("not-present.txt"),
80    )
81    test_util.expect_failures(
82        env,
83        fake_meta,
84        "check executable failure",
85        "not-present.txt",
86    )
87
88    actual.executable().path().contains("subject")
89    test_util.expect_no_failures(env, fake_meta, "check executable success")
90
91    actual.executable().path().contains("not-present")
92    test_util.expect_failures(
93        env,
94        fake_meta,
95        "check executable failure",
96        "not-present",
97    )
98    actual.runfiles_manifest().path().contains("MANIFEST")
99    test_util.expect_no_failures(env, fake_meta, "check runfiles_manifest success")
100
101_tests.append(_default_info_subject_test)
102
103def default_info_subject_test_suite(name):
104    test_suite(
105        name = name,
106        tests = _tests,
107    )
108
109def _simple_impl(ctx):
110    executable = ctx.actions.declare_file(ctx.label.name)
111    ctx.actions.write(executable, "")
112    return [DefaultInfo(
113        files = depset([ctx.file.default_output]),
114        default_runfiles = ctx.runfiles([ctx.file.default_runfile, executable]),
115        data_runfiles = ctx.runfiles([ctx.file.data_runfile]),
116        executable = executable,
117    )]
118
119_simple = rule(
120    implementation = _simple_impl,
121    attrs = {
122        "default_output": attr.label(default = "default_output.txt", allow_single_file = True),
123        "default_runfile": attr.label(default = "default_runfile.txt", allow_single_file = True),
124        "data_runfile": attr.label(default = "data_runfile.txt", allow_single_file = True),
125    },
126)
127