updated README

This commit is contained in:
Katarzyna Miernikiewicz 2020-09-18 09:58:52 +00:00
parent 9fec584455
commit 3b1def654d
9 changed files with 89 additions and 17 deletions

View File

@ -38,6 +38,14 @@ cd examples
absolute/path/to/the/output_directory absolute/path/to/the/output_directory
``` ```
All three tools support evaluating one input file (possibly relying on multiple other files, e.x. by jsonnet `import` command; the files must be held in the same directory as input file) into one or more output files. Example jsonnet codes to evaluate in a one-in-one-out manner can be found [here](https://github.com/google/jsonnet/tree/master/examples). Example code producing multiple output files or YAML stream files can be found in the `examples/jsonnet_codes` directory, in files called `multiple_files_example.jsonnet` and `yaml_stream_example.jsonnet`, respectively. All three tools support evaluating one input file (possibly relying on multiple other files, e.x. by jsonnet `import` command; the files must be held in the same directory as input file) into one or more output files. Example jsonnet codes to evaluate in a one-in-one-out manner can be found [here](https://github.com/google/jsonnet/tree/master/examples). Example code producing multiple output files or YAML stream files can be found in the `examples/jsonnet_codes` directory (along with some other examples copied with minimal changes from the library files), in files called `multiple_files_example.jsonnet` and `yaml_stream_example.jsonnet`, respectively. In the `examples/jsonnet_codes_expected_output` directory one can found outputs the mentioned above files' evaluation should produce.
The formatter reads one input file and produces one output file as a result. Example code for this tool can also be found in `examples/jsonnet_codes` directory, in a file called `formatter_example.jsonnet`. The formatter reads one input file and produces one output file as a result. Example code for this tool can also be found in `examples/jsonnet_codes` 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:
```
cd tests
./tests
```

View File

@ -24,6 +24,7 @@ add_custom_command(
include_directories( include_directories(
${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/headers
${PROJECT_BINARY_DIR}/gen_files ${PROJECT_BINARY_DIR}/gen_files
) )

View File

@ -0,0 +1,30 @@
/*
Copyright 2015 Google Inc. 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.
*/
local martinis = import 'martinis.libsonnet';
{
'Vodka Martini': martinis['Vodka Martini'],
Manhattan: {
ingredients: [
{ kind: 'Rye', qty: 2.5 },
{ kind: 'Sweet Red Vermouth', qty: 1 },
{ kind: 'Angostura', qty: 'dash' },
],
garnish: importstr 'garnish.txt',
served: 'Straight Up',
},
}

View File

@ -32,7 +32,7 @@ class JsonnetTestHelper {
void TestTearDown(); void TestTearDown();
void Read_input(char* filename); void Read_input(char* filename);
void Evaluate_jsonnet_code(char* filename, Evaluation type); void Evaluate_jsonnet_code(char* filename, Evaluation type, bool expected_correct);
void Write_output(char* filename_or_directory, Evaluation type); void Write_output(char* filename_or_directory, Evaluation type);
std::string Read_output(char* filename); std::string Read_output(char* filename);

View File

@ -21,21 +21,15 @@ file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/tests_expected_output)
add_custom_target(test_preparation ALL 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/* ${PROJECT_BINARY_DIR}/tests/tests_input
#COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/yaml_stream_example.jsonnet ${PROJECT_BINARY_DIR}/tests/tests_input
#COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/arith.jsonnet ${PROJECT_BINARY_DIR}/tests/tests_input
#COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/negroni.jsonnet ${PROJECT_BINARY_DIR}/tests/tests_input
#COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/utils.libsonnet ${PROJECT_BINARY_DIR}/tests/tests_input
COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* ${PROJECT_BINARY_DIR}/tests/tests_expected_output COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* ${PROJECT_BINARY_DIR}/tests/tests_expected_output
#COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/arith.jsonnet.golden ${PROJECT_BINARY_DIR}/tests/tests_expected_output
#COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/negroni.jsonnet.golden ${PROJECT_BINARY_DIR}/tests/tests_expected_output
) )
include_directories( include_directories(
${PROJECT_SOURCE_DIR}/examples ${PROJECT_SOURCE_DIR}/headers
) )
add_executable(tests add_executable(tests
jsonnet_tests.h ${PROJECT_SOURCE_DIR}/headers/jsonnet_tests.h
jsonnet_tests.cc jsonnet_tests.cc
jsonnet_tests_utils.cc jsonnet_tests_utils.cc
) )

View File

@ -20,11 +20,13 @@ class JsonnetTest : public JsonnetTestHelper, public testing::Test {
void TearDown() override { TestTearDown(); } void TearDown() override { TestTearDown(); }
}; };
// Basic test
TEST_F(JsonnetTest, SetUp_TearDown) { TEST_F(JsonnetTest, SetUp_TearDown) {
ASSERT_FALSE(if_jsonnet_vm_was_used); ASSERT_FALSE(if_jsonnet_vm_was_used);
ASSERT_FALSE(if_input_was_read); ASSERT_FALSE(if_input_was_read);
} }
// One file evaluation to one file
TEST_F(JsonnetTest, One_file_no_dependencies) { TEST_F(JsonnetTest, One_file_no_dependencies) {
char input_file[] = "arith.jsonnet"; char input_file[] = "arith.jsonnet";
char output_file[] = "arith_output"; char output_file[] = "arith_output";
@ -32,7 +34,7 @@ TEST_F(JsonnetTest, One_file_no_dependencies) {
char output_to_expect[] = "tests_expected_output/arith.golden"; char output_to_expect[] = "tests_expected_output/arith.golden";
Read_input(input_file); Read_input(input_file);
Evaluate_jsonnet_code(input_file, BASE); Evaluate_jsonnet_code(input_file, BASE, true);
Write_output(output_file, BASE); Write_output(output_file, BASE);
std::string produced_output = Read_output(output_to_read); std::string produced_output = Read_output(output_to_read);
@ -41,6 +43,7 @@ TEST_F(JsonnetTest, One_file_no_dependencies) {
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str()); ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
} }
// One file evaluating to one file, dependent on some other files
TEST_F(JsonnetTest, One_file_some_dependencies) { TEST_F(JsonnetTest, One_file_some_dependencies) {
char input_file[] = "negroni.jsonnet"; char input_file[] = "negroni.jsonnet";
char output_file[] = "negroni_output"; char output_file[] = "negroni_output";
@ -48,7 +51,7 @@ TEST_F(JsonnetTest, One_file_some_dependencies) {
char output_to_expect[] = "tests_expected_output/negroni.golden"; char output_to_expect[] = "tests_expected_output/negroni.golden";
Read_input(input_file); Read_input(input_file);
Evaluate_jsonnet_code(input_file, BASE); Evaluate_jsonnet_code(input_file, BASE, true);
Write_output(output_file, BASE); Write_output(output_file, BASE);
std::string produced_output = Read_output(output_to_read); std::string produced_output = Read_output(output_to_read);
@ -57,6 +60,7 @@ TEST_F(JsonnetTest, One_file_some_dependencies) {
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str()); ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
} }
// One file evaluating to two files
TEST_F(JsonnetTest, Multiple_files) { TEST_F(JsonnetTest, Multiple_files) {
char input_file[] = "multiple_files_example.jsonnet"; char input_file[] = "multiple_files_example.jsonnet";
char output_file[] = ""; char output_file[] = "";
@ -66,7 +70,7 @@ TEST_F(JsonnetTest, Multiple_files) {
char output_to_expect_2[] = "tests_expected_output/second_file.json"; char output_to_expect_2[] = "tests_expected_output/second_file.json";
Read_input(input_file); Read_input(input_file);
Evaluate_jsonnet_code(input_file, MULTIPLE_FILES); Evaluate_jsonnet_code(input_file, MULTIPLE_FILES, true);
Write_output(output_file, MULTIPLE_FILES); Write_output(output_file, MULTIPLE_FILES);
std::string produced_output_1 = Read_output(output_to_read_1); std::string produced_output_1 = Read_output(output_to_read_1);
@ -77,3 +81,28 @@ TEST_F(JsonnetTest, Multiple_files) {
ASSERT_STREQ(produced_output_1.c_str(), expected_output_1.c_str()); ASSERT_STREQ(produced_output_1.c_str(), expected_output_1.c_str());
ASSERT_STREQ(produced_output_2.c_str(), expected_output_2.c_str()); ASSERT_STREQ(produced_output_2.c_str(), expected_output_2.c_str());
} }
// One file evaluating to yaml stream format
TEST_F(JsonnetTest, Yaml_stream) {
char input_file[] = "yaml_stream_example.jsonnet";
char output_file[] = "yaml_stream_example.yaml";
char output_to_read[] = "tests_output/yaml_stream_example.yaml";
char output_to_expect[] = "tests_expected_output/yaml_stream_example.yaml";
Read_input(input_file);
Evaluate_jsonnet_code(input_file, YAML_STREAM, true);
Write_output(output_file, YAML_STREAM);
std::string produced_output = Read_output(output_to_read);
std::string expected_output = Read_output(output_to_expect);
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
}
// One file depended on some other files not accessible by the sandbox
TEST_F(JsonnetTest, Bad_evaluation) {
char input_file[] = "imports.jsonnet";
Read_input(input_file);
Evaluate_jsonnet_code(input_file, BASE, false);
}

View File

@ -65,11 +65,13 @@ void JsonnetTestHelper::Read_input(char* filename) {
api->c_read_input(0, in_file_var.PtrBefore())); api->c_read_input(0, in_file_var.PtrBefore()));
input = std::make_unique<sapi::v::RemotePtr>(input_ptr); input = std::make_unique<sapi::v::RemotePtr>(input_ptr);
if_input_was_read = true;
return; return;
} }
// Evaluate jsonnet code. // Evaluate jsonnet code.
void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type) { void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type, bool expected_correct) {
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee.c_str()); sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee.c_str());
sapi::v::Int error; sapi::v::Int error;
char* output_ptr; char* output_ptr;
@ -100,9 +102,16 @@ void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type) {
} }
} }
if (expected_correct) {
ASSERT_THAT(error.GetValue(), testing::Eq(0)); ASSERT_THAT(error.GetValue(), testing::Eq(0));
} else {
ASSERT_THAT(error.GetValue(), testing::Eq(1));
}
output = std::make_unique<sapi::v::RemotePtr>(output_ptr); output = std::make_unique<sapi::v::RemotePtr>(output_ptr);
if_jsonnet_vm_was_used = true;
return; return;
} }
@ -147,6 +156,7 @@ void JsonnetTestHelper::Write_output(char* filename_or_directory,
return; return;
} }
// Reading the output written to a file by library function / expected output
std::string JsonnetTestHelper::Read_output(char* filename) { std::string JsonnetTestHelper::Read_output(char* filename) {
std::ifstream input_stream(filename); std::ifstream input_stream(filename);
std::string contents((std::istreambuf_iterator<char>(input_stream)), std::istreambuf_iterator<char>()); std::string contents((std::istreambuf_iterator<char>(input_stream)), std::istreambuf_iterator<char>());