forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_data_io.cc
More file actions
176 lines (153 loc) · 5.62 KB
/
Copy pathtest_data_io.cc
File metadata and controls
176 lines (153 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "testutil/test_data_io.h"
#include <fcntl.h>
#include <fstream>
#include "google/rpc/code.pb.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/text_format.h"
#include "gtest/gtest.h"
#include "absl/flags/flag.h"
#include "absl/memory/memory.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "internal/status_util.h"
#include "re2/re2.h"
ABSL_FLAG(std::string, test_data_folder,
"com_google_cel_spec/testdata/",
"The location to read test data from.");
ABSL_FLAG(std::string, output_dir, "",
"The location to write test data to. Writes to standard out if not "
"specified.");
ABSL_FLAG(bool, binary, false, "If binary output should be used.");
namespace google {
namespace api {
namespace expr {
namespace testutil {
using testdata::TestData;
using testdata::TestValue;
namespace {
std::unique_ptr<google::protobuf::io::FileInputStream> OpenForRead(
const std::string& filename) {
int file_descriptor;
do {
file_descriptor = open(filename.c_str(), O_RDONLY);
} while (file_descriptor < 0 && errno == EINTR);
if (file_descriptor >= 0) {
auto result =
absl::make_unique<google::protobuf::io::FileInputStream>(file_descriptor);
result->SetCloseOnDelete(true);
return result;
} else {
return nullptr;
}
}
std::unique_ptr<google::protobuf::io::FileOutputStream> OpenForWrite(
const std::string& filename) {
int file_descriptor;
do {
file_descriptor = open(filename.c_str(), O_WRONLY | O_CREAT);
} while (file_descriptor < 0 && errno == EINTR);
if (file_descriptor >= 0) {
auto result =
absl::make_unique<google::protobuf::io::FileOutputStream>(file_descriptor);
result->SetCloseOnDelete(true);
return result;
} else {
std::cerr << "Could not open file: " << errno << std::endl;
return nullptr;
}
}
std::string GetTestCaseFileName(absl::string_view dir,
absl::string_view test_name, bool binary) {
return absl::StrCat(dir, test_name, binary ? kBinaryPbExt : kTextPbExt);
}
} // namespace
google::rpc::Status ReadPbFile(absl::string_view absolute_file_path,
google::protobuf::Message* message) {
message->Clear();
auto in_stream = OpenForRead(std::string(absolute_file_path));
if (in_stream == nullptr) {
return internal::NotFoundError(
absl::StrCat("File not found: ", absolute_file_path));
}
if (absl::EndsWith(absolute_file_path, kTextPbExt)) {
if (google::protobuf::TextFormat::Parse(in_stream.get(), message)) {
return internal::OkStatus();
}
} else {
if (message->ParseFromZeroCopyStream(in_stream.get())) {
return internal::OkStatus();
}
}
return internal::InvalidArgumentError(
absl::StrCat("Parsing file contents failed: ", absolute_file_path));
}
google::rpc::Status WritePbFile(const google::protobuf::Message& message,
absl::string_view absolute_file_path) {
auto out_stream = OpenForWrite(std::string(absolute_file_path));
if (out_stream == nullptr) {
return internal::InvalidArgumentError(
absl::StrCat("Could not open file: ", absolute_file_path));
}
if (absl::EndsWith(absolute_file_path, kTextPbExt)) {
if (!google::protobuf::TextFormat::Print(message, out_stream.get())) {
return internal::InvalidArgumentError(
absl::StrCat("Unable to write to file: ", absolute_file_path));
}
} else {
if (!message.SerializePartialToZeroCopyStream(out_stream.get())) {
return internal::InvalidArgumentError(
absl::StrCat("Unable to write to file: ", absolute_file_path));
}
}
std::cout << "Wrote: " << absolute_file_path << std::endl;
return internal::OkStatus();
}
google::rpc::Status WriteTestData(absl::string_view test_name,
const TestData& values) {
if (absl::GetFlag(FLAGS_output_dir).empty()) {
google::protobuf::io::OstreamOutputStream os(&std::cout);
google::protobuf::TextFormat::Print(values, &os);
return internal::OkStatus();
}
return WritePbFile(
values, GetTestCaseFileName(absl::GetFlag(FLAGS_output_dir), test_name,
absl::GetFlag(FLAGS_binary)));
}
TestData ReadTestData(absl::string_view test_name) {
TestData data;
auto dir = absl::StrCat(std::getenv("TEST_SRCDIR"), "/",
absl::GetFlag(FLAGS_test_data_folder));
auto status = ReadPbFile(
GetTestCaseFileName(dir, test_name, absl::GetFlag(FLAGS_binary)), &data);
if (status.code() == google::rpc::Code::OK) {
return data;
}
// Check for the other file, just to be nice.
if (ReadPbFile(
GetTestCaseFileName(dir, test_name, !absl::GetFlag(FLAGS_binary)),
&data)
.code() == google::rpc::Code::OK) {
return data;
}
// Die with the error for the first file.
GOOGLE_LOG(FATAL) << status.ShortDebugString();
}
std::string TestDataParamName::operator()(
const ::testing::TestParamInfo<std::pair<TestValue, TestValue>>& info)
const {
std::string first = info.param.first.name();
std::string second = info.param.second.name();
RE2::GlobalReplace(&first, RE2("[^a-zA-Z0-9_]"), "_");
RE2::GlobalReplace(&second, RE2("[^a-zA-Z0-9_]"), "_");
return absl::StrCat(info.index, "_", first, "_v_", second);
}
std::string TestDataParamName::operator()(
const ::testing::TestParamInfo<TestValue>& info) const {
std::string name = info.param.name();
RE2::GlobalReplace(&name, RE2("[^a-zA-Z0-9_]"), "_");
return absl::StrCat(info.index, "_", name);
}
} // namespace testutil
} // namespace expr
} // namespace api
} // namespace google