xref: /aosp_15_r20/external/protobuf/build_files_updated_unittest.sh (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1*1b3f573fSAndroid Build Coastguard Worker#!/bin/bash
2*1b3f573fSAndroid Build Coastguard Worker
3*1b3f573fSAndroid Build Coastguard Worker# This script verifies that BUILD files and cmake files are in sync with src/Makefile.am
4*1b3f573fSAndroid Build Coastguard Worker
5*1b3f573fSAndroid Build Coastguard Workerset -eo pipefail
6*1b3f573fSAndroid Build Coastguard Worker
7*1b3f573fSAndroid Build Coastguard Workerif [ "$(uname)" != "Linux" ]; then
8*1b3f573fSAndroid Build Coastguard Worker  echo "build_files_updated_unittest only supported on Linux. Skipping..."
9*1b3f573fSAndroid Build Coastguard Worker  exit 0
10*1b3f573fSAndroid Build Coastguard Workerfi
11*1b3f573fSAndroid Build Coastguard Worker
12*1b3f573fSAndroid Build Coastguard Worker# Keep in sync with files needed by update_file_lists.sh
13*1b3f573fSAndroid Build Coastguard Workergenerated_files=(
14*1b3f573fSAndroid Build Coastguard Worker  "BUILD"
15*1b3f573fSAndroid Build Coastguard Worker  "cmake/extract_includes.bat.in"
16*1b3f573fSAndroid Build Coastguard Worker  "cmake/libprotobuf-lite.cmake"
17*1b3f573fSAndroid Build Coastguard Worker  "cmake/libprotobuf.cmake"
18*1b3f573fSAndroid Build Coastguard Worker  "cmake/libprotoc.cmake"
19*1b3f573fSAndroid Build Coastguard Worker  "cmake/tests.cmake"
20*1b3f573fSAndroid Build Coastguard Worker  "src/Makefile.am"
21*1b3f573fSAndroid Build Coastguard Worker)
22*1b3f573fSAndroid Build Coastguard Worker
23*1b3f573fSAndroid Build Coastguard Worker# If we're running in Bazel, use the Bazel-provided temp-dir.
24*1b3f573fSAndroid Build Coastguard Workerif [ -n "${TEST_TMPDIR}" ]; then
25*1b3f573fSAndroid Build Coastguard Worker  # Env-var TEST_TMPDIR is set, assume that this is Bazel.
26*1b3f573fSAndroid Build Coastguard Worker  # Bazel may have opinions whether we are allowed to delete TEST_TMPDIR.
27*1b3f573fSAndroid Build Coastguard Worker  test_root="${TEST_TMPDIR}/build_files_updated_unittest"
28*1b3f573fSAndroid Build Coastguard Worker  mkdir "${test_root}"
29*1b3f573fSAndroid Build Coastguard Workerelse
30*1b3f573fSAndroid Build Coastguard Worker  # Seems like we're not executed by Bazel.
31*1b3f573fSAndroid Build Coastguard Worker  test_root=$(mktemp -d)
32*1b3f573fSAndroid Build Coastguard Workerfi
33*1b3f573fSAndroid Build Coastguard Worker
34*1b3f573fSAndroid Build Coastguard Worker# From now on, fail if there are any unbound variables.
35*1b3f573fSAndroid Build Coastguard Workerset -u
36*1b3f573fSAndroid Build Coastguard Worker
37*1b3f573fSAndroid Build Coastguard Worker# Remove artifacts after test is finished.
38*1b3f573fSAndroid Build Coastguard Workerfunction cleanup {
39*1b3f573fSAndroid Build Coastguard Worker  rm -rf "${test_root}"
40*1b3f573fSAndroid Build Coastguard Worker}
41*1b3f573fSAndroid Build Coastguard Workertrap cleanup EXIT
42*1b3f573fSAndroid Build Coastguard Worker
43*1b3f573fSAndroid Build Coastguard Worker# Create golden dir and add snapshot of current state.
44*1b3f573fSAndroid Build Coastguard Workergolden_dir="${test_root}/golden"
45*1b3f573fSAndroid Build Coastguard Workermkdir -p "${golden_dir}/cmake" "${golden_dir}/src"
46*1b3f573fSAndroid Build Coastguard Workerfor file in ${generated_files[@]}; do
47*1b3f573fSAndroid Build Coastguard Worker  cp "${file}" "${golden_dir}/${file}"
48*1b3f573fSAndroid Build Coastguard Workerdone
49*1b3f573fSAndroid Build Coastguard Worker
50*1b3f573fSAndroid Build Coastguard Worker# Create test dir, copy current state into it, and execute update script.
51*1b3f573fSAndroid Build Coastguard Workertest_dir="${test_root}/test"
52*1b3f573fSAndroid Build Coastguard Workercp -R "${golden_dir}" "${test_dir}"
53*1b3f573fSAndroid Build Coastguard Worker
54*1b3f573fSAndroid Build Coastguard Workercp "update_file_lists.sh" "${test_dir}/update_file_lists.sh"
55*1b3f573fSAndroid Build Coastguard Workerchmod +x "${test_dir}/update_file_lists.sh"
56*1b3f573fSAndroid Build Coastguard Workercd "${test_root}/test"
57*1b3f573fSAndroid Build Coastguard Workerbash "${test_dir}/update_file_lists.sh"
58*1b3f573fSAndroid Build Coastguard Worker
59*1b3f573fSAndroid Build Coastguard Worker# Test whether there are any differences
60*1b3f573fSAndroid Build Coastguard Workerfor file in ${generated_files[@]}; do
61*1b3f573fSAndroid Build Coastguard Worker  diff -du "${golden_dir}/${file}" "${test_dir}/${file}"
62*1b3f573fSAndroid Build Coastguard Workerdone
63