| #!/usr/bin/env bash | |
| # | |
| # Copyright 2026 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. | |
| # | |
| # End-to-end tests for tree artifacts. | |
| # --- begin runfiles.bash initialization v3 --- | |
| set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash | |
| source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ | |
| source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ | |
| source "$0.runfiles/$f" 2>/dev/null || \ | |
| source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | |
| source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | |
| { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e | |
| # --- end runfiles.bash initialization v3 --- | |
| source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \ | |
| || { echo "integration_test_setup.sh not found!" >&2; exit 1; } | |
| function set_up() { | |
| # Delete directories created by any of the tests to ensure we start with a | |
| # clean workspace. | |
| rm -rf foo tree_artifact | |
| } | |
| function write_tree_artifact_helpers() { | |
| cat <<'EOF' | |
| def _create_files_internal(ctx, output_dirs, output_files, files_map, base_dir = ""): | |
| if not output_dirs and not output_files: | |
| fail("No outputs defined") | |
| outputs = [ctx.actions.declare_directory(dir) for dir in output_dirs] + \ | |
| [ctx.actions.declare_file(file) for file in output_files] | |
| first_output = (output_dirs + output_files)[0] | |
| output_base = outputs[0].path[:-(len(first_output) + 1)] + "/" + base_dir | |
| args = ctx.actions.args() | |
| args.add(output_base) | |
| args.add_all(files_map.items(), map_each = list) | |
| ctx.actions.run_shell( | |
| outputs = outputs, | |
| command = ''' | |
| base_dir="$1" | |
| shift 1 | |
| while [[ $# -gt 0 ]]; do | |
| path="${base_dir}/$1" | |
| mkdir -p "${path%/*}" | |
| echo "$2" > "${path}" | |
| shift 2 | |
| done | |
| ''', | |
| arguments = [args], | |
| ) | |
| return DefaultInfo(files = depset(outputs)) | |
| def _create_files(ctx): | |
| return _create_files_internal( | |
| ctx, | |
| ctx.attr.output_directories, | |
| ctx.attr.output_files, | |
| ctx.attr.contents, | |
| ) | |
| create_files = rule( | |
| implementation = _create_files, | |
| attrs = { | |
| "contents": attr.string_dict(mandatory = True), | |
| "output_directories": attr.string_list(), | |
| "output_files": attr.string_list(), | |
| }, | |
| ) | |
| def _create_dir(ctx): | |
| dir_name = ctx.attr.directory or ctx.label.name | |
| return _create_files_internal( | |
| ctx, | |
| base_dir = dir_name, | |
| output_dirs = [dir_name], | |
| output_files = [], | |
| files_map = ctx.attr.contents, | |
| ) | |
| create_dir = rule( | |
| implementation = _create_dir, | |
| attrs = { | |
| "contents": attr.string_dict(mandatory = True), | |
| "directory": attr.string(), | |
| }, | |
| ) | |
| EOF | |
| } | |
| function test_treeartifact_becomes_nested() { | |
| mkdir -p foo | |
| cat >> foo/BUILD <<EOF | |
| load(":def.bzl", "r") | |
| r(name = "a") | |
| EOF | |
| cat >> foo/def.bzl <<EOF | |
| def _r(ctx): | |
| d = ctx.actions.declare_directory("%s_dir" % ctx.label.name) | |
| ctx.actions.run_shell( | |
| outputs = [d], | |
| command = "cd %s && pwd" % d.path, | |
| ) | |
| return [DefaultInfo(files = depset([d]))] | |
| r = rule(implementation = _r) | |
| EOF | |
| bazel build foo:a || fail "Expected success" | |
| # Now nest the output directory under the original tree artifact | |
| rm foo/def.bzl | |
| cat >> foo/def.bzl <<EOF | |
| def _r(ctx): | |
| d = ctx.actions.declare_directory("%s_dir/nested" % ctx.label.name) | |
| ctx.actions.run_shell( | |
| outputs = [d], | |
| command = "cd %s && pwd" % d.path, | |
| ) | |
| return [DefaultInfo(files = depset([d]))] | |
| r = rule(implementation = _r) | |
| EOF | |
| bazel build foo:a || fail "Expected success" | |
| } | |
| function test_tree_relative_path() { | |
| mkdir -p tree_artifact | |
| cat >> tree_artifact/BUILD <<EOF | |
| load(":def.bzl", "tree_artifact", "list_tree_relative_paths") | |
| tree_artifact( | |
| name = "tree", | |
| ) | |
| list_tree_relative_paths( | |
| name = "list_paths", | |
| srcs = [":tree"], | |
| ) | |
| EOF | |
| cat >> tree_artifact/def.bzl <<EOF | |
| def _tree_artifact_impl(ctx): | |
| nested = ctx.actions.declare_directory("%s_dir" % ctx.label.name) | |
| ctx.actions.run_shell( | |
| outputs = [nested], | |
| command = "cd %s && mkdir sub1{,/sub2} && touch sub1/{,sub2/}file{1,2}" % (nested.path), | |
| ) | |
| return [DefaultInfo(files = depset([nested]))] | |
| tree_artifact = rule( | |
| implementation = _tree_artifact_impl, | |
| ) | |
| def _tree_relative_path_mapper(src): | |
| return [ | |
| "src.short_path: %s" % src.short_path, | |
| "src.tree_relative_path: %s" % src.tree_relative_path | |
| ] | |
| def _list_tree_relative_paths_impl(ctx): | |
| out = ctx.actions.declare_file("%s_out" % ctx.label.name) | |
| args = ctx.actions.args() | |
| args.add_all( | |
| ctx.files.srcs, | |
| map_each = _tree_relative_path_mapper, | |
| expand_directories = True) | |
| args.set_param_file_format("multiline") | |
| ctx.actions.write(out, args) | |
| return [DefaultInfo(files = depset([out]))] | |
| list_tree_relative_paths = rule( | |
| implementation = _list_tree_relative_paths_impl, | |
| attrs = { | |
| "srcs": attr.label_list(allow_files = True), | |
| } | |
| ) | |
| EOF | |
| bazel build tree_artifact:list_paths || fail "Expected success" | |
| expected="src.short_path: tree_artifact/tree_dir/sub1/file1 | |
| src.tree_relative_path: sub1/file1 | |
| src.short_path: tree_artifact/tree_dir/sub1/file2 | |
| src.tree_relative_path: sub1/file2 | |
| src.short_path: tree_artifact/tree_dir/sub1/sub2/file1 | |
| src.tree_relative_path: sub1/sub2/file1 | |
| src.short_path: tree_artifact/tree_dir/sub1/sub2/file2 | |
| src.tree_relative_path: sub1/sub2/file2" | |
| assert_equals "$(cat "${PRODUCT_NAME}-bin/tree_artifact/list_paths_out")" "$expected" | |
| } | |
| function test_tree_relative_path_fails_on_non_tree_files() { | |
| mkdir -p tree_artifact | |
| cat >> tree_artifact/BUILD <<EOF | |
| load(":def.bzl", "fails_for_tree_artifact_root", "fails_for_plain_files") | |
| fails_for_tree_artifact_root( | |
| name = "fails_for_tree_artifact_root", | |
| ) | |
| fails_for_plain_files( | |
| name = "fails_for_plain_files", | |
| ) | |
| EOF | |
| cat >> tree_artifact/def.bzl <<EOF | |
| def _fails_for_tree_artifact_root_impl(ctx): | |
| d = ctx.actions.declare_directory("%s_dir" % ctx.label.name) | |
| ctx.actions.run_shell( | |
| outputs = [d], | |
| arguments = ["%s" % d.tree_relative_path], | |
| command = "cd %s && pwd" % (d.path), | |
| ) | |
| return [DefaultInfo(files = depset([d]))] | |
| fails_for_tree_artifact_root = rule( | |
| implementation = _fails_for_tree_artifact_root_impl, | |
| ) | |
| def _fails_for_plain_files_impl(ctx): | |
| f = ctx.actions.declare_file("%s_file" % ctx.label.name) | |
| ctx.actions.run_shell( | |
| outputs = [f], | |
| arguments = ["%s" % f.tree_relative_path], | |
| command = "touch %s" % (f.path), | |
| ) | |
| return [DefaultInfo(files = depset([f]))] | |
| fails_for_plain_files = rule( | |
| implementation = _fails_for_plain_files_impl, | |
| ) | |
| EOF | |
| bazel build tree_artifact:fails_for_tree_artifact_root >& "$TEST_log" \ | |
| && fail "Expected failure" | |
| expect_log "tree_relative_path not allowed for files that are not tree artifact files." | |
| bazel build tree_artifact:fails_for_plain_files >& "$TEST_log" \ | |
| && fail "Expected failure" | |
| expect_log "tree_relative_path not allowed for files that are not tree artifact files." | |
| } | |
| function test_builds_with_file_starting_with_dot_dot() { | |
| mkdir foo | |
| write_tree_artifact_helpers > foo/def.bzl | |
| cat > foo/BUILD <<'EOF' | |
| load(":def.bzl", "create_dir") | |
| create_dir(name="dir", contents={"..file" : "text"}) | |
| EOF | |
| bazel build //foo:dir >& "${TEST_log}" || fail "Expected build to succeed" | |
| assert_equals "..file" "$(ls -A "${PRODUCT_NAME}-bin/foo/dir")" | |
| assert_equals "text" "$(<"${PRODUCT_NAME}-bin/foo/dir/..file")" | |
| } | |
| function test_tree_artifact_replaced_by_file_sandboxed__creates_file() { | |
| if is_windows; then | |
| # Sandboxed spawn strategy is not supported on Windows. | |
| return 0 | |
| fi | |
| mkdir foo | |
| write_tree_artifact_helpers > foo/def.bzl | |
| cat > foo/BUILD <<'EOF' | |
| load(":def.bzl", "create_dir") | |
| create_dir(name="foo", contents={"a": "b"}, directory="dir") | |
| EOF | |
| bazel build --spawn_strategy=sandboxed //foo >& "${TEST_log}" \ | |
| || fail "Build failed" | |
| cat > foo/BUILD <<'EOF' | |
| load(":def.bzl", "create_files") | |
| create_files( | |
| name = "foo", | |
| contents = {"dir/file": "hello"}, | |
| output_files = ["dir/file"] | |
| ) | |
| EOF | |
| bazel build --spawn_strategy=sandboxed //foo >& "${TEST_log}" \ | |
| || fail "Build failed" | |
| assert_equals "hello" "$(<"${PRODUCT_NAME}-bin/foo/dir/file")" | |
| } | |
| run_suite "tree artifact tests" |