NOLINT added, changed using CHECKS to a function returning absl::Status

This commit is contained in:
Katarzyna Miernikiewicz 2020-09-25 16:03:06 +00:00
parent 2276f5d48e
commit ac7c1ee1ee
10 changed files with 193 additions and 187 deletions

View File

@ -15,9 +15,9 @@
#include <cstdlib>
#include <iostream>
#include "jsonnet_base_sandbox.h"
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
absl::Status JsonnetMain(std::string in_file, std::string out_file){
absl::Status JsonnetMain(std::string in_file, std::string out_file) {
// Initialize sandbox.
JsonnetBaseSandbox sandbox(in_file, out_file);
SAPI_RETURN_IF_ERROR(sandbox.Init())
@ -25,23 +25,24 @@ absl::Status JsonnetMain(std::string in_file, std::string out_file){
JsonnetApi api(&sandbox);
// Initialize library's main structure.
SAPI_ASSIGN_OR_RETURN(JsonnetVm* jsonnet_vm, api.c_jsonnet_make());
SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
sapi::v::RemotePtr vm_pointer(jsonnet_vm);
// Read input file.
std::string in_file_in_sandboxee(std::string("/input/") +
basename(&in_file[0]));
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
SAPI_ASSIGN_OR_RETURN(char* input, api.c_read_input(false, in_file_var.PtrBefore()));
SAPI_ASSIGN_OR_RETURN(char* input,
api.c_read_input(false, in_file_var.PtrBefore()));
// Process jsonnet data.
sapi::v::RemotePtr input_pointer(input);
sapi::v::Int error;
SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet(
&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter()));
&vm_pointer, in_file_var.PtrBefore(),
&input_pointer, error.PtrAfter()));
CHECK(!error.GetValue())
<< "Jsonnet code evaluation failed: "
<< error.GetValue() << "\n"
<< "Jsonnet code evaluation failed: " << error.GetValue() << "\n"
<< "Make sure all files used by your jsonnet file are in the same "
"directory as your file.";
@ -51,16 +52,18 @@ absl::Status JsonnetMain(std::string in_file, std::string out_file){
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
sapi::v::RemotePtr output_pointer(output);
SAPI_ASSIGN_OR_RETURN(bool success, api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
SAPI_ASSIGN_OR_RETURN(
bool success,
api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
CHECK(success) << "Writing to output file failed: " << success;
// Clean up.
SAPI_ASSIGN_OR_RETURN(char* result, api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
SAPI_ASSIGN_OR_RETURN(char* result,
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
return absl::OkStatus();
}
int main(int argc, char* argv[]) {
@ -81,7 +84,7 @@ int main(int argc, char* argv[]) {
if (!status.ok()) {
LOG(ERROR) << "Failed: " << status.ToString();
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "jsonnet_base_transaction.h"
#include "jsonnet_base_transaction.h" // NOLINT(build/include)
absl::Status JsonnetTransaction::Main() {
JsonnetApi api(sandbox());

View File

@ -28,7 +28,7 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
// We need only the input file here, not the whole input directory
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder *) override {
sandbox2::PolicyBuilder*) override {
return sandbox2::PolicyBuilder()
.AllowStaticStartup()
.AllowOpen()
@ -51,70 +51,73 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
std::string out_file_;
};
int main(int argc, char *argv[]) {
absl::Status JsonnetMain(std::string in_file, std::string out_file) {
// Initialize sandbox.
JsonnetSapiSandbox sandbox(in_file, out_file);
SAPI_RETURN_IF_ERROR(sandbox.Init())
JsonnetApi api(&sandbox);
// Initialize library's main structure.
SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
sapi::v::RemotePtr vm_pointer(jsonnet_vm);
// Read input file.
std::string in_file_in_sandboxee(std::string("/input/") +
basename(&in_file[0]));
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
SAPI_ASSIGN_OR_RETURN(char* input,
api.c_read_input(false, in_file_var.PtrBefore()));
// Process jsonnet data.
sapi::v::RemotePtr input_pointer(input);
sapi::v::Int error;
SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_fmt_snippet(
&vm_pointer, in_file_var.PtrBefore(),
&input_pointer, error.PtrAfter()));
CHECK(!error.GetValue()) << "Jsonnet code evaluation failed: "
<< error.GetValue() << "\n";
// Write data to file.
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::v::RemotePtr output_pointer(output);
SAPI_ASSIGN_OR_RETURN(
bool success,
api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
CHECK(success) << "Writing to output file failed: " << success;
// Clean up.
SAPI_ASSIGN_OR_RETURN(char* result,
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
return absl::OkStatus();
}
int main(int argc, char* argv[]) {
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (!(argc == 3)) {
std::cerr << "Usage:\n"
<< basename(argv[0]) << " absolute/path/to/INPUT.jsonnet"
<< " absolute/path/to/OUTPUT.jsonnet\n";
<< " absolute/path/to/OUTPUT\n";
return EXIT_FAILURE;
}
std::string in_file(argv[1]);
std::string out_file(argv[2]);
// Initialize sandbox.
JsonnetSapiSandbox sandbox(in_file, out_file);
absl::Status status = sandbox.Init();
CHECK(status.ok()) << "Sandbox initialization failed " << status;
JsonnetApi api(&sandbox);
// Initialize library's main structure.
sapi::StatusOr<JsonnetVm *> jsonnet_vm = api.c_jsonnet_make();
sapi::v::RemotePtr vm_pointer(jsonnet_vm.value());
CHECK(jsonnet_vm.ok()) << "JsonnetVm initialization failed: "
<< jsonnet_vm.status();
// Read input file.
sapi::v::ConstCStr in_file_var(in_file.c_str());
sapi::StatusOr<char *> input =
api.c_read_input(false, in_file_var.PtrBefore());
CHECK(input.ok()) << "Reading input file failed " << input.status();
// Process jsonnet data.
sapi::v::RemotePtr input_pointer(input.value());
sapi::v::Int error;
sapi::StatusOr<char *> output = api.c_jsonnet_fmt_snippet(
&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter());
CHECK(output.ok() && !error.GetValue())
<< "Jsonnet code evaluation failed: " << output.status() << " "
<< error.GetValue();
// Write data to file.
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::v::RemotePtr output_pointer(output.value());
sapi::StatusOr<bool> success;
success = api.c_write_output_file(&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;
status = api.c_free_input(&input_pointer);
CHECK(status.ok()) << "Input freeing failed: " << status;
absl::Status status = JsonnetMain(in_file, out_file);
if (!status.ok()) {
LOG(ERROR) << "Failed: " << status.ToString();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@ -30,7 +30,7 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
// We need a slightly different policy than the default one
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder *) override {
sandbox2::PolicyBuilder*) override {
return sandbox2::PolicyBuilder()
.AllowStaticStartup()
.AllowOpen()
@ -55,73 +55,74 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
std::string out_directory_;
};
int main(int argc, char *argv[]) {
absl::Status JsonnetMain(std::string in_file, std::string out_file) {
// Initialize sandbox.
JsonnetSapiSandbox sandbox(in_file, out_file);
SAPI_RETURN_IF_ERROR(sandbox.Init())
JsonnetApi api(&sandbox);
// Initialize library's main structure.
SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
sapi::v::RemotePtr vm_pointer(jsonnet_vm);
// Read input file.
std::string in_file_in_sandboxee(std::string("/input/") +
basename(&in_file[0]));
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
SAPI_ASSIGN_OR_RETURN(char* input,
api.c_read_input(false, in_file_var.PtrBefore()));
// Process jsonnet data.
sapi::v::RemotePtr input_pointer(input);
sapi::v::Int error;
SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet_multi(
&vm_pointer, in_file_var.PtrBefore(),
&input_pointer, error.PtrAfter()));
CHECK(!error.GetValue())
<< "Jsonnet code evaluation failed: " << error.GetValue() << "\n"
<< "Make sure all files used by your jsonnet file are in the same "
"directory as your file.";
// Write data to file.
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::v::RemotePtr output_pointer(output);
SAPI_ASSIGN_OR_RETURN(
bool success,
api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
CHECK(success) << "Writing to output file failed: " << success;
// Clean up.
SAPI_ASSIGN_OR_RETURN(char* result,
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
return absl::OkStatus();
}
int main(int argc, char* argv[]) {
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (!(argc == 3)) {
std::cerr << "Usage:\n"
<< basename(argv[0]) << " absolute/path/to/INPUT.jsonnet"
<< " absolute/path/to/OUTPUT_DIRECTORY\n";
<< " absolute/path/to/OUTPUT\n";
return EXIT_FAILURE;
}
std::string in_file(argv[1]);
std::string out_directory(argv[2]);
std::string out_file(argv[2]);
// Initialize sandbox.
JsonnetSapiSandbox sandbox(in_file, out_directory);
absl::Status status = sandbox.Init();
CHECK(status.ok()) << "Sandbox initialization failed " << status;
JsonnetApi api(&sandbox);
// Initialize library's main structure.
sapi::StatusOr<JsonnetVm *> jsonnet_vm = api.c_jsonnet_make();
sapi::v::RemotePtr vm_pointer(jsonnet_vm.value());
CHECK(jsonnet_vm.ok()) << "JsonnetVm initialization failed: "
<< jsonnet_vm.status();
// Read input file.
std::string in_file_in_sandboxee(std::string("/input/") +
basename(&in_file[0]));
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
sapi::StatusOr<char *> input =
api.c_read_input(false, in_file_var.PtrBefore());
CHECK(input.ok()) << "Reading input file failed " << input.status();
// Process jsonnet data.
sapi::v::RemotePtr input_pointer(input.value());
sapi::v::Int error;
sapi::StatusOr<char *> output = api.c_jsonnet_evaluate_snippet_multi(
&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter());
CHECK(output.ok() && !error.GetValue())
<< "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.";
// 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(
&output_pointer, out_file_var.PtrBefore(), true);
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;
status = api.c_free_input(&input_pointer);
CHECK(status.ok()) << "Input freeing failed: " << status;
absl::Status status = JsonnetMain(in_file, out_file);
if (!status.ok()) {
LOG(ERROR) << "Failed: " << status.ToString();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@ -15,9 +15,58 @@
#include <cstdlib>
#include <iostream>
#include "jsonnet_base_sandbox.h"
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
int main(int argc, char *argv[]) {
absl::Status JsonnetMain(std::string in_file, std::string out_file) {
// Initialize sandbox.
JsonnetBaseSandbox sandbox(in_file, out_file);
SAPI_RETURN_IF_ERROR(sandbox.Init())
JsonnetApi api(&sandbox);
// Initialize library's main structure.
SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
sapi::v::RemotePtr vm_pointer(jsonnet_vm);
// Read input file.
std::string in_file_in_sandboxee(std::string("/input/") +
basename(&in_file[0]));
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
SAPI_ASSIGN_OR_RETURN(char* input,
api.c_read_input(false, in_file_var.PtrBefore()));
// Process jsonnet data.
sapi::v::RemotePtr input_pointer(input);
sapi::v::Int error;
SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet_stream(
&vm_pointer, in_file_var.PtrBefore(),
&input_pointer, error.PtrAfter()));
CHECK(!error.GetValue())
<< "Jsonnet code evaluation failed: " << error.GetValue() << "\n"
<< "Make sure all files used by your jsonnet file are in the same "
"directory as your file.";
// Write data to file.
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::v::RemotePtr output_pointer(output);
SAPI_ASSIGN_OR_RETURN(
bool success,
api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
CHECK(success) << "Writing to output file failed: " << success;
// Clean up.
SAPI_ASSIGN_OR_RETURN(char* result,
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
return absl::OkStatus();
}
int main(int argc, char* argv[]) {
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
@ -31,61 +80,11 @@ int main(int argc, char *argv[]) {
std::string in_file(argv[1]);
std::string out_file(argv[2]);
// Initialize sandbox.
JsonnetBaseSandbox sandbox(in_file, out_file);
absl::Status status = sandbox.Init();
CHECK(status.ok()) << "Sandbox initialization failed " << status;
JsonnetApi api(&sandbox);
// Initialize library's main structure.
sapi::StatusOr<JsonnetVm *> jsonnet_vm = api.c_jsonnet_make();
sapi::v::RemotePtr vm_pointer(jsonnet_vm.value());
CHECK(jsonnet_vm.ok()) << "JsonnetVm initialization failed: "
<< jsonnet_vm.status();
// Read input file.
std::string in_file_in_sandboxee(std::string("/input/") +
basename(&in_file[0]));
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
sapi::StatusOr<char *> input =
api.c_read_input(false, in_file_var.PtrBefore());
CHECK(input.ok()) << "Reading input file failed " << input.status();
// Process jsonnet data.
sapi::v::RemotePtr input_pointer(input.value());
sapi::v::Int error;
sapi::StatusOr<char *> output = api.c_jsonnet_evaluate_snippet_stream(
&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter());
CHECK(output.ok() && !error.GetValue())
<< "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.";
// Write data to file.
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::v::RemotePtr output_pointer(output.value());
sapi::StatusOr<bool> success;
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;
status = api.c_free_input(&input_pointer);
CHECK(status.ok()) << "Input freeing failed: " << status;
absl::Status status = JsonnetMain(in_file, out_file);
if (!status.ok()) {
LOG(ERROR) << "Failed: " << status.ToString();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "jsonnet_base_sandbox.h"
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
class JsonnetTransaction : public sapi::Transaction {
public:

View File

@ -21,7 +21,7 @@
#include <string>
#include "gtest/gtest.h"
#include "jsonnet_base_sandbox.h"
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
#include "jsonnet_sapi.sapi.h"
#include "sandboxed_api/util/flag.h"
#include "sandboxed_api/util/status_matchers.h"

View File

@ -1,5 +1,5 @@
--- jsonnet.cpp 2020-09-09 12:15:33.687539042 +0000
+++ write_helper.cpp 2020-09-17 15:35:02.684743876 +0000
+++ write_helper.cpp 2020-09-25 15:38:37.317147682 +0000
@@ -14,559 +14,125 @@
limitations under the License.
*/
@ -20,7 +20,7 @@
#include <vector>
-#include "utils.h"
+#include "jsonnet_helper.h"
+#include "jsonnet_helper.h" // NOLINT(build/include)
-extern "C" {
-#include <libjsonnet.h>

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "jsonnet_helper.h"
#include "jsonnet_helper.h" // NOLINT(build/include)
#include <string.h>

View File

@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "jsonnet/cmd/utils.h"
#include "jsonnet/cmd/utils.h" // NOLINT(build/include)
extern "C" {
#include <libjsonnet.h>
#include <libjsonnet_fmt.h>
#include <libjsonnet.h> // NOLINT(build/include)
#include <libjsonnet_fmt.h> // NOLINT(build/include)
}
extern "C" struct JsonnetVm* c_jsonnet_make(void);