base for tests ready

This commit is contained in:
Katarzyna Miernikiewicz 2020-09-15 16:16:05 +00:00
parent c9dcda68e9
commit 4c20174f83
9 changed files with 166 additions and 39 deletions

View File

@ -61,7 +61,7 @@ int main(int argc, char* argv[]) {
<< "Jsonnet code evaluation failed: " << output.status() << " "
<< error.GetValue() << "\n"
<< "Make sure all files used by your jsonnet file are in the same "
"directory as your file";
"directory as your file.";
// Write data to file.
std::string out_file_in_sandboxee(std::string("/output/") +

View File

@ -97,20 +97,24 @@ int main(int argc, char* argv[]) {
<< "Jsonnet code evaluation failed: " << output.status() << " "
<< error.GetValue() << "\n"
<< "Make sure all files used by your jsonnet file are in the same "
"directory as your file";
"directory as your file.";
// Write data to file.
std::string out_file_in_sandboxee(std::string("/output/"));
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
sapi::v::RemotePtr output_pointer(output.value());
sapi::StatusOr<bool> success = api.c_write_multi_output_files(
&vm_pointer, &output_pointer, out_file_var.PtrBefore());
&output_pointer, out_file_var.PtrBefore());
CHECK(success.ok() && success.value())
<< "Writing to output file failed " << success.status() << " "
<< success.value();
// Clean up.
sapi::StatusOr<char*> result =
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0);
CHECK(result.ok()) << "JsonnetVm realloc failed: " << result.status();
status = api.c_jsonnet_destroy(&vm_pointer);
CHECK(status.ok()) << "JsonnetVm destroy failed: " << status;

View File

@ -61,7 +61,7 @@ int main(int argc, char* argv[]) {
<< "Jsonnet code evaluation failed: " << output.status() << " "
<< error.GetValue() << "\n"
<< "Make sure all files used by your jsonnet file are in the same "
"directory as your file";
"directory as your file.";
// Write data to file.
std::string out_file_in_sandboxee(std::string("/output/") +
@ -70,13 +70,17 @@ int main(int argc, char* argv[]) {
sapi::v::RemotePtr output_pointer(output.value());
sapi::StatusOr<bool> success;
success = api.c_write_output_stream(&vm_pointer, &output_pointer,
success = api.c_write_output_stream(&output_pointer,
out_file_var.PtrBefore());
CHECK(success.ok() && success.value())
<< "Writing to output file failed " << success.status() << " "
<< success.value();
// Clean up.
sapi::StatusOr<char*> result =
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0);
CHECK(result.ok()) << "JsonnetVm realloc failed: " << result.status();
status = api.c_jsonnet_destroy(&vm_pointer);
CHECK(status.ok()) << "JsonnetVm destroy failed: " << status;

View File

@ -1,6 +1,6 @@
--- jsonnet.cpp 2020-09-09 12:15:33.687539042 +0000
+++ write_helper.cpp 2020-09-10 11:21:44.273825853 +0000
@@ -14,559 +14,126 @@
+++ write_helper.cpp 2020-09-15 14:27:01.907774290 +0000
@@ -14,559 +14,123 @@
limitations under the License.
*/
@ -102,8 +102,7 @@
- evalMulti(false),
- evalStream(false)
+/** Writes output files for multiple file output */
+bool write_multi_output_files(JsonnetVm *vm, char *output,
+ const std::string &output_dir) {
+bool write_multi_output_files(char *output, const std::string &output_dir) {
+ // If multiple file output is used, then iterate over each string from
+ // the sequence of strings returned by jsonnet_evaluate_snippet_multi,
+ // construct pairs of filename and content, and write each output file.
@ -119,7 +118,6 @@
+ c = c2;
+ r[filename] = json;
+ }
+ jsonnet_realloc(vm, output, 0);
+
+ std::ostream *o;
+ std::ofstream f;
@ -496,8 +494,7 @@
- return false;
- }
- o = &f;
+bool write_output_stream(JsonnetVm *vm, char *output,
+ const std::string &output_file) {
+bool write_output_stream(char *output, const std::string &output_file) {
+ std::ostream *o;
+ std::ofstream f;
+
@ -523,7 +520,7 @@
+ ++c;
+ r.emplace_back(json);
+ }
+ jsonnet_realloc(vm, output, 0);
+
+ for (const auto &str : r) {
+ (*o) << "---\n";
+ (*o) << str;

View File

@ -58,14 +58,14 @@ bool c_write_output_file(const char* output, const char* output_file) {
return write_output_file(output, s_output_file);
}
bool c_write_multi_output_files(struct JsonnetVm* vm, char* output, char* output_dir) {
bool c_write_multi_output_files(char* output, char* output_dir) {
std::string s_output_dir(output_dir);
return write_multi_output_files(vm, output, s_output_dir);
return write_multi_output_files(output, s_output_dir);
}
bool c_write_output_stream(struct JsonnetVm* vm, char* output, char* output_file) {
bool c_write_output_stream(char* output, char* output_file) {
std::string s_output_file(output_file);
return write_output_stream(vm, output, s_output_file);
return write_output_stream(output, s_output_file);
}
char* c_jsonnet_realloc(struct JsonnetVm* vm, char* str, size_t sz) {

View File

@ -46,16 +46,20 @@ extern "C" bool c_write_output_file(const char* output,
extern "C" char* c_jsonnet_realloc(struct JsonnetVm* vm, char* str, size_t sz);
extern "C" bool c_write_multi_output_files(struct JsonnetVm* vm, char* output,
extern "C" bool c_write_multi_output_files(//struct JsonnetVm* vm,
char* output,
char* output_dir);
bool write_multi_output_files(struct JsonnetVm* vm, char* output,
bool write_multi_output_files(//struct JsonnetVm* vm,
char* output,
const std::string& output_dir);
extern "C" bool c_write_output_stream(struct JsonnetVm* vm, char* output,
extern "C" bool c_write_output_stream(//struct JsonnetVm* vm,
char* output,
char* output_file);
bool write_output_stream(struct JsonnetVm* vm, char* output,
bool write_output_stream(//struct JsonnetVm* vm,
char* output,
const std::string& output_file);
extern "C" char* c_jsonnet_fmt_snippet(struct JsonnetVm* vm,

View File

@ -10,4 +10,123 @@
// 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.
// limitations under the License.
#include "jsonnet_tests.h"
// Prepare what is needed to perform a test.
void JsonnetTestHelper::TestSetUp() {
// Set up sandbox and api.
sandbox = std::make_unique<JsonnetBaseSandbox>();
ASSERT_THAT(sandbox->Init(), sapi::IsOk());
api = std::make_unique<JsonnetApi>(sandbox.get());
// Initialize library's main structure.
SAPI_ASSERT_OK_AND_ASSIGN(JsonnetVm * vm_ptr, api->c_jsonnet_make());
vm = std::make_unique<sapi::v::RemotePtr>(vm_ptr);
return;
}
// Clean up after a test.
void JsonnetTestHelper::TestTearDown() {
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());
ASSERT_THAT(api->c_free_input(input.get()), sapi::IsOk());
return;
}
// Read input from file.
void JsonnetTestHelper::Read_input(char* filename) {
std::string in_file_in_sandboxee(std::string("/input/") +
basename(&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 = std::make_unique<sapi::v::RemotePtr>(input_ptr);
return;
}
// Evaluate jsonnet code.
void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type) {
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee.c_str());
sapi::v::Int error;
char* output_ptr;
switch (type) {
case BASE: {
SAPI_ASSERT_OK_AND_ASSIGN(
output_ptr,
api->c_jsonnet_evaluate_snippet(vm.get(), in_file_var.PtrBefore(),
input.get(), error.PtrAfter()));
break;
}
case MULTIPLE_FILES: {
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 YAML_STREAM: {
SAPI_ASSERT_OK_AND_ASSIGN(
output_ptr, api->c_jsonnet_evaluate_snippet_stream(
vm.get(), in_file_var.PtrBefore(), input.get(),
error.PtrAfter()));
break;
}
}
ASSERT_THAT(error.GetValue(), testing::Eq(0));
output = std::make_unique<sapi::v::RemotePtr>(output_ptr);
return;
}
// Write output to file.
void JsonnetTestHelper::Write_output(char* filename_or_directory,
Evaluation type) {
bool success;
switch (type) {
case BASE: {
std::string out_file_in_sandboxee(std::string("/output/") +
basename(&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 MULTIPLE_FILES: {
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()));
break;
}
case YAML_STREAM: {
std::string out_file_in_sandboxee(std::string("/output/") +
basename(&out_file[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));
return;
}

View File

@ -16,8 +16,6 @@
class JsonnetTest : public JsonnetTestHelper, public testing::Test {
protected:
void SetUp() override { JsonnetTestSetUp(); }
void TearDown() override { JsonnetTestTearDown(); }
void SetUp() override { TestSetUp(); }
void TearDown() override { TestTearDown(); }
};

View File

@ -12,27 +12,28 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gtest/gtest.h"
#include "jsonnet_base_sandbox.h"
#include "jsonnet_sapi.sapi.h"
#include "gtest/gtest.h"
#include "sandboxed_api/util/flag.h"
#include "sandboxed_api/util/status_matchers.h"
class JsonnetTestHelper {
protected:
enum Evaluation { BASE, MULTIPLE_FILES, YAML_STREAM };
protected:
enum Evaluation { BASE, MULTIPLE_FILES, YAML_STREAM };
void JsonnetTestSetUp();
void JsonnetTestTearDown();
void TestSetUp();
void TestTearDown();
char* Read_input(const char* filename);
char* Evaluate_jsonnet_code(struct JsonnetVm* vm, const char* filename, Evaluation type);
bool Write_output(struct JsonnetVm* vm, char* output, char* filename_or_directory, Evaluation type);
void Read_input(char* filename);
void Evaluate_jsonnet_code(char* filename, Evaluation type);
void Write_output(char* filename_or_directory, Evaluation type);
std::unique_ptr<JsonnetBaseSandbox> sandbox;
std::unique_ptr<JsonnetApi> api;
std::unique_ptr<sapi::v::RemotePtr> input;
JsonnetVm* vm;
std::unique_ptr<JsonnetBaseSandbox> sandbox;
std::unique_ptr<JsonnetApi> api;
std::unique_ptr<sapi::v::RemotePtr> input;
std::unique_ptr<sapi::v::RemotePtr> output;
std::unique_ptr<sapi::v::RemotePtr> vm;
std::string input_filename_in_sandboxee;
};