blob: d0264d05492b0b92294678391dfd43cd72ba7af1 [file] [log] [blame]
#!/usr/bin/env bash
# Copyright 2019 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.
#
# Renerates most Stardoc golden files for Stardoc golden tests.
#
# When run, every golden file which is changed as a result of this script should
# be manually examined and *heavily* scrutinized, as this usually indicates a large
# in-place change of core rendering functionality of Stardoc.
set -eu
function run_buildozer () {
# buildozer uses return code 3 to signal a no-op, so we can't use "set -e"
set +e
buildozer "$@"
ret=$?
set -e
if [[ $ret != 0 && $ret != 3 ]]; then
return $ret
fi
}
# Allow users to override the bazel command with e.g. bazelisk.
: "${BAZEL:=bazel}"
echo "** Verifying Bazel version..."
if ! ${BAZEL} build --experimental_enable_starlark_doc_extract &> /dev/null; then
echo >&2 "Please use a development version of Bazel, or Bazel 7 once it is released."
echo >&2 "For example: USE_BAZEL_VERSION=last_green BAZEL=bazelisk ./update-stardoc-tests.sh"
exit 1
fi
# Some tests cannot be automatically regenerated using this script, as they don't fall under the normal
# golden test pattern
EXCLUDED_TESTS="namespace_test_with_allowlist|multi_level_namespace_test_with_allowlist|local_repository_test"
echo "** Querying for tests..."
regen_legacy_targets=$(${BAZEL} query //test:all | grep regenerate_with_jar | grep -vE "_golden_stardoc|$EXCLUDED_TESTS")
regen_starlark_doc_extract_targets=$(${BAZEL} query //test:all | grep regenerate_ | grep -vE "_legacy|_golden\.extract|$EXCLUDED_TESTS")
echo "** Building goldens..."
${BAZEL} build $regen_legacy_targets $regen_starlark_doc_extract_targets
echo "** Copying starlark_doc_extract goldens..."
for regen_target in $regen_starlark_doc_extract_targets; do
base_target_name=$(echo $regen_target | sed 's/\/\/test://g')
testdata_pkg_name=$(echo $base_target_name | sed 's/regenerate_//g' | sed 's/_golden//g')
out_file="bazel-bin/test/${base_target_name}.out"
if [[ $regen_target == *"proto_format"* ]]; then
ext="binaryproto"
else
ext="md"
fi
golden="test/testdata/${testdata_pkg_name}/golden.${ext}"
cp "${out_file}" "${golden}"
chmod 644 "${golden}"
done
echo "** Copying legacy goldens..."
for regen_target in $regen_legacy_targets; do
base_target_name=$(echo $regen_target | sed 's/\/\/test://g')
testdata_pkg_name=$(echo $base_target_name | sed 's/regenerate_with_jar_//g' | sed 's/_legacy_golden//g')
out_file="bazel-bin/test/${base_target_name}.out"
if [[ $regen_target == *"proto_format"* ]]; then
ext="binaryproto"
else
ext="md"
fi
golden="test/testdata/${testdata_pkg_name}/golden.${ext}"
legacy_golden="test/testdata/${testdata_pkg_name}/legacy_golden.${ext}"
legacy_golden_target="${legacy_golden#test/}"
if diff "${out_file}" "${golden}" > /dev/null; then
# legacy_golden is no longer needed; we only need golden
[[ -e "${legacy_golden}" ]] && rm "${legacy_golden}"
run_buildozer "remove legacy_golden_file" "//test:${testdata_pkg_name}"
else
cp "${out_file}" "${legacy_golden}"
chmod 644 "${legacy_golden}"
run_buildozer "set legacy_golden_file \"${legacy_golden_target}\"" "//test:${testdata_pkg_name}"
fi
done
echo "** Files copied."
echo "Please note that not all golden files are correctly copied by this script."
echo "You may want to manually run:"
echo ""
echo "${BAZEL} test //test:all"
echo ""
echo "...and manually update tests which are still broken."