Skip to content

Commit 8e24400

Browse files
authored
Finished converting change_update_all_test to use bazel_integration_test (#35)
1 parent b02aafd commit 8e24400

File tree

3 files changed

+69
-74
lines changed

3 files changed

+69
-74
lines changed

examples/BUILD.bazel

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,28 @@ default_test_runner(
4646
for example in _EXAMPLE_NAMES
4747
]
4848

49-
# GH030: Finish implementation.
50-
#
51-
# # Integration tests executed against current version of Bazel with custom test runner
52-
# bazel_integration_test(
53-
# name = "change_update_all_test",
54-
# bazel_version = CURRENT_BAZEL_VERSION,
55-
# test_runner_srcs = ["change_update_all_test.sh"],
56-
# workspace_files = integration_test_utils.glob_workspace_files("simple") +
57-
# ADDITIONAL_WORKSPACE_FILES,
58-
# workspace_path = "simple",
59-
# )
49+
sh_binary(
50+
name = "change_update_all_test_runner",
51+
testonly = True,
52+
srcs = ["change_update_all_test.sh"],
53+
data = [
54+
"@cgrindel_rules_bazel_integration_test//tools:create_scratch_dir",
55+
],
56+
deps = [
57+
"@bazel_tools//tools/bash/runfiles",
58+
"@cgrindel_bazel_shlib//lib:assertions",
59+
],
60+
)
61+
62+
# Integration tests executed against current version of Bazel with custom test runner
63+
bazel_integration_test(
64+
name = "change_update_all_test",
65+
bazel_version = CURRENT_BAZEL_VERSION,
66+
test_runner = ":change_update_all_test_runner",
67+
workspace_files = integration_test_utils.glob_workspace_files("simple") +
68+
ADDITIONAL_WORKSPACE_FILES,
69+
workspace_path = "simple",
70+
)
6071

6172
bazel_integration_tests(
6273
name = "simple_test",
@@ -72,8 +83,7 @@ test_suite(
7283
name = "integration_tests",
7384
tags = integration_test_utils.DEFAULT_INTEGRATION_TEST_TAGS,
7485
tests = [
75-
# GH030: Enable this test once it is implemented.
76-
# ":change_update_all_test",
86+
":change_update_all_test",
7787
] + [
7888
example + "_test"
7989
for example in _EXAMPLE_NAMES

examples/change_update_all_test.sh

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,33 @@
11
#!/usr/bin/env bash
22

3-
set -euo pipefail
4-
5-
# Switch the default Xcode to be incompatible, then use DEVELOPER_DIR
6-
# attribute on spm_repositories to specify the one that should be used.
7-
8-
exit_on_error() {
9-
local err_msg="${1:-}"
10-
[[ -n "${err_msg}" ]] || err_msg="Unspecified error occurred."
11-
echo >&2 "${err_msg}"
12-
exit 1
13-
}
14-
15-
normalize_path() {
16-
local path="${1}"
17-
if [[ -d "${path}" ]]; then
18-
local dirname="$(dirname "${path}")"
19-
else
20-
local dirname="$(dirname "${path}")"
21-
local basename="$(basename "${path}")"
22-
fi
23-
dirname="$(cd "${dirname}" > /dev/null && pwd)"
24-
if [[ -z "${basename:-}" ]]; then
25-
echo "${dirname}"
26-
fi
27-
echo "${dirname}/${basename}"
28-
}
29-
30-
starting_dir="$(pwd)"
31-
bazel_cmds=()
3+
# --- begin runfiles.bash initialization v2 ---
4+
# Copy-pasted from the Bazel Bash runfiles library v2.
5+
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
6+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
7+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
8+
source "$0.runfiles/$f" 2>/dev/null || \
9+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
10+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
11+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
12+
# --- end runfiles.bash initialization v2 ---
13+
14+
assertions_sh_location=cgrindel_bazel_shlib/lib/assertions.sh
15+
assertions_sh="$(rlocation "${assertions_sh_location}")" || \
16+
(echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1)
17+
source "${assertions_sh}"
18+
19+
create_scratch_dir_sh_location=cgrindel_rules_bazel_integration_test/tools/create_scratch_dir.sh
20+
create_scratch_dir_sh="$(rlocation "${create_scratch_dir_sh_location}")" || \
21+
(echo >&2 "Failed to locate ${create_scratch_dir_sh_location}" && exit 1)
22+
23+
24+
# MARK - Process Flags
3225

3326
# Process args
3427
while (("$#")); do
3528
case "${1}" in
3629
"--bazel")
37-
bazel_rel_path="${2}"
38-
shift 2
39-
;;
40-
"--bazel_cmd")
41-
bazel_cmds+=("${2}")
30+
bazel="${2}"
4231
shift 2
4332
;;
4433
"--workspace")
@@ -51,42 +40,30 @@ while (("$#")); do
5140
esac
5241
done
5342

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

58-
starting_path="$(pwd)"
59-
starting_path="${starting_path%%*( )}"
60-
bazel="$(normalize_path "${bazel_rel_path}")"
61-
6246
workspace_dir="$(dirname "${workspace_path}")"
63-
cd "${workspace_dir}"
6447

65-
# BEGIN Custom test logic
48+
# MARK - Create Scratch Directory
6649

67-
modified_file="main.swift"
68-
backup_file="${modified_file}.orig"
50+
scratch_dir="$("${create_scratch_dir_sh}" --workspace "${workspace_dir}")"
51+
cd "${scratch_dir}"
6952

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

75-
# Set trap for cleanup
76-
cleanup() {
77-
mv -f "${backup_file}" "${modified_file}"
78-
cd "${starting_dir}"
79-
}
80-
trap cleanup EXIT
55+
# Should not fail, no changes
56+
"${bazel}" test //... || fail "Expected test to succeed as nothing has changed yet."
8157

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

62+
# Should fail due to unformatted
63+
"${bazel}" test //... && fail "Expected test to fail due to unformatted files."
64+
8665
# Format the Swift files and copy them back to the workspace
87-
"${bazel}" run //:update_all
66+
"${bazel}" run //:update_all || fail "Expected update_all to succeed."
8867

8968
# This should succeed now that the formatted files have been updated
90-
"${bazel}" test //...
91-
92-
# END Custom test logic
69+
"${bazel}" test //... || fail "Expected test to succeed after update_all."

swiftformat/deps.bzl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,15 @@ def swiftformat_rules_dependencies():
4848
maybe(
4949
http_archive,
5050
name = "cgrindel_rules_bazel_integration_test",
51-
sha256 = "ef6cf463a269d969bdb3b31eed53c50d3667f02e004abc9ce7a3a2413eadca6a",
52-
strip_prefix = "rules_bazel_integration_test-0.3.0",
53-
urls = ["https://github.com/cgrindel/rules_bazel_integration_test/archive/v0.3.0.tar.gz"],
51+
sha256 = "50b808269ee09373c099256103c40629db8a66fd884030d7a36cf9a2e8675b75",
52+
strip_prefix = "rules_bazel_integration_test-0.3.1",
53+
urls = ["https://github.com/cgrindel/rules_bazel_integration_test/archive/v0.3.1.tar.gz"],
54+
)
55+
56+
maybe(
57+
http_archive,
58+
name = "cgrindel_bazel_shlib",
59+
sha256 = "39c250852fb455e5de18f836c0c339075d6e52ea5ec52a76d62ef9e2eed56337",
60+
strip_prefix = "bazel_shlib-0.2.1",
61+
urls = ["https://github.com/cgrindel/bazel_shlib/archive/v0.2.1.tar.gz"],
5462
)

0 commit comments

Comments
 (0)