From 1a02c75bedcbcc3f9654909fd73b46e45185a2c3 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Thu, 27 Jan 2022 11:43:05 +0100 Subject: [PATCH 1/4] Jsonnet: Move to `contrib/` --- {oss-internship-2020 => contrib}/jsonnet/CMakeLists.txt | 2 +- {oss-internship-2020 => contrib}/jsonnet/README.md | 0 .../jsonnet/examples/CMakeLists.txt | 0 .../jsonnet/examples/jsonnet_base_example.cc | 0 .../jsonnet/examples/jsonnet_base_transaction.cc | 0 .../jsonnet/examples/jsonnet_codes/arith.jsonnet | 0 .../jsonnet/examples/jsonnet_codes/formatter_example.jsonnet | 0 .../jsonnet/examples/jsonnet_codes/imports.jsonnet | 0 .../examples/jsonnet_codes/multiple_files_example.jsonnet | 0 .../jsonnet/examples/jsonnet_codes/negroni.jsonnet | 0 .../jsonnet/examples/jsonnet_codes/utils.libsonnet | 0 .../examples/jsonnet_codes/yaml_stream_example.jsonnet | 0 .../examples/jsonnet_codes_expected_output/arith.golden | 0 .../examples/jsonnet_codes_expected_output/first_file.json | 0 .../examples/jsonnet_codes_expected_output/negroni.golden | 0 .../examples/jsonnet_codes_expected_output/second_file.json | 0 .../jsonnet_codes_expected_output/yaml_stream_example.yaml | 0 .../jsonnet/examples/jsonnet_formatter_example.cc | 0 .../jsonnet/examples/jsonnet_multiple_files_example.cc | 0 .../jsonnet/examples/jsonnet_yaml_stream_example.cc | 0 .../jsonnet/headers/jsonnet_base_sandbox.h | 0 .../jsonnet/headers/jsonnet_base_transaction.h | 0 {oss-internship-2020 => contrib}/jsonnet/jsonnet.patch | 0 {oss-internship-2020 => contrib}/jsonnet/jsonnet_helper.cc | 0 {oss-internship-2020 => contrib}/jsonnet/jsonnet_helper.h | 0 {oss-internship-2020 => contrib}/jsonnet/tests/CMakeLists.txt | 4 ++-- .../jsonnet/tests/jsonnet_test_helper.cc | 0 .../jsonnet/tests/jsonnet_test_helper.h | 0 .../jsonnet/tests/jsonnet_tests.cc | 0 29 files changed, 3 insertions(+), 3 deletions(-) rename {oss-internship-2020 => contrib}/jsonnet/CMakeLists.txt (96%) rename {oss-internship-2020 => contrib}/jsonnet/README.md (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/CMakeLists.txt (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_base_example.cc (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_base_transaction.cc (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes/arith.jsonnet (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes/formatter_example.jsonnet (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes/imports.jsonnet (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes/multiple_files_example.jsonnet (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes/negroni.jsonnet (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes/utils.libsonnet (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes/yaml_stream_example.jsonnet (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes_expected_output/arith.golden (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes_expected_output/first_file.json (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes_expected_output/negroni.golden (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes_expected_output/second_file.json (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_codes_expected_output/yaml_stream_example.yaml (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_formatter_example.cc (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_multiple_files_example.cc (100%) rename {oss-internship-2020 => contrib}/jsonnet/examples/jsonnet_yaml_stream_example.cc (100%) rename {oss-internship-2020 => contrib}/jsonnet/headers/jsonnet_base_sandbox.h (100%) rename {oss-internship-2020 => contrib}/jsonnet/headers/jsonnet_base_transaction.h (100%) rename {oss-internship-2020 => contrib}/jsonnet/jsonnet.patch (100%) rename {oss-internship-2020 => contrib}/jsonnet/jsonnet_helper.cc (100%) rename {oss-internship-2020 => contrib}/jsonnet/jsonnet_helper.h (100%) rename {oss-internship-2020 => contrib}/jsonnet/tests/CMakeLists.txt (94%) rename oss-internship-2020/jsonnet/tests/jsonnet_tests_utils.cc => contrib/jsonnet/tests/jsonnet_test_helper.cc (100%) rename oss-internship-2020/jsonnet/headers/jsonnet_tests.h => contrib/jsonnet/tests/jsonnet_test_helper.h (100%) rename {oss-internship-2020 => contrib}/jsonnet/tests/jsonnet_tests.cc (100%) diff --git a/oss-internship-2020/jsonnet/CMakeLists.txt b/contrib/jsonnet/CMakeLists.txt similarity index 96% rename from oss-internship-2020/jsonnet/CMakeLists.txt rename to contrib/jsonnet/CMakeLists.txt index e413fbd..02cc319 100644 --- a/oss-internship-2020/jsonnet/CMakeLists.txt +++ b/contrib/jsonnet/CMakeLists.txt @@ -28,7 +28,7 @@ FetchContent_Declare(jsonnet GIT_REPOSITORY https://github.com/google/jsonnet.git GIT_TAG v0.18.0 # 2021-12-21 ) -set(BUILD_TESTS OFF) +set(BUILD_TESTS OFF CACHE BOOL "") # Do not build jsonnet tests FetchContent_MakeAvailable(jsonnet) create_directory_symlink("${jsonnet_SOURCE_DIR}" "${PROJECT_BINARY_DIR}/jsonnet") diff --git a/oss-internship-2020/jsonnet/README.md b/contrib/jsonnet/README.md similarity index 100% rename from oss-internship-2020/jsonnet/README.md rename to contrib/jsonnet/README.md diff --git a/oss-internship-2020/jsonnet/examples/CMakeLists.txt b/contrib/jsonnet/examples/CMakeLists.txt similarity index 100% rename from oss-internship-2020/jsonnet/examples/CMakeLists.txt rename to contrib/jsonnet/examples/CMakeLists.txt diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_base_example.cc b/contrib/jsonnet/examples/jsonnet_base_example.cc similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_base_example.cc rename to contrib/jsonnet/examples/jsonnet_base_example.cc diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_base_transaction.cc b/contrib/jsonnet/examples/jsonnet_base_transaction.cc similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_base_transaction.cc rename to contrib/jsonnet/examples/jsonnet_base_transaction.cc diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/arith.jsonnet b/contrib/jsonnet/examples/jsonnet_codes/arith.jsonnet similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes/arith.jsonnet rename to contrib/jsonnet/examples/jsonnet_codes/arith.jsonnet diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/formatter_example.jsonnet b/contrib/jsonnet/examples/jsonnet_codes/formatter_example.jsonnet similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes/formatter_example.jsonnet rename to contrib/jsonnet/examples/jsonnet_codes/formatter_example.jsonnet diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/imports.jsonnet b/contrib/jsonnet/examples/jsonnet_codes/imports.jsonnet similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes/imports.jsonnet rename to contrib/jsonnet/examples/jsonnet_codes/imports.jsonnet diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/multiple_files_example.jsonnet b/contrib/jsonnet/examples/jsonnet_codes/multiple_files_example.jsonnet similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes/multiple_files_example.jsonnet rename to contrib/jsonnet/examples/jsonnet_codes/multiple_files_example.jsonnet diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/negroni.jsonnet b/contrib/jsonnet/examples/jsonnet_codes/negroni.jsonnet similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes/negroni.jsonnet rename to contrib/jsonnet/examples/jsonnet_codes/negroni.jsonnet diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/utils.libsonnet b/contrib/jsonnet/examples/jsonnet_codes/utils.libsonnet similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes/utils.libsonnet rename to contrib/jsonnet/examples/jsonnet_codes/utils.libsonnet diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/yaml_stream_example.jsonnet b/contrib/jsonnet/examples/jsonnet_codes/yaml_stream_example.jsonnet similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes/yaml_stream_example.jsonnet rename to contrib/jsonnet/examples/jsonnet_codes/yaml_stream_example.jsonnet diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/arith.golden b/contrib/jsonnet/examples/jsonnet_codes_expected_output/arith.golden similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/arith.golden rename to contrib/jsonnet/examples/jsonnet_codes_expected_output/arith.golden diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/first_file.json b/contrib/jsonnet/examples/jsonnet_codes_expected_output/first_file.json similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/first_file.json rename to contrib/jsonnet/examples/jsonnet_codes_expected_output/first_file.json diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/negroni.golden b/contrib/jsonnet/examples/jsonnet_codes_expected_output/negroni.golden similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/negroni.golden rename to contrib/jsonnet/examples/jsonnet_codes_expected_output/negroni.golden diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/second_file.json b/contrib/jsonnet/examples/jsonnet_codes_expected_output/second_file.json similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/second_file.json rename to contrib/jsonnet/examples/jsonnet_codes_expected_output/second_file.json diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/yaml_stream_example.yaml b/contrib/jsonnet/examples/jsonnet_codes_expected_output/yaml_stream_example.yaml similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/yaml_stream_example.yaml rename to contrib/jsonnet/examples/jsonnet_codes_expected_output/yaml_stream_example.yaml diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_formatter_example.cc b/contrib/jsonnet/examples/jsonnet_formatter_example.cc similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_formatter_example.cc rename to contrib/jsonnet/examples/jsonnet_formatter_example.cc diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_multiple_files_example.cc b/contrib/jsonnet/examples/jsonnet_multiple_files_example.cc similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_multiple_files_example.cc rename to contrib/jsonnet/examples/jsonnet_multiple_files_example.cc diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_yaml_stream_example.cc b/contrib/jsonnet/examples/jsonnet_yaml_stream_example.cc similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_yaml_stream_example.cc rename to contrib/jsonnet/examples/jsonnet_yaml_stream_example.cc diff --git a/oss-internship-2020/jsonnet/headers/jsonnet_base_sandbox.h b/contrib/jsonnet/headers/jsonnet_base_sandbox.h similarity index 100% rename from oss-internship-2020/jsonnet/headers/jsonnet_base_sandbox.h rename to contrib/jsonnet/headers/jsonnet_base_sandbox.h diff --git a/oss-internship-2020/jsonnet/headers/jsonnet_base_transaction.h b/contrib/jsonnet/headers/jsonnet_base_transaction.h similarity index 100% rename from oss-internship-2020/jsonnet/headers/jsonnet_base_transaction.h rename to contrib/jsonnet/headers/jsonnet_base_transaction.h diff --git a/oss-internship-2020/jsonnet/jsonnet.patch b/contrib/jsonnet/jsonnet.patch similarity index 100% rename from oss-internship-2020/jsonnet/jsonnet.patch rename to contrib/jsonnet/jsonnet.patch diff --git a/oss-internship-2020/jsonnet/jsonnet_helper.cc b/contrib/jsonnet/jsonnet_helper.cc similarity index 100% rename from oss-internship-2020/jsonnet/jsonnet_helper.cc rename to contrib/jsonnet/jsonnet_helper.cc diff --git a/oss-internship-2020/jsonnet/jsonnet_helper.h b/contrib/jsonnet/jsonnet_helper.h similarity index 100% rename from oss-internship-2020/jsonnet/jsonnet_helper.h rename to contrib/jsonnet/jsonnet_helper.h diff --git a/oss-internship-2020/jsonnet/tests/CMakeLists.txt b/contrib/jsonnet/tests/CMakeLists.txt similarity index 94% rename from oss-internship-2020/jsonnet/tests/CMakeLists.txt rename to contrib/jsonnet/tests/CMakeLists.txt index a408edb..9925b61 100644 --- a/oss-internship-2020/jsonnet/tests/CMakeLists.txt +++ b/contrib/jsonnet/tests/CMakeLists.txt @@ -23,9 +23,9 @@ add_custom_target(test_preparation ALL ) add_executable(tests - ${PROJECT_SOURCE_DIR}/headers/jsonnet_tests.h + jsonnet_test_helper.cc + jsonnet_test_helper.h jsonnet_tests.cc - jsonnet_tests_utils.cc ) target_include_directories(tests PUBLIC diff --git a/oss-internship-2020/jsonnet/tests/jsonnet_tests_utils.cc b/contrib/jsonnet/tests/jsonnet_test_helper.cc similarity index 100% rename from oss-internship-2020/jsonnet/tests/jsonnet_tests_utils.cc rename to contrib/jsonnet/tests/jsonnet_test_helper.cc diff --git a/oss-internship-2020/jsonnet/headers/jsonnet_tests.h b/contrib/jsonnet/tests/jsonnet_test_helper.h similarity index 100% rename from oss-internship-2020/jsonnet/headers/jsonnet_tests.h rename to contrib/jsonnet/tests/jsonnet_test_helper.h diff --git a/oss-internship-2020/jsonnet/tests/jsonnet_tests.cc b/contrib/jsonnet/tests/jsonnet_tests.cc similarity index 100% rename from oss-internship-2020/jsonnet/tests/jsonnet_tests.cc rename to contrib/jsonnet/tests/jsonnet_tests.cc From cb2aef7d26ade8da9376c1b5d7614e329ecf8025 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Thu, 27 Jan 2022 12:17:18 +0100 Subject: [PATCH 2/4] Jsonnet: Don't copy/patch in every CMake run --- contrib/jsonnet/examples/CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contrib/jsonnet/examples/CMakeLists.txt b/contrib/jsonnet/examples/CMakeLists.txt index 40318f3..1a40fb5 100644 --- a/contrib/jsonnet/examples/CMakeLists.txt +++ b/contrib/jsonnet/examples/CMakeLists.txt @@ -12,14 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/gen_files") -file(COPY "${jsonnet_SOURCE_DIR}/cmd/jsonnet.cpp" DESTINATION "${PROJECT_BINARY_DIR}/gen_files/") -file(COPY "${PROJECT_SOURCE_DIR}/jsonnet.patch" DESTINATION "${PROJECT_BINARY_DIR}/gen_files/") +configure_file("${jsonnet_SOURCE_DIR}/cmd/jsonnet.cpp" + "${PROJECT_BINARY_DIR}/gen_files/jsonnet.cpp" COPYONLY) add_custom_command( - OUTPUT ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc - COMMAND cd ${PROJECT_BINARY_DIR}/gen_files && patch < ${PROJECT_SOURCE_DIR}/jsonnet.patch > /dev/null - COMMAND mv ${PROJECT_BINARY_DIR}/gen_files/jsonnet.cpp ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc + OUTPUT "${PROJECT_BINARY_DIR}/gen_files/write_helper.cc" + WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/gen_files" + COMMAND patch -o write_helper.cc + < "${PROJECT_SOURCE_DIR}/jsonnet.patch" > /dev/null ) list(APPEND JSONNET_SAPI_INCLUDE_DIRS From dc0b14d87188d8ae3fc62ea0392cf44c678fc01f Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Thu, 27 Jan 2022 13:59:14 +0100 Subject: [PATCH 3/4] Jsonnet: Restructure tests - Move and rename headers to main dir - Fixup header guards - Rename test main binary that conflicted with directory name `tests` --- contrib/jsonnet/CMakeLists.txt | 26 ++++++++- contrib/jsonnet/examples/CMakeLists.txt | 1 - .../{headers => }/jsonnet_base_sandbox.h | 6 +-- .../{headers => }/jsonnet_base_transaction.h | 6 +-- contrib/jsonnet/jsonnet_helper.h | 53 +++++++++---------- .../{tests => }/jsonnet_test_helper.cc | 2 +- .../jsonnet/{tests => }/jsonnet_test_helper.h | 6 +-- contrib/jsonnet/{tests => }/jsonnet_tests.cc | 2 +- contrib/jsonnet/tests/CMakeLists.txt | 40 -------------- 9 files changed, 61 insertions(+), 81 deletions(-) rename contrib/jsonnet/{headers => }/jsonnet_base_sandbox.h (92%) rename contrib/jsonnet/{headers => }/jsonnet_base_transaction.h (89%) rename contrib/jsonnet/{tests => }/jsonnet_test_helper.cc (98%) rename contrib/jsonnet/{tests => }/jsonnet_test_helper.h (94%) rename contrib/jsonnet/{tests => }/jsonnet_tests.cc (98%) delete mode 100644 contrib/jsonnet/tests/CMakeLists.txt diff --git a/contrib/jsonnet/CMakeLists.txt b/contrib/jsonnet/CMakeLists.txt index 02cc319..6b009cb 100644 --- a/contrib/jsonnet/CMakeLists.txt +++ b/contrib/jsonnet/CMakeLists.txt @@ -64,5 +64,29 @@ if(SAPI_ENABLE_TESTS) include(GoogleTest) enable_testing() - add_subdirectory(tests) + # Create directories so the tests will be able to access them + file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/tests/tests_input" + "${PROJECT_BINARY_DIR}/tests/tests_output" + "${PROJECT_BINARY_DIR}/tests/tests_expected_output") + + add_custom_target(test_preparation ALL + COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/* + ${PROJECT_BINARY_DIR}/tests/tests_input + COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* + ${PROJECT_BINARY_DIR}/tests/tests_expected_output + ) + + add_executable(jsonnet_tests + jsonnet_test_helper.cc + jsonnet_test_helper.h + jsonnet_tests.cc + ) + target_include_directories(jsonnet_tests PUBLIC + ${PROJECT_SOURCE_DIR} + ) + target_link_libraries(jsonnet_tests + jsonnet_sapi + sapi::test_main + ) + gtest_discover_tests(jsonnet_tests) endif() diff --git a/contrib/jsonnet/examples/CMakeLists.txt b/contrib/jsonnet/examples/CMakeLists.txt index 1a40fb5..64b03b2 100644 --- a/contrib/jsonnet/examples/CMakeLists.txt +++ b/contrib/jsonnet/examples/CMakeLists.txt @@ -24,7 +24,6 @@ add_custom_command( list(APPEND JSONNET_SAPI_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} - ${PROJECT_SOURCE_DIR}/headers ${PROJECT_BINARY_DIR} ${PROJECT_BINARY_DIR}/gen_files ) diff --git a/contrib/jsonnet/headers/jsonnet_base_sandbox.h b/contrib/jsonnet/jsonnet_base_sandbox.h similarity index 92% rename from contrib/jsonnet/headers/jsonnet_base_sandbox.h rename to contrib/jsonnet/jsonnet_base_sandbox.h index 4d2eb1e..caff406 100644 --- a/contrib/jsonnet/headers/jsonnet_base_sandbox.h +++ b/contrib/jsonnet/jsonnet_base_sandbox.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef JSONNET_BASE_SANDBOX_H_ -#define JSONNET_BASE_SANDBOX_H_ +#ifndef CONTRIB_JSONNET_BASE_SANDBOX_H_ +#define CONTRIB_JSONNET_BASE_SANDBOX_H_ #include #include @@ -52,4 +52,4 @@ class JsonnetBaseSandbox : public JsonnetSandbox { std::string out_file_; }; -#endif // JSONNET_BASE_SANDBOX_H_ +#endif // CONTRIB_JSONNET_BASE_SANDBOX_H_ diff --git a/contrib/jsonnet/headers/jsonnet_base_transaction.h b/contrib/jsonnet/jsonnet_base_transaction.h similarity index 89% rename from contrib/jsonnet/headers/jsonnet_base_transaction.h rename to contrib/jsonnet/jsonnet_base_transaction.h index 7208ae3..f45685d 100644 --- a/contrib/jsonnet/headers/jsonnet_base_transaction.h +++ b/contrib/jsonnet/jsonnet_base_transaction.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef JSNONNET_BASE_TRANSACTION_H_ -#define JSNONNET_BASE_TRANSACTION_H_ +#ifndef CONTRIB_JSNONNET_BASE_TRANSACTION_H_ +#define CONTRIB_JSNONNET_BASE_TRANSACTION_H_ #include "jsonnet_base_sandbox.h" // NOLINT(build/include) @@ -35,4 +35,4 @@ class JsonnetTransaction : public sapi::Transaction { absl::Status Main() override; }; -#endif // JSNONNET_BASE_TRANSACTION_H_ +#endif // CONTRIB_JSNONNET_BASE_TRANSACTION_H_ diff --git a/contrib/jsonnet/jsonnet_helper.h b/contrib/jsonnet/jsonnet_helper.h index f38ed74..c34d6e9 100644 --- a/contrib/jsonnet/jsonnet_helper.h +++ b/contrib/jsonnet/jsonnet_helper.h @@ -12,55 +12,52 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef JSONNET_HELPER_H_ -#define JSONNET_HELPER_H_ +#ifndef CONTRIB_JSONNET_HELPER_H_ +#define CONTRIB_JSONNET_HELPER_H_ extern "C" { -#include // NOLINT(build/include) +#include // NOLINT(build/include) #include // NOLINT(build/include) } #include "jsonnet/cmd/utils.h" // NOLINT(build/include) -extern "C" struct JsonnetVm* c_jsonnet_make(void); +extern "C" { +struct JsonnetVm* c_jsonnet_make(void); -extern "C" void c_jsonnet_destroy(struct JsonnetVm* vm); +void c_jsonnet_destroy(struct JsonnetVm* vm); -extern "C" char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm, - const char* filename, char* snippet, - int* error); +char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm, const char* filename, + char* snippet, int* error); -extern "C" char* c_jsonnet_evaluate_snippet_multi(struct JsonnetVm* vm, - const char* filename, - const char* snippet, - int* error); +char* c_jsonnet_evaluate_snippet_multi(struct JsonnetVm* vm, + const char* filename, + const char* snippet, int* error); -extern "C" char* c_jsonnet_evaluate_snippet_stream(struct JsonnetVm* vm, - const char* filename, - const char* snippet, - int* error); +char* c_jsonnet_evaluate_snippet_stream(struct JsonnetVm* vm, + const char* filename, + const char* snippet, int* error); -extern "C" char* c_read_input(bool filename_is_code, const char* filename); +char* c_read_input(bool filename_is_code, const char* filename); -extern "C" void c_free_input(char* input); +void c_free_input(char* input); -extern "C" bool c_write_output_file(const char* output, - const char* output_file); +bool c_write_output_file(const char* output, const char* output_file); -extern "C" char* c_jsonnet_realloc(struct JsonnetVm* vm, char* str, size_t sz); +char* c_jsonnet_realloc(struct JsonnetVm* vm, char* str, size_t sz); -extern "C" bool c_write_multi_output_files(char* output, char* output_dir, - bool show_output_file_names); +bool c_write_multi_output_files(char* output, char* output_dir, + bool show_output_file_names); bool write_multi_output_files(char* output, const std::string& output_dir, bool show_output_file_names); -extern "C" bool c_write_output_stream(char* output, char* output_file); +bool c_write_output_stream(char* output, char* output_file); bool write_output_stream(char* output, const std::string& output_file); -extern "C" char* c_jsonnet_fmt_snippet(struct JsonnetVm* vm, - const char* filename, - const char* snippet, int* error); +char* c_jsonnet_fmt_snippet(struct JsonnetVm* vm, const char* filename, + const char* snippet, int* error); +} -#endif // JSONNET_HELPER_H_ +#endif // CONTRIB_JSONNET_HELPER_H_ diff --git a/contrib/jsonnet/tests/jsonnet_test_helper.cc b/contrib/jsonnet/jsonnet_test_helper.cc similarity index 98% rename from contrib/jsonnet/tests/jsonnet_test_helper.cc rename to contrib/jsonnet/jsonnet_test_helper.cc index d5b3900..894ea99 100644 --- a/contrib/jsonnet/tests/jsonnet_test_helper.cc +++ b/contrib/jsonnet/jsonnet_test_helper.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "jsonnet_tests.h" // NOLINT(build/include) +#include "jsonnet_test_helper.h" // NOLINT(build/include) // Prepares what is needed to perform a test. void JsonnetTestHelper::TestSetUp() { diff --git a/contrib/jsonnet/tests/jsonnet_test_helper.h b/contrib/jsonnet/jsonnet_test_helper.h similarity index 94% rename from contrib/jsonnet/tests/jsonnet_test_helper.h rename to contrib/jsonnet/jsonnet_test_helper.h index 898249b..06da104 100644 --- a/contrib/jsonnet/tests/jsonnet_test_helper.h +++ b/contrib/jsonnet/jsonnet_test_helper.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef JSONNET_TESTS_H_ -#define JSONNET_TESTS_H_ +#ifndef CONTRIB_JSONNET_TESTS_H_ +#define CONTRIB_JSONNET_TESTS_H_ #include @@ -54,4 +54,4 @@ class JsonnetTestHelper { bool input_was_read_; }; -#endif // JSONNET_TESTS_H_ +#endif // CONTRIB_JSONNET_TESTS_H_ diff --git a/contrib/jsonnet/tests/jsonnet_tests.cc b/contrib/jsonnet/jsonnet_tests.cc similarity index 98% rename from contrib/jsonnet/tests/jsonnet_tests.cc rename to contrib/jsonnet/jsonnet_tests.cc index 56c168b..c317674 100644 --- a/contrib/jsonnet/tests/jsonnet_tests.cc +++ b/contrib/jsonnet/jsonnet_tests.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "jsonnet_tests.h" // NOLINT(build/include) +#include "jsonnet_test_helper.h" // NOLINT(build/include) namespace { diff --git a/contrib/jsonnet/tests/CMakeLists.txt b/contrib/jsonnet/tests/CMakeLists.txt deleted file mode 100644 index 9925b61..0000000 --- a/contrib/jsonnet/tests/CMakeLists.txt +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright 2020 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 -# -# 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. - -# We need to prepare convenient directories so the tests will be able to access them -file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/tests_input) -file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/tests_output) -file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/tests_expected_output) - -add_custom_target(test_preparation ALL - COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/* ${PROJECT_BINARY_DIR}/tests/tests_input - COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* ${PROJECT_BINARY_DIR}/tests/tests_expected_output -) - -add_executable(tests - jsonnet_test_helper.cc - jsonnet_test_helper.h - jsonnet_tests.cc -) - -target_include_directories(tests PUBLIC - ${PROJECT_SOURCE_DIR}/headers -) - -target_link_libraries(tests - jsonnet_sapi sapi::sapi - gtest gmock gtest_main -) - -gtest_discover_tests(tests) From d5eed956472844a4180314f75451fac4f9848a69 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Thu, 27 Jan 2022 14:29:02 +0100 Subject: [PATCH 4/4] Jsonnet: More test improvements - Integrate `JsonnetTestHelper` into `JsonnetTest` class, remov multiple inheritance - Do not copy strings where it can be avoided - Add missing includes to header files --- contrib/jsonnet/CMakeLists.txt | 12 +- contrib/jsonnet/README.md | 6 +- contrib/jsonnet/jsonnet_base_sandbox.h | 8 +- contrib/jsonnet/jsonnet_base_transaction.h | 3 + contrib/jsonnet/jsonnet_test_helper.cc | 163 ------------------ contrib/jsonnet/jsonnet_test_helper.h | 57 ------- contrib/jsonnet/jsonnet_tests.cc | 182 ++++++++++++++++++++- 7 files changed, 191 insertions(+), 240 deletions(-) delete mode 100644 contrib/jsonnet/jsonnet_test_helper.cc delete mode 100644 contrib/jsonnet/jsonnet_test_helper.h diff --git a/contrib/jsonnet/CMakeLists.txt b/contrib/jsonnet/CMakeLists.txt index 6b009cb..a27e792 100644 --- a/contrib/jsonnet/CMakeLists.txt +++ b/contrib/jsonnet/CMakeLists.txt @@ -65,20 +65,18 @@ if(SAPI_ENABLE_TESTS) enable_testing() # Create directories so the tests will be able to access them - file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/tests/tests_input" - "${PROJECT_BINARY_DIR}/tests/tests_output" - "${PROJECT_BINARY_DIR}/tests/tests_expected_output") + file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/tests_input" + "${PROJECT_BINARY_DIR}/tests_output" + "${PROJECT_BINARY_DIR}/tests_expected_output") add_custom_target(test_preparation ALL COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/* - ${PROJECT_BINARY_DIR}/tests/tests_input + ${PROJECT_BINARY_DIR}/tests_input COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* - ${PROJECT_BINARY_DIR}/tests/tests_expected_output + ${PROJECT_BINARY_DIR}/tests_expected_output ) add_executable(jsonnet_tests - jsonnet_test_helper.cc - jsonnet_test_helper.h jsonnet_tests.cc ) target_include_directories(jsonnet_tests PUBLIC diff --git a/contrib/jsonnet/README.md b/contrib/jsonnet/README.md index fc30b41..c2a91bc 100644 --- a/contrib/jsonnet/README.md +++ b/contrib/jsonnet/README.md @@ -69,9 +69,9 @@ directory, in a file called `formatter_example.jsonnet`. ## Testing A few tests prepared with a use of -[Google Test](https://github.com/google/googletest) framework can be found in -the `tests/` directory. To run them type: +[Google Test](https://github.com/google/googletest) framework are included. To +run them type: ``` -ctest ./tests +ctest -R JsonnetTest. ``` diff --git a/contrib/jsonnet/jsonnet_base_sandbox.h b/contrib/jsonnet/jsonnet_base_sandbox.h index caff406..d2e7961 100644 --- a/contrib/jsonnet/jsonnet_base_sandbox.h +++ b/contrib/jsonnet/jsonnet_base_sandbox.h @@ -18,15 +18,19 @@ #include #include +#include +#include +#include + #include "jsonnet_sapi.sapi.h" // NOLINT(build/include) -#include "sandboxed_api/util/flag.h" #include "sandboxed_api/transaction.h" +#include "sandboxed_api/util/flag.h" #include "sandboxed_api/vars.h" class JsonnetBaseSandbox : public JsonnetSandbox { public: explicit JsonnetBaseSandbox(std::string in_file, std::string out_file) - : in_file_(in_file), out_file_(out_file) {} + : in_file_(std::move(in_file)), out_file_(std::move(out_file)) {} std::unique_ptr ModifyPolicy( sandbox2::PolicyBuilder *) override { diff --git a/contrib/jsonnet/jsonnet_base_transaction.h b/contrib/jsonnet/jsonnet_base_transaction.h index f45685d..bd8a572 100644 --- a/contrib/jsonnet/jsonnet_base_transaction.h +++ b/contrib/jsonnet/jsonnet_base_transaction.h @@ -15,6 +15,9 @@ #ifndef CONTRIB_JSNONNET_BASE_TRANSACTION_H_ #define CONTRIB_JSNONNET_BASE_TRANSACTION_H_ +#include +#include + #include "jsonnet_base_sandbox.h" // NOLINT(build/include) class JsonnetTransaction : public sapi::Transaction { diff --git a/contrib/jsonnet/jsonnet_test_helper.cc b/contrib/jsonnet/jsonnet_test_helper.cc deleted file mode 100644 index 894ea99..0000000 --- a/contrib/jsonnet/jsonnet_test_helper.cc +++ /dev/null @@ -1,163 +0,0 @@ -// Copyright 2020 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 -// -// 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. - -#include "jsonnet_test_helper.h" // NOLINT(build/include) - -// Prepares what is needed to perform a test. -void JsonnetTestHelper::TestSetUp() { - // Get paths to where input and output is stored. - char buffer[256]; - int error = readlink("/proc/self/exe", buffer, 256); - ASSERT_GE(error, 0); - - std::pair parts_of_path = - sapi::file::SplitPath(buffer); - absl::string_view binary_path = parts_of_path.first; - - std::string input_path = - sapi::file::JoinPath(binary_path, "tests_input", "dummy_input"); - std::string output_path = - sapi::file::JoinPath(binary_path, "tests_output", "dummy_input"); - - // Set up sandbox and api. - sandbox_ = absl::make_unique(input_path, output_path); - ASSERT_THAT(sandbox_->Init(), sapi::IsOk()); - api_ = absl::make_unique(sandbox_.get()); - - // Initialize library's main structure. - SAPI_ASSERT_OK_AND_ASSIGN(JsonnetVm * vm_ptr, api_->c_jsonnet_make()); - vm_ = absl::make_unique(vm_ptr); - - jsonnet_vm_was_used_ = false; - input_was_read_ = false; -} - -// Cleans up after a test. -void JsonnetTestHelper::TestTearDown() { - if (jsonnet_vm_was_used_) { - SAPI_ASSERT_OK_AND_ASSIGN( - char* result, api_->c_jsonnet_realloc(vm_.get(), output_.get(), 0)); - } - ASSERT_THAT(api_->c_jsonnet_destroy(vm_.get()), sapi::IsOk()); - if (input_was_read_) { - ASSERT_THAT(api_->c_free_input(input_.get()), sapi::IsOk()); - } -} - -// Reads input from file. -void JsonnetTestHelper::ReadInput(const char* filename) { - std::string in_file_in_sandboxee(std::string("/input/") + - basename(const_cast(&filename[0]))); - input_filename_in_sandboxee_ = std::move(in_file_in_sandboxee); - sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str()); - - SAPI_ASSERT_OK_AND_ASSIGN(char* input_ptr, - api_->c_read_input(0, in_file_var.PtrBefore())); - input_ = absl::make_unique(input_ptr); - - input_was_read_ = true; -} - -// Evaluates jsonnet code. -void JsonnetTestHelper::EvaluateJsonnetCode(Evaluation type, - bool expected_correct) { - sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str()); - sapi::v::Int error; - char* output_ptr; - - switch (type) { - case kBase: { - SAPI_ASSERT_OK_AND_ASSIGN( - output_ptr, - api_->c_jsonnet_evaluate_snippet(vm_.get(), in_file_var.PtrBefore(), - input_.get(), error.PtrAfter())); - break; - } - - case kMultipleFiles: { - SAPI_ASSERT_OK_AND_ASSIGN( - output_ptr, api_->c_jsonnet_evaluate_snippet_multi( - vm_.get(), in_file_var.PtrBefore(), input_.get(), - error.PtrAfter())); - break; - } - - case kYamlStream: { - SAPI_ASSERT_OK_AND_ASSIGN( - output_ptr, api_->c_jsonnet_evaluate_snippet_stream( - vm_.get(), in_file_var.PtrBefore(), input_.get(), - error.PtrAfter())); - break; - } - } - - if (expected_correct) { - ASSERT_THAT(error.GetValue(), testing::Eq(0)); - } else { - ASSERT_THAT(error.GetValue(), testing::Eq(1)); - } - - output_ = absl::make_unique(output_ptr); - - jsonnet_vm_was_used_ = true; -} - -// Writes output to file. -void JsonnetTestHelper::WriteOutput(const char* filename_or_directory, - Evaluation type) { - bool success; - - switch (type) { - case kBase: { - std::string out_file_in_sandboxee( - std::string("/output/") + - basename(const_cast(&filename_or_directory[0]))); - sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); - - SAPI_ASSERT_OK_AND_ASSIGN( - success, - api_->c_write_output_file(output_.get(), out_file_var.PtrBefore())); - break; - } - case kMultipleFiles: { - std::string out_file_in_sandboxee(std::string("/output/")); - sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); - SAPI_ASSERT_OK_AND_ASSIGN( - success, api_->c_write_multi_output_files( - output_.get(), out_file_var.PtrBefore(), false)); - break; - } - - case kYamlStream: { - std::string out_file_in_sandboxee( - std::string("/output/") + - basename(const_cast(&filename_or_directory[0]))); - sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); - SAPI_ASSERT_OK_AND_ASSIGN( - success, - api_->c_write_output_stream(output_.get(), out_file_var.PtrBefore())); - break; - } - } - - ASSERT_THAT(success, testing::Eq(true)); -} - -// Reads the output written to a file by library function / expected output -std::string JsonnetTestHelper::ReadOutput(const char* filename) { - std::ifstream input_stream(filename); - std::string contents((std::istreambuf_iterator(input_stream)), - std::istreambuf_iterator()); - return contents; -} diff --git a/contrib/jsonnet/jsonnet_test_helper.h b/contrib/jsonnet/jsonnet_test_helper.h deleted file mode 100644 index 06da104..0000000 --- a/contrib/jsonnet/jsonnet_test_helper.h +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2020 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 -// -// 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. - -#ifndef CONTRIB_JSONNET_TESTS_H_ -#define CONTRIB_JSONNET_TESTS_H_ - -#include - -#include -#include -#include -#include -#include -#include - -#include "jsonnet_base_sandbox.h" // NOLINT(build/include) -#include "jsonnet_sapi.sapi.h" // NOLINT(build/include) -#include "gtest/gtest.h" -#include "sandboxed_api/util/flag.h" -#include "sandboxed_api/util/path.h" -#include "sandboxed_api/util/status_matchers.h" - -class JsonnetTestHelper { - protected: - enum Evaluation { kBase, kMultipleFiles, kYamlStream }; - - void TestSetUp(); - void TestTearDown(); - - void ReadInput(const char* filename); - void EvaluateJsonnetCode(Evaluation type, bool expected_correct); - void WriteOutput(const char* filename_or_directory, Evaluation type); - std::string ReadOutput(const char* filename); - - std::unique_ptr sandbox_; - std::unique_ptr api_; - std::unique_ptr input_; - std::unique_ptr output_; - std::unique_ptr vm_; - - std::string input_filename_in_sandboxee_; - bool jsonnet_vm_was_used_; - bool input_was_read_; -}; - -#endif // CONTRIB_JSONNET_TESTS_H_ diff --git a/contrib/jsonnet/jsonnet_tests.cc b/contrib/jsonnet/jsonnet_tests.cc index c317674..e26ff64 100644 --- a/contrib/jsonnet/jsonnet_tests.cc +++ b/contrib/jsonnet/jsonnet_tests.cc @@ -12,20 +12,186 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "jsonnet_test_helper.h" // NOLINT(build/include) +#include + +#include +#include +#include +#include +#include + +#include "gtest/gtest.h" +#include "jsonnet_base_sandbox.h" // NOLINT(build/include) +#include "jsonnet_sapi.sapi.h" // NOLINT(build/include) +#include "sandboxed_api/util/flag.h" +#include "sandboxed_api/util/path.h" +#include "sandboxed_api/util/status_matchers.h" namespace { -class JsonnetTest : public JsonnetTestHelper, public testing::Test { +class JsonnetTest : public ::testing::Test { protected: - void SetUp() override { TestSetUp(); } - void TearDown() override { TestTearDown(); } + enum Evaluation { kBase, kMultipleFiles, kYamlStream }; + + void SetUp() override { + // Get paths to where input and output is stored. + char buffer[256]; + int error = readlink("/proc/self/exe", buffer, 256); + ASSERT_GE(error, 0); + + std::pair parts_of_path = + sapi::file::SplitPath(buffer); + absl::string_view binary_path = parts_of_path.first; + + std::string input_path = + sapi::file::JoinPath(binary_path, "tests_input", "dummy_input"); + std::string output_path = + sapi::file::JoinPath(binary_path, "tests_output", "dummy_input"); + + // Set up sandbox and api. + sandbox_ = absl::make_unique(input_path, output_path); + ASSERT_THAT(sandbox_->Init(), sapi::IsOk()); + api_ = absl::make_unique(sandbox_.get()); + + // Initialize library's main structure. + SAPI_ASSERT_OK_AND_ASSIGN(JsonnetVm* vm_ptr, api_->c_jsonnet_make()); + vm_ = absl::make_unique(vm_ptr); + } + + void TearDown() override { + if (jsonnet_vm_was_used_) { + SAPI_ASSERT_OK_AND_ASSIGN( + char* result, api_->c_jsonnet_realloc(vm_.get(), output_.get(), 0)); + } + ASSERT_THAT(api_->c_jsonnet_destroy(vm_.get()), sapi::IsOk()); + if (input_was_read_) { + ASSERT_THAT(api_->c_free_input(input_.get()), sapi::IsOk()); + } + } + + // Reads input from file. + void ReadInput(const char* filename); + + // Evaluates jsonnet code. + void EvaluateJsonnetCode(Evaluation type, bool expected_correct); + + // Writes output to file. + void WriteOutput(const char* filename_or_directory, Evaluation type); + + // Reads the output written to a file by library function / expected output. + std::string ReadOutput(const char* filename); + + std::unique_ptr sandbox_; + std::unique_ptr api_; + std::unique_ptr input_; + std::unique_ptr output_; + std::unique_ptr vm_; + + std::string input_filename_in_sandboxee_; + bool jsonnet_vm_was_used_ = false; + bool input_was_read_ = false; }; -// Basic test -TEST_F(JsonnetTest, SetUp_TearDown) { - ASSERT_FALSE(jsonnet_vm_was_used_); - ASSERT_FALSE(input_was_read_); +void JsonnetTest::ReadInput(const char* filename) { + std::string in_file_in_sandboxee(std::string("/input/") + + basename(const_cast(&filename[0]))); + input_filename_in_sandboxee_ = std::move(in_file_in_sandboxee); + sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str()); + + SAPI_ASSERT_OK_AND_ASSIGN(char* input_ptr, + api_->c_read_input(0, in_file_var.PtrBefore())); + input_ = absl::make_unique(input_ptr); + + input_was_read_ = true; +} + +void JsonnetTest::EvaluateJsonnetCode(Evaluation type, bool expected_correct) { + sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str()); + sapi::v::Int error; + char* output_ptr; + + switch (type) { + case kBase: { + SAPI_ASSERT_OK_AND_ASSIGN( + output_ptr, + api_->c_jsonnet_evaluate_snippet(vm_.get(), in_file_var.PtrBefore(), + input_.get(), error.PtrAfter())); + break; + } + + case kMultipleFiles: { + SAPI_ASSERT_OK_AND_ASSIGN( + output_ptr, api_->c_jsonnet_evaluate_snippet_multi( + vm_.get(), in_file_var.PtrBefore(), input_.get(), + error.PtrAfter())); + break; + } + + case kYamlStream: { + SAPI_ASSERT_OK_AND_ASSIGN( + output_ptr, api_->c_jsonnet_evaluate_snippet_stream( + vm_.get(), in_file_var.PtrBefore(), input_.get(), + error.PtrAfter())); + break; + } + } + + if (expected_correct) { + ASSERT_THAT(error.GetValue(), testing::Eq(0)); + } else { + ASSERT_THAT(error.GetValue(), testing::Eq(1)); + } + + output_ = absl::make_unique(output_ptr); + + jsonnet_vm_was_used_ = true; +} + +void JsonnetTest::WriteOutput(const char* filename_or_directory, + Evaluation type) { + bool success; + + switch (type) { + case kBase: { + std::string out_file_in_sandboxee( + std::string("/output/") + + basename(const_cast(&filename_or_directory[0]))); + sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); + + SAPI_ASSERT_OK_AND_ASSIGN( + success, + api_->c_write_output_file(output_.get(), out_file_var.PtrBefore())); + break; + } + case kMultipleFiles: { + std::string out_file_in_sandboxee(std::string("/output/")); + sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); + SAPI_ASSERT_OK_AND_ASSIGN( + success, api_->c_write_multi_output_files( + output_.get(), out_file_var.PtrBefore(), false)); + break; + } + + case kYamlStream: { + std::string out_file_in_sandboxee( + std::string("/output/") + + basename(const_cast(&filename_or_directory[0]))); + sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); + SAPI_ASSERT_OK_AND_ASSIGN( + success, + api_->c_write_output_stream(output_.get(), out_file_var.PtrBefore())); + break; + } + } + + ASSERT_THAT(success, testing::Eq(true)); +} + +std::string JsonnetTest::ReadOutput(const char* filename) { + std::ifstream input_stream(filename); + std::string contents((std::istreambuf_iterator(input_stream)), + std::istreambuf_iterator()); + return contents; } // One file evaluation to one file