Skip to content

Commit

Permalink
fix: hidden files being copied to the wrong directory (#404)
Browse files Browse the repository at this point in the history
The initial copy will copy over hidden files in sub-directories. The
copy `find` logic for hidden files was looking through the entire tree.
We really just want to copy the hidden files in the top-level directory.

Also, adjusted the glob pattern for the "non-hidden" file copy per a
shellcheck warning.
  • Loading branch information
cgrindel authored Dec 2, 2024
1 parent 8763d91 commit 0d844b3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ sh_binary(
],
deps = [
"@bazel_tools//tools/bash/runfiles",
"@cgrindel_bazel_starlib//shlib/lib:arrays",
"@cgrindel_bazel_starlib//shlib/lib:assertions",
],
)
Expand Down
8 changes: 8 additions & 0 deletions examples/simple/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")
load(
"@cgrindel_bazel_starlib//updatesrc:defs.bzl",
Expand All @@ -19,3 +20,10 @@ genrule(
outs = ["copy_of_hidden_file"],
cmd = "cat $(location .hidden_file) > $@",
)

build_test(
name = "copy_hidden_file_test",
targets = [
":copy_hidden_file",
],
)
1 change: 1 addition & 0 deletions examples/simple/mockascript/private/.another_hidden_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Used to test hidden file copy in a sub-directory
16 changes: 16 additions & 0 deletions examples/simple/mockascript/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")

package(default_visibility = ["//mockascript:__subpackages__"])
Expand All @@ -9,3 +10,18 @@ bzl_library(
)

bzlformat_pkg()

# This exists to ensure that the hidden file is copied by create_scratch_dir.sh
genrule(
name = "copy_hidden_file",
srcs = [".another_hidden_file"],
outs = ["copy_of_hidden_file"],
cmd = "cat $(location .another_hidden_file) > $@",
)

build_test(
name = "copy_hidden_file_test",
targets = [
":copy_hidden_file",
],
)
20 changes: 20 additions & 0 deletions examples/use_create_scratch_dir_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ assertions_sh="$(rlocation "${assertions_sh_location}")" || \
(echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1)
source "${assertions_sh}"

arrays_sh_location=cgrindel_bazel_starlib/shlib/lib/arrays.sh
arrays_sh="$(rlocation "${arrays_sh_location}")" || \
(echo >&2 "Failed to locate ${arrays_sh_location}" && exit 1)
source "${arrays_sh}"

create_scratch_dir_sh_location=rules_bazel_integration_test/tools/create_scratch_dir.sh
create_scratch_dir_sh="$(rlocation "${create_scratch_dir_sh_location}")" || \
(echo >&2 "Failed to locate ${create_scratch_dir_sh_location}" && exit 1)
Expand Down Expand Up @@ -47,3 +52,18 @@ cd "${scratch_dir}"

# Should not fail
"${bazel}" test //... || fail "Expected tests to succeed in the scratch directory."

expected_hidden_files=( ./.bazelrc ./.hidden_file ./mockascript/private/.another_hidden_file )
found_hidden_files=()
while IFS=$'\n' read -r line; do found_hidden_files+=("$line"); done < <(
find . -type f -name ".*"
)
assert_equal "${#expected_hidden_files[@]}" "${#found_hidden_files[@]}" "expected count"

sorted=()
while IFS=$'\n' read -r line; do sorted+=("$line"); done < <(
sort_items "${found_hidden_files[@]}"
)
for (( i = 0; i < "${#expected_hidden_files[@]}"; i++ )); do
assert_equal "${expected_hidden_files[i]}" "${sorted[i]}" "hidden file ${i} ${expected_hidden_files[i]}"
done
8 changes: 4 additions & 4 deletions tools/create_scratch_dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ mkdir -p "${scratch_dir}"
# Change to the workspace dir
cd "${workspace_dir}"

# Copy the non-hidden files
cp -R -L * "${scratch_dir}"
# Copy the files. All but the top-level hidden files will be copied.
cp -R -L ./* "${scratch_dir}"

# Copy the hidden files
find . \( -type f -or -type l \) -name ".*" -print0 | xargs -0 -I {} cp {} "${scratch_dir}/"
# Copy the top-level, hidden files.
find . -maxdepth 1 \( -type f -or -type l \) -name ".*" -print0 | xargs -0 -I {} cp {} "${scratch_dir}/"

# Output the scratch directory
echo "${scratch_dir}"

0 comments on commit 0d844b3

Please sign in to comment.