Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows,examples: //examples/shell now works #5480

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/shell/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package(default_visibility = ["//visibility:public"])
sh_binary(
name = "bin",
srcs = ["bin.sh"],
deps = [":lib"],
deps = [
":lib",
"@bazel_tools//tools/bash/runfiles",
],
)

sh_library(
Expand All @@ -12,14 +15,18 @@ sh_library(
"data/file.txt",
"lib.sh",
],
deps = ["@bazel_tools//tools/bash/runfiles"],
)

sh_test(
name = "test",
size = "small",
srcs = ["test.sh"],
data = ["data/test_file.txt"],
deps = [":lib"],
deps = [
":lib",
"@bazel_tools//tools/bash/runfiles",
],
)

filegroup(
Expand Down
28 changes: 22 additions & 6 deletions examples/shell/bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu
set -euo pipefail
# --- begin runfiles.bash initialization ---
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
if [[ -f "$0.runfiles_manifest" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
elif [[ -f "$0.runfiles/MANIFEST" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
export RUNFILES_DIR="$0.runfiles"
fi
fi
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
else
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
exit 1
fi
# --- end runfiles.bash initialization ---

# This allows the script to be both a binary and a library script. If our binary has defined
# RUNFILES then we use it, otherwise we look for our own runfiles.
RUNFILES=${RUNFILES:-$0.runfiles/io_bazel}

source "${RUNFILES}/examples/shell/lib.sh"
source "$(rlocation io_bazel/examples/shell/lib.sh)"

showfile
28 changes: 22 additions & 6 deletions examples/shell/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu

# This allows the script to be both a binary and a library script. If our binary has defined
# RUNFILES then we use it, otherwise we look for our own runfiles.
RUNFILES=${RUNFILES:-$0.runfiles/io_bazel}
set -euo pipefail
# --- begin runfiles.bash initialization ---
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
if [[ -f "$0.runfiles_manifest" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
elif [[ -f "$0.runfiles/MANIFEST" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
export RUNFILES_DIR="$0.runfiles"
fi
fi
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
else
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
exit 1
fi
# --- end runfiles.bash initialization ---

function showfile {
cat "${RUNFILES}/examples/shell/data/file.txt"
cat "$(rlocation io_bazel/examples/shell/data/file.txt)"
}
30 changes: 23 additions & 7 deletions examples/shell/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,33 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu
set -euo pipefail
# --- begin runfiles.bash initialization ---
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
if [[ -f "$0.runfiles_manifest" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
elif [[ -f "$0.runfiles/MANIFEST" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
export RUNFILES_DIR="$0.runfiles"
fi
fi
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
else
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
exit 1
fi
# --- end runfiles.bash initialization ---

# This allows RUNFILES to be declared outside the script it you want.
# RUNFILES for test is the directory of the script.
RUNFILES=${RUNFILES:-$($(cd $(dirname ${BASH_SOURCE[0]})); pwd)}

source "${RUNFILES}/examples/shell/lib.sh"
source "$(rlocation io_bazel/examples/shell/lib.sh)"

function test_output {
OUTPUT=$(showfile)
EXPECTED_OUTPUT=$(cat "${RUNFILES}/examples/shell/data/test_file.txt")
EXPECTED_OUTPUT=$(cat "$(rlocation io_bazel/examples/shell/data/test_file.txt)")

if [ "${OUTPUT}" != "${EXPECTED_OUTPUT}" ]; then
# This would be a failure case.
Expand Down
3 changes: 3 additions & 0 deletions src/test/shell/bazel/bazel_example_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ function test_native_python_with_zip() {
}

function test_shell() {
bazel query "@bazel_tools//tools/bash/runfiles" --output=build
assert_build "//examples/shell:bin"
unset RUNFILES_DIR
unset RUNFILES_MANIFEST_FILE
assert_bazel_run "//examples/shell:bin" "Hello Bazel!"
assert_test_ok "//examples/shell:test"
}
Expand Down