Skip to content

Commit

Permalink
Finished converting change_update_all_test to use bazel_integration_t…
Browse files Browse the repository at this point in the history
…est (#35)
  • Loading branch information
cgrindel authored Dec 4, 2021
1 parent b02aafd commit 8e24400
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 74 deletions.
36 changes: 23 additions & 13 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,28 @@ default_test_runner(
for example in _EXAMPLE_NAMES
]

# GH030: Finish implementation.
#
# # Integration tests executed against current version of Bazel with custom test runner
# bazel_integration_test(
# name = "change_update_all_test",
# bazel_version = CURRENT_BAZEL_VERSION,
# test_runner_srcs = ["change_update_all_test.sh"],
# workspace_files = integration_test_utils.glob_workspace_files("simple") +
# ADDITIONAL_WORKSPACE_FILES,
# workspace_path = "simple",
# )
sh_binary(
name = "change_update_all_test_runner",
testonly = True,
srcs = ["change_update_all_test.sh"],
data = [
"@cgrindel_rules_bazel_integration_test//tools:create_scratch_dir",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
"@cgrindel_bazel_shlib//lib:assertions",
],
)

# Integration tests executed against current version of Bazel with custom test runner
bazel_integration_test(
name = "change_update_all_test",
bazel_version = CURRENT_BAZEL_VERSION,
test_runner = ":change_update_all_test_runner",
workspace_files = integration_test_utils.glob_workspace_files("simple") +
ADDITIONAL_WORKSPACE_FILES,
workspace_path = "simple",
)

bazel_integration_tests(
name = "simple_test",
Expand All @@ -72,8 +83,7 @@ test_suite(
name = "integration_tests",
tags = integration_test_utils.DEFAULT_INTEGRATION_TEST_TAGS,
tests = [
# GH030: Enable this test once it is implemented.
# ":change_update_all_test",
":change_update_all_test",
] + [
example + "_test"
for example in _EXAMPLE_NAMES
Expand Down
93 changes: 35 additions & 58 deletions examples/change_update_all_test.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
#!/usr/bin/env bash

set -euo pipefail

# Switch the default Xcode to be incompatible, then use DEVELOPER_DIR
# attribute on spm_repositories to specify the one that should be used.

exit_on_error() {
local err_msg="${1:-}"
[[ -n "${err_msg}" ]] || err_msg="Unspecified error occurred."
echo >&2 "${err_msg}"
exit 1
}

normalize_path() {
local path="${1}"
if [[ -d "${path}" ]]; then
local dirname="$(dirname "${path}")"
else
local dirname="$(dirname "${path}")"
local basename="$(basename "${path}")"
fi
dirname="$(cd "${dirname}" > /dev/null && pwd)"
if [[ -z "${basename:-}" ]]; then
echo "${dirname}"
fi
echo "${dirname}/${basename}"
}

starting_dir="$(pwd)"
bazel_cmds=()
# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -uo pipefail; 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 v2 ---

assertions_sh_location=cgrindel_bazel_shlib/lib/assertions.sh
assertions_sh="$(rlocation "${assertions_sh_location}")" || \
(echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1)
source "${assertions_sh}"

create_scratch_dir_sh_location=cgrindel_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)


# MARK - Process Flags

# Process args
while (("$#")); do
case "${1}" in
"--bazel")
bazel_rel_path="${2}"
shift 2
;;
"--bazel_cmd")
bazel_cmds+=("${2}")
bazel="${2}"
shift 2
;;
"--workspace")
Expand All @@ -51,42 +40,30 @@ while (("$#")); do
esac
done


[[ -n "${bazel_rel_path:-}" ]] || exit_on_error "Must specify the location of the Bazel binary."
[[ -n "${bazel:-}" ]] || exit_on_error "Must specify the location of the Bazel binary."
[[ -n "${workspace_path:-}" ]] || exit_on_error "Must specify the location of the workspace file."

starting_path="$(pwd)"
starting_path="${starting_path%%*( )}"
bazel="$(normalize_path "${bazel_rel_path}")"

workspace_dir="$(dirname "${workspace_path}")"
cd "${workspace_dir}"

# BEGIN Custom test logic
# MARK - Create Scratch Directory

modified_file="main.swift"
backup_file="${modified_file}.orig"
scratch_dir="$("${create_scratch_dir_sh}" --workspace "${workspace_dir}")"
cd "${scratch_dir}"

# Rename the file and copy the contents of the symlink to the original filename
# This should handle if the source if a symlink or the actual file.
mv "${modified_file}" "${backup_file}"
cp "${backup_file}" "${modified_file}"
# MARK - Test Change, Test, and Update All

# Set trap for cleanup
cleanup() {
mv -f "${backup_file}" "${modified_file}"
cd "${starting_dir}"
}
trap cleanup EXIT
# Should not fail, no changes
"${bazel}" test //... || fail "Expected test to succeed as nothing has changed yet."

# Modify the file
# The spaces at the front of the statement should be removed by the formatter.
echo " let bar = 2" >> main.swift

# Should fail due to unformatted
"${bazel}" test //... && fail "Expected test to fail due to unformatted files."

# Format the Swift files and copy them back to the workspace
"${bazel}" run //:update_all
"${bazel}" run //:update_all || fail "Expected update_all to succeed."

# This should succeed now that the formatted files have been updated
"${bazel}" test //...

# END Custom test logic
"${bazel}" test //... || fail "Expected test to succeed after update_all."
14 changes: 11 additions & 3 deletions swiftformat/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ def swiftformat_rules_dependencies():
maybe(
http_archive,
name = "cgrindel_rules_bazel_integration_test",
sha256 = "ef6cf463a269d969bdb3b31eed53c50d3667f02e004abc9ce7a3a2413eadca6a",
strip_prefix = "rules_bazel_integration_test-0.3.0",
urls = ["https://github.com/cgrindel/rules_bazel_integration_test/archive/v0.3.0.tar.gz"],
sha256 = "50b808269ee09373c099256103c40629db8a66fd884030d7a36cf9a2e8675b75",
strip_prefix = "rules_bazel_integration_test-0.3.1",
urls = ["https://github.com/cgrindel/rules_bazel_integration_test/archive/v0.3.1.tar.gz"],
)

maybe(
http_archive,
name = "cgrindel_bazel_shlib",
sha256 = "39c250852fb455e5de18f836c0c339075d6e52ea5ec52a76d62ef9e2eed56337",
strip_prefix = "bazel_shlib-0.2.1",
urls = ["https://github.com/cgrindel/bazel_shlib/archive/v0.2.1.tar.gz"],
)

0 comments on commit 8e24400

Please sign in to comment.