From dea15165c654a7f99406f5f72c7536ed4957b3b3 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Thu, 29 Dec 2022 13:09:34 -0800 Subject: [PATCH] Add simple golden test for CMake file list generation I updated our Bazel CI jobs to cover `//pkg/...` and thereby exercise this new test. That made me realize that `//pkg/...` was not fully buildable because there was a reference to the non-existent target `@utf8_range//:dist_files`, so I also fixed that. PiperOrigin-RevId: 498437497 --- kokoro/linux/bazel/common.cfg | 2 +- pkg/BUILD.bazel | 1 - pkg/test/BUILD.bazel | 40 ++++++++++++++++++++++++++ pkg/test/file_lists.cmake.golden | 20 +++++++++++++ pkg/test/gen_file_lists_golden_test.sh | 14 +++++++++ pkg/test/test_lib.cc | 31 ++++++++++++++++++++ pkg/test/test_lib.h | 31 ++++++++++++++++++++ 7 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 pkg/test/BUILD.bazel create mode 100644 pkg/test/file_lists.cmake.golden create mode 100755 pkg/test/gen_file_lists_golden_test.sh create mode 100644 pkg/test/test_lib.cc create mode 100644 pkg/test/test_lib.h diff --git a/kokoro/linux/bazel/common.cfg b/kokoro/linux/bazel/common.cfg index 54447d9a98..667e9a2288 100644 --- a/kokoro/linux/bazel/common.cfg +++ b/kokoro/linux/bazel/common.cfg @@ -11,7 +11,7 @@ env_vars { env_vars { key: "BAZEL_TARGETS" - value: "//src/... @com_google_protobuf_examples//..." + value: "//pkg/... //src/... @com_google_protobuf_examples//..." } env_vars { diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index d7f04bbcd0..4886ae8b08 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -91,7 +91,6 @@ pkg_filegroup( "//build_defs:dist_files", "//conformance:all_dist_files", "//src:all_dist_files", - "@utf8_range//:dist_files", "@com_google_protobuf_examples//:dist_files", ], ) diff --git a/pkg/test/BUILD.bazel b/pkg/test/BUILD.bazel new file mode 100644 index 0000000000..f1ae410bfc --- /dev/null +++ b/pkg/test/BUILD.bazel @@ -0,0 +1,40 @@ +# Tests for CMake file list generation + +load("//pkg:build_systems.bzl", "gen_file_lists") +load("//pkg:cc_dist_library.bzl", "cc_dist_library") + +cc_library( + name = "test_lib", + testonly = True, + srcs = ["test_lib.cc"], + hdrs = ["test_lib.h"], +) + +cc_dist_library( + name = "test_lib_dist", + testonly = True, + deps = [":test_lib"], +) + +gen_file_lists( + name = "gen_file_lists", + testonly = True, + out_stem = "file_lists", + src_libs = { + ":test_lib_dist": "libtest", + }, +) + +# This test checks the output from gen_file_lists() against a golden file. +sh_test( + name = "gen_file_lists_golden_test", + srcs = ["gen_file_lists_golden_test.sh"], + args = [ + "$(location file_lists.cmake.golden)", + "$(location :gen_file_lists)", + ], + data = [ + "file_lists.cmake.golden", + ":gen_file_lists", + ], +) diff --git a/pkg/test/file_lists.cmake.golden b/pkg/test/file_lists.cmake.golden new file mode 100644 index 0000000000..66a619dc9b --- /dev/null +++ b/pkg/test/file_lists.cmake.golden @@ -0,0 +1,20 @@ +# Auto-generated by @//pkg/test:gen_file_lists_cmake +# +# This file contains lists of sources based on Bazel rules. It should +# be included from a hand-written CMake file that defines targets. +# +# Changes to this file will be overwritten based on Bazel definitions. + +if(${CMAKE_VERSION} VERSION_GREATER 3.10 OR ${CMAKE_VERSION} VERSION_EQUAL 3.10) + include_guard() +endif() + +# @//pkg/test:test_lib_dist +set(libtest_srcs + ${protobuf_SOURCE_DIR}/pkg/test/test_lib.cc +) + +# @//pkg/test:test_lib_dist +set(libtest_hdrs + ${protobuf_SOURCE_DIR}/pkg/test/test_lib.h +) diff --git a/pkg/test/gen_file_lists_golden_test.sh b/pkg/test/gen_file_lists_golden_test.sh new file mode 100755 index 0000000000..37b73455ab --- /dev/null +++ b/pkg/test/gen_file_lists_golden_test.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -ex + +if cmp "$1" "$2"; then + # Files are identical, as expected. + exit 0; +else + echo "Golden file $1 does not match generated output $2:" + echo "--------------------------------------------------" + diff -u "$1" "$2" + echo "--------------------------------------------------" + exit 1 +fi diff --git a/pkg/test/test_lib.cc b/pkg/test/test_lib.cc new file mode 100644 index 0000000000..d032921628 --- /dev/null +++ b/pkg/test/test_lib.cc @@ -0,0 +1,31 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Placeholder file for testing diff --git a/pkg/test/test_lib.h b/pkg/test/test_lib.h new file mode 100644 index 0000000000..d032921628 --- /dev/null +++ b/pkg/test/test_lib.h @@ -0,0 +1,31 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Placeholder file for testing