CMake: Build contrib sandboxes if requested

Add a new option `SAPI_ENABLE_CONTRIB_TESTS` that builds the sandboxes
in `contrib/` if set.

The new `contrib/CMakeLists.txt` should be extended each time we add a
new sandbox to `contrib/`.

This is in preparation of a follow-up change that should test the
sandboxes using GitHub Actions.

Drive-by:
- Rename the tests in `jsonnet_tests.cc` to conform to the style guide.
PiperOrigin-RevId: 425874289
Change-Id: I5b31546ccf84444c2480287220ef88abbd78a235
This commit is contained in:
Christian Blichmann 2022-02-02 07:01:48 -08:00 committed by Copybara-Service
parent ee5ebaa48f
commit e21afae5a7
6 changed files with 44 additions and 9 deletions

View File

@ -135,3 +135,7 @@ if(SAPI_ENABLE_TESTS)
endif()
add_subdirectory(sandboxed_api)
if(SAPI_ENABLE_TESTS AND SAPI_ENABLE_CONTRIB_TESTS)
add_subdirectory(contrib)
endif()

View File

@ -49,6 +49,9 @@ option(SAPI_DOWNLOAD_ZLIB
option(SAPI_ENABLE_TESTS
"Build unit tests" ${_sapi_enable_tests_examples_default}
)
# Disabled by default, as this will download a lot of extra content.
option(SAPI_ENABLE_CONTRIB_TESTS "Build tests for sandboxes in 'contrib'" OFF)
option(SAPI_ENABLE_GENERATOR
"Build Clang based code generator from source" OFF
)

View File

@ -195,8 +195,14 @@ endfunction()
# directories and `EXCLUDE_FROM_ALL`.
# This is useful in embedding projects to be able to refer to pre-sandboxed
# libraries easily.
# In order to be able build everything in one go, this macro also accepts a
# `INCLUDE_FROM_ALL` option. It is expected that this will only be used from
# `contrib/CMakeLists.txt`.
macro(add_sapi_subdirectory)
add_subdirectory("${SAPI_SOURCE_DIR}/${ARGV0}"
"${SAPI_BINARY_DIR}/${ARGV0}"
EXCLUDE_FROM_ALL)
cmake_parse_arguments(_sd "INCLUDE_FROM_ALL" "" "" ${ARGN})
if(NOT ${_sd_INCLUDE_FROM_ALL})
set(_sd_exclude_from_all EXCLUDE_FROM_ALL)
endif()
add_subdirectory("${SAPI_SOURCE_DIR}/${ARGV0}" "${SAPI_BINARY_DIR}/${ARGV0}"
${_sd_exclude_from_all})
endmacro()

22
contrib/CMakeLists.txt Normal file
View File

@ -0,0 +1,22 @@
# Copyright 2022 Google LLC
#
# 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
#
# https://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.
# Append to this list whenever a new sandboxed library is added to `contrib/`.
set(SAPI_CONTRIB_SANDBOXES
jsonnet
)
foreach(_contrib IN LISTS SAPI_CONTRIB_SANDBOXES)
add_sapi_subdirectory("contrib/${_contrib}" INCLUDE_FROM_ALL)
endforeach()

View File

@ -29,7 +29,7 @@ FetchContent_Declare(jsonnet
GIT_REPOSITORY https://github.com/google/jsonnet.git
GIT_TAG v0.18.0 # 2021-12-21
)
set(BUILD_TESTS OFF CACHE BOOL "") # Do not build jsonnet tests
option(BUILD_TESTS "" OFF) # Do not build jsonnet tests
FetchContent_MakeAvailable(jsonnet)
create_directory_symlink("${jsonnet_SOURCE_DIR}"
"${PROJECT_BINARY_DIR}/jsonnet")

View File

@ -193,7 +193,7 @@ std::string JsonnetTest::ReadOutput(const char* filename) {
}
// One file evaluation to one file
TEST_F(JsonnetTest, One_file_no_dependencies) {
TEST_F(JsonnetTest, OneFileNoDependencies) {
constexpr char kInputFile[] = "arith.jsonnet";
constexpr char kOutputFile[] = "arith_output";
constexpr char kOutputToRead[] = "tests_output/arith_output";
@ -210,7 +210,7 @@ TEST_F(JsonnetTest, One_file_no_dependencies) {
}
// One file evaluating to one file, dependent on some other files
TEST_F(JsonnetTest, One_file_some_dependencies) {
TEST_F(JsonnetTest, OneFileSomeDependencies) {
constexpr char kInputFile[] = "negroni.jsonnet";
constexpr char kOutputFile[] = "negroni_output";
constexpr char kOutputToRead[] = "tests_output/negroni_output";
@ -227,7 +227,7 @@ TEST_F(JsonnetTest, One_file_some_dependencies) {
}
// One file evaluating to two files
TEST_F(JsonnetTest, Multiple_files) {
TEST_F(JsonnetTest, MultipleFiles) {
constexpr char kInputFile[] = "multiple_files_example.jsonnet";
constexpr char kOutputFile[] = "";
constexpr char kOutputToRead1[] = "tests_output/first_file.json";
@ -249,7 +249,7 @@ TEST_F(JsonnetTest, Multiple_files) {
}
// One file evaluating to yaml stream format
TEST_F(JsonnetTest, Yaml_stream) {
TEST_F(JsonnetTest, YamlStream) {
constexpr char kInputFile[] = "yaml_stream_example.jsonnet";
constexpr char kOutputFile[] = "yaml_stream_example.yaml";
constexpr char kOutputToRead[] = "tests_output/yaml_stream_example.yaml";
@ -267,7 +267,7 @@ TEST_F(JsonnetTest, Yaml_stream) {
}
// One file depended on some other files not accessible by the sandbox
TEST_F(JsonnetTest, Bad_evaluation) {
TEST_F(JsonnetTest, BadEvaluation) {
constexpr char kInputFile[] = "imports.jsonnet";
ReadInput(kInputFile);