summaryrefslogtreecommitdiff
path: root/test/apple_support_test.bzl
blob: 8adae7d508bb93835e51899a1b1ecd40318d97ff (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Copyright 2018 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.
"""Definition of a test rule to test apple_support."""

load(
    "@build_bazel_apple_support//lib:apple_support.bzl",
    "apple_support",
)

# Contents of the tool that writes the state of the action into a file. The first argument to this
# script is the path to the output file.
_TEST_TOOL_CONTENTS = """#!/bin/bash

set -eu

OUTPUT_FILE="$1"
shift

echo "XCODE_PATH_ENV=$DEVELOPER_DIR" > "$OUTPUT_FILE"
echo "SDKROOT_PATH_ENV=$SDKROOT" >> "$OUTPUT_FILE"

for arg in "$@"; do
    if [[ "$arg" == @* ]]; then
        cat "${arg:1}" >> "$OUTPUT_FILE"
    else
        echo "$arg" >> "$OUTPUT_FILE"
    fi
done
"""

# Template for the test script used to validate that the action outputs contain the expected
# values.
_TEST_SCRIPT_CONTENTS = """
#!/bin/bash

set -eu

FILES=(
    {file_paths}
)

function assert_contains_line() {{
    file="$1"
    contents="$2"

    if [[ -f "$file" ]]; then
      grep -Fxq "$contents" "$file" || \
          (echo "In file: $file"; \
           echo "Expected contents not found: $contents"; \
           echo "File contents:"; \
           cat "$file"; \
           exit 1)
    else
      echo "$file doesn't exist"
      exit 1
    fi
}}

function assert_not_contains() {{
    file="$1"
    contents="$2"

    if [[ -f "$file" ]]; then
      grep -Fq "$contents" "$file" || return 0
    else
      echo "$file doesn't exist"
      exit 1
    fi

    echo "In file: $file"
    echo "Contents found but not expected: $contents"
    echo "File contents:"
    cat "$file"
    exit 1
}}

XCODE_PATH_ENV="$DEVELOPER_DIR"
SDKROOT_PATH_ENV="$SDKROOT"

for file in "${{FILES[@]}}"; do
    assert_contains_line "$file" "XCODE_PATH_ENV=$XCODE_PATH_ENV"
    assert_contains_line "$file" "SDKROOT_PATH_ENV=$SDKROOT_PATH_ENV"
    assert_not_contains "$file" "{xcode_path_placeholder}"
    assert_not_contains "$file" "{sdkroot_path_placeholder}"
done

echo "Test passed"

exit 0
"""

def _apple_support_test_impl(ctx):
    """Implementation of the apple_support_test rule."""

    # Declare all the action outputs
    run_output = ctx.actions.declare_file(
        "{}_run_output".format(ctx.label.name),
    )
    run_output_xcode_path_in_args = ctx.actions.declare_file(
        "{}_run_output_xcode_path_in_args".format(ctx.label.name),
    )
    run_output_xcode_path_in_file = ctx.actions.declare_file(
        "{}_run_output_xcode_path_in_file".format(ctx.label.name),
    )
    run_shell_output = ctx.actions.declare_file(
        "{}_run_shell_output".format(ctx.label.name),
    )

    test_tool = ctx.actions.declare_file("{}_test_tool".format(ctx.label.name))
    ctx.actions.write(test_tool, _TEST_TOOL_CONTENTS, is_executable = True)

    # Create one action per possible combination of inputs to the apple_support.run and
    # apple_support.run_shell helper methods.
    apple_support.run(
        ctx,
        outputs = [run_output],
        executable = test_tool,
        arguments = [run_output.path],
    )

    apple_support.run(
        ctx,
        outputs = [run_output_xcode_path_in_args],
        executable = test_tool,
        arguments = [
            run_output_xcode_path_in_args.path,
            "XCODE_PATH_ARG={}".format(apple_support.path_placeholders.xcode()),
            "FRAMEWORKS_PATH_ARG={}".format(
                apple_support.path_placeholders.platform_frameworks(ctx),
            ),
            "SDKROOT_PATH_ARG={}".format(apple_support.path_placeholders.sdkroot()),
        ],
        xcode_path_resolve_level = apple_support.xcode_path_resolve_level.args,
    )

    action_args = ctx.actions.args()
    action_args.add(
        "XCODE_PATH_ARG={}".format(apple_support.path_placeholders.xcode()),
    )
    action_args.add(
        "FRAMEWORKS_PATH_ARG={}".format(apple_support.path_placeholders.platform_frameworks(ctx)),
    )
    action_args.add(
        "SDKROOT_PATH_ARG={}".format(apple_support.path_placeholders.sdkroot()),
    )
    action_args.set_param_file_format("multiline")
    action_args.use_param_file("@%s", use_always = True)

    apple_support.run(
        ctx,
        outputs = [run_output_xcode_path_in_file],
        executable = test_tool,
        arguments = [
            run_output_xcode_path_in_file.path,
            action_args,
        ],
        xcode_path_resolve_level = apple_support.xcode_path_resolve_level.args_and_files,
    )

    apple_support.run_shell(
        ctx,
        outputs = [run_shell_output],
        tools = [test_tool],
        command = "{tool} {output}".format(
            output = run_shell_output.path,
            tool = test_tool.path,
        ),
    )

    test_files = [
        run_output,
        run_output_xcode_path_in_args,
        run_output_xcode_path_in_file,
        run_shell_output,
    ]

    test_script = ctx.actions.declare_file("{}_test_script".format(ctx.label.name))
    ctx.actions.write(test_script, _TEST_SCRIPT_CONTENTS.format(
        file_paths = "\n    ".join([x.short_path for x in test_files]),
        sdkroot_path_placeholder = apple_support.path_placeholders.sdkroot(),
        xcode_path_placeholder = apple_support.path_placeholders.xcode(),
    ), is_executable = True)

    return [
        testing.ExecutionInfo(apple_support.action_required_execution_requirements(ctx)),
        testing.TestEnvironment(apple_support.action_required_env(ctx)),
        DefaultInfo(
            executable = test_script,
            files = depset([run_output_xcode_path_in_args]),
            runfiles = ctx.runfiles(
                files = test_files,
            ),
        ),
    ]

apple_support_test = rule(
    implementation = _apple_support_test_impl,
    attrs = apple_support.action_required_attrs(),
    fragments = ["apple"],
    test = True,
)