aboutsummaryrefslogtreecommitdiff
path: root/tests/default_info_subject/default_info_subject_tests.bzl
blob: e6cfc10d5342b56e8fd2499d6cef6c840e6a9ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# 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
#
#     http://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.

"""Tests for DefaultInfoSubject."""

load("//lib:analysis_test.bzl", "analysis_test")
load("//lib:test_suite.bzl", "test_suite")
load("//lib:truth.bzl", "matching", "subjects")
load("//lib:util.bzl", "util")
load("//tests:test_util.bzl", "test_util")

_tests = []

def _default_info_subject_test(name):
    util.helper_target(
        _simple,
        name = name + "_subject",
    )
    analysis_test(
        name = name,
        target = name + "_subject",
        impl = _default_info_subject_test_impl,
    )

def _default_info_subject_test_impl(env, target):
    fake_meta = test_util.fake_meta(env)
    actual = subjects.default_info(
        target[DefaultInfo],
        meta = fake_meta,
    )

    actual.runfiles().contains_predicate(
        matching.str_matches("default_runfile.txt"),
    )
    test_util.expect_no_failures(env, fake_meta, "check default runfiles success")

    actual.runfiles().contains_predicate(
        matching.str_matches("not-present.txt"),
    )
    test_util.expect_failures(
        env,
        fake_meta,
        "check default runfiles failure",
        "not-present.txt",
    )

    actual.data_runfiles().contains_predicate(
        matching.str_matches("data_runfile.txt"),
    )
    test_util.expect_no_failures(env, fake_meta, "check data runfiles success")

    actual.data_runfiles().contains_predicate(
        matching.str_matches("not-present.txt"),
    )
    test_util.expect_failures(
        env,
        fake_meta,
        "check data runfiles failure",
        "not-present.txt",
    )

    actual.default_outputs().contains_predicate(
        matching.file_path_matches("default_output.txt"),
    )
    test_util.expect_no_failures(env, fake_meta, "check executable success")

    actual.default_outputs().contains_predicate(
        matching.file_path_matches("not-present.txt"),
    )
    test_util.expect_failures(
        env,
        fake_meta,
        "check executable failure",
        "not-present.txt",
    )

    actual.executable().path().contains("subject")
    test_util.expect_no_failures(env, fake_meta, "check executable success")

    actual.executable().path().contains("not-present")
    test_util.expect_failures(
        env,
        fake_meta,
        "check executable failure",
        "not-present",
    )
    actual.runfiles_manifest().path().contains("MANIFEST")
    test_util.expect_no_failures(env, fake_meta, "check runfiles_manifest success")

_tests.append(_default_info_subject_test)

def default_info_subject_test_suite(name):
    test_suite(
        name = name,
        tests = _tests,
    )

def _simple_impl(ctx):
    executable = ctx.actions.declare_file(ctx.label.name)
    ctx.actions.write(executable, "")
    return [DefaultInfo(
        files = depset([ctx.file.default_output]),
        default_runfiles = ctx.runfiles([ctx.file.default_runfile, executable]),
        data_runfiles = ctx.runfiles([ctx.file.data_runfile]),
        executable = executable,
    )]

_simple = rule(
    implementation = _simple_impl,
    attrs = {
        "default_output": attr.label(default = "default_output.txt", allow_single_file = True),
        "default_runfile": attr.label(default = "default_runfile.txt", allow_single_file = True),
        "data_runfile": attr.label(default = "data_runfile.txt", allow_single_file = True),
    },
)